Skip to content

Commit

Permalink
gateio: Fix futures subscription generation (thrasher-corp#1725)
Browse files Browse the repository at this point in the history
* gateio/websocket: fix futures filtering when generating subscriptions

* rework test

* gk: nits

---------

Co-authored-by: Ryan O'Hara-Reid <[email protected]>
  • Loading branch information
shazbert and Ryan O'Hara-Reid authored Nov 25, 2024
1 parent fa9549d commit 3b2b15e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 32 deletions.
15 changes: 8 additions & 7 deletions exchanges/gateio/gateio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2877,13 +2877,14 @@ func TestGenerateDeliveryFuturesDefaultSubscriptions(t *testing.T) {
}
func TestGenerateFuturesDefaultSubscriptions(t *testing.T) {
t.Parallel()
if _, err := g.GenerateFuturesDefaultSubscriptions(currency.USDT); err != nil {
t.Error(err)
}

if _, err := g.GenerateFuturesDefaultSubscriptions(currency.BTC); err != nil {
t.Error(err)
}
subs, err := g.GenerateFuturesDefaultSubscriptions(currency.USDT)
require.NoError(t, err)
require.NotEmpty(t, subs)
subs, err = g.GenerateFuturesDefaultSubscriptions(currency.BTC)
require.NoError(t, err)
require.NotEmpty(t, subs)
_, err = g.GenerateFuturesDefaultSubscriptions(currency.TABOO)
require.Error(t, err)
}
func TestGenerateOptionsDefaultSubscriptions(t *testing.T) {
t.Parallel()
Expand Down
36 changes: 11 additions & 25 deletions exchanges/gateio/gateio_ws_futures.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"net/http"
"slices"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -88,11 +89,7 @@ func (g *Gateio) WsFuturesConnect(ctx context.Context, conn stream.Connection) e
func (g *Gateio) GenerateFuturesDefaultSubscriptions(settlement currency.Code) (subscription.List, error) {
channelsToSubscribe := defaultFuturesSubscriptions
if g.Websocket.CanUseAuthenticatedEndpoints() {
channelsToSubscribe = append(channelsToSubscribe,
futuresOrdersChannel,
futuresUserTradesChannel,
futuresBalancesChannel,
)
channelsToSubscribe = append(channelsToSubscribe, futuresOrdersChannel, futuresUserTradesChannel, futuresBalancesChannel)
}

pairs, err := g.GetEnabledPairs(asset.Futures)
Expand All @@ -103,28 +100,17 @@ func (g *Gateio) GenerateFuturesDefaultSubscriptions(settlement currency.Code) (
return nil, err
}

switch {
case settlement.Equal(currency.USDT):
pairs = slices.DeleteFunc(pairs, func(p currency.Pair) bool { return !p.Quote.Equal(currency.USDT) })
case settlement.Equal(currency.BTC):
pairs = slices.DeleteFunc(pairs, func(p currency.Pair) bool { return p.Quote.Equal(currency.USDT) })
default:
return nil, fmt.Errorf("settlement currency %s not supported", settlement)
}

var subscriptions subscription.List
for i := range channelsToSubscribe {
switch {
case settlement.Equal(currency.USDT):
pairs, err = pairs.GetPairsByQuote(currency.USDT)
if err != nil {
return nil, err
}
case settlement.Equal(currency.BTC):
offset := 0
for x := range pairs {
if pairs[x].Quote.Equal(currency.USDT) {
continue // skip USDT pairs
}
pairs[offset] = pairs[x]
offset++
}
pairs = pairs[:offset]
default:
return nil, fmt.Errorf("settlement currency %s not supported", settlement)
}

for j := range pairs {
params := make(map[string]any)
switch channelsToSubscribe[i] {
Expand Down

0 comments on commit 3b2b15e

Please sign in to comment.