Skip to content

Commit

Permalink
fixup! GateIO: Split asset.Futures into CoinM and USDT
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Jan 21, 2025
1 parent 876972b commit b5d4f59
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 155 deletions.
181 changes: 27 additions & 154 deletions exchanges/gateio/gateio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1885,7 +1885,7 @@ func TestGetActiveOrders(t *testing.T) {
Side: order.AnySide,
AssetType: a,
})
assert.NoError(t, err, "GetActiveOrders should not error for %s", a)
assert.NoErrorf(t, err, "GetActiveOrders should not error for %s", a)
}
}

Expand All @@ -1903,67 +1903,36 @@ func TestGetOrderHistory(t *testing.T) {
AssetType: a,
}
_, err := g.GetOrderHistory(context.Background(), &multiOrderRequest)
assert.NoError(t, err, "GetOrderHistory should not error for %s", a)
assert.NoErrorf(t, err, "GetOrderHistory should not error for %s", a)
}
}

func TestGetHistoricCandles(t *testing.T) {
t.Parallel()
startTime := time.Now().Add(-time.Hour * 10)
if _, err := g.GetHistoricCandles(context.Background(), getPair(t, asset.Spot), asset.Spot, kline.OneDay, startTime, time.Now()); err != nil {
t.Errorf("%s GetHistoricCandles() error: %v", g.Name, err)
}
if _, err := g.GetHistoricCandles(context.Background(), getPair(t, asset.Margin), asset.Margin, kline.OneDay, startTime, time.Now()); err != nil {
t.Errorf("%s GetHistoricCandles() error: %v", g.Name, err)
}
if _, err := g.GetHistoricCandles(context.Background(), getPair(t, asset.CrossMargin), asset.CrossMargin, kline.OneDay, startTime, time.Now()); err != nil {
t.Errorf("%s GetHistoricCandles() error: %v", g.Name, err)
}
if _, err := g.GetHistoricCandles(context.Background(), getPair(t, asset.Futures), asset.Futures, kline.OneDay, startTime, time.Now()); err != nil {
t.Errorf("%s GetHistoricCandles() error: %v", g.Name, err)
}
if _, err := g.GetHistoricCandles(context.Background(), getPair(t, asset.DeliveryFutures), asset.DeliveryFutures, kline.OneDay, startTime, time.Now()); err != nil {
t.Errorf("%s GetHistoricCandles() error: %v", g.Name, err)
}
if _, err := g.GetHistoricCandles(context.Background(), getPair(t, asset.Options), asset.Options, kline.OneDay, startTime, time.Now()); !errors.Is(err, asset.ErrNotSupported) {
t.Errorf("%s GetHistoricCandles() expecting: %v, but found %v", g.Name, asset.ErrNotSupported, err)
}
if _, err := g.GetHistoricCandles(context.Background(), getPair(t, asset.Options), asset.Options, kline.OneDay, startTime, time.Now()); !errors.Is(err, asset.ErrNotSupported) {
t.Errorf("%s GetHistoricCandles() expecting: %v, but found %v", g.Name, asset.ErrNotSupported, err)
for _, a := range g.GetAssetTypes(false) {
_, err := g.GetHistoricCandles(context.Background(), getPair(t, a), a, kline.OneDay, startTime, time.Now())
if a == asset.Options {
assert.ErrorIs(t, err, asset.ErrNotSupported, "GetHistoricCandles should error correctly for options")
} else {
assert.NoErrorf(t, err, "GetHistoricCandles should not error for %s", a)
}
}
}

func TestGetHistoricCandlesExtended(t *testing.T) {
t.Parallel()
startTime := time.Now().Add(-time.Hour * 5)
_, err := g.GetHistoricCandlesExtended(context.Background(),
getPair(t, asset.Spot), asset.Spot, kline.OneMin, startTime, time.Now())
if err != nil {
t.Fatal(err)
}
_, err = g.GetHistoricCandlesExtended(context.Background(),
getPair(t, asset.Margin), asset.Margin, kline.OneMin, startTime, time.Now())
if err != nil {
t.Fatal(err)
}
_, err = g.GetHistoricCandlesExtended(context.Background(),
getPair(t, asset.DeliveryFutures), asset.DeliveryFutures, kline.OneMin, time.Now().Add(-time.Hour*5), time.Now())
if err != nil {
t.Error(err)
}
_, err = g.GetHistoricCandlesExtended(context.Background(), getPair(t, asset.Futures), asset.Futures, kline.OneMin, startTime, time.Now())
if err != nil {
t.Error(err)
}
_, err = g.GetHistoricCandlesExtended(context.Background(),
getPair(t, asset.CrossMargin), asset.CrossMargin, kline.OneMin, startTime, time.Now())
if err != nil {
t.Error(err)
}
if _, err = g.GetHistoricCandlesExtended(context.Background(), getPair(t, asset.Options), asset.Options, kline.OneDay, startTime, time.Now()); !errors.Is(err, asset.ErrNotSupported) {
t.Errorf("%s GetHistoricCandlesExtended() expecting: %v, but found %v", g.Name, asset.ErrNotSupported, err)
for _, a := range g.GetAssetTypes(false) {
_, err := g.GetHistoricCandlesExtended(context.Background(), getPair(t, a), a, kline.OneMin, startTime, time.Now())
if a == asset.Options {
assert.ErrorIs(t, err, asset.ErrNotSupported, "GetHistoricCandlesExtended should error correctly for options")
} else {
assert.NoErrorf(t, err, "GetHistoricCandlesExtended should not error for %s", a)
}
}
}

func TestGetAvailableTransferTrains(t *testing.T) {
t.Parallel()
_, err := g.GetAvailableTransferChains(context.Background(), currency.USDT)
Expand Down Expand Up @@ -2087,113 +2056,17 @@ func TestCrossMarginBalanceLoan(t *testing.T) {
}
}

const wsFuturesTickerPushDataJSON = `{"time": 1541659086, "channel": "futures.tickers","event": "update", "error": null, "result": [ { "contract": "BTC_USD","last": "118.4","change_percentage": "0.77","funding_rate": "-0.000114","funding_rate_indicative": "0.01875","mark_price": "118.35","index_price": "118.36","total_size": "73648","volume_24h": "745487577","volume_24h_btc": "117", "volume_24h_usd": "419950", "quanto_base_rate": "", "volume_24h_quote": "1665006","volume_24h_settle": "178","volume_24h_base": "5526","low_24h": "99.2","high_24h": "132.5"} ]}`

func TestFuturesTicker(t *testing.T) {
t.Parallel()
if err := g.WsHandleFuturesData(context.Background(), []byte(wsFuturesTickerPushDataJSON), asset.Futures); err != nil {
t.Errorf("%s websocket push data error: %v", g.Name, err)
}
}

const wsFuturesTradesPushDataJSON = `{"channel": "futures.trades","event": "update", "time": 1541503698, "result": [{"size": -108,"id": 27753479,"create_time": 1545136464,"create_time_ms": 1545136464123,"price": "96.4","contract": "BTC_USD"}]}`

func TestFuturesTrades(t *testing.T) {
t.Parallel()
if err := g.WsHandleFuturesData(context.Background(), []byte(wsFuturesTradesPushDataJSON), asset.Futures); err != nil {
t.Errorf("%s websocket push data error: %v", g.Name, err)
}
}

const (
wsFuturesOrderbookTickerJSON = `{ "time": 1615366379, "channel": "futures.book_ticker", "event": "update", "error": null, "result": { "t": 1615366379123, "u": 2517661076, "s": "BTC_USD", "b": "54696.6", "B": 37000, "a": "54696.7", "A": 47061 }}`
)

func TestOrderbookData(t *testing.T) {
t.Parallel()
if err := g.WsHandleFuturesData(context.Background(), []byte(wsFuturesOrderbookTickerJSON), asset.Futures); err != nil {
t.Errorf("%s websocket orderbook ticker push data error: %v", g.Name, err)
}
}

const wsFuturesOrderPushDataJSON = `{ "channel": "futures.orders", "event": "update", "time": 1541505434, "result": [ { "contract": "BTC_USD", "create_time": 1628736847, "create_time_ms": 1628736847325, "fill_price": 40000.4, "finish_as": "filled", "finish_time": 1628736848, "finish_time_ms": 1628736848321, "iceberg": 0, "id": 4872460, "is_close": false, "is_liq": false, "is_reduce_only": false, "left": 0, "mkfr": -0.00025, "price": 40000.4, "refr": 0, "refu": 0, "size": 1, "status": "finished", "text": "-", "tif": "gtc", "tkfr": 0.0005, "user": "110xxxxx" } ]}`

func TestFuturesOrderPushData(t *testing.T) {
t.Parallel()
if err := g.WsHandleFuturesData(context.Background(), []byte(wsFuturesOrderPushDataJSON), asset.Futures); err != nil {
t.Errorf("%s websocket futures order push data error: %v", g.Name, err)
}
}

const wsFuturesUsertradesPushDataJSON = `{"time": 1543205083, "channel": "futures.usertrades","event": "update", "error": null, "result": [{"id": "3335259","create_time": 1628736848,"create_time_ms": 1628736848321,"contract": "BTC_USD","order_id": "4872460","size": 1,"price": "40000.4","role": "maker","text": "api","fee": 0.0009290592,"point_fee": 0}]}`

func TestFuturesUserTrades(t *testing.T) {
t.Parallel()
if err := g.WsHandleFuturesData(context.Background(), []byte(wsFuturesUsertradesPushDataJSON), asset.Futures); err != nil {
t.Errorf("%s websocket futures user trades push data error: %v", g.Name, err)
}
}

const wsFuturesLiquidationPushDataJSON = `{"channel": "futures.liquidates", "event": "update", "time": 1541505434, "result": [{"entry_price": 209,"fill_price": 215.1,"left": 0,"leverage": 0.0,"liq_price": 213,"margin": 0.007816722941,"mark_price": 213,"order_id": 4093362,"order_price": 215.1,"size": -124,"time": 1541486601,"time_ms": 1541486601123,"contract": "BTC_USD","user": "1040xxxx"} ]}`

func TestFuturesLiquidationPushData(t *testing.T) {
t.Parallel()
if err := g.WsHandleFuturesData(context.Background(), []byte(wsFuturesLiquidationPushDataJSON), asset.Futures); err != nil {
t.Errorf("%s websocket futures liquidation push data error: %v", g.Name, err)
}
}

const wsFuturesAutoDelevergesNotification = `{"channel": "futures.auto_deleverages", "event": "update", "time": 1541505434, "result": [{"entry_price": 209,"fill_price": 215.1,"position_size": 10,"trade_size": 10,"time": 1541486601,"time_ms": 1541486601123,"contract": "BTC_USD","user": "1040"} ]}`

func TestFuturesAutoDeleverges(t *testing.T) {
t.Parallel()
if err := g.WsHandleFuturesData(context.Background(), []byte(wsFuturesAutoDelevergesNotification), asset.Futures); err != nil {
t.Errorf("%s websocket futures auto deleverge push data error: %v", g.Name, err)
}
}

const wsFuturesPositionClosePushDataJSON = ` {"channel": "futures.position_closes", "event": "update", "time": 1541505434, "result": [ { "contract": "BTC_USD", "pnl": -0.000624354791, "side": "long", "text": "web", "time": 1547198562, "time_ms": 1547198562123, "user": "211xxxx" } ]}`

func TestPositionClosePushData(t *testing.T) {
// TestFuturesDataHandler ensures that messages from various futures channels do not error
func TestFuturesDataHandler(t *testing.T) {
t.Parallel()
if err := g.WsHandleFuturesData(context.Background(), []byte(wsFuturesPositionClosePushDataJSON), asset.Futures); err != nil {
t.Errorf("%s websocket futures position close push data error: %v", g.Name, err)
}
}

const wsFuturesBalanceNotificationPushDataJSON = `{"channel": "futures.balances", "event": "update", "time": 1541505434, "result": [ { "balance": 9.998739899488, "change": -0.000002074115, "text": "BTC_USD:3914424", "time": 1547199246, "time_ms": 1547199246123, "type": "fee", "user": "211xxx" } ]}`

func TestFuturesBalanceNotification(t *testing.T) {
t.Parallel()
if err := g.WsHandleFuturesData(context.Background(), []byte(wsFuturesBalanceNotificationPushDataJSON), asset.Futures); err != nil {
t.Errorf("%s websocket futures balance notification push data error: %v", g.Name, err)
}
}

const wsFuturesReduceRiskLimitNotificationPushDataJSON = `{"time": 1551858330, "channel": "futures.reduce_risk_limits", "event": "update", "error": null, "result": [ { "cancel_orders": 0, "contract": "ETH_USD", "leverage_max": 10, "liq_price": 136.53, "maintenance_rate": 0.09, "risk_limit": 450, "time": 1551858330, "time_ms": 1551858330123, "user": "20011" } ]}`

func TestFuturesReduceRiskLimitPushData(t *testing.T) {
t.Parallel()
if err := g.WsHandleFuturesData(context.Background(), []byte(wsFuturesReduceRiskLimitNotificationPushDataJSON), asset.Futures); err != nil {
t.Errorf("%s websocket futures reduce risk limit notification push data error: %v", g.Name, err)
}
}

const wsFuturesPositionsNotificationPushDataJSON = `{"time": 1588212926,"channel": "futures.positions", "event": "update", "error": null, "result": [ { "contract": "BTC_USD", "cross_leverage_limit": 0, "entry_price": 40000.36666661111, "history_pnl": -0.000108569505, "history_point": 0, "last_close_pnl": -0.000050123368,"leverage": 0,"leverage_max": 100,"liq_price": 0.1,"maintenance_rate": 0.005,"margin": 49.999890611186,"mode": "single","realised_pnl": -1.25e-8,"realised_point": 0,"risk_limit": 100,"size": 3,"time": 1628736848,"time_ms": 1628736848321,"user": "110xxxxx"}]}`

func TestFuturesPositionsNotification(t *testing.T) {
t.Parallel()
if err := g.WsHandleFuturesData(context.Background(), []byte(wsFuturesPositionsNotificationPushDataJSON), asset.Futures); err != nil {
t.Errorf("%s websocket futures positions change notification push data error: %v", g.Name, err)
}
}

const wsFuturesAutoOrdersPushDataJSON = `{"time": 1596798126,"channel": "futures.autoorders", "event": "update", "error": null, "result": [ { "user": 123456, "trigger": { "strategy_type": 0, "price_type": 0, "price": "10000", "rule": 2, "expiration": 86400 }, "initial": { "contract": "BTC_USDT", "size": 10, "price": "10000", "tif": "gtc", "text": "web", "iceberg": 0, "is_close": false, "is_reduce_only": false }, "id": 9256, "trade_id": 0, "status": "open", "reason": "", "create_time": 1596798126, "name": "price_autoorders", "is_stop_order": false, "stop_trigger": { "rule": 0, "trigger_price": "", "order_price": "" } } ]}`

func TestFuturesAutoOrderPushData(t *testing.T) {
t.Parallel()
if err := g.WsHandleFuturesData(context.Background(), []byte(wsFuturesAutoOrdersPushDataJSON), asset.Futures); err != nil {
t.Errorf("%s websocket futures auto orders push data error: %v", g.Name, err)
g := new(Gateio) //nolint:govet // Intentional shadow to avoid future copy/paste mistakes
require.NoError(t, testexch.Setup(g), "Test instance Setup must not error")
testexch.FixtureToDataHandler(t, "testdata/wsFutures.json", func(m []byte) error { return g.WsHandleFuturesData(context.Background(), m, asset.CoinMarginedFutures) })
close(g.Websocket.DataHandler)
assert.Len(t, g.Websocket.DataHandler, 12, "Should see 12 messages")
for resp := range g.Websocket.DataHandler {
_, isErr := resp.(error)
assert.False(t, isErr, "Should not get any errors down the data handler")
}
}

Expand Down
2 changes: 1 addition & 1 deletion exchanges/gateio/gateio_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -1835,7 +1835,7 @@ func (g *Gateio) GetHistoricCandlesExtended(ctx context.Context, pair currency.P
Volume: candles[j].QuoteCcyVolume,
})
}
case asset.CoinMarginedFutures, asset.DeliveryFutures:
case asset.CoinMarginedFutures, asset.USDTMarginedFutures, asset.DeliveryFutures:
var candles []FuturesCandlestick
switch a {
case asset.CoinMarginedFutures:
Expand Down
12 changes: 12 additions & 0 deletions exchanges/gateio/testdata/wsFutures.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{"time":1541659086,"channel":"futures.tickers","event":"update","error":null,"result":[{"contract":"BTC_USD","last":"118.4","change_percentage":"0.77","funding_rate":"-0.000114","funding_rate_indicative":"0.01875","mark_price":"118.35","index_price":"118.36","total_size":"73648","volume_24h":"745487577","volume_24h_btc":"117","volume_24h_usd":"419950","quanto_base_rate":"","volume_24h_quote":"1665006","volume_24h_settle":"178","volume_24h_base":"5526","low_24h":"99.2","high_24h":"132.5"}]}
{"channel":"futures.trades","event":"update","time":1541503698,"result":[{"size":-108,"id":27753479,"create_time":1545136464,"create_time_ms":1545136464123,"price":"96.4","contract":"BTC_USD"}]}
{"time":1615366379,"channel":"futures.book_ticker","event":"update","error":null,"result":{"t":1615366379123,"u":2517661076,"s":"BTC_USD","b":"54696.6","B":37000,"a":"54696.7","A":47061}}
{"channel":"futures.orders","event":"update","time":1541505434,"result":[{"contract":"BTC_USD","create_time":1628736847,"create_time_ms":1628736847325,"fill_price":40000.4,"finish_as":"filled","finish_time":1628736848,"finish_time_ms":1628736848321,"iceberg":0,"id":4872460,"is_close":false,"is_liq":false,"is_reduce_only":false,"left":0,"mkfr":-0.00025,"price":40000.4,"refr":0,"refu":0,"size":1,"status":"finished","text":"-","tif":"gtc","tkfr":0.0005,"user":"110xxxxx"}]}
{"time":1543205083,"channel":"futures.usertrades","event":"update","error":null,"result":[{"id":"3335259","create_time":1628736848,"create_time_ms":1628736848321,"contract":"BTC_USD","order_id":"4872460","size":1,"price":"40000.4","role":"maker","text":"api","fee":0.0009290592,"point_fee":0}]}
{"channel":"futures.liquidates","event":"update","time":1541505434,"result":[{"entry_price":209,"fill_price":215.1,"left":0,"leverage":0,"liq_price":213,"margin":0.007816722941,"mark_price":213,"order_id":4093362,"order_price":215.1,"size":-124,"time":1541486601,"time_ms":1541486601123,"contract":"BTC_USD","user":"1040xxxx"}]}
{"channel": "futures.auto_deleverages", "event": "update", "time": 1541505434, "result": [{"entry_price": 209,"fill_price": 215.1,"position_size": 10,"trade_size": 10,"time": 1541486601,"time_ms": 1541486601123,"contract": "BTC_USD","user": "1040"} ]}
{"channel":"futures.position_closes","event":"update","time":1541505434,"result":[{"contract":"BTC_USD","pnl":-0.000624354791,"side":"long","text":"web","time":1547198562,"time_ms":1547198562123,"user":"211xxxx"}]}
{"channel":"futures.balances","event":"update","time":1541505434,"result":[{"balance":9.998739899488,"change":-2.074115e-06,"text":"BTC_USD:3914424","time":1547199246,"time_ms":1547199246123,"type":"fee","user":"211xxx"}]}
{"time":1551858330,"channel":"futures.reduce_risk_limits","event":"update","error":null,"result":[{"cancel_orders":0,"contract":"BTC_USD","leverage_max":10,"liq_price":136.53,"maintenance_rate":0.09,"risk_limit":450,"time":1551858330,"time_ms":1551858330123,"user":"20011"}]}
{"time": 1588212926,"channel": "futures.positions", "event": "update", "error": null, "result": [ { "contract": "BTC_USD", "cross_leverage_limit": 0, "entry_price": 40000.36666661111, "history_pnl": -0.000108569505, "history_point": 0, "last_close_pnl": -0.000050123368,"leverage": 0,"leverage_max": 100,"liq_price": 0.1,"maintenance_rate": 0.005,"margin": 49.999890611186,"mode": "single","realised_pnl": -1.25e-8,"realised_point": 0,"risk_limit": 100,"size": 3,"time": 1628736848,"time_ms": 1628736848321,"user": "110xxxxx"}]}
{"time":1596798126,"channel":"futures.autoorders","event":"update","error":null,"result":[{"user":123456,"trigger":{"strategy_type":0,"price_type":0,"price":"10000","rule":2,"expiration":86400},"initial":{"contract":"BTC_USDT","size":10,"price":"10000","tif":"gtc","text":"web","iceberg":0,"is_close":false,"is_reduce_only":false},"id":9256,"trade_id":0,"status":"open","reason":"","create_time":1596798126,"name":"price_autoorders","is_stop_order":false,"stop_trigger":{"rule":0,"trigger_price":"","order_price":""}}]}

0 comments on commit b5d4f59

Please sign in to comment.