From 4fc1bf0ad15324b527be0da5a7d58abcb2f5963f Mon Sep 17 00:00:00 2001 From: Gareth Kirwan Date: Wed, 23 Oct 2024 03:05:47 +0200 Subject: [PATCH] Kucoin: Abstract a subscriptionNames for assets solution (#1669) --- exchanges/kucoin/kucoin_test.go | 20 +++++++++++++++++ exchanges/kucoin/kucoin_websocket.go | 33 +++++++++++++++++----------- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/exchanges/kucoin/kucoin_test.go b/exchanges/kucoin/kucoin_test.go index e059e8fc..86c3ac38 100644 --- a/exchanges/kucoin/kucoin_test.go +++ b/exchanges/kucoin/kucoin_test.go @@ -4335,3 +4335,23 @@ func TestCancelBatchOrders(t *testing.T) { _, err := ku.CancelBatchOrders(context.Background(), nil) assert.ErrorIs(t, common.ErrFunctionNotSupported, err) } + +func TestChannelName(t *testing.T) { + t.Parallel() + for _, tt := range []struct { + a asset.Item + ch string + exp string + }{ + {asset.Futures, futuresOrderbookDepth50Channel, futuresOrderbookDepth50Channel}, + {asset.Futures, subscription.OrderbookChannel, futuresOrderbookDepth5Channel}, + {asset.Futures, subscription.CandlesChannel, marketCandlesChannel}, + {asset.Futures, subscription.TickerChannel, futuresTickerChannel}, + {asset.Spot, subscription.OrderbookChannel, marketOrderbookDepth5Channel}, + {asset.Spot, subscription.AllTradesChannel, marketMatchChannel}, + {asset.Spot, subscription.CandlesChannel, marketCandlesChannel}, + {asset.Spot, subscription.TickerChannel, marketTickerChannel}, + } { + assert.Equal(t, tt.exp, channelName(&subscription.Subscription{Channel: tt.ch}, tt.a)) + } +} diff --git a/exchanges/kucoin/kucoin_websocket.go b/exchanges/kucoin/kucoin_websocket.go index ed97c1ab..8b4aa535 100644 --- a/exchanges/kucoin/kucoin_websocket.go +++ b/exchanges/kucoin/kucoin_websocket.go @@ -88,6 +88,19 @@ var ( maxWSOrderbookWorkers = 10 ) +var subscriptionNames = map[asset.Item]map[string]string{ + asset.Futures: { + subscription.TickerChannel: futuresTickerChannel, + subscription.OrderbookChannel: futuresOrderbookDepth5Channel, // This does not require a REST request to get the orderbook. + }, + asset.All: { + subscription.TickerChannel: marketTickerChannel, + subscription.OrderbookChannel: marketOrderbookDepth5Channel, // This does not require a REST request to get the orderbook. + subscription.CandlesChannel: marketCandlesChannel, + subscription.AllTradesChannel: marketMatchChannel, + }, +} + var defaultSubscriptions = subscription.List{ {Enabled: true, Asset: asset.All, Channel: subscription.TickerChannel}, {Enabled: true, Asset: asset.All, Channel: subscription.OrderbookChannel, Interval: kline.HundredMilliseconds}, @@ -1664,21 +1677,15 @@ func (ku *Kucoin) checkSubscriptions() { // channelName returns the correct channel name for the asset func channelName(s *subscription.Subscription, a asset.Item) string { - switch s.Channel { - case subscription.TickerChannel: - if a == asset.Futures { - return futuresTickerChannel + if byAsset, hasAsset := subscriptionNames[a]; hasAsset { + if name, ok := byAsset[s.Channel]; ok { + return name } - return marketTickerChannel - case subscription.OrderbookChannel: - if a == asset.Futures { - return futuresOrderbookDepth5Channel + } + if allAssets, hasAll := subscriptionNames[asset.All]; hasAll { + if name, ok := allAssets[s.Channel]; ok { + return name } - return marketOrderbookDepth5Channel // This does not require a REST request to get the orderbook. - case subscription.CandlesChannel: - return marketCandlesChannel // No support in GCT yet for Futures candles - case subscription.AllTradesChannel: - return marketMatchChannel // No support in GCT yet for Futures all trades } return s.Channel }