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

@@ -17,6 +17,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/trade"
"github.com/thrasher-corp/gocryptotrader/log"
)
@@ -544,9 +545,9 @@ func (b *Bitmex) processOrderbook(data []OrderBookL2, action string, p currency.
}
// GenerateDefaultSubscriptions Adds default subscriptions to websocket to be handled by ManageSubscriptions()
func (b *Bitmex) GenerateDefaultSubscriptions() ([]stream.ChannelSubscription, error) {
func (b *Bitmex) GenerateDefaultSubscriptions() ([]subscription.Subscription, error) {
channels := []string{bitmexWSOrderbookL2, bitmexWSTrade}
subscriptions := []stream.ChannelSubscription{
subscriptions := []subscription.Subscription{
{
Channel: bitmexWSAnnouncement,
},
@@ -568,10 +569,10 @@ func (b *Bitmex) GenerateDefaultSubscriptions() ([]stream.ChannelSubscription, e
// There are no L2 orderbook for index assets
continue
}
subscriptions = append(subscriptions, stream.ChannelSubscription{
Channel: channels[z] + ":" + pFmt.Format(contracts[y]),
Currency: contracts[y],
Asset: assets[x],
subscriptions = append(subscriptions, subscription.Subscription{
Channel: channels[z] + ":" + pFmt.Format(contracts[y]),
Pair: contracts[y],
Asset: assets[x],
})
}
}
@@ -580,7 +581,7 @@ func (b *Bitmex) GenerateDefaultSubscriptions() ([]stream.ChannelSubscription, e
}
// GenerateAuthenticatedSubscriptions Adds authenticated subscriptions to websocket to be handled by ManageSubscriptions()
func (b *Bitmex) GenerateAuthenticatedSubscriptions() ([]stream.ChannelSubscription, error) {
func (b *Bitmex) GenerateAuthenticatedSubscriptions() ([]subscription.Subscription, error) {
if !b.Websocket.CanUseAuthenticatedEndpoints() {
return nil, nil
}
@@ -596,7 +597,7 @@ func (b *Bitmex) GenerateAuthenticatedSubscriptions() ([]stream.ChannelSubscript
channels := []string{bitmexWSExecution,
bitmexWSPosition,
}
subscriptions := []stream.ChannelSubscription{
subscriptions := []subscription.Subscription{
{
Channel: bitmexWSAffiliate,
},
@@ -618,10 +619,10 @@ func (b *Bitmex) GenerateAuthenticatedSubscriptions() ([]stream.ChannelSubscript
}
for i := range channels {
for j := range contracts {
subscriptions = append(subscriptions, stream.ChannelSubscription{
Channel: channels[i] + ":" + pFmt.Format(contracts[j]),
Currency: contracts[j],
Asset: asset.PerpetualContract,
subscriptions = append(subscriptions, subscription.Subscription{
Channel: channels[i] + ":" + pFmt.Format(contracts[j]),
Pair: contracts[j],
Asset: asset.PerpetualContract,
})
}
}
@@ -629,7 +630,7 @@ func (b *Bitmex) GenerateAuthenticatedSubscriptions() ([]stream.ChannelSubscript
}
// Subscribe subscribes to a websocket channel
func (b *Bitmex) Subscribe(channelsToSubscribe []stream.ChannelSubscription) error {
func (b *Bitmex) Subscribe(channelsToSubscribe []subscription.Subscription) error {
var subscriber WebsocketRequest
subscriber.Command = "subscribe"
for i := range channelsToSubscribe {
@@ -645,7 +646,7 @@ func (b *Bitmex) Subscribe(channelsToSubscribe []stream.ChannelSubscription) err
}
// Unsubscribe sends a websocket message to stop receiving data from the channel
func (b *Bitmex) Unsubscribe(channelsToUnsubscribe []stream.ChannelSubscription) error {
func (b *Bitmex) Unsubscribe(channelsToUnsubscribe []subscription.Subscription) error {
var unsubscriber WebsocketRequest
unsubscriber.Command = "unsubscribe"