mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 15:09:42 +00:00
subscriptions: Encapsulate, replace Pair with Pairs and refactor; improve exchange support
* Websocket: Use ErrSubscribedAlready instead of errChannelAlreadySubscribed * Subscriptions: Replace Pair with Pairs Given that some subscriptions have multiple pairs, support that as the standard. * Docs: Update subscriptions in add new exch * RPC: Update Subscription Pairs * Linter: Disable testifylint.Len We deliberately use Equal over Len to avoid spamming the contents of large Slices * Websocket: Add suffix to state consts * Binance: Subscription Pairs support * Bitfinex: Subscription Pairs support * Bithumb: Subscription Pairs support * Bitmex: Subscription Pairs support * Bitstamp: Subscription Pairs support * BTCMarkets: Subscription Pairs support * BTSE: Subscription Pairs support * Coinbase: Subscription Pairs support * Coinut: Subscription Pairs support * GateIO: Subscription Pairs support * Gemini: Subscription Pairs support and improvement * Hitbtc: Subscription Pairs support * Huboi: Subscription Pairs support * Kucoin: Subscription Pairs support * Okcoin: Subscription Pairs support * Poloniex: Subscription Pairs support * Kraken: Add subscription Pairs support Note: This is a naieve implementation because we want to rebase the kraken websocket rewrite on top of this * Bybit: Subscription Pairs support * Okx: Subscription Pairs support * Bitmex: Subsription configuration * Fixes unauthenticated websocket left as CanUseAuth * Fixes auth subs happening privately * CoinbasePro: Subscription Configuration * Consolidate ProductIDs when all subscriptions are for the same list * Websocket: Log actual sent message when Verbose * Subscriptions: Improve clarity of which key is which in Match * Subscriptions: Lint fix for HugeParam * Subscriptions: Add AddPairs and move keys from test * Subscriptions: Simplify subscription keys and add key types * Subscriptions: Add List.GroupPairs Rename sub.AddPairs * Subscription: Fix ExactKey not matching 0 pairs * Subscriptions: Remove unused IdentityKey and HasPairKey * Subscriptions: Fix GetKey test * Subscriptions: Test coverage improvements * Websocket: Change State on Add/Remove * Subscriptions: Improve error context * Subscriptions: Fix Enable: false subs not ignored * Bitfinex: Fix WsAuth test failing on DataHandler DataHandler is eaten by dataMonitor now, so we need to use ToRoutine * Deribit: Subscription Pairs support * Websocket: Accept nil lists for checkSubscriptions If the user passes in a nil (implicitly empty) list, we would not panic. Therefore the burden of correctness about that data lies with them. The list of subscriptions is empty, and that's okay, and possibly convenient * Websocket: Add context to NilPointer errors * Subscriptions: Add context to nil errors * Exchange: Fix error expectations in UnsubToWSChans
This commit is contained in:
@@ -721,7 +721,7 @@ func (f *FTX) WsConnect() error {
|
||||
}
|
||||
}
|
||||
// Generates the default subscription set, based off enabled pairs.
|
||||
subs, err := f.GenerateDefaultSubscriptions()
|
||||
subs, err := f.generateSubscriptions()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -733,10 +733,10 @@ func (f *FTX) WsConnect() error {
|
||||
- Create function to generate default subscriptions:
|
||||
|
||||
```go
|
||||
// GenerateDefaultSubscriptions generates default subscription
|
||||
func (f *FTX) GenerateDefaultSubscriptions() ([]subscription.Subscription, error) {
|
||||
var subscriptions []subscription.Subscription
|
||||
subscriptions = append(subscriptions, subscription.Subscription{
|
||||
// generateSubscriptions generates default subscription
|
||||
func (f *FTX) generateSubscriptions() (subscription.List, error) {
|
||||
var subscriptions subscription.List
|
||||
subscriptions = append(subscriptions, &subscription.Subscription{
|
||||
Channel: wsMarkets,
|
||||
})
|
||||
// Ranges over available channels, pairs and asset types to produce a full
|
||||
@@ -754,9 +754,9 @@ func (f *FTX) GenerateDefaultSubscriptions() ([]subscription.Subscription, error
|
||||
"-")
|
||||
for x := range channels {
|
||||
subscriptions = append(subscriptions,
|
||||
subscription.Subscription{
|
||||
&subscription.Subscription{
|
||||
Channel: channels[x],
|
||||
Pair: newPair,
|
||||
Pair: currency.Pairs{newPair},
|
||||
Asset: assets[a],
|
||||
})
|
||||
}
|
||||
@@ -766,9 +766,7 @@ func (f *FTX) GenerateDefaultSubscriptions() ([]subscription.Subscription, error
|
||||
if f.IsWebsocketAuthenticationSupported() {
|
||||
var authchan = []string{wsOrders, wsFills}
|
||||
for x := range authchan {
|
||||
subscriptions = append(subscriptions, subscription.Subscription{
|
||||
Channel: authchan[x],
|
||||
})
|
||||
subscriptions = append(subscriptions, &subscription.Subscription{Channel: authchan[x]})
|
||||
}
|
||||
}
|
||||
return subscriptions, nil
|
||||
@@ -809,7 +807,7 @@ type WsSub struct {
|
||||
|
||||
```go
|
||||
// Subscribe sends a websocket message to receive data from the channel
|
||||
func (f *FTX) Subscribe(channelsToSubscribe []subscription.Subscription) error {
|
||||
func (f *FTX) Subscribe(channelsToSubscribe subscription.List) error {
|
||||
// For subscriptions we try to batch as much as possible to limit the amount
|
||||
// of connection usage but sometimes this is not supported on the exchange
|
||||
// API.
|
||||
@@ -825,13 +823,8 @@ channels:
|
||||
case wsFills, wsOrders, wsMarkets:
|
||||
// Authenticated wsFills && wsOrders or wsMarkets which is a channel subscription for the full set of tradable markets do not need a currency pair association.
|
||||
default:
|
||||
a, err := f.GetPairAssetType(channelsToSubscribe[i].Pair)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
continue channels
|
||||
}
|
||||
// Ensures our outbound currency pair is formatted correctly, sometimes our configuration format is different from what our request format needs to be.
|
||||
formattedPair, err := f.FormatExchangeCurrency(channelsToSubscribe[i].Pair, a)
|
||||
formattedPair, err := f.FormatExchangeCurrency(channelsToSubscribe[i].Pair, channelsToSubscribe[i].Asset)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
continue channels
|
||||
@@ -846,10 +839,7 @@ channels:
|
||||
// When we have a successful subscription, we can alert our internal management system of the success.
|
||||
f.Websocket.AddSuccessfulSubscriptions(channelsToSubscribe[i])
|
||||
}
|
||||
if errs != nil {
|
||||
return errs
|
||||
}
|
||||
return nil
|
||||
return errs
|
||||
}
|
||||
```
|
||||
|
||||
@@ -1063,7 +1053,7 @@ func (f *FTX) WsAuth(ctx context.Context) error {
|
||||
|
||||
```go
|
||||
// Unsubscribe sends a websocket message to stop receiving data from the channel
|
||||
func (f *FTX) Unsubscribe(channelsToUnsubscribe []subscription.Subscription) error {
|
||||
func (f *FTX) Unsubscribe(channelsToUnsubscribe subscription.List) error {
|
||||
// As with subscribing we want to batch as much as possible, but sometimes this cannot be achieved due to API shortfalls.
|
||||
var errs common.Errors
|
||||
channels:
|
||||
@@ -1074,13 +1064,7 @@ channels:
|
||||
switch channelsToUnsubscribe[i].Channel {
|
||||
case wsFills, wsOrders, wsMarkets:
|
||||
default:
|
||||
a, err := f.GetPairAssetType(channelsToUnsubscribe[i].Pair)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
continue channels
|
||||
}
|
||||
|
||||
formattedPair, err := f.FormatExchangeCurrency(channelsToUnsubscribe[i].Pair, a)
|
||||
formattedPair, err := f.FormatExchangeCurrency(channelsToUnsubscribe[i].Pair, channelsToUnsubscribe[i].Asset)
|
||||
if err != nil {
|
||||
errs = append(errs, err)
|
||||
continue channels
|
||||
@@ -1135,8 +1119,8 @@ func (f *FTX) Setup(exch *config.Exchange) error {
|
||||
Subscriber: f.Subscribe,
|
||||
// Unsubscriber function outlined above.
|
||||
UnSubscriber: f.Unsubscribe,
|
||||
// GenerateDefaultSubscriptions function outlined above.
|
||||
GenerateSubscriptions: f.GenerateDefaultSubscriptions,
|
||||
// GenerateSubscriptions function outlined above.
|
||||
GenerateSubscriptions: f.generateSubscriptions,
|
||||
// Defines the capabilities of the websocket outlined in supported
|
||||
// features struct. This allows the websocket connection to be flushed
|
||||
// appropriately if we have a pair/asset enable/disable change. This is
|
||||
|
||||
Reference in New Issue
Block a user