Skip to content

Commit

Permalink
fixup! Huobi: Add subscription configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Nov 4, 2024
1 parent ef0ae7d commit 86b8b4c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
16 changes: 13 additions & 3 deletions exchanges/huobi/huobi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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")
}
33 changes: 21 additions & 12 deletions exchanges/huobi/huobi_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }}
`

0 comments on commit 86b8b4c

Please sign in to comment.