Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Orderbook: Add GetDepth to Base #1328

Merged
merged 3 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
gbjk marked this conversation as resolved.
Show resolved Hide resolved
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