mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-24 07:26:47 +00:00
* 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
206 lines
5.9 KiB
Go
206 lines
5.9 KiB
Go
package gateio
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"github.com/thrasher-corp/gocryptotrader/currency"
|
|
"github.com/thrasher-corp/gocryptotrader/exchange/websocket/buffer"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/subscription"
|
|
testexch "github.com/thrasher-corp/gocryptotrader/internal/testing/exchange"
|
|
)
|
|
|
|
func TestProcessOrderbookUpdate(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
m := newWsOBUpdateManager(0)
|
|
err := m.ProcessOrderbookUpdate(t.Context(), g, 1337, &orderbook.Update{})
|
|
assert.ErrorIs(t, err, currency.ErrCurrencyPairEmpty)
|
|
|
|
pair := currency.NewPair(currency.BABY, currency.BABYDOGE)
|
|
err = g.Websocket.Orderbook.LoadSnapshot(&orderbook.Base{
|
|
Exchange: g.Name,
|
|
Pair: pair,
|
|
Asset: asset.USDTMarginedFutures,
|
|
Bids: []orderbook.Tranche{{Price: 1, Amount: 1}},
|
|
Asks: []orderbook.Tranche{{Price: 1, Amount: 1}},
|
|
LastUpdated: time.Now(),
|
|
UpdatePushedAt: time.Now(),
|
|
LastUpdateID: 1336,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
err = m.ProcessOrderbookUpdate(t.Context(), g, 1337, &orderbook.Update{
|
|
UpdateID: 1338,
|
|
Pair: pair,
|
|
Asset: asset.USDTMarginedFutures,
|
|
AllowEmpty: true,
|
|
UpdateTime: time.Now(),
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
// Test orderbook snapshot is behind update
|
|
err = m.ProcessOrderbookUpdate(t.Context(), g, 1340, &orderbook.Update{
|
|
UpdateID: 1341,
|
|
Pair: pair,
|
|
Asset: asset.USDTMarginedFutures,
|
|
AllowEmpty: true,
|
|
UpdateTime: time.Now(),
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
cache := m.LoadCache(pair, asset.USDTMarginedFutures)
|
|
|
|
cache.mtx.Lock()
|
|
assert.Len(t, cache.updates, 1)
|
|
assert.True(t, cache.updating)
|
|
cache.mtx.Unlock()
|
|
|
|
// Test orderbook snapshot is behind update
|
|
err = m.ProcessOrderbookUpdate(t.Context(), g, 1342, &orderbook.Update{
|
|
UpdateID: 1343,
|
|
Pair: pair,
|
|
Asset: asset.USDTMarginedFutures,
|
|
AllowEmpty: true,
|
|
UpdateTime: time.Now(),
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
cache.mtx.Lock()
|
|
assert.Len(t, cache.updates, 2)
|
|
assert.True(t, cache.updating)
|
|
cache.mtx.Unlock()
|
|
|
|
time.Sleep(time.Millisecond * 2) // Allow sync delay to pass
|
|
|
|
cache.mtx.Lock()
|
|
assert.Empty(t, cache.updates)
|
|
assert.False(t, cache.updating)
|
|
cache.mtx.Unlock()
|
|
}
|
|
|
|
func TestLoadCache(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
m := newWsOBUpdateManager(0)
|
|
pair := currency.NewPair(currency.BABY, currency.BABYDOGE)
|
|
cache := m.LoadCache(pair, asset.USDTMarginedFutures)
|
|
assert.NotNil(t, cache)
|
|
assert.Len(t, m.lookup, 1)
|
|
|
|
// Test cache is reused
|
|
cache2 := m.LoadCache(pair, asset.USDTMarginedFutures)
|
|
assert.Equal(t, cache, cache2)
|
|
}
|
|
|
|
func TestSyncOrderbook(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
g := new(Gateio)
|
|
require.NoError(t, testexch.Setup(g), "Setup must not error")
|
|
require.NoError(t, g.UpdateTradablePairs(t.Context(), false))
|
|
|
|
// Add dummy subscription so that it can be matched and a limit/level can be extracted for initial orderbook sync spot.
|
|
err := g.Websocket.AddSubscriptions(nil, &subscription.Subscription{Channel: subscription.OrderbookChannel, Interval: kline.HundredMilliseconds})
|
|
require.NoError(t, err)
|
|
|
|
m := newWsOBUpdateManager(defaultWSSnapshotSyncDelay)
|
|
|
|
for _, a := range []asset.Item{asset.Spot, asset.USDTMarginedFutures} {
|
|
pair := currency.NewPair(currency.ETH, currency.USDT)
|
|
err := g.CurrencyPairs.EnablePair(a, pair)
|
|
require.NoError(t, err)
|
|
cache := m.LoadCache(pair, a)
|
|
|
|
cache.updates = []pendingUpdate{{update: &orderbook.Update{Pair: pair, Asset: a}}}
|
|
cache.updating = true
|
|
err = cache.SyncOrderbook(t.Context(), g, pair, a)
|
|
require.NoError(t, err)
|
|
require.False(t, cache.updating)
|
|
require.Empty(t, cache.updates)
|
|
|
|
expectedLimit := 20
|
|
if a == asset.Spot {
|
|
expectedLimit = 100
|
|
}
|
|
|
|
b, err := g.Websocket.Orderbook.GetOrderbook(pair, a)
|
|
require.NoError(t, err)
|
|
require.Len(t, b.Bids, expectedLimit)
|
|
require.Len(t, b.Asks, expectedLimit)
|
|
}
|
|
}
|
|
|
|
func TestApplyPendingUpdates(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
g := new(Gateio)
|
|
require.NoError(t, testexch.Setup(g), "Setup must not error")
|
|
require.NoError(t, g.UpdateTradablePairs(t.Context(), false))
|
|
|
|
m := newWsOBUpdateManager(defaultWSSnapshotSyncDelay)
|
|
pair := currency.NewPair(currency.LTC, currency.USDT)
|
|
err := g.Websocket.Orderbook.LoadSnapshot(&orderbook.Base{
|
|
Exchange: g.Name,
|
|
Pair: pair,
|
|
Asset: asset.USDTMarginedFutures,
|
|
Bids: []orderbook.Tranche{{Price: 1, Amount: 1}},
|
|
Asks: []orderbook.Tranche{{Price: 1, Amount: 1}},
|
|
LastUpdated: time.Now(),
|
|
UpdatePushedAt: time.Now(),
|
|
LastUpdateID: 1335,
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
cache := m.LoadCache(pair, asset.USDTMarginedFutures)
|
|
|
|
update := &orderbook.Update{
|
|
UpdateID: 1339,
|
|
Pair: pair,
|
|
Asset: asset.USDTMarginedFutures,
|
|
AllowEmpty: true,
|
|
UpdateTime: time.Now(),
|
|
}
|
|
|
|
cache.updates = []pendingUpdate{{update: update, firstUpdateID: 1337}}
|
|
err = cache.applyPendingUpdates(g, asset.USDTMarginedFutures)
|
|
require.ErrorIs(t, err, errOrderbookSnapshotOutdated)
|
|
|
|
cache.updates[0].firstUpdateID = 1336
|
|
err = cache.applyPendingUpdates(g, asset.USDTMarginedFutures)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
func TestApplyOrderbookUpdate(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
g := new(Gateio)
|
|
require.NoError(t, testexch.Setup(g), "Setup must not error")
|
|
require.NoError(t, g.UpdateTradablePairs(t.Context(), false))
|
|
|
|
pair := currency.NewBTCUSDT()
|
|
|
|
update := &orderbook.Update{
|
|
Pair: pair,
|
|
Asset: asset.USDTMarginedFutures,
|
|
AllowEmpty: true,
|
|
UpdateTime: time.Now(),
|
|
}
|
|
|
|
err := applyOrderbookUpdate(g, update)
|
|
require.ErrorIs(t, err, buffer.ErrDepthNotFound)
|
|
|
|
update.Asset = asset.Spot
|
|
err = applyOrderbookUpdate(g, update)
|
|
require.ErrorIs(t, err, buffer.ErrDepthNotFound)
|
|
|
|
update.Pair = currency.NewPair(currency.BABY, currency.BABYDOGE)
|
|
err = applyOrderbookUpdate(g, update)
|
|
require.NoError(t, err)
|
|
}
|