orderbook: consolidate slice array types to orderbook package (#1992)

* orderbook: consolidate slice array types to orderbook package

* Update exchanges/bybit/bybit_types.go

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* linter: fix and add test

* cranktakular: nits

* cranktakular: nits

* Update exchanges/orderbook/orderbook_types.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* Update exchanges/gateio/gateio_test.go

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* gk: nits consolidation

* gk: rm unifySpotOrderbook func

* gk: nit but different

* linter: fix

* gk: nits

* glorious: nits

* Update exchanges/binance/binance.go

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* Update exchanges/binance/binance_cfutures.go

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* Update exchanges/binanceus/binanceus.go

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* thrasher-:nits

* thrasher-: more nit

---------

Co-authored-by: Ryan O'Hara-Reid <ryan.oharareid@thrasher.io>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>
Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>
This commit is contained in:
Ryan O'Hara-Reid
2025-10-02 14:22:20 +10:00
committed by GitHub
parent eb60a3c40e
commit ac91fabcd5
43 changed files with 413 additions and 854 deletions

View File

@@ -170,35 +170,18 @@ func (e *Exchange) GetTicker(ctx context.Context, symbol string, hourly bool) (*
// the amount.
func (e *Exchange) GetOrderbook(ctx context.Context, symbol string) (*Orderbook, error) {
type response struct {
Timestamp types.Time `json:"timestamp"`
Bids [][2]types.Number `json:"bids"`
Asks [][2]types.Number `json:"asks"`
Timestamp types.Time `json:"timestamp"`
Bids orderbook.LevelsArrayPriceAmount `json:"bids"`
Asks orderbook.LevelsArrayPriceAmount `json:"asks"`
}
path := "/v" + bitstampAPIVersion + "/" + bitstampAPIOrderbook + "/" + strings.ToLower(symbol) + "/"
var resp response
err := e.SendHTTPRequest(ctx, exchange.RestSpot, path, &resp)
if err != nil {
if err := e.SendHTTPRequest(ctx, exchange.RestSpot, path, &resp); err != nil {
return nil, err
}
ob := &Orderbook{
Timestamp: resp.Timestamp.Time(),
Bids: make([]OrderbookBase, len(resp.Bids)),
Asks: make([]OrderbookBase, len(resp.Asks)),
}
for x := range resp.Bids {
ob.Bids[x].Price = resp.Bids[x][0].Float64()
ob.Bids[x].Amount = resp.Bids[x][1].Float64()
}
for x := range resp.Asks {
ob.Asks[x].Price = resp.Asks[x][0].Float64()
ob.Asks[x].Amount = resp.Asks[x][1].Float64()
}
return ob, nil
return &Orderbook{Timestamp: resp.Timestamp.Time(), Bids: resp.Bids.Levels(), Asks: resp.Asks.Levels()}, nil
}
// GetTradingPairs returns a list of trading pairs which Bitstamp

View File

@@ -197,7 +197,7 @@ func TestGetOrderbook(t *testing.T) {
ob, err := e.GetOrderbook(t.Context(), currency.BTC.String()+currency.USD.String())
require.NoError(t, err, "GetOrderbook must not error")
assert.NotEmpty(t, ob.Timestamp, "Timestamp should not be empty")
for i, o := range [][]OrderbookBase{ob.Asks, ob.Bids} {
for i, o := range [][]orderbook.Level{ob.Asks, ob.Bids} {
s := []string{"Ask", "Bid"}[i]
if assert.NotEmptyf(t, o, "Should have items in %ss", s) {
a := o[0]

View File

@@ -4,6 +4,7 @@ import (
"time"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-corp/gocryptotrader/types"
)
@@ -37,17 +38,11 @@ type Ticker struct {
PercentChange24 float64 `json:"percent_change_24,string"`
}
// OrderbookBase holds singular price information
type OrderbookBase struct {
Price float64
Amount float64
}
// Orderbook holds orderbook information
type Orderbook struct {
Timestamp time.Time
Bids []OrderbookBase
Asks []OrderbookBase
Bids []orderbook.Level
Asks []orderbook.Level
}
// TradingPair holds trading pair information
@@ -276,10 +271,10 @@ type websocketOrderBookResponse struct {
}
type websocketOrderBook struct {
Asks [][2]types.Number `json:"asks"`
Bids [][2]types.Number `json:"bids"`
Timestamp types.Time `json:"timestamp"`
Microtimestamp types.Time `json:"microtimestamp"`
Asks orderbook.LevelsArrayPriceAmount `json:"asks"`
Bids orderbook.LevelsArrayPriceAmount `json:"bids"`
Timestamp types.Time `json:"timestamp"`
Microtimestamp types.Time `json:"microtimestamp"`
}
// OHLCResponse holds returned candle data

View File

@@ -300,23 +300,14 @@ func (e *Exchange) handleWSOrderbook(msg []byte) error {
}
obUpdate := &orderbook.Book{
Bids: make(orderbook.Levels, len(wsOrderBookResp.Data.Bids)),
Asks: make(orderbook.Levels, len(wsOrderBookResp.Data.Asks)),
Bids: wsOrderBookResp.Data.Bids.Levels(),
Asks: wsOrderBookResp.Data.Asks.Levels(),
Pair: p,
LastUpdated: wsOrderBookResp.Data.Microtimestamp.Time(),
Asset: asset.Spot,
Exchange: e.Name,
ValidateOrderbook: e.ValidateOrderbook,
}
for i := range wsOrderBookResp.Data.Asks {
obUpdate.Asks[i].Price = wsOrderBookResp.Data.Asks[i][0].Float64()
obUpdate.Asks[i].Amount = wsOrderBookResp.Data.Asks[i][1].Float64()
}
for i := range wsOrderBookResp.Data.Bids {
obUpdate.Bids[i].Price = wsOrderBookResp.Data.Bids[i][0].Float64()
obUpdate.Bids[i].Amount = wsOrderBookResp.Data.Bids[i][1].Float64()
}
filterOrderbookZeroBidPrice(obUpdate)
return e.Websocket.Orderbook.LoadSnapshot(obUpdate)
}