mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-02 23:16:51 +00:00
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:
@@ -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/trade"
|
||||
"github.com/thrasher-corp/gocryptotrader/log"
|
||||
)
|
||||
@@ -230,30 +231,30 @@ func (b *Bitstamp) handleWSOrder(wsResp *websocketResponse, msg []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Bitstamp) generateDefaultSubscriptions() ([]stream.ChannelSubscription, error) {
|
||||
func (b *Bitstamp) generateDefaultSubscriptions() ([]subscription.Subscription, error) {
|
||||
enabledCurrencies, err := b.GetEnabledPairs(asset.Spot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var subscriptions []stream.ChannelSubscription
|
||||
var subscriptions []subscription.Subscription
|
||||
for i := range enabledCurrencies {
|
||||
p, err := b.FormatExchangeCurrency(enabledCurrencies[i], asset.Spot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for j := range defaultSubChannels {
|
||||
subscriptions = append(subscriptions, stream.ChannelSubscription{
|
||||
Channel: defaultSubChannels[j] + "_" + p.String(),
|
||||
Asset: asset.Spot,
|
||||
Currency: p,
|
||||
subscriptions = append(subscriptions, subscription.Subscription{
|
||||
Channel: defaultSubChannels[j] + "_" + p.String(),
|
||||
Asset: asset.Spot,
|
||||
Pair: p,
|
||||
})
|
||||
}
|
||||
if b.Websocket.CanUseAuthenticatedEndpoints() {
|
||||
for j := range defaultAuthSubChannels {
|
||||
subscriptions = append(subscriptions, stream.ChannelSubscription{
|
||||
Channel: defaultAuthSubChannels[j] + "_" + p.String(),
|
||||
Asset: asset.Spot,
|
||||
Currency: p,
|
||||
subscriptions = append(subscriptions, subscription.Subscription{
|
||||
Channel: defaultAuthSubChannels[j] + "_" + p.String(),
|
||||
Asset: asset.Spot,
|
||||
Pair: p,
|
||||
Params: map[string]interface{}{
|
||||
"auth": struct{}{},
|
||||
},
|
||||
@@ -265,7 +266,7 @@ func (b *Bitstamp) generateDefaultSubscriptions() ([]stream.ChannelSubscription,
|
||||
}
|
||||
|
||||
// Subscribe sends a websocket message to receive data from the channel
|
||||
func (b *Bitstamp) Subscribe(channelsToSubscribe []stream.ChannelSubscription) error {
|
||||
func (b *Bitstamp) Subscribe(channelsToSubscribe []subscription.Subscription) error {
|
||||
var errs error
|
||||
var auth *WebsocketAuthResponse
|
||||
|
||||
@@ -303,7 +304,7 @@ func (b *Bitstamp) Subscribe(channelsToSubscribe []stream.ChannelSubscription) e
|
||||
}
|
||||
|
||||
// Unsubscribe sends a websocket message to stop receiving data from the channel
|
||||
func (b *Bitstamp) Unsubscribe(channelsToUnsubscribe []stream.ChannelSubscription) error {
|
||||
func (b *Bitstamp) Unsubscribe(channelsToUnsubscribe []subscription.Subscription) error {
|
||||
var errs error
|
||||
for i := range channelsToUnsubscribe {
|
||||
req := websocketEventRequest{
|
||||
|
||||
Reference in New Issue
Block a user