From de5e92f366cebcb29b78806baca02bf8b8383dc1 Mon Sep 17 00:00:00 2001 From: Gareth Kirwan Date: Fri, 25 Aug 2023 20:24:14 +0700 Subject: [PATCH 1/3] Orderbook: Add GetDepth to Base Base.GetDepth returns the concrete book of which Base is a copy This is probably useful for immutably monitoring orderbook health and state whereas FetchOrderbook would trigger a refresh. --- exchanges/orderbook/orderbook.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/exchanges/orderbook/orderbook.go b/exchanges/orderbook/orderbook.go index f4e366f4962..aebfdf162cc 100644 --- a/exchanges/orderbook/orderbook.go +++ b/exchanges/orderbook/orderbook.go @@ -17,8 +17,7 @@ func Get(exchange string, p currency.Pair, a asset.Item) (*Base, error) { return service.Retrieve(exchange, p, a) } -// GetDepth returns a Depth pointer allowing the caller to stream orderbook -// changes +// GetDepth returns a Depth pointer allowing the caller to stream orderbook changes func GetDepth(exchange string, p currency.Pair, a asset.Item) (*Depth, error) { return service.GetDepth(exchange, p, a) } @@ -202,6 +201,13 @@ func (s *Service) Retrieve(exchange string, p currency.Pair, a asset.Item) (*Bas return book.Retrieve() } +// GetDepth returns the concrete book of which Base is a copy +// This is probably useful for immutably monitoring orderbook health and state +// whereas FetchOrderbook would trigger a refresh. +func (b *Base) GetDepth() (*Depth, error) { + return service.GetDepth(b.Exchange, b.Pair, b.Asset) +} + // TotalBidsAmount returns the total amount of bids and the total orderbook // bids value func (b *Base) TotalBidsAmount() (amountCollated, total float64) { From 856156853da210cbd15b976b19a1b25ac97c52fa Mon Sep 17 00:00:00 2001 From: Gareth Kirwan Date: Mon, 28 Aug 2023 10:39:48 +0700 Subject: [PATCH 2/3] Orderbook: Reword GetDepth comment --- exchanges/orderbook/orderbook.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/exchanges/orderbook/orderbook.go b/exchanges/orderbook/orderbook.go index aebfdf162cc..f7e024869ee 100644 --- a/exchanges/orderbook/orderbook.go +++ b/exchanges/orderbook/orderbook.go @@ -201,9 +201,7 @@ func (s *Service) Retrieve(exchange string, p currency.Pair, a asset.Item) (*Bas return book.Retrieve() } -// GetDepth returns the concrete book of which Base is a copy -// This is probably useful for immutably monitoring orderbook health and state -// whereas FetchOrderbook would trigger a refresh. +// GetDepth returns the concrete book allowing the caller to stream orderbook changes func (b *Base) GetDepth() (*Depth, error) { return service.GetDepth(b.Exchange, b.Pair, b.Asset) } From 0a1500a09a3ea82646a18290ff52beb6eea25e31 Mon Sep 17 00:00:00 2001 From: Gareth Kirwan Date: Mon, 28 Aug 2023 10:48:08 +0700 Subject: [PATCH 3/3] Orderbook: Add test for Base.GetDepth --- exchanges/orderbook/orderbook_test.go | 28 +++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/exchanges/orderbook/orderbook_test.go b/exchanges/orderbook/orderbook_test.go index 016c5298410..b78e8551671 100644 --- a/exchanges/orderbook/orderbook_test.go +++ b/exchanges/orderbook/orderbook_test.go @@ -294,6 +294,34 @@ func TestGetDepth(t *testing.T) { } } +func TestBaseGetDepth(t *testing.T) { + c, err := currency.NewPairFromStrings("BTC", "UST") + if err != nil { + t.Error(err) + } + base := &Base{ + Pair: c, + Asks: []Item{{Price: 100, Amount: 10}}, + Bids: []Item{{Price: 200, Amount: 10}}, + Exchange: "Exchange", + Asset: asset.Spot, + } + + if _, err = base.GetDepth(); !errors.Is(err, errCannotFindOrderbook) { + t.Errorf("expecting %s error but received %v", errCannotFindOrderbook, err) + } + + if err = base.Process(); err != nil { + t.Error(err) + } + + if result, err := base.GetDepth(); err != nil { + t.Errorf("failed to get orderbook. Error %s", err) + } else if !result.pair.Equal(c) { + t.Errorf("Mismatched pairs: %v %v", result.pair, c) + } +} + func TestDeployDepth(t *testing.T) { c, err := currency.NewPairFromStrings("BTC", "USD") if err != nil {