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

@@ -22,9 +22,7 @@ import (
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
"github.com/thrasher-corp/gocryptotrader/types"
)
// Exchange implements exchange.IBotExchange and contains additional specific api methods for interacting with Bybit
@@ -247,29 +245,18 @@ func (e *Exchange) GetOrderBook(ctx context.Context, category, symbol string, li
params.Set("limit", strconv.FormatInt(limit, 10))
}
var resp orderbookResponse
err = e.SendHTTPRequest(ctx, exchange.RestSpot, common.EncodeURLValues("market/orderbook", params), defaultEPL, &resp)
if err != nil {
if err := e.SendHTTPRequest(ctx, exchange.RestSpot, common.EncodeURLValues("market/orderbook", params), defaultEPL, &resp); err != nil {
return nil, err
}
return &Orderbook{
Symbol: resp.Symbol,
UpdateID: resp.UpdateID,
Symbol: resp.Symbol,
GenerationTime: resp.Timestamp.Time(),
Bids: processOB(resp.Bids),
Asks: processOB(resp.Asks),
Bids: resp.Bids.Levels(),
Asks: resp.Asks.Levels(),
}, nil
}
func processOB(ob [][2]types.Number) []orderbook.Level {
o := make([]orderbook.Level, len(ob))
for x := range ob {
o[x].Price = ob[x][0].Float64()
o[x].Amount = ob[x][1].Float64()
}
return o
}
func fillCategoryAndSymbol(category, symbol string, optionalSymbol ...bool) (url.Values, error) {
if category == "" {
return nil, errCategoryNotSet

View File

@@ -1,22 +0,0 @@
package bybit
import "github.com/thrasher-corp/gocryptotrader/encoding/json"
// UnmarshalJSON deserializes incoming data into orderbookResponse instance.
func (a *orderbookResponse) UnmarshalJSON(data []byte) error {
type Alias orderbookResponse
child := &struct {
*Alias
}{
Alias: (*Alias)(a),
}
err := json.Unmarshal(data, child)
if err != nil {
var resp []any
err = json.Unmarshal(data, &resp)
if err != nil {
return err
}
}
return nil
}

View File

@@ -47,7 +47,6 @@ const (
canManipulateRealOrders = false
skipAuthenticatedFunctionsForMockTesting = "skipping authenticated function for mock testing"
skippingWebsocketFunctionsForMockTesting = "skipping websocket function for mock testing"
)
var (

View File

@@ -16,11 +16,11 @@ import (
var supportedOptionsTypes = []string{"BTC", "ETH", "SOL"}
type orderbookResponse struct {
Symbol string `json:"s"`
Asks [][2]types.Number `json:"a"`
Bids [][2]types.Number `json:"b"`
Timestamp types.Time `json:"ts"`
UpdateID int64 `json:"u"`
Symbol string `json:"s"`
Asks orderbook.LevelsArrayPriceAmount `json:"a"`
Bids orderbook.LevelsArrayPriceAmount `json:"b"`
Timestamp types.Time `json:"ts"`
UpdateID int64 `json:"u"`
}
// Authenticate stores authentication variables required
@@ -1749,11 +1749,11 @@ type Orderbook struct {
// WsOrderbookDetail represents an orderbook detail information.
type WsOrderbookDetail struct {
Symbol string `json:"s"`
Bids [][2]types.Number `json:"b"`
Asks [][2]types.Number `json:"a"`
UpdateID int64 `json:"u"`
Sequence int64 `json:"seq"`
Symbol string `json:"s"`
Bids orderbook.LevelsArrayPriceAmount `json:"b"`
Asks orderbook.LevelsArrayPriceAmount `json:"a"`
UpdateID int64 `json:"u"`
Sequence int64 `json:"seq"`
}
// SubscriptionResponse represents a subscription response.

View File

@@ -653,16 +653,6 @@ func (e *Exchange) wsProcessOrderbook(assetType asset.Item, resp *WebsocketRespo
if err != nil {
return err
}
asks := make([]orderbook.Level, len(result.Asks))
for i := range result.Asks {
asks[i].Price = result.Asks[i][0].Float64()
asks[i].Amount = result.Asks[i][1].Float64()
}
bids := make([]orderbook.Level, len(result.Bids))
for i := range result.Bids {
bids[i].Price = result.Bids[i][0].Float64()
bids[i].Amount = result.Bids[i][1].Float64()
}
if resp.Type == "snapshot" {
return e.Websocket.Orderbook.LoadSnapshot(&orderbook.Book{
@@ -672,14 +662,14 @@ func (e *Exchange) wsProcessOrderbook(assetType asset.Item, resp *WebsocketRespo
LastUpdated: resp.OrderbookLastUpdated.Time(),
LastUpdateID: result.UpdateID,
LastPushed: resp.PushTimestamp.Time(),
Asks: asks,
Bids: bids,
Asks: result.Asks.Levels(),
Bids: result.Bids.Levels(),
})
}
return e.Websocket.Orderbook.Update(&orderbook.Update{
Pair: cp,
Asks: asks,
Bids: bids,
Asks: result.Asks.Levels(),
Bids: result.Bids.Levels(),
Asset: assetType,
UpdateID: result.UpdateID,
UpdateTime: resp.OrderbookLastUpdated.Time(),