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

@@ -3676,3 +3676,13 @@ func TestMarshalJSONNumber(t *testing.T) {
assert.Equal(t, tc.expected, string(payload), "MarshalJSON should return expected value")
}
}
func TestUnmarshalJSONOrderbookLevels(t *testing.T) {
t.Parallel()
var ob OrderbookLevels
require.NoError(t, ob.UnmarshalJSON([]byte(`[{"p":"123.45","s":"0.001"}]`)))
assert.Equal(t, 123.45, ob[0].Price, "Price should be correct")
assert.Equal(t, 0.001, ob[0].Amount, "Amount should be correct")
require.Error(t, ob.UnmarshalJSON([]byte(`["p":"123.45","s":"0.001"]`)))
}

View File

@@ -7,6 +7,7 @@ import (
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/encoding/json"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-corp/gocryptotrader/types"
)
@@ -519,26 +520,41 @@ type Ticker struct {
// OrderbookData holds orderbook ask and bid datas.
type OrderbookData struct {
ID int64 `json:"id"`
Current types.Time `json:"current"` // The timestamp of the response data being generated (in milliseconds)
Update types.Time `json:"update"` // The timestamp of when the orderbook last changed (in milliseconds)
Asks [][2]types.Number `json:"asks"`
Bids [][2]types.Number `json:"bids"`
ID int64 `json:"id"`
Current types.Time `json:"current"` // The timestamp of the response data being generated (in milliseconds)
Update types.Time `json:"update"` // The timestamp of when the orderbook last changed (in milliseconds)
Asks orderbook.LevelsArrayPriceAmount `json:"asks"`
Bids orderbook.LevelsArrayPriceAmount `json:"bids"`
}
// MakeOrderbook converts OrderbookData into an Orderbook
func (a *OrderbookData) MakeOrderbook() *Orderbook {
asks := make([]OrderbookItem, len(a.Asks))
for x := range a.Asks {
asks[x].Price = a.Asks[x][0]
asks[x].Amount = a.Asks[x][1]
return &Orderbook{ID: a.ID, Current: a.Current, Update: a.Update, Asks: OrderbookLevels(a.Asks.Levels()), Bids: OrderbookLevels(a.Bids.Levels())}
}
// OrderbookLevels represents a slice of orderbook levels.
type OrderbookLevels orderbook.Levels
// UnmarshalJSON implements the json.Unmarshaler interface for OrderbookLevels.
func (o *OrderbookLevels) UnmarshalJSON(data []byte) error {
var levels []OrderbookItem
if err := json.Unmarshal(data, &levels); err != nil {
return err
}
bids := make([]OrderbookItem, len(a.Bids))
for x := range a.Bids {
bids[x].Price = a.Bids[x][0]
bids[x].Amount = a.Bids[x][1]
*o = make(OrderbookLevels, len(levels))
for x := range levels {
(*o)[x] = orderbook.Level{
Price: levels[x].Price.Float64(),
Amount: levels[x].Amount.Float64(),
}
}
return &Orderbook{ID: a.ID, Current: a.Current, Update: a.Update, Asks: asks, Bids: bids}
return nil
}
// Levels converts OrderbookLevels to orderbook.Levels.
func (o *OrderbookLevels) Levels() orderbook.Levels {
return orderbook.Levels(*o)
}
// OrderbookItem stores an orderbook item
@@ -552,8 +568,8 @@ type Orderbook struct {
ID int64 `json:"id"`
Current types.Time `json:"current"` // The timestamp of the response data being generated (in milliseconds)
Update types.Time `json:"update"` // The timestamp of when the orderbook last changed (in milliseconds)
Bids []OrderbookItem `json:"bids"`
Asks []OrderbookItem `json:"asks"`
Bids OrderbookLevels `json:"bids"`
Asks OrderbookLevels `json:"asks"`
}
// Trade represents market trade.
@@ -2053,21 +2069,21 @@ type WsOrderbookTickerData struct {
// WsOrderbookUpdate represents websocket orderbook update push data
type WsOrderbookUpdate struct {
UpdateTime types.Time `json:"t"`
Pair currency.Pair `json:"s"`
FirstUpdateID int64 `json:"U"` // First update order book id in this event since last update
LastUpdateID int64 `json:"u"`
Bids [][2]types.Number `json:"b"`
Asks [][2]types.Number `json:"a"`
UpdateTime types.Time `json:"t"`
Pair currency.Pair `json:"s"`
FirstUpdateID int64 `json:"U"` // First update order book id in this event since last update
LastUpdateID int64 `json:"u"`
Bids orderbook.LevelsArrayPriceAmount `json:"b"`
Asks orderbook.LevelsArrayPriceAmount `json:"a"`
}
// WsOrderbookSnapshot represents a websocket orderbook snapshot push data
type WsOrderbookSnapshot struct {
UpdateTime types.Time `json:"t"`
LastUpdateID int64 `json:"lastUpdateId"`
CurrencyPair currency.Pair `json:"s"`
Bids [][2]types.Number `json:"bids"`
Asks [][2]types.Number `json:"asks"`
UpdateTime types.Time `json:"t"`
LastUpdateID int64 `json:"lastUpdateId"`
CurrencyPair currency.Pair `json:"s"`
Bids orderbook.LevelsArrayPriceAmount `json:"bids"`
Asks orderbook.LevelsArrayPriceAmount `json:"asks"`
}
// WsSpotOrder represents an order push data through the websocket channel.

View File

@@ -373,24 +373,14 @@ func (e *Exchange) processOrderbookUpdate(ctx context.Context, incoming []byte,
if err := json.Unmarshal(incoming, &data); err != nil {
return err
}
asks := make([]orderbook.Level, len(data.Asks))
for x := range data.Asks {
asks[x].Price = data.Asks[x][0].Float64()
asks[x].Amount = data.Asks[x][1].Float64()
}
bids := make([]orderbook.Level, len(data.Bids))
for x := range data.Bids {
bids[x].Price = data.Bids[x][0].Float64()
bids[x].Amount = data.Bids[x][1].Float64()
}
return e.wsOBUpdateMgr.ProcessOrderbookUpdate(ctx, e, data.FirstUpdateID, &orderbook.Update{
UpdateID: data.LastUpdateID,
UpdateTime: data.UpdateTime.Time(),
LastPushed: lastPushed,
Pair: data.Pair,
Asset: asset.Spot,
Asks: asks,
Bids: bids,
Asks: data.Asks.Levels(),
Bids: data.Bids.Levels(),
AllowEmpty: true,
})
}
@@ -401,17 +391,6 @@ func (e *Exchange) processOrderbookSnapshot(incoming []byte, lastPushed time.Tim
return err
}
asks := make([]orderbook.Level, len(data.Asks))
for x := range data.Asks {
asks[x].Price = data.Asks[x][0].Float64()
asks[x].Amount = data.Asks[x][1].Float64()
}
bids := make([]orderbook.Level, len(data.Bids))
for x := range data.Bids {
bids[x].Price = data.Bids[x][0].Float64()
bids[x].Amount = data.Bids[x][1].Float64()
}
for _, a := range standardMarginAssetTypes {
if enabled, _ := e.CurrencyPairs.IsPairEnabled(data.CurrencyPair, a); enabled {
if err := e.Websocket.Orderbook.LoadSnapshot(&orderbook.Book{
@@ -420,8 +399,8 @@ func (e *Exchange) processOrderbookSnapshot(incoming []byte, lastPushed time.Tim
Asset: a,
LastUpdated: data.UpdateTime.Time(),
LastPushed: lastPushed,
Bids: bids,
Asks: asks,
Bids: data.Bids.Levels(),
Asks: data.Asks.Levels(),
}); err != nil {
return err
}

View File

@@ -670,34 +670,24 @@ func (e *Exchange) UpdateOrderbookWithLimit(ctx context.Context, p currency.Pair
if err != nil {
return nil, err
}
book := &orderbook.Book{
ob := &orderbook.Book{
Exchange: e.Name,
Asset: a,
ValidateOrderbook: e.ValidateOrderbook,
Pair: p.Upper(),
Pair: p,
LastUpdateID: o.ID,
LastUpdated: o.Update.Time(),
LastPushed: o.Current.Time(),
Bids: o.Bids.Levels(),
Asks: o.Asks.Levels(),
}
book.Bids = make(orderbook.Levels, len(o.Bids))
for x := range o.Bids {
book.Bids[x] = orderbook.Level{
Amount: o.Bids[x].Amount.Float64(),
Price: o.Bids[x].Price.Float64(),
}
if err := ob.Process(); err != nil {
return nil, err
}
book.Asks = make(orderbook.Levels, len(o.Asks))
for x := range o.Asks {
book.Asks[x] = orderbook.Level{
Amount: o.Asks[x].Amount.Float64(),
Price: o.Asks[x].Price.Float64(),
}
}
err = book.Process()
if err != nil {
return book, err
}
return orderbook.Get(e.Name, book.Pair, a)
return orderbook.Get(e.Name, p, a)
}
// UpdateAccountInfo retrieves balances for all enabled currencies for the