Skip to content

Commit

Permalink
fixup! Bybit: Subscription configuration for spot
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Aug 3, 2024
1 parent ca0f844 commit b4796b8
Showing 1 changed file with 26 additions and 63 deletions.
89 changes: 26 additions & 63 deletions exchanges/bybit/bybit_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (
"time"

"github.com/gorilla/websocket"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/common/crypto"
"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/fill"
"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 Down Expand Up @@ -157,73 +157,36 @@ func (by *Bybit) Subscribe(channelsToSubscribe subscription.List) error {
return by.handleSpotSubscription("subscribe", channelsToSubscribe)
}

func (by *Bybit) handleSubscriptions(assetType asset.Item, operation string, channelsToSubscribe subscription.List) ([]SubscriptionArgument, error) {
var args []SubscriptionArgument
arg := SubscriptionArgument{
Operation: operation,
RequestID: strconv.FormatInt(by.Websocket.Conn.GenerateMessageID(false), 10),
Arguments: []string{},
}
authArg := SubscriptionArgument{
auth: true,
Operation: operation,
RequestID: strconv.FormatInt(by.Websocket.Conn.GenerateMessageID(false), 10),
Arguments: []string{},
}

var selectedChannels, positions, execution, order, wallet, greeks, dCP = 0, 1, 2, 3, 4, 5, 6
chanMap := map[string]int{
chanPositions: positions,
chanExecution: execution,
chanOrder: order,
chanWallet: wallet,
chanGreeks: greeks,
chanDCP: dCP}

pairFormat, err := by.GetPairFormat(assetType, true)
func (by *Bybit) handleSubscriptions(assetType asset.Item, operation string, subs subscription.List) (args []SubscriptionArgument, err error) {

Check failure on line 160 in exchanges/bybit/bybit_websocket.go

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 'assetType' seems to be unused, consider removing or renaming it as _ (revive)
subs, err = subs.ExpandTemplates(by)
if err != nil {
return nil, err
}
for i := range channelsToSubscribe {
if len(channelsToSubscribe[i].Pairs) != 1 {
return nil, subscription.ErrNotSinglePair
}
pair := channelsToSubscribe[i].Pairs[0]
switch channelsToSubscribe[i].Channel {
case chanOrderbook:
arg.Arguments = append(arg.Arguments, fmt.Sprintf("%s.%d.%s", channelsToSubscribe[i].Channel, channelsToSubscribe[i].Levels, pair.Format(pairFormat).String()))
case chanPublicTrade, chanPublicTicker, chanLiquidation, chanLeverageTokenTicker, chanLeverageTokenNav:
arg.Arguments = append(arg.Arguments, channelsToSubscribe[i].Channel+"."+pair.Format(pairFormat).String())
case chanKline, chanLeverageTokenKline:
interval, err := intervalToString(kline.FiveMin)
if err != nil {
return nil, err
}
arg.Arguments = append(arg.Arguments, channelsToSubscribe[i].Channel+"."+interval+"."+pair.Format(pairFormat).String())
case chanPositions, chanExecution, chanOrder, chanWallet, chanGreeks, chanDCP:
if chanMap[channelsToSubscribe[i].Channel]&selectedChannels > 0 {
continue
}
authArg.Arguments = append(authArg.Arguments, channelsToSubscribe[i].Channel)
// adding the channel to selected channels so that we will not visit it again.
selectedChannels |= chanMap[channelsToSubscribe[i].Channel]
}
if len(arg.Arguments) >= 10 {
args = append(args, arg)
arg = SubscriptionArgument{
Operation: operation,
RequestID: strconv.FormatInt(by.Websocket.Conn.GenerateMessageID(false), 10),
Arguments: []string{},
}
return
}
chans := []string{}
authChans := []string{}
for _, s := range subs {
if s.Authenticated {
authChans = append(authChans, s.QualifiedChannel)
} else {
chans = append(chans, s.QualifiedChannel)
}
}
if len(arg.Arguments) != 0 {
args = append(args, arg)
for _, b := range common.Batch(chans, 10) {
args = append(args, SubscriptionArgument{
Operation: operation,
RequestID: strconv.FormatInt(by.Websocket.Conn.GenerateMessageID(false), 10),
Arguments: b,
})
}
if len(authArg.Arguments) != 0 {
args = append(args, authArg)
if len(authChans) != 0 {
args = append(args, SubscriptionArgument{
auth: true,
Operation: operation,
RequestID: strconv.FormatInt(by.Websocket.Conn.GenerateMessageID(false), 10),
Arguments: authChans,
})
}
return args, nil
return
}

// Unsubscribe sends a websocket message to stop receiving data from the channel
Expand Down

0 comments on commit b4796b8

Please sign in to comment.