From f74876a4951b71fad75548eac401cfc53d4619bb Mon Sep 17 00:00:00 2001 From: Ryan O'Hara-Reid Date: Thu, 14 Nov 2024 09:58:03 +1100 Subject: [PATCH 1/4] orderbook: return identification details from depth --- exchanges/orderbook/depth.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/exchanges/orderbook/depth.go b/exchanges/orderbook/depth.go index d97b8cea282..876b83f9dc9 100644 --- a/exchanges/orderbook/depth.go +++ b/exchanges/orderbook/depth.go @@ -8,6 +8,7 @@ import ( "github.com/gofrs/uuid" "github.com/thrasher-corp/gocryptotrader/common" + "github.com/thrasher-corp/gocryptotrader/common/key" "github.com/thrasher-corp/gocryptotrader/currency" "github.com/thrasher-corp/gocryptotrader/dispatch" "github.com/thrasher-corp/gocryptotrader/exchanges/alert" @@ -792,3 +793,10 @@ func (d *Depth) GetPair() (currency.Pair, error) { } return d.pair, nil } + +// GetKey returns the key associated with the depth +func (d *Depth) GetKey() key.PairAsset { + d.m.RLock() + defer d.m.RUnlock() + return key.PairAsset{Base: d.pair.Base.Item, Quote: d.pair.Quote.Item, Asset: d.asset} +} From 6d00fd83f3655f752bb25f38058ec67174c92bb9 Mon Sep 17 00:00:00 2001 From: Ryan O'Hara-Reid Date: Wed, 22 Jan 2025 11:45:13 +1100 Subject: [PATCH 2/4] Add methods, drop error as you cannot deploy without it anyway, add tests --- exchanges/orderbook/depth.go | 26 +++++++++++----- exchanges/orderbook/depth_test.go | 52 +++++++++++++++++++++---------- 2 files changed, 55 insertions(+), 23 deletions(-) diff --git a/exchanges/orderbook/depth.go b/exchanges/orderbook/depth.go index 876b83f9dc9..e5b572ba94a 100644 --- a/exchanges/orderbook/depth.go +++ b/exchanges/orderbook/depth.go @@ -12,6 +12,7 @@ import ( "github.com/thrasher-corp/gocryptotrader/currency" "github.com/thrasher-corp/gocryptotrader/dispatch" "github.com/thrasher-corp/gocryptotrader/exchanges/alert" + "github.com/thrasher-corp/gocryptotrader/exchanges/asset" "github.com/thrasher-corp/gocryptotrader/log" ) @@ -785,18 +786,29 @@ func (d *Depth) GetTranches(count int) (ask, bid []Tranche, err error) { } // GetPair returns the pair associated with the depth -func (d *Depth) GetPair() (currency.Pair, error) { +func (d *Depth) GetPair() currency.Pair { d.m.RLock() defer d.m.RUnlock() - if d.pair.IsEmpty() { - return currency.Pair{}, currency.ErrCurrencyPairEmpty - } - return d.pair, nil + return d.pair +} + +// GetAsset returns the asset associated with the depth +func (d *Depth) GetAsset() asset.Item { + d.m.RLock() + defer d.m.RUnlock() + return d.asset +} + +// GetExchange returns the exchange associated with the depth +func (d *Depth) GetExchange() string { + d.m.RLock() + defer d.m.RUnlock() + return d.exchange } // GetKey returns the key associated with the depth -func (d *Depth) GetKey() key.PairAsset { +func (d *Depth) GetKey() key.ExchangePairAsset { d.m.RLock() defer d.m.RUnlock() - return key.PairAsset{Base: d.pair.Base.Item, Quote: d.pair.Quote.Item, Asset: d.asset} + return key.ExchangePairAsset{Exchange: d.exchange, Base: d.pair.Base.Item, Quote: d.pair.Quote.Item, Asset: d.asset} } diff --git a/exchanges/orderbook/depth_test.go b/exchanges/orderbook/depth_test.go index da96ef1b558..ab63a8162d8 100644 --- a/exchanges/orderbook/depth_test.go +++ b/exchanges/orderbook/depth_test.go @@ -10,6 +10,8 @@ import ( "github.com/gofrs/uuid" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/thrasher-corp/gocryptotrader/common/key" "github.com/thrasher-corp/gocryptotrader/currency" "github.com/thrasher-corp/gocryptotrader/exchanges/asset" ) @@ -677,22 +679,6 @@ func TestGetTranches(t *testing.T) { assert.Len(t, bidT, 5, "bids should have correct number of tranches") } -func TestGetPair(t *testing.T) { - t.Parallel() - depth := NewDepth(id) - - _, err := depth.GetPair() - assert.ErrorIs(t, err, currency.ErrCurrencyPairEmpty, "GetPair should error correctly") - - expected := currency.NewPair(currency.BTC, currency.WABI) - depth.pair = expected - - pair, err := depth.GetPair() - assert.NoError(t, err, "GetPair should not error") - - assert.Equal(t, expected, pair, "GetPair should return correct pair") -} - func getInvalidDepth() *Depth { depth := NewDepth(id) _ = depth.Invalidate(errors.New("invalid reasoning")) @@ -910,3 +896,37 @@ var movementTests = []struct { {[]any{20.0, true}, Movement{NominalPercentage: 0.7105459985041137, ImpactPercentage: FullLiquidityExhaustedPercentage, SlippageCost: 190.0, FullBookSideConsumed: true}}, }}, } + +func TestGetPair(t *testing.T) { + t.Parallel() + depth := NewDepth(id) + require.Empty(t, depth.GetPair()) + depth.pair = currency.NewPair(currency.BTC, currency.WABI) + require.Equal(t, depth.pair, depth.GetPair()) +} + +func TestGetAsset(t *testing.T) { + t.Parallel() + depth := NewDepth(id) + require.Empty(t, depth.GetAsset()) + depth.asset = asset.Spot + require.Equal(t, depth.asset, depth.GetAsset()) +} + +func TestGetExchange(t *testing.T) { + t.Parallel() + depth := NewDepth(id) + require.Empty(t, depth.GetExchange()) + depth.exchange = "test" + require.Equal(t, depth.exchange, depth.GetExchange()) +} + +func TestGetKey(t *testing.T) { + t.Parallel() + depth := NewDepth(id) + require.Empty(t, depth.GetKey()) + depth.exchange = "test" + depth.pair = currency.NewPair(currency.BTC, currency.WABI) + depth.asset = asset.Spot + require.Equal(t, key.ExchangePairAsset{Exchange: depth.exchange, Base: depth.pair.Base.Item, Quote: depth.pair.Quote.Item, Asset: depth.asset}, depth.GetKey()) +} From 022e07a79ef4665013ddcd23995fc85659084bc6 Mon Sep 17 00:00:00 2001 From: Ryan O'Hara-Reid Date: Wed, 22 Jan 2025 11:59:10 +1100 Subject: [PATCH 3/4] Update exchanges/orderbook/depth_test.go Co-authored-by: Scott --- exchanges/orderbook/depth_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/exchanges/orderbook/depth_test.go b/exchanges/orderbook/depth_test.go index ab63a8162d8..14ef68cbe25 100644 --- a/exchanges/orderbook/depth_test.go +++ b/exchanges/orderbook/depth_test.go @@ -928,5 +928,7 @@ func TestGetKey(t *testing.T) { depth.exchange = "test" depth.pair = currency.NewPair(currency.BTC, currency.WABI) depth.asset = asset.Spot - require.Equal(t, key.ExchangePairAsset{Exchange: depth.exchange, Base: depth.pair.Base.Item, Quote: depth.pair.Quote.Item, Asset: depth.asset}, depth.GetKey()) +require.Equal(t, + key.ExchangePairAsset{Exchange: depth.exchange, Base: depth.pair.Base.Item, Quote: depth.pair.Quote.Item, Asset: depth.asset}, + depth.GetKey()) } From 907828bfe440a21da8b0c4cc8c8b73ccdd0e8d3e Mon Sep 17 00:00:00 2001 From: shazbert Date: Thu, 23 Jan 2025 07:31:46 +1100 Subject: [PATCH 4/4] linter: fix --- exchanges/orderbook/depth_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exchanges/orderbook/depth_test.go b/exchanges/orderbook/depth_test.go index 14ef68cbe25..9db77e43b2e 100644 --- a/exchanges/orderbook/depth_test.go +++ b/exchanges/orderbook/depth_test.go @@ -928,7 +928,7 @@ func TestGetKey(t *testing.T) { depth.exchange = "test" depth.pair = currency.NewPair(currency.BTC, currency.WABI) depth.asset = asset.Spot -require.Equal(t, - key.ExchangePairAsset{Exchange: depth.exchange, Base: depth.pair.Base.Item, Quote: depth.pair.Quote.Item, Asset: depth.asset}, + require.Equal(t, + key.ExchangePairAsset{Exchange: depth.exchange, Base: depth.pair.Base.Item, Quote: depth.pair.Quote.Item, Asset: depth.asset}, depth.GetKey()) }