exchanges/websocket: Implement subscription configuration (#1394)

* Websockets: Move Subscription to its own package

This allows the small type to be imported from both `config` and from
`stream` without an import cycle, so we don't have to repeat ourselves

* Subs: Renamed Currency to Pair

This was being mis-used through much of the code, and since we're
already touching everything, we might as well fix it

* Websockets: Add Subscription configuration

* Binance: Add subscription configuration

* Kucoin: Subscription configuration

* Simplify GenerateDefaultSubs
* Improve TestGenSubs coverage
* Test Candle Sub generation
* Support Candle intervals
* Full responsibility for formatting Channel name on GenerateDefaultSubs
  OR consumer of Subscribe
* Simplify generatePayloads as a result
* Fix test coverage of asset types in processMarketSnapshot

* Exchanges: Abstract ParallelChanOp

* Tests: Generic ws mock instances

* Kucoin: Fix intermittent conflict in test currs

Use isolated test instance for `TestGetOpenInterest`.

`TestGetOpenInterest` would occassionally change pairs before
GenerateDefault Subs.
This commit is contained in:
Gareth Kirwan
2024-01-24 05:54:07 +01:00
committed by GitHub
parent 301551ac20
commit e007f69f7c
67 changed files with 3705 additions and 3167 deletions

View File

@@ -18,6 +18,7 @@ import (
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
"github.com/thrasher-corp/gocryptotrader/exchanges/orderbook"
"github.com/thrasher-corp/gocryptotrader/exchanges/stream"
"github.com/thrasher-corp/gocryptotrader/exchanges/subscription"
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
"github.com/thrasher-corp/gocryptotrader/exchanges/trade"
"github.com/thrasher-corp/gocryptotrader/log"
@@ -465,15 +466,15 @@ func (h *HitBTC) WsProcessOrderbookUpdate(update *WsOrderbook) error {
}
// GenerateDefaultSubscriptions Adds default subscriptions to websocket to be handled by ManageSubscriptions()
func (h *HitBTC) GenerateDefaultSubscriptions() ([]stream.ChannelSubscription, error) {
func (h *HitBTC) GenerateDefaultSubscriptions() ([]subscription.Subscription, error) {
var channels = []string{"subscribeTicker",
"subscribeOrderbook",
"subscribeTrades",
"subscribeCandles"}
var subscriptions []stream.ChannelSubscription
var subscriptions []subscription.Subscription
if h.Websocket.CanUseAuthenticatedEndpoints() {
subscriptions = append(subscriptions, stream.ChannelSubscription{
subscriptions = append(subscriptions, subscription.Subscription{
Channel: "subscribeReports",
})
}
@@ -489,10 +490,10 @@ func (h *HitBTC) GenerateDefaultSubscriptions() ([]stream.ChannelSubscription, e
}
enabledCurrencies[j].Delimiter = ""
subscriptions = append(subscriptions, stream.ChannelSubscription{
Channel: channels[i],
Currency: fPair,
Asset: asset.Spot,
subscriptions = append(subscriptions, subscription.Subscription{
Channel: channels[i],
Pair: fPair,
Asset: asset.Spot,
})
}
}
@@ -500,7 +501,7 @@ func (h *HitBTC) GenerateDefaultSubscriptions() ([]stream.ChannelSubscription, e
}
// Subscribe sends a websocket message to receive data from the channel
func (h *HitBTC) Subscribe(channelsToSubscribe []stream.ChannelSubscription) error {
func (h *HitBTC) Subscribe(channelsToSubscribe []subscription.Subscription) error {
var errs error
for i := range channelsToSubscribe {
subscribe := WsRequest{
@@ -508,8 +509,8 @@ func (h *HitBTC) Subscribe(channelsToSubscribe []stream.ChannelSubscription) err
ID: h.Websocket.Conn.GenerateMessageID(false),
}
if channelsToSubscribe[i].Currency.String() != "" {
subscribe.Params.Symbol = channelsToSubscribe[i].Currency.String()
if channelsToSubscribe[i].Pair.String() != "" {
subscribe.Params.Symbol = channelsToSubscribe[i].Pair.String()
}
if strings.EqualFold(channelsToSubscribe[i].Channel, "subscribeTrades") {
subscribe.Params.Limit = 100
@@ -532,7 +533,7 @@ func (h *HitBTC) Subscribe(channelsToSubscribe []stream.ChannelSubscription) err
}
// Unsubscribe sends a websocket message to stop receiving data from the channel
func (h *HitBTC) Unsubscribe(channelsToUnsubscribe []stream.ChannelSubscription) error {
func (h *HitBTC) Unsubscribe(channelsToUnsubscribe []subscription.Subscription) error {
var errs error
for i := range channelsToUnsubscribe {
unsubscribeChannel := strings.Replace(channelsToUnsubscribe[i].Channel,
@@ -545,7 +546,7 @@ func (h *HitBTC) Unsubscribe(channelsToUnsubscribe []stream.ChannelSubscription)
Method: unsubscribeChannel,
}
unsubscribe.Params.Symbol = channelsToUnsubscribe[i].Currency.String()
unsubscribe.Params.Symbol = channelsToUnsubscribe[i].Pair.String()
if strings.EqualFold(unsubscribeChannel, "unsubscribeTrades") {
unsubscribe.Params.Limit = 100
} else if strings.EqualFold(unsubscribeChannel, "unsubscribeCandles") {