Skip to content

Commit

Permalink
Orderbook: Add GetDepth to Base (thrasher-corp#1328)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
gbjk authored and shazbert committed Nov 9, 2023
1 parent 612b9da commit 59596de
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
8 changes: 6 additions & 2 deletions exchanges/orderbook/orderbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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) {
Expand Down
28 changes: 28 additions & 0 deletions exchanges/orderbook/orderbook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 59596de

Please sign in to comment.