Skip to content

Commit

Permalink
Huobi: Add subscription configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Aug 4, 2024
1 parent 0ea5c8d commit 221e3b3
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 35 deletions.
38 changes: 38 additions & 0 deletions exchanges/huobi/huobi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package huobi
import (
"context"
"errors"
"fmt"
"log"
"os"
"strconv"
Expand All @@ -26,6 +27,7 @@ import (
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/sharedtestvalues"
"github.com/thrasher-corp/gocryptotrader/exchanges/stream"
"github.com/thrasher-corp/gocryptotrader/exchanges/subscription"
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
testexch "github.com/thrasher-corp/gocryptotrader/internal/testing/exchange"
"github.com/thrasher-corp/gocryptotrader/portfolio/withdraw"
Expand Down Expand Up @@ -3010,3 +3012,39 @@ func TestGetCurrencyTradeURL(t *testing.T) {
}
}
}

func TestGenerateSubscriptions(t *testing.T) {
t.Parallel()

h := new(Huobi)

Check failure on line 3019 in exchanges/huobi/huobi_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: Huobi

Check failure on line 3019 in exchanges/huobi/huobi_test.go

View workflow job for this annotation

GitHub Actions / GoCryptoTrader back-end (ubuntu-latest, amd64, true, false)

undefined: Huobi

Check failure on line 3019 in exchanges/huobi/huobi_test.go

View workflow job for this annotation

GitHub Actions / GoCryptoTrader back-end (ubuntu-latest, 386, true, true)

undefined: Huobi

Check failure on line 3019 in exchanges/huobi/huobi_test.go

View workflow job for this annotation

GitHub Actions / GoCryptoTrader back-end (macos-latest, amd64, true, true)

undefined: Huobi

Check failure on line 3019 in exchanges/huobi/huobi_test.go

View workflow job for this annotation

GitHub Actions / GoCryptoTrader back-end (macos-13, amd64, true, true)

undefined: Huobi

Check failure on line 3019 in exchanges/huobi/huobi_test.go

View workflow job for this annotation

GitHub Actions / GoCryptoTrader back-end (windows-latest, amd64, true, true)

undefined: 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{}
for _, s := range h.Features.Subscriptions {
for _, a := range h.GetAssetTypes(true) {
if s.Asset != asset.All && s.Asset != a {
continue
}
pairs, err := h.GetEnabledPairs(a)
require.NoErrorf(t, err, "GetEnabledPairs %s must not error", a)
pairs = common.SortStrings(pairs).Format(currency.PairFormat{Uppercase: true, Delimiter: ""})
s := s.Clone() //nolint:govet // Intentional lexical scope shadow
s.Asset = a
for i, p := range pairs {
s := s.Clone() //nolint:govet // Intentional lexical scope shadow
switch s.Channel {
case subscription.OrderbookChannel:
s.QualifiedChannel = fmt.Sprintf("%s.%d.%s", channelName(s), s.Levels, p)

Check failure on line 3040 in exchanges/huobi/huobi_test.go

View workflow job for this annotation

GitHub Actions / lint

not enough arguments in call to channelName

Check failure on line 3040 in exchanges/huobi/huobi_test.go

View workflow job for this annotation

GitHub Actions / GoCryptoTrader back-end (ubuntu-latest, amd64, true, false)

not enough arguments in call to channelName

Check failure on line 3040 in exchanges/huobi/huobi_test.go

View workflow job for this annotation

GitHub Actions / GoCryptoTrader back-end (ubuntu-latest, 386, true, true)

not enough arguments in call to channelName

Check failure on line 3040 in exchanges/huobi/huobi_test.go

View workflow job for this annotation

GitHub Actions / GoCryptoTrader back-end (macos-latest, amd64, true, true)

not enough arguments in call to channelName

Check failure on line 3040 in exchanges/huobi/huobi_test.go

View workflow job for this annotation

GitHub Actions / GoCryptoTrader back-end (macos-13, amd64, true, true)

not enough arguments in call to channelName

Check failure on line 3040 in exchanges/huobi/huobi_test.go

View workflow job for this annotation

GitHub Actions / GoCryptoTrader back-end (windows-latest, amd64, true, true)

not enough arguments in call to channelName
default:
s.QualifiedChannel = channelName(s) + "." + p.String()

Check failure on line 3042 in exchanges/huobi/huobi_test.go

View workflow job for this annotation

GitHub Actions / lint

not enough arguments in call to channelName

Check failure on line 3042 in exchanges/huobi/huobi_test.go

View workflow job for this annotation

GitHub Actions / GoCryptoTrader back-end (ubuntu-latest, amd64, true, false)

not enough arguments in call to channelName

Check failure on line 3042 in exchanges/huobi/huobi_test.go

View workflow job for this annotation

GitHub Actions / GoCryptoTrader back-end (ubuntu-latest, 386, true, true)

not enough arguments in call to channelName

Check failure on line 3042 in exchanges/huobi/huobi_test.go

View workflow job for this annotation

GitHub Actions / GoCryptoTrader back-end (macos-latest, amd64, true, true)

not enough arguments in call to channelName

Check failure on line 3042 in exchanges/huobi/huobi_test.go

View workflow job for this annotation

GitHub Actions / GoCryptoTrader back-end (macos-13, amd64, true, true)

not enough arguments in call to channelName

Check failure on line 3042 in exchanges/huobi/huobi_test.go

View workflow job for this annotation

GitHub Actions / GoCryptoTrader back-end (windows-latest, amd64, true, true)

not enough arguments in call to channelName
}
s.Pairs = pairs[i : i+1]
exp = append(exp, s)
}
}
}
testsubs.EqualLists(t, exp, subs)

Check failure on line 3049 in exchanges/huobi/huobi_test.go

View workflow job for this annotation

GitHub Actions / lint

undefined: testsubs (typecheck)

Check failure on line 3049 in exchanges/huobi/huobi_test.go

View workflow job for this annotation

GitHub Actions / GoCryptoTrader back-end (ubuntu-latest, amd64, true, false)

undefined: testsubs

Check failure on line 3049 in exchanges/huobi/huobi_test.go

View workflow job for this annotation

GitHub Actions / GoCryptoTrader back-end (ubuntu-latest, 386, true, true)

undefined: testsubs

Check failure on line 3049 in exchanges/huobi/huobi_test.go

View workflow job for this annotation

GitHub Actions / GoCryptoTrader back-end (macos-latest, amd64, true, true)

undefined: testsubs

Check failure on line 3049 in exchanges/huobi/huobi_test.go

View workflow job for this annotation

GitHub Actions / GoCryptoTrader back-end (macos-13, amd64, true, true)

undefined: testsubs

Check failure on line 3049 in exchanges/huobi/huobi_test.go

View workflow job for this annotation

GitHub Actions / GoCryptoTrader back-end (windows-latest, amd64, true, true)

undefined: testsubs
}
94 changes: 60 additions & 34 deletions exchanges/huobi/huobi_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/url"
"strconv"
"strings"
"text/template"
"time"

"github.com/gorilla/websocket"
Expand All @@ -17,6 +18,7 @@ import (
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/exchanges/account"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-corp/gocryptotrader/exchanges/stream"
Expand All @@ -30,11 +32,13 @@ const (
baseWSURL = "wss://api.huobi.pro"
futuresWSURL = "wss://api.hbdm.com/"

wsMarketURL = baseWSURL + "/ws"
wsMarketKline = "market.%s.kline.1min"
wsMarketDepth = "market.%s.depth.step0"
wsMarketTrade = "market.%s.trade.detail"
wsMarketTicker = "market.%s.detail"
wsMarketURL = baseWSURL + "/ws"
wsCandlesChannel = "market.%s.kline"
wsOrderbookChannel = "market.%s.depth"
wsTradesChannel = "market.%s.trade.detail"
wsMarketDetailChannel = "market.%s.detail"
wsMyOrdersChannel = "orders.%s"
wsMyTradesChannel = "orders.%s.update"

wsAccountsOrdersEndPoint = "/ws/v1"
wsAccountsList = "accounts.list"
Expand All @@ -56,6 +60,24 @@ const (
rateLimit = 20
)

var defaultSubscriptions = subscription.List{
{Enabled: true, Asset: asset.All, Channel: subscription.TickerChannel},
{Enabled: true, Asset: asset.All, Channel: subscription.CandlesChannel, Interval: kline.OneMin},
{Enabled: true, Asset: asset.All, Channel: subscription.OrderbookChannel, Levels: 0},
{Enabled: true, Asset: asset.All, Channel: subscription.AllTradesChannel},
{Enabled: true, Asset: asset.All, Channel: subscription.MyOrdersChannel, Authenticated: true},
{Enabled: true, Asset: asset.All, Channel: subscription.MyTradesChannel, Authenticated: true},
}

var subscriptionNames = map[string]string{
subscription.TickerChannel: wsMarketDetailChannel,
subscription.CandlesChannel: wsCandlesChannel,
subscription.OrderbookChannel: wsOrderbookChannel,
subscription.AllTradesChannel: wsTradesChannel,
subscription.MyTradesChannel: wsMyOrdersChannel,
subscription.MyOrdersChannel: wsMyTradesChannel,
}

// Instantiates a communications channel between websocket connections
var comms = make(chan WsMessage)

Expand Down Expand Up @@ -514,35 +536,17 @@ func (h *HUOBI) WsProcessOrderbook(update *WsDepth, symbol string) error {
return h.Websocket.Orderbook.LoadSnapshot(&newOrderBook)
}

// GenerateDefaultSubscriptions Adds default subscriptions to websocket to be handled by ManageSubscriptions()
func (h *HUOBI) GenerateDefaultSubscriptions() (subscription.List, error) {
var channels = []string{wsMarketKline,
wsMarketDepth,
wsMarketTrade,
wsMarketTicker}
var subscriptions subscription.List
if h.Websocket.CanUseAuthenticatedEndpoints() {
channels = append(channels, "orders.%v", "orders.%v.update")
subscriptions = append(subscriptions, &subscription.Subscription{
Channel: "accounts",
})
}
enabledCurrencies, err := h.GetEnabledPairs(asset.Spot)
if err != nil {
return nil, err
}
for i := range channels {
for j := range enabledCurrencies {
enabledCurrencies[j].Delimiter = ""
channel := fmt.Sprintf(channels[i],
enabledCurrencies[j].Lower().String())
subscriptions = append(subscriptions, &subscription.Subscription{
Channel: channel,
Pairs: currency.Pairs{enabledCurrencies[j]},
})
}
}
return subscriptions, nil
// generateSubscriptions returns a list of subscriptions from the configured subscriptions feature
func (h *HUOBI) generateSubscriptions() (subscription.List, error) {
return h.Features.Subscriptions.ExpandTemplates(h)
}

// GetSubscriptionTemplate returns a subscription channel template
func (h *HUOBI) GetSubscriptionTemplate(_ *subscription.Subscription) (*template.Template, error) {
return template.New("master.tmpl").Funcs(template.FuncMap{
"channelName": channelName,
"interval": h.FormatExchangeKlineInterval,
}).Parse(subTplText)
}

// Subscribe sends a websocket message to receive data from the channel
Expand Down Expand Up @@ -810,3 +814,25 @@ 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
// 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
}
return fmt.Sprintf(name, p)
}

const subTplText = `
{{ range $asset, $pairs := $.AssetPairs }}
{{- range $p := $pairs -}}
{{- chanelName $.S $p -}}
{{- if eq $.S.Channel eq "candles" -}} . {{- interval $.S.Interval }}
{{- if eq $.S.Channel eq "orderbook" -}} .step {{- interval $Levels }}
{{ $.PairSeparator }}
{{- end }}
{{ $.AssetSeparator }}
{{- end }}
`
3 changes: 2 additions & 1 deletion exchanges/huobi/huobi_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ func (h *HUOBI) SetDefaults() {
GlobalResultLimit: 2000,
},
},
Subscriptions: defaultSubscriptions.Clone(),
}

h.Requester, err = request.New(h.Name,
Expand Down Expand Up @@ -212,7 +213,7 @@ func (h *HUOBI) Setup(exch *config.Exchange) error {
Connector: h.WsConnect,
Subscriber: h.Subscribe,
Unsubscriber: h.Unsubscribe,
GenerateSubscriptions: h.GenerateDefaultSubscriptions,
GenerateSubscriptions: h.generateSubscriptions,
Features: &h.Features.Supports.WebsocketCapabilities,
})
if err != nil {
Expand Down

0 comments on commit 221e3b3

Please sign in to comment.