mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-31 23:16:54 +00:00
codebase: Cleanup various things (#1935)
* codebase: Rid base64/hex to string common funcs * codebase: Rid local scope variable usage and other improvements * codebase: Refactor currency pair usage across multiple exchanges - Updated HitBTC tests to use the new currency pair format. - Modified Kraken futures types to use currency.Pair instead of string for Symbol. - Adjusted Kraken wrapper methods to handle currency pairs correctly. - Refined OKX tests and types to utilize currency.Pair for instrument IDs. - Enhanced Poloniex tests to consistently use predefined currency pairs. - Streamlined order and orderbook tests to replace string pairs with currency.NewBTCUSD(). - Improved Yobit tests to utilize a standardized currency pair format. - Updated validator wrapper to use currency pairs directly instead of string conversions. * codebase: Use types.Number where possible * refactor: update PayoutFee type to types.Number for consistency * Refactor: Remove crypto functions to use standard library and other minor changes - Removed custom crypto functions for SHA256, SHA512, and MD5 from the common/crypto package. - Replaced usages of removed functions with standard library implementations in various files including: - cmd/websocket_client/main.go - engine/apiserver.go - exchanges/kraken/kraken.go - exchanges/lbank/lbank.go - exchanges/okx/okx_business_websocket.go - exchanges/kucoin/kucoin_websocket.go - gctscript/vm/vm.go - Updated tests to reflect changes in the crypto functions. - Renamed several functions for clarity, particularly in the context of order book updates across multiple exchanges. * refactor: replace assert with require for consistency in test assertions * refactor: Improve Binance futures candlestick test, standardise orderbook update function names and improve test parallelism * refactor: Replace require.Len with require.Equal for better output in TestGetFuturesKlineData
This commit is contained in:
@@ -519,11 +519,11 @@ 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]string `json:"asks"`
|
||||
Bids [][2]string `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 [][2]types.Number `json:"asks"`
|
||||
Bids [][2]types.Number `json:"bids"`
|
||||
}
|
||||
|
||||
// MakeOrderbook parse Orderbook asks/bids Price and Amount and create an Orderbook Instance with asks and bids data in []OrderbookItem.
|
||||
@@ -532,36 +532,16 @@ func (a *OrderbookData) MakeOrderbook() (*Orderbook, error) {
|
||||
ID: a.ID,
|
||||
Current: a.Current,
|
||||
Update: a.Update,
|
||||
Asks: make([]OrderbookItem, len(a.Asks)),
|
||||
Bids: make([]OrderbookItem, len(a.Bids)),
|
||||
}
|
||||
ob.Asks = make([]OrderbookItem, len(a.Asks))
|
||||
ob.Bids = make([]OrderbookItem, len(a.Bids))
|
||||
for x := range a.Asks {
|
||||
price, err := strconv.ParseFloat(a.Asks[x][0], 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
amount, err := strconv.ParseFloat(a.Asks[x][1], 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ob.Asks[x] = OrderbookItem{
|
||||
Price: types.Number(price),
|
||||
Amount: amount,
|
||||
}
|
||||
ob.Asks[x].Price = a.Asks[x][0]
|
||||
ob.Asks[x].Amount = a.Asks[x][1].Float64()
|
||||
}
|
||||
for x := range a.Bids {
|
||||
price, err := strconv.ParseFloat(a.Bids[x][0], 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
amount, err := strconv.ParseFloat(a.Bids[x][1], 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ob.Bids[x] = OrderbookItem{
|
||||
Price: types.Number(price),
|
||||
Amount: amount,
|
||||
}
|
||||
ob.Bids[x].Price = a.Bids[x][0]
|
||||
ob.Bids[x].Amount = a.Bids[x][1].Float64()
|
||||
}
|
||||
return ob, nil
|
||||
}
|
||||
|
||||
@@ -384,7 +384,7 @@ func (g *Gateio) processOrderbookUpdate(ctx context.Context, incoming []byte, up
|
||||
bids[x].Price = data.Bids[x][0].Float64()
|
||||
bids[x].Amount = data.Bids[x][1].Float64()
|
||||
}
|
||||
return g.wsOBUpdateMgr.ProcessUpdate(ctx, g, data.FirstUpdateID, &orderbook.Update{
|
||||
return g.wsOBUpdateMgr.ProcessOrderbookUpdate(ctx, g, data.FirstUpdateID, &orderbook.Update{
|
||||
UpdateID: data.LastUpdateID,
|
||||
UpdateTime: data.UpdateTime.Time(),
|
||||
UpdatePushedAt: updatePushedAt,
|
||||
|
||||
@@ -422,7 +422,7 @@ func (g *Gateio) processFuturesOrderbookUpdate(ctx context.Context, incoming []b
|
||||
bids[x].Amount = data.Bids[x].Size
|
||||
}
|
||||
|
||||
return g.wsOBUpdateMgr.ProcessUpdate(ctx, g, data.FirstUpdatedID, &orderbook.Update{
|
||||
return g.wsOBUpdateMgr.ProcessOrderbookUpdate(ctx, g, data.FirstUpdatedID, &orderbook.Update{
|
||||
UpdateID: data.LastUpdatedID,
|
||||
UpdateTime: data.Timestamp.Time(),
|
||||
UpdatePushedAt: pushTime,
|
||||
|
||||
@@ -513,7 +513,7 @@ func (g *Gateio) processOptionsOrderbookUpdate(ctx context.Context, incoming []b
|
||||
bids[x].Price = data.Bids[x].Price.Float64()
|
||||
bids[x].Amount = data.Bids[x].Size
|
||||
}
|
||||
return g.wsOBUpdateMgr.ProcessUpdate(ctx, g, data.FirstUpdatedID, &orderbook.Update{
|
||||
return g.wsOBUpdateMgr.ProcessOrderbookUpdate(ctx, g, data.FirstUpdatedID, &orderbook.Update{
|
||||
UpdateID: data.LastUpdatedID,
|
||||
UpdateTime: data.Timestamp.Time(),
|
||||
UpdatePushedAt: pushTime,
|
||||
|
||||
@@ -40,8 +40,8 @@ func newWsOBUpdateManager(snapshotSyncDelay time.Duration) *wsOBUpdateManager {
|
||||
return &wsOBUpdateManager{lookup: make(map[key.PairAsset]*updateCache), snapshotSyncDelay: snapshotSyncDelay}
|
||||
}
|
||||
|
||||
// ProcessUpdate processes an orderbook update by syncing snapshot, caching updates and applying them
|
||||
func (m *wsOBUpdateManager) ProcessUpdate(ctx context.Context, g *Gateio, firstUpdateID int64, update *orderbook.Update) error {
|
||||
// ProcessOrderbookUpdate processes an orderbook update by syncing snapshot, caching updates and applying them
|
||||
func (m *wsOBUpdateManager) ProcessOrderbookUpdate(ctx context.Context, g *Gateio, firstUpdateID int64, update *orderbook.Update) error {
|
||||
cache := m.LoadCache(update.Pair, update.Asset)
|
||||
cache.mtx.Lock()
|
||||
defer cache.mtx.Unlock()
|
||||
|
||||
@@ -15,11 +15,11 @@ import (
|
||||
testexch "github.com/thrasher-corp/gocryptotrader/internal/testing/exchange"
|
||||
)
|
||||
|
||||
func TestProcessUpdate(t *testing.T) {
|
||||
func TestProcessOrderbookUpdate(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
m := newWsOBUpdateManager(0)
|
||||
err := m.ProcessUpdate(t.Context(), g, 1337, &orderbook.Update{})
|
||||
err := m.ProcessOrderbookUpdate(t.Context(), g, 1337, &orderbook.Update{})
|
||||
assert.ErrorIs(t, err, currency.ErrCurrencyPairEmpty)
|
||||
|
||||
pair := currency.NewPair(currency.BABY, currency.BABYDOGE)
|
||||
@@ -35,7 +35,7 @@ func TestProcessUpdate(t *testing.T) {
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
err = m.ProcessUpdate(t.Context(), g, 1337, &orderbook.Update{
|
||||
err = m.ProcessOrderbookUpdate(t.Context(), g, 1337, &orderbook.Update{
|
||||
UpdateID: 1338,
|
||||
Pair: pair,
|
||||
Asset: asset.USDTMarginedFutures,
|
||||
@@ -45,7 +45,7 @@ func TestProcessUpdate(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// Test orderbook snapshot is behind update
|
||||
err = m.ProcessUpdate(t.Context(), g, 1340, &orderbook.Update{
|
||||
err = m.ProcessOrderbookUpdate(t.Context(), g, 1340, &orderbook.Update{
|
||||
UpdateID: 1341,
|
||||
Pair: pair,
|
||||
Asset: asset.USDTMarginedFutures,
|
||||
@@ -62,7 +62,7 @@ func TestProcessUpdate(t *testing.T) {
|
||||
cache.mtx.Unlock()
|
||||
|
||||
// Test orderbook snapshot is behind update
|
||||
err = m.ProcessUpdate(t.Context(), g, 1342, &orderbook.Update{
|
||||
err = m.ProcessOrderbookUpdate(t.Context(), g, 1342, &orderbook.Update{
|
||||
UpdateID: 1343,
|
||||
Pair: pair,
|
||||
Asset: asset.USDTMarginedFutures,
|
||||
|
||||
Reference in New Issue
Block a user