From d350ada492140e041424cddae08f0d71b62bba66 Mon Sep 17 00:00:00 2001 From: Gareth Kirwan Date: Thu, 21 Nov 2024 09:57:17 +0700 Subject: [PATCH] Subscriptions: Fix all asset subs erroring on no enabled assets Only subs with Empty asset should do anything when assets are empty. If asset is set to all and no assets are enabled, we should return nothing --- exchanges/bitmex/bitmex_test.go | 6 ++++++ exchanges/bitmex/bitmex_websocket.go | 14 +++++++------- exchanges/subscription/template.go | 9 ++++++--- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/exchanges/bitmex/bitmex_test.go b/exchanges/bitmex/bitmex_test.go index 79feea67ebb..16abafb4931 100644 --- a/exchanges/bitmex/bitmex_test.go +++ b/exchanges/bitmex/bitmex_test.go @@ -1127,6 +1127,12 @@ func TestGenerateSubscriptions(t *testing.T) { subs, err := b.generateSubscriptions() require.NoError(t, err, "generateSubscriptions must not error") testsubs.EqualLists(t, exp, subs) + + for _, a := range b.GetAssetTypes(true) { + b.CurrencyPairs.SetAssetEnabled(a, false) + } + _, err = b.generateSubscriptions() + require.NoError(t, err, "generateSubscriptions must not error when no assets are enabled") } func TestSubscribe(t *testing.T) { diff --git a/exchanges/bitmex/bitmex_websocket.go b/exchanges/bitmex/bitmex_websocket.go index a18fa161216..f539787cd1e 100644 --- a/exchanges/bitmex/bitmex_websocket.go +++ b/exchanges/bitmex/bitmex_websocket.go @@ -650,16 +650,16 @@ func channelName(s *subscription.Subscription, a asset.Item) string { const subTplText = ` {{- if $.S.Asset }} - {{ range $asset, $pairs := $.AssetPairs }} + {{- range $asset, $pairs := $.AssetPairs }} {{- with $name := channelName $.S $asset }} - {{- range $i, $p := $pairs -}} - {{- $name -}} : {{- $p -}} - {{ $.PairSeparator }} + {{- range $i, $p := $pairs }} + {{- $name -}} : {{- $p }} + {{- $.PairSeparator }} {{- end }} {{- end }} - {{ $.AssetSeparator }} + {{- $.AssetSeparator }} {{- end }} -{{- else -}} - {{ channelName $.S $.S.Asset }} +{{- else }} + {{- channelName $.S $.S.Asset }} {{- end }} ` diff --git a/exchanges/subscription/template.go b/exchanges/subscription/template.go index ae994f7919d..52634490eba 100644 --- a/exchanges/subscription/template.go +++ b/exchanges/subscription/template.go @@ -113,16 +113,19 @@ func expandTemplate(e iExchange, s *Subscription, ap assetPairs, assets asset.It switch s.Asset { case asset.All: + if len(ap) == 0 { + return List{}, nil // Nothing is enabled for any asset, only deliberately Empty asset subs can proceed + } subCtx.AssetPairs = ap default: + if s.Asset != asset.Empty && len(ap[s.Asset]) == 0 { + return List{}, nil // Nothing is enabled for this sub asset + } // This deliberately includes asset.Empty to harmonise handling subCtx.AssetPairs = assetPairs{ s.Asset: ap[s.Asset], } assets = asset.Items{s.Asset} - if s.Asset != asset.Empty && len(ap[s.Asset]) == 0 { - return List{}, nil // Nothing is enabled for this sub asset - } } buf := &bytes.Buffer{}