From 86b8b4c036280e56d64a457b1fe6a5020a3fcd45 Mon Sep 17 00:00:00 2001 From: Gareth Kirwan Date: Mon, 4 Nov 2024 09:48:45 +0700 Subject: [PATCH] fixup! Huobi: Add subscription configuration --- exchanges/huobi/huobi_test.go | 16 ++++++++++++--- exchanges/huobi/huobi_websocket.go | 33 +++++++++++++++++++----------- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/exchanges/huobi/huobi_test.go b/exchanges/huobi/huobi_test.go index cba55a94d2d..7206169cc6b 100644 --- a/exchanges/huobi/huobi_test.go +++ b/exchanges/huobi/huobi_test.go @@ -2935,8 +2935,6 @@ func TestGenerateSubscriptions(t *testing.T) { h := new(HUOBI) require.NoError(t, testexch.Setup(h), "Test instance Setup must not error") - - h.Websocket.SetCanUseAuthenticatedEndpoints(true) subs, err := h.generateSubscriptions() require.NoError(t, err, "generateSubscriptions must not error") exp := subscription.List{} @@ -2967,14 +2965,26 @@ func TestGenerateSubscriptions(t *testing.T) { testsubs.EqualLists(t, exp, subs) } +// TestSubscribe exercises live public subscriptions func TestSubscribe(t *testing.T) { t.Parallel() h := new(HUOBI) require.NoError(t, testexch.Setup(h), "Test instance Setup must not error") subs, err := h.Features.Subscriptions.ExpandTemplates(h) require.NoError(t, err, "ExpandTemplates must not error") - h.Features.Subscriptions = subscription.List{} testexch.SetupWs(t, h) err = h.Subscribe(subs) require.NoError(t, err, "Subscribe must not error") + got := h.Websocket.GetSubscriptions() + require.Equal(t, 4, len(got), "Should get correct number of subscriptions") + for _, s := range got { + assert.Equal(t, subscription.SubscribedState, s.State()) + } +} + +func TestChannelName(t *testing.T) { + p := currency.NewPair(currency.BTC, currency.USD) + assert.Equal(t, "market.BTCUSD.kline", channelName(&subscription.Subscription{Channel: subscription.CandlesChannel}, p)) + assert.Panics(t, func() { channelName(&subscription.Subscription{Channel: wsOrderbookChannel}, p) }) + assert.Panics(t, func() { channelName(&subscription.Subscription{Channel: subscription.MyAccountChannel}, p) }, "Should panic on V2 endpoints until implemented") } diff --git a/exchanges/huobi/huobi_websocket.go b/exchanges/huobi/huobi_websocket.go index 8294c3f6614..7f654c22822 100644 --- a/exchanges/huobi/huobi_websocket.go +++ b/exchanges/huobi/huobi_websocket.go @@ -75,8 +75,11 @@ var subscriptionNames = map[string]string{ subscription.CandlesChannel: wsCandlesChannel, subscription.OrderbookChannel: wsOrderbookChannel, subscription.AllTradesChannel: wsTradesChannel, + /* TODO: Pending upcoming V2 support, these are dropped from the translation table so that the sub conf will be correct and not need upgrading, but will error on usage subscription.MyTradesChannel: wsMyOrdersChannel, subscription.MyOrdersChannel: wsMyTradesChannel, + subscription.MyAccountChannel: wsMyAccountChannel, + */ } // Instantiates a communications channel between websocket connections @@ -799,24 +802,30 @@ func (h *HUOBI) wsGetOrderDetails(ctx context.Context, orderID string) (*WsAuthe return &response, nil } -// channelName converts global channel Names used in config of channel input into bitmex channel names +// channelName converts global channel Names used in config of channel input into exchange channel names // returns the name unchanged if no match is found func channelName(s *subscription.Subscription, p currency.Pair) string { - name := s.Channel - if n, ok := subscriptionNames[name]; ok { - name = n + if n, ok := subscriptionNames[s.Channel]; ok { + return fmt.Sprintf(n, p) } - return fmt.Sprintf(name, p) + if s.Authenticated { + panic(fmt.Errorf("%w: Private endpoints not currently supported", common.ErrNotYetImplemented)) + } + panic(subscription.ErrPrivateChannelName) } const subTplText = ` -{{ range $asset, $pairs := $.AssetPairs }} - {{- range $p := $pairs -}} - {{- channelName $.S $p -}} - {{- if eq $.S.Channel "candles" -}} . {{- interval $.S.Interval }}{{ end }} - {{- if eq $.S.Channel "orderbook" -}} .step {{- $.S.Levels }}{{ end }} - {{ $.PairSeparator }} +{{- if $.S.Asset }} + {{ range $asset, $pairs := $.AssetPairs }} + {{- range $p := $pairs }} + {{- channelName $.S $p -}} + {{- if eq $.S.Channel "candles" -}} . {{- interval $.S.Interval }}{{ end }} + {{- if eq $.S.Channel "orderbook" -}} .step {{- $.S.Levels }}{{ end }} + {{ $.PairSeparator }} + {{- end }} + {{ $.AssetSeparator }} {{- end }} - {{ $.AssetSeparator }} +{{- else -}} + {{ channelName $.S nil }} {{- end }} `