From c2dfb37efde51fa1bab18b8f782bd00a063f7675 Mon Sep 17 00:00:00 2001 From: Gareth Kirwan Date: Thu, 10 Oct 2024 04:48:16 +0100 Subject: [PATCH] Subscriptions: Add List.Public, List.Enabled and List.Private (#1629) --- exchanges/exchange.go | 7 +----- exchanges/kucoin/kucoin_websocket.go | 7 +----- exchanges/subscription/list.go | 33 ++++++++++++++++++++++++++++ exchanges/subscription/list_test.go | 31 ++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 12 deletions(-) diff --git a/exchanges/exchange.go b/exchanges/exchange.go index ff6c2ab4..3599b5e9 100644 --- a/exchanges/exchange.go +++ b/exchanges/exchange.go @@ -172,12 +172,7 @@ func (b *Base) SetSubscriptionsFromConfig() { // Set config from the defaults, including any disabled subscriptions b.Config.Features.Subscriptions = b.Features.Subscriptions } - b.Features.Subscriptions = subscription.List{} - for _, s := range b.Config.Features.Subscriptions { - if s.Enabled { - b.Features.Subscriptions = append(b.Features.Subscriptions, s) - } - } + b.Features.Subscriptions = b.Config.Features.Subscriptions.Enabled() if b.Verbose { names := make([]string, 0, len(b.Features.Subscriptions)) for _, s := range b.Features.Subscriptions { diff --git a/exchanges/kucoin/kucoin_websocket.go b/exchanges/kucoin/kucoin_websocket.go index 5c79c8d6..27c71925 100644 --- a/exchanges/kucoin/kucoin_websocket.go +++ b/exchanges/kucoin/kucoin_websocket.go @@ -1658,12 +1658,7 @@ func (ku *Kucoin) checkSubscriptions() { return false }) if upgraded { - ku.Features.Subscriptions = subscription.List{} - for _, s := range ku.Config.Features.Subscriptions { - if s.Enabled { - ku.Features.Subscriptions = append(ku.Features.Subscriptions, s) - } - } + ku.Features.Subscriptions = ku.Config.Features.Subscriptions.Enabled() } } diff --git a/exchanges/subscription/list.go b/exchanges/subscription/list.go index f250010d..37d886c9 100644 --- a/exchanges/subscription/list.go +++ b/exchanges/subscription/list.go @@ -113,3 +113,36 @@ func (l List) assetPairs(e iExchange) (assetPairs, error) { } return ap, nil } + +// Enabled returns a new list of only enabled subscriptions +func (l List) Enabled() List { + n := make(List, 0, len(l)) + for _, s := range l { + if s.Enabled { + n = append(n, s) + } + } + return slices.Clip(n) +} + +// Private returns only subscriptions which require authentication +func (l List) Private() List { + n := List{} + for _, s := range l { + if s.Authenticated { + n = append(n, s) + } + } + return n +} + +// Public returns only subscriptions which do not require authentication +func (l List) Public() List { + n := List{} + for _, s := range l { + if !s.Authenticated { + n = append(n, s) + } + } + return n +} diff --git a/exchanges/subscription/list_test.go b/exchanges/subscription/list_test.go index cd92d171..62df6681 100644 --- a/exchanges/subscription/list_test.go +++ b/exchanges/subscription/list_test.go @@ -106,3 +106,34 @@ func TestListClone(t *testing.T) { l[0].Interval = kline.OneHour assert.NotEqual(t, n[0], l[0], "Subscriptions should be cloned") } + +var filterable = List{ + {Channel: "a", Enabled: true, Authenticated: false}, + {Channel: "b", Enabled: true, Authenticated: true}, + {Channel: "c", Enabled: false, Authenticated: true}, + {Channel: "d", Enabled: false, Authenticated: false}, +} + +func TestListEnabled(t *testing.T) { + t.Parallel() + l := filterable.Enabled() + require.Len(t, l, 2) + assert.Equal(t, filterable[:2], l) + assert.Len(t, filterable, 4) +} + +func TestListPublic(t *testing.T) { + t.Parallel() + l := filterable.Public() + require.Len(t, l, 2) + assert.Equal(t, List{filterable[0], filterable[3]}, l) + assert.Len(t, filterable, 4) +} + +func TestListPrivate(t *testing.T) { + t.Parallel() + l := filterable.Private() + require.Len(t, l, 2) + assert.Equal(t, filterable[1:3], l) + assert.Len(t, filterable, 4) +}