orderbook: Add methods for extracting associated details from depth (#1783)

* orderbook: return identification details from depth

* Add methods, drop error as you cannot deploy without it anyway, add tests

* Update exchanges/orderbook/depth_test.go

Co-authored-by: Scott <gloriousCode@users.noreply.github.com>

* linter: fix

* gk: nits

---------

Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
Co-authored-by: Scott <gloriousCode@users.noreply.github.com>
This commit is contained in:
Ryan O'Hara-Reid
2025-02-20 16:10:07 +11:00
committed by GitHub
parent e99adca86f
commit 636adb8822
2 changed files with 64 additions and 22 deletions

View File

@@ -8,9 +8,11 @@ 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"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/log"
)
@@ -785,12 +787,30 @@ func (d *Depth) GetTranches(count int) (ask, bid []Tranche, err error) {
return d.askTranches.retrieve(count), d.bidTranches.retrieve(count), nil
}
// GetPair returns the pair associated with the depth
func (d *Depth) GetPair() (currency.Pair, error) {
// Pair returns the pair associated with the depth
func (d *Depth) Pair() 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
}
// Asset returns the asset associated with the depth
func (d *Depth) Asset() asset.Item {
d.m.RLock()
defer d.m.RUnlock()
return d.asset
}
// Exchange returns the exchange associated with the depth
func (d *Depth) Exchange() string {
d.m.RLock()
defer d.m.RUnlock()
return d.exchange
}
// Key returns a combined key for the depth
func (d *Depth) Key() key.ExchangePairAsset {
d.m.RLock()
defer d.m.RUnlock()
return key.ExchangePairAsset{Exchange: d.exchange, Base: d.pair.Base.Item, Quote: d.pair.Quote.Item, Asset: d.asset}
}

View File

@@ -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"
)
@@ -690,22 +692,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"))
@@ -923,3 +909,39 @@ var movementTests = []struct {
{[]any{20.0, true}, Movement{NominalPercentage: 0.7105459985041137, ImpactPercentage: FullLiquidityExhaustedPercentage, SlippageCost: 190.0, FullBookSideConsumed: true}},
}},
}
func TestPair(t *testing.T) {
t.Parallel()
depth := NewDepth(id)
require.Empty(t, depth.Pair())
depth.pair = currency.NewPair(currency.BTC, currency.WABI)
require.Equal(t, depth.pair, depth.Pair())
}
func TestAsset(t *testing.T) {
t.Parallel()
depth := NewDepth(id)
require.Empty(t, depth.Asset())
depth.asset = asset.Spot
require.Equal(t, depth.asset, depth.Asset())
}
func TestExchange(t *testing.T) {
t.Parallel()
depth := NewDepth(id)
require.Empty(t, depth.Exchange())
depth.exchange = "test"
require.Equal(t, depth.exchange, depth.Exchange())
}
func TestKey(t *testing.T) {
t.Parallel()
depth := NewDepth(id)
require.Empty(t, depth.Key())
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.Key())
}