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

@@ -562,3 +562,20 @@ func BenchmarkProcess(b *testing.B) {
}
}
}
func TestLevelsArrayPriceAmountUnmarshalJSON(t *testing.T) {
t.Parallel()
var asks LevelsArrayPriceAmount
err := asks.UnmarshalJSON([]byte(`[[1,2],["3","4"]]`))
require.NoError(t, err)
assert.Len(t, asks, 2)
assert.Equal(t, 1.0, asks[0].Price)
assert.Equal(t, 2.0, asks[0].Amount)
assert.Equal(t, 3.0, asks[1].Price)
assert.Equal(t, 4.0, asks[1].Amount)
assert.Equal(t, 2, len(asks.Levels()))
err = asks.UnmarshalJSON([]byte(`invalid`))
assert.Error(t, err)
}

View File

@@ -9,7 +9,9 @@ import (
"github.com/thrasher-corp/gocryptotrader/common/key"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/dispatch"
"github.com/thrasher-corp/gocryptotrader/encoding/json"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/types"
)
const (
@@ -184,3 +186,26 @@ type SideAmounts struct {
QuoteValue float64
BaseAmount float64
}
// LevelsArrayPriceAmount used to unmarshal orderbook levels from JSON slice of arrays
// e.g. [[price, amount], [price, amount]] or [][2]types.Number type declaration
type LevelsArrayPriceAmount Levels
// UnmarshalJSON implements json.Unmarshaler
func (l *LevelsArrayPriceAmount) UnmarshalJSON(data []byte) error {
var v [][2]types.Number
if err := json.Unmarshal(data, &v); err != nil {
return err
}
*l = make(LevelsArrayPriceAmount, len(v))
for x := range v {
(*l)[x].Price = v[x][0].Float64()
(*l)[x].Amount = v[x][1].Float64()
}
return nil
}
// Levels converts the LevelsArrayPriceAmount to a orderbook.Levels type
func (l *LevelsArrayPriceAmount) Levels() Levels {
return Levels(*l)
}