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

@@ -118,40 +118,17 @@ func (e *Exchange) GetExchangeInfo(ctx context.Context) (ExchangeInfo, error) {
}
// GetOrderBook returns full orderbook information
//
// OrderBookDataRequestParams contains the following members
// symbol: string of currency pair
// limit: returned limit amount
func (e *Exchange) GetOrderBook(ctx context.Context, obd OrderBookDataRequestParams) (*OrderBook, error) {
params := url.Values{}
symbol, err := e.FormatSymbol(obd.Symbol, asset.Spot)
func (e *Exchange) GetOrderBook(ctx context.Context, pair currency.Pair, limit uint64) (*OrderBookResponse, error) {
symbol, err := e.FormatSymbol(pair, asset.Spot)
if err != nil {
return nil, err
}
params := url.Values{}
params.Set("symbol", symbol)
params.Set("limit", strconv.Itoa(obd.Limit))
params.Set("limit", strconv.FormatUint(limit, 10))
var resp *OrderBookData
if err := e.SendHTTPRequest(ctx, exchange.RestSpotSupplementary, common.EncodeURLValues(orderBookDepth, params), orderbookLimit(obd.Limit), &resp); err != nil {
return nil, err
}
ob := &OrderBook{
Bids: make([]OrderbookItem, len(resp.Bids)),
Asks: make([]OrderbookItem, len(resp.Asks)),
LastUpdateID: resp.LastUpdateID,
}
for x := range resp.Bids {
ob.Bids[x].Price = resp.Bids[x][0].Float64()
ob.Bids[x].Quantity = resp.Bids[x][1].Float64()
}
for x := range resp.Asks {
ob.Asks[x].Price = resp.Asks[x][0].Float64()
ob.Asks[x].Quantity = resp.Asks[x][1].Float64()
}
return ob, nil
var resp *OrderBookResponse
return resp, e.SendHTTPRequest(ctx, exchange.RestSpotSupplementary, common.EncodeURLValues(orderBookDepth, params), orderbookLimit(limit), &resp)
}
// GetMostRecentTrades returns recent trade activity

View File

@@ -84,7 +84,7 @@ func (e *Exchange) FuturesExchangeInfo(ctx context.Context) (CExchangeInfo, erro
}
// GetFuturesOrderbook gets orderbook data for CoinMarginedFutures,
func (e *Exchange) GetFuturesOrderbook(ctx context.Context, symbol currency.Pair, limit int64) (*OrderBook, error) {
func (e *Exchange) GetFuturesOrderbook(ctx context.Context, symbol currency.Pair, limit uint64) (*OrderBookResponse, error) {
symbolValue, err := e.FormatSymbol(symbol, asset.CoinMarginedFutures)
if err != nil {
return nil, err
@@ -93,7 +93,7 @@ func (e *Exchange) GetFuturesOrderbook(ctx context.Context, symbol currency.Pair
params := url.Values{}
params.Set("symbol", symbolValue)
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
params.Set("limit", strconv.FormatUint(limit, 10))
}
rateBudget := cFuturesOrderbook1000Rate
@@ -106,27 +106,8 @@ func (e *Exchange) GetFuturesOrderbook(ctx context.Context, symbol currency.Pair
rateBudget = cFuturesOrderbook500Rate
}
var data *OrderbookData
if err := e.SendHTTPRequest(ctx, exchange.RestCoinMargined, cfuturesOrderbook+params.Encode(), rateBudget, &data); err != nil {
return nil, err
}
ob := &OrderBook{
Bids: make([]OrderbookItem, len(data.Bids)),
Asks: make([]OrderbookItem, len(data.Asks)),
}
for x := range data.Bids {
ob.Bids[x].Price = data.Bids[x][0].Float64()
ob.Bids[x].Quantity = data.Bids[x][1].Float64()
}
for x := range data.Asks {
ob.Asks[x].Price = data.Asks[x][0].Float64()
ob.Asks[x].Quantity = data.Asks[x][1].Float64()
}
return ob, nil
var resp *OrderBookResponse
return resp, e.SendHTTPRequest(ctx, exchange.RestCoinMargined, cfuturesOrderbook+params.Encode(), rateBudget, &resp)
}
// GetFuturesPublicTrades gets recent public trades for CoinMarginedFutures,

View File

@@ -25,6 +25,7 @@ import (
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
"github.com/thrasher-corp/gocryptotrader/exchanges/margin"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-corp/gocryptotrader/exchanges/sharedtestvalues"
"github.com/thrasher-corp/gocryptotrader/exchanges/subscription"
testexch "github.com/thrasher-corp/gocryptotrader/internal/testing/exchange"
@@ -1120,14 +1121,8 @@ func TestFetchTradablePairs(t *testing.T) {
func TestGetOrderBook(t *testing.T) {
t.Parallel()
_, err := e.GetOrderBook(t.Context(),
OrderBookDataRequestParams{
Symbol: currency.NewBTCUSDT(),
Limit: 1000,
})
if err != nil {
t.Error("Binance GetOrderBook() error", err)
}
_, err := e.GetOrderBook(t.Context(), currency.NewBTCUSDT(), 1000)
assert.NoError(t, err)
}
func TestGetMostRecentTrades(t *testing.T) {
@@ -2102,30 +2097,30 @@ func TestWsDepthUpdate(t *testing.T) {
require.NoError(t, testexch.Setup(e), "Test instance Setup must not error")
e.setupOrderbookManager(t.Context())
seedLastUpdateID := int64(161)
book := OrderBook{
Asks: []OrderbookItem{
{Price: 6621.80000000, Quantity: 0.00198100},
{Price: 6622.14000000, Quantity: 4.00000000},
{Price: 6622.46000000, Quantity: 2.30000000},
{Price: 6622.47000000, Quantity: 1.18633300},
{Price: 6622.64000000, Quantity: 4.00000000},
{Price: 6622.73000000, Quantity: 0.02900000},
{Price: 6622.76000000, Quantity: 0.12557700},
{Price: 6622.81000000, Quantity: 2.08994200},
{Price: 6622.82000000, Quantity: 0.01500000},
{Price: 6623.17000000, Quantity: 0.16831300},
book := OrderBookResponse{
Asks: []orderbook.Level{
{Price: 6621.80000000, Amount: 0.00198100},
{Price: 6622.14000000, Amount: 4.00000000},
{Price: 6622.46000000, Amount: 2.30000000},
{Price: 6622.47000000, Amount: 1.18633300},
{Price: 6622.64000000, Amount: 4.00000000},
{Price: 6622.73000000, Amount: 0.02900000},
{Price: 6622.76000000, Amount: 0.12557700},
{Price: 6622.81000000, Amount: 2.08994200},
{Price: 6622.82000000, Amount: 0.01500000},
{Price: 6623.17000000, Amount: 0.16831300},
},
Bids: []OrderbookItem{
{Price: 6621.55000000, Quantity: 0.16356700},
{Price: 6621.45000000, Quantity: 0.16352600},
{Price: 6621.41000000, Quantity: 0.86091200},
{Price: 6621.25000000, Quantity: 0.16914100},
{Price: 6621.23000000, Quantity: 0.09193600},
{Price: 6621.22000000, Quantity: 0.00755100},
{Price: 6621.13000000, Quantity: 0.08432000},
{Price: 6621.03000000, Quantity: 0.00172000},
{Price: 6620.94000000, Quantity: 0.30506700},
{Price: 6620.93000000, Quantity: 0.00200000},
Bids: []orderbook.Level{
{Price: 6621.55000000, Amount: 0.16356700},
{Price: 6621.45000000, Amount: 0.16352600},
{Price: 6621.41000000, Amount: 0.86091200},
{Price: 6621.25000000, Amount: 0.16914100},
{Price: 6621.23000000, Amount: 0.09193600},
{Price: 6621.22000000, Amount: 0.00755100},
{Price: 6621.13000000, Amount: 0.08432000},
{Price: 6621.03000000, Amount: 0.00172000},
{Price: 6620.94000000, Amount: 0.30506700},
{Price: 6620.93000000, Amount: 0.00200000},
},
LastUpdateID: seedLastUpdateID,
}

View File

@@ -8,6 +8,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"
)
@@ -123,35 +124,14 @@ type CoinInfo struct {
Withdrawing float64 `json:"withdrawing,string"`
}
// OrderBookDataRequestParams represents Klines request data.
type OrderBookDataRequestParams struct {
Symbol currency.Pair `json:"symbol"` // Required field; example LTCBTC,BTCUSDT
Limit int `json:"limit"` // Default 100; max 5000. If limit > 5000, then the response will truncate to 5000
}
// OrderbookItem stores an individual orderbook item
type OrderbookItem struct {
Price float64
Quantity float64
}
// OrderBookData is resp data from orderbook endpoint
type OrderBookData struct {
Code int `json:"code"`
Msg string `json:"msg"`
LastUpdateID int64 `json:"lastUpdateId"`
Bids [][2]types.Number `json:"bids"`
Asks [][2]types.Number `json:"asks"`
}
// OrderBook actual structured data that can be used for orderbook
type OrderBook struct {
Symbol string
LastUpdateID int64
Code int
Msg string
Bids []OrderbookItem
Asks []OrderbookItem
// OrderBookResponse is resp data from orderbook endpoint
type OrderBookResponse struct {
Code int64 `json:"code"`
Msg string `json:"msg"`
LastUpdateID int64 `json:"lastUpdateId"`
Timestamp types.Time `json:"T"`
Bids orderbook.LevelsArrayPriceAmount `json:"bids"`
Asks orderbook.LevelsArrayPriceAmount `json:"asks"`
}
// DepthUpdateParams is used as an embedded type for WebsocketDepthStream
@@ -163,13 +143,13 @@ type DepthUpdateParams []struct {
// WebsocketDepthStream is the difference for the update depth stream
type WebsocketDepthStream struct {
Event string `json:"e"`
Timestamp types.Time `json:"E"`
Pair string `json:"s"`
FirstUpdateID int64 `json:"U"`
LastUpdateID int64 `json:"u"`
UpdateBids [][2]types.Number `json:"b"`
UpdateAsks [][2]types.Number `json:"a"`
Event string `json:"e"`
Timestamp types.Time `json:"E"`
Pair string `json:"s"`
FirstUpdateID int64 `json:"U"`
LastUpdateID int64 `json:"u"`
UpdateBids orderbook.LevelsArrayPriceAmount `json:"b"`
UpdateAsks orderbook.LevelsArrayPriceAmount `json:"a"`
}
// RecentTradeRequestParams represents Klines request data.

View File

@@ -88,7 +88,7 @@ func (e *Exchange) UExchangeInfo(ctx context.Context) (UFuturesExchangeInfo, err
}
// UFuturesOrderbook gets orderbook data for usdt margined futures
func (e *Exchange) UFuturesOrderbook(ctx context.Context, symbol currency.Pair, limit int64) (*OrderBook, error) {
func (e *Exchange) UFuturesOrderbook(ctx context.Context, symbol currency.Pair, limit int64) (*OrderBookResponse, error) {
symbolValue, err := e.FormatSymbol(symbol, asset.USDTMarginedFutures)
if err != nil {
return nil, err
@@ -114,27 +114,8 @@ func (e *Exchange) UFuturesOrderbook(ctx context.Context, symbol currency.Pair,
rateBudget = uFuturesOrderbook500Rate
}
var data *OrderbookData
if err := e.SendHTTPRequest(ctx, exchange.RestUSDTMargined, ufuturesOrderbook+params.Encode(), rateBudget, &data); err != nil {
return nil, err
}
ob := &OrderBook{
Symbol: symbolValue,
LastUpdateID: data.LastUpdateID,
Bids: make([]OrderbookItem, len(data.Bids)),
Asks: make([]OrderbookItem, len(data.Asks)),
}
for x := range data.Asks {
ob.Asks[x].Price = data.Asks[x][0].Float64()
ob.Asks[x].Quantity = data.Asks[x][1].Float64()
}
for x := range data.Bids {
ob.Bids[x].Price = data.Bids[x][0].Float64()
ob.Bids[x].Quantity = data.Bids[x][1].Float64()
}
return ob, nil
var resp *OrderBookResponse
return resp, e.SendHTTPRequest(ctx, exchange.RestUSDTMargined, ufuturesOrderbook+params.Encode(), rateBudget, &resp)
}
// URecentTrades gets recent trades for usdt margined futures

View File

@@ -449,11 +449,7 @@ func stringToOrderStatus(status string) (order.Status, error) {
// SeedLocalCache seeds depth data
func (e *Exchange) SeedLocalCache(ctx context.Context, p currency.Pair) error {
ob, err := e.GetOrderBook(ctx,
OrderBookDataRequestParams{
Symbol: p,
Limit: 1000,
})
ob, err := e.GetOrderBook(ctx, p, 1000)
if err != nil {
return err
}
@@ -461,28 +457,20 @@ func (e *Exchange) SeedLocalCache(ctx context.Context, p currency.Pair) error {
}
// SeedLocalCacheWithBook seeds the local orderbook cache
func (e *Exchange) SeedLocalCacheWithBook(p currency.Pair, orderbookNew *OrderBook) error {
func (e *Exchange) SeedLocalCacheWithBook(p currency.Pair, orderbookNew *OrderBookResponse) error {
t := orderbookNew.Timestamp.Time()
if t.IsZero() {
t = time.Now() // Time not provided for this REST book.
}
newOrderBook := orderbook.Book{
Pair: p,
Asset: asset.Spot,
Exchange: e.Name,
LastUpdateID: orderbookNew.LastUpdateID,
ValidateOrderbook: e.ValidateOrderbook,
Bids: make(orderbook.Levels, len(orderbookNew.Bids)),
Asks: make(orderbook.Levels, len(orderbookNew.Asks)),
LastUpdated: time.Now(), // Time not provided in REST book.
}
for i := range orderbookNew.Bids {
newOrderBook.Bids[i] = orderbook.Level{
Amount: orderbookNew.Bids[i].Quantity,
Price: orderbookNew.Bids[i].Price,
}
}
for i := range orderbookNew.Asks {
newOrderBook.Asks[i] = orderbook.Level{
Amount: orderbookNew.Asks[i].Quantity,
Price: orderbookNew.Asks[i].Price,
}
Bids: orderbookNew.Bids.Levels(),
Asks: orderbookNew.Asks.Levels(),
LastUpdated: t,
}
return e.Websocket.Orderbook.LoadSnapshot(&newOrderBook)
}
@@ -618,23 +606,9 @@ func (e *Exchange) manageSubs(ctx context.Context, op string, subs subscription.
// ProcessOrderbookUpdate processes the websocket orderbook update
func (e *Exchange) ProcessOrderbookUpdate(cp currency.Pair, a asset.Item, ws *WebsocketDepthStream) error {
updateBid := make([]orderbook.Level, len(ws.UpdateBids))
for i := range ws.UpdateBids {
updateBid[i] = orderbook.Level{
Price: ws.UpdateBids[i][0].Float64(),
Amount: ws.UpdateBids[i][1].Float64(),
}
}
updateAsk := make([]orderbook.Level, len(ws.UpdateAsks))
for i := range ws.UpdateAsks {
updateAsk[i] = orderbook.Level{
Price: ws.UpdateAsks[i][0].Float64(),
Amount: ws.UpdateAsks[i][1].Float64(),
}
}
return e.Websocket.Orderbook.Update(&orderbook.Update{
Bids: updateBid,
Asks: updateAsk,
Bids: ws.UpdateBids.Levels(),
Asks: ws.UpdateAsks.Levels(),
Pair: cp,
UpdateID: ws.LastUpdateID,
UpdateTime: ws.Timestamp.Time(),

View File

@@ -512,60 +512,44 @@ func (e *Exchange) UpdateTicker(ctx context.Context, p currency.Pair, a asset.It
}
// UpdateOrderbook updates and returns the orderbook for a currency pair
func (e *Exchange) UpdateOrderbook(ctx context.Context, p currency.Pair, assetType asset.Item) (*orderbook.Book, error) {
func (e *Exchange) UpdateOrderbook(ctx context.Context, p currency.Pair, a asset.Item) (*orderbook.Book, error) {
if p.IsEmpty() {
return nil, currency.ErrCurrencyPairEmpty
}
if err := e.CurrencyPairs.IsAssetEnabled(assetType); err != nil {
if err := e.CurrencyPairs.IsAssetEnabled(a); err != nil {
return nil, err
}
book := &orderbook.Book{
Exchange: e.Name,
Pair: p,
Asset: assetType,
ValidateOrderbook: e.ValidateOrderbook,
}
var orderbookNew *OrderBook
var err error
switch assetType {
var orderbookNew *OrderBookResponse
var err error
switch a {
case asset.Spot, asset.Margin:
orderbookNew, err = e.GetOrderBook(ctx,
OrderBookDataRequestParams{
Symbol: p,
Limit: 1000,
})
orderbookNew, err = e.GetOrderBook(ctx, p, 1000)
case asset.USDTMarginedFutures:
orderbookNew, err = e.UFuturesOrderbook(ctx, p, 1000)
case asset.CoinMarginedFutures:
orderbookNew, err = e.GetFuturesOrderbook(ctx, p, 1000)
default:
return nil, fmt.Errorf("[%s] %w", assetType, asset.ErrNotSupported)
return nil, fmt.Errorf("[%s] %w", a, asset.ErrNotSupported)
}
if err != nil {
return book, err
return nil, err
}
book.Bids = make(orderbook.Levels, len(orderbookNew.Bids))
for x := range orderbookNew.Bids {
book.Bids[x] = orderbook.Level{
Amount: orderbookNew.Bids[x].Quantity,
Price: orderbookNew.Bids[x].Price,
}
}
book.Asks = make(orderbook.Levels, len(orderbookNew.Asks))
for x := range orderbookNew.Asks {
book.Asks[x] = orderbook.Level{
Amount: orderbookNew.Asks[x].Quantity,
Price: orderbookNew.Asks[x].Price,
}
ob := &orderbook.Book{
Exchange: e.Name,
Pair: p,
Asset: a,
ValidateOrderbook: e.ValidateOrderbook,
Bids: orderbookNew.Bids.Levels(),
Asks: orderbookNew.Asks.Levels(),
}
err = book.Process()
if err != nil {
return book, err
if err := ob.Process(); err != nil {
return nil, err
}
return orderbook.Get(e.Name, p, assetType)
return orderbook.Get(e.Name, p, a)
}
// UpdateAccountInfo retrieves balances for all enabled currencies for the

View File

@@ -181,7 +181,7 @@ func openOrdersLimit(symbol string) request.EndpointLimit {
return spotOpenOrdersSpecificRate
}
func orderbookLimit(depth int) request.EndpointLimit {
func orderbookLimit(depth uint64) request.EndpointLimit {
switch {
case depth <= 100:
return spotOrderbookDepth100Rate

View File

@@ -38,16 +38,6 @@ var (
uValidPeriods = []string{"5m", "15m", "30m", "1h", "2h", "4h", "6h", "12h", "1d"}
)
// USDT Margined Futures
// OrderbookData stores ob data for umargined and cmargined futures
type OrderbookData struct {
LastUpdateID int64 `json:"lastUpdateID"`
Timestamp types.Time `json:"T"`
Bids [][2]types.Number `json:"bids"`
Asks [][2]types.Number `json:"asks"`
}
// UPublicTradesData stores trade data
type UPublicTradesData struct {
ID int64 `json:"id"`