Orderbook: Add GetDepth to Base (#1328)

* 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.

* Orderbook: Reword GetDepth comment

* Orderbook: Add test for Base.GetDepth
This commit is contained in:
Gareth Kirwan
2023-08-29 06:49:24 +01:00
committed by GitHub
parent d3102a08dc
commit 9a0f261211
2 changed files with 34 additions and 2 deletions

View File

@@ -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,11 @@ func (s *Service) Retrieve(exchange string, p currency.Pair, a asset.Item) (*Bas
return book.Retrieve()
}
// 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)
}
// TotalBidsAmount returns the total amount of bids and the total orderbook
// bids value
func (b *Base) TotalBidsAmount() (amountCollated, total float64) {

View File

@@ -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 {