Skip to content

Commit

Permalink
Exchanges: Bybit Pairs support
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Feb 26, 2024
1 parent e372bd0 commit b1420bc
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
9 changes: 5 additions & 4 deletions exchanges/bybit/bybit_inverse_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"

"github.com/gorilla/websocket"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/stream"
"github.com/thrasher-corp/gocryptotrader/exchanges/subscription"
Expand Down Expand Up @@ -44,7 +45,7 @@ func (by *Bybit) GenerateInverseDefaultSubscriptions() ([]subscription.Subscript
subscriptions = append(subscriptions,
subscription.Subscription{
Channel: channels[x],
Pair: pairs[z],
Pairs: currency.Pairs{pairs[z]},
Asset: asset.CoinMarginedFutures,
})
}
Expand All @@ -53,16 +54,16 @@ func (by *Bybit) GenerateInverseDefaultSubscriptions() ([]subscription.Subscript
}

// InverseSubscribe sends a subscription message to linear public channels.
func (by *Bybit) InverseSubscribe(channelSubscriptions []subscription.Subscription) error {
func (by *Bybit) InverseSubscribe(channelSubscriptions subscription.List) error {
return by.handleInversePayloadSubscription("subscribe", channelSubscriptions)
}

// InverseUnsubscribe sends an unsubscription messages through linear public channels.
func (by *Bybit) InverseUnsubscribe(channelSubscriptions []subscription.Subscription) error {
func (by *Bybit) InverseUnsubscribe(channelSubscriptions subscription.List) error {
return by.handleInversePayloadSubscription("unsubscribe", channelSubscriptions)
}

func (by *Bybit) handleInversePayloadSubscription(operation string, channelSubscriptions []subscription.Subscription) error {
func (by *Bybit) handleInversePayloadSubscription(operation string, channelSubscriptions subscription.List) error {
payloads, err := by.handleSubscriptions(asset.CoinMarginedFutures, operation, channelSubscriptions)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion exchanges/bybit/bybit_linear_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (by *Bybit) GenerateLinearDefaultSubscriptions() ([]subscription.Subscripti
subscriptions = append(subscriptions,
subscription.Subscription{
Channel: channels[x],
Pair: pairs[p],
Pairs: currency.Pairs{pairs[p]},
Asset: a,
})
}
Expand Down
3 changes: 2 additions & 1 deletion exchanges/bybit/bybit_options_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strconv"

"github.com/gorilla/websocket"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
"github.com/thrasher-corp/gocryptotrader/exchanges/stream"
"github.com/thrasher-corp/gocryptotrader/exchanges/subscription"
Expand Down Expand Up @@ -51,7 +52,7 @@ func (by *Bybit) GenerateOptionsDefaultSubscriptions() ([]subscription.Subscript
subscriptions = append(subscriptions,
subscription.Subscription{
Channel: channels[x],
Pair: pairs[z],
Pairs: currency.Pairs{pairs[z]},
Asset: asset.Options,
})
}
Expand Down
17 changes: 9 additions & 8 deletions exchanges/bybit/bybit_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,11 @@ func (by *Bybit) WsAuth(ctx context.Context) error {
}

// Subscribe sends a websocket message to receive data from the channel
func (by *Bybit) Subscribe(channelsToSubscribe []subscription.Subscription) error {
func (by *Bybit) Subscribe(channelsToSubscribe subscription.List) error {
return by.handleSpotSubscription("subscribe", channelsToSubscribe)
}

func (by *Bybit) handleSubscriptions(assetType asset.Item, operation string, channelsToSubscribe []subscription.Subscription) ([]SubscriptionArgument, error) {
func (by *Bybit) handleSubscriptions(assetType asset.Item, operation string, channelsToSubscribe subscription.List) ([]SubscriptionArgument, error) {
var args []SubscriptionArgument
arg := SubscriptionArgument{
Operation: operation,
Expand Down Expand Up @@ -166,17 +166,18 @@ func (by *Bybit) handleSubscriptions(assetType asset.Item, operation string, cha
return nil, err
}
for i := range channelsToSubscribe {
pair := channelsToSubscribe[i].Pairs[0]
switch channelsToSubscribe[i].Channel {
case chanOrderbook:
arg.Arguments = append(arg.Arguments, fmt.Sprintf("%s.%d.%s", channelsToSubscribe[i].Channel, 50, channelsToSubscribe[i].Pair.Format(pairFormat).String()))
arg.Arguments = append(arg.Arguments, fmt.Sprintf("%s.%d.%s", channelsToSubscribe[i].Channel, 50, pair.Format(pairFormat).String()))
case chanPublicTrade, chanPublicTicker, chanLiquidation, chanLeverageTokenTicker, chanLeverageTokenNav:
arg.Arguments = append(arg.Arguments, channelsToSubscribe[i].Channel+"."+channelsToSubscribe[i].Pair.Format(pairFormat).String())
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+"."+channelsToSubscribe[i].Pair.Format(pairFormat).String())
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
Expand Down Expand Up @@ -204,11 +205,11 @@ func (by *Bybit) handleSubscriptions(assetType asset.Item, operation string, cha
}

// Unsubscribe sends a websocket message to stop receiving data from the channel
func (by *Bybit) Unsubscribe(channelsToUnsubscribe []subscription.Subscription) error {
func (by *Bybit) Unsubscribe(channelsToUnsubscribe subscription.List) error {
return by.handleSpotSubscription("unsubscribe", channelsToUnsubscribe)
}

func (by *Bybit) handleSpotSubscription(operation string, channelsToSubscribe []subscription.Subscription) error {
func (by *Bybit) handleSpotSubscription(operation string, channelsToSubscribe subscription.List) error {
payloads, err := by.handleSubscriptions(asset.Spot, operation, channelsToSubscribe)
if err != nil {
return err
Expand Down Expand Up @@ -275,7 +276,7 @@ func (by *Bybit) GenerateDefaultSubscriptions() ([]subscription.Subscription, er
subscriptions = append(subscriptions,
subscription.Subscription{
Channel: channels[x],
Pair: pairs[z],
Pairs: currency.Pairs{pairs[z]},
Asset: asset.Spot,
})
}
Expand Down

0 comments on commit b1420bc

Please sign in to comment.