Files
gocryptotrader/exchanges/bybit/bybit_websocket.go
Gareth Kirwan f9437dbd08 Bitfinex: Websocket subscription improvements (#1353)
* Websockets: Add keys to websocket subscriptions

* This switches all RO uses of the mutex to use a RLock method.
* The mutex used for discrete field access has had scope drift from
  name 'connectionMutex' so rename to more appropriate fieldsMutex
* The mutex used for Set/CanUseAuthEndpoints moves from the
  subscriptions endpoint to the fieldsMutex
* Add GetSubscription by key
* Expose stream.Matcher type

* Bitfinex: Subscribe and Unsubscribe atomicly

* Fix Auth failures ignored
* This change makes it so that Subscribe and Unsubscribe wait for success
** Tells the DataHandler about errors
** Errors are returned to consumers
* Subscribes concurrently to the channels
* It also simplifies the chanId to stream mapping
* Removes unable to locate chanID: %d errors which are just noise
* Paves the way for unified channelSubscription id handling
* Adds support for subId for Book subscriptions, which is more robust

* Vastly simplifies what we need to test TestWsSubscribedResponse
This test was working to ensure that the various fancy key parsing
mechanisms all worked. Now that we use subId, we just need a thorough
test of that
* Expose Match.Set in order to capture websocket incoming data
Can't see another way of doing this. Doesn't seem too bad

* Allow tests to run with auth or WS
These flags made it difficult to run the tests whilst working on
websockets

* Enable API auth and WS in testconfig
This change minimises the changes requires for a full test run against
live endpoints, so that new contributors have a clearer testing path.
I cannot see any reason to turn WS off and Auth endpoints off when we're
not going to run API tests without Creds being set, and we're not going
to do live fire tests without canManipulateRealOrders

* TestWsSubscribe and various fixes
** Enables the websocket for live non-authed integration tests by default
** Adds an integration test for subscriptions
** Changes the Ws tests to respect canManipulateRealOrders
** Uses WsConnect instead of setupWs; fixes seqNo config not sent for WS tests
** Allows api creds to live in config/testdata.json which might be
  less likely to accidentally commit, and less obtrusive

* Bitfinex: Support period and timeframe for Candles

* Fixes manual Subscribe() symbol or key formatting
* Unifies handling of params for DefaultSubscriptions and manual
  subsrciptions

* Bitfinex: Handle conf and info WS channel events

* Bitfinex: Better tests for subscriptions

* fixup! Websockets: Add keys to websocket subscriptions

* fixup! Bitfinex: Subscribe and Unsubscribe atomicly

* fixup! Websockets: Add keys to websocket subscriptions

* Websockets: Add Pending subscription status

Add a status tracker so that Sub/Unsub can prevent duplicates,
and also fixes when first message comes before we have added the sub
to the tracker

* Websockets: Add State instead of pending

This change allows more clarity about the current state and
checks for specifically already Unsubing

* Bitfinex: Fix first sub message maybe lost

The only link we have between a sub req and the sub resp is the subID.
And the only link we have between a sub message and the sub is the chanID.
We can't derive a link using Pair or anything else.

This meant that by sending the resp and its chanID down the IncomingData
channel, we allowed the channel reader to maybe process the next
message, the first message on the channel, before the runtime executed
the switch back to subscribeToChan waiting on the chan.

To fix this, we key initially on subId.(string), and then replace it
with chanId.(int64) when we have it *inside* the wsHandleData so we
know we've procedurally handled it before the next message.

subscribeToChan is then free to remove the subId keyed Sub regardless of
error or not

If there's an error, we don't need to inline handling because there
won't be any second update.

Expands test coverage to make sure those subId keyed subscriptions are
removed.

* Websocket: Validate state in SetChanState

* fixup! Bitfinex: Fix first sub message maybe lost

* Websockets: Rename RemoveUnsuccessfulSubs

Implementation doesn't imply Unsuccessful or need to.
This change supports the registering of Pending subs

* Bitfinex: Fix race in Tests
2023-11-02 12:10:43 +11:00

574 lines
15 KiB
Go

package bybit
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"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/asset"
"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/ticker"
"github.com/thrasher-corp/gocryptotrader/exchanges/trade"
"github.com/thrasher-corp/gocryptotrader/log"
)
const (
bybitWSBaseURL = "wss://stream.bybit.com/"
wsSpotPublicTopicV2 = "spot/quote/ws/v2"
wsSpotPrivate = "spot/ws"
bybitWebsocketTimer = 20 * time.Second
wsOrderbook = "depth"
wsTicker = "bookTicker"
wsTrades = "trade"
wsKlines = "kline"
wsAccountInfo = "outboundAccountInfo"
wsOrderExecution = "executionReport"
wsTickerInfo = "ticketInfo"
sub = "sub" // event for subscribe
cancel = "cancel" // event for unsubscribe
)
var comms = make(chan stream.Response)
// WsConnect connects to a websocket feed
func (by *Bybit) WsConnect() error {
if !by.Websocket.IsEnabled() || !by.IsEnabled() || !by.IsAssetWebsocketSupported(asset.Spot) {
return errors.New(stream.WebsocketNotEnabled)
}
var dialer websocket.Dialer
err := by.Websocket.Conn.Dial(&dialer, http.Header{})
if err != nil {
return err
}
by.Websocket.Conn.SetupPingHandler(stream.PingHandler{
MessageType: websocket.TextMessage,
Message: []byte(`{"op":"ping"}`),
Delay: bybitWebsocketTimer,
})
by.Websocket.Wg.Add(1)
go by.wsReadData(by.Websocket.Conn)
if by.IsWebsocketAuthenticationSupported() {
err = by.WsAuth(context.TODO())
if err != nil {
by.Websocket.DataHandler <- err
by.Websocket.SetCanUseAuthenticatedEndpoints(false)
}
}
by.Websocket.Wg.Add(1)
go by.WsDataHandler()
return nil
}
// WsAuth sends an authentication message to receive auth data
func (by *Bybit) WsAuth(ctx context.Context) error {
var dialer websocket.Dialer
err := by.Websocket.AuthConn.Dial(&dialer, http.Header{})
if err != nil {
return err
}
by.Websocket.AuthConn.SetupPingHandler(stream.PingHandler{
MessageType: websocket.TextMessage,
Message: []byte(`{"op":"ping"}`),
Delay: bybitWebsocketTimer,
})
by.Websocket.Wg.Add(1)
go by.wsReadData(by.Websocket.AuthConn)
creds, err := by.GetCredentials(ctx)
if err != nil {
return err
}
intNonce := (time.Now().Unix() + 1) * 1000
strNonce := strconv.FormatInt(intNonce, 10)
hmac, err := crypto.GetHMAC(
crypto.HashSHA256,
[]byte("GET/realtime"+strNonce),
[]byte(creds.Secret),
)
if err != nil {
return err
}
sign := crypto.HexEncodeToString(hmac)
req := Authenticate{
Operation: "auth",
Args: []interface{}{creds.Key, intNonce, sign},
}
return by.Websocket.AuthConn.SendJSONMessage(req)
}
// Subscribe sends a websocket message to receive data from the channel
func (by *Bybit) Subscribe(channelsToSubscribe []stream.ChannelSubscription) error {
var errs error
for i := range channelsToSubscribe {
var subReq WsReq
subReq.Topic = channelsToSubscribe[i].Channel
subReq.Event = sub
formattedPair, err := by.FormatExchangeCurrency(channelsToSubscribe[i].Currency, asset.Spot)
if err != nil {
errs = common.AppendError(errs, err)
continue
}
if channelsToSubscribe[i].Channel == wsKlines {
subReq.Parameters = WsParams{
Symbol: formattedPair.String(),
IsBinary: true,
KlineType: "1m",
}
} else {
subReq.Parameters = WsParams{
Symbol: formattedPair.String(),
IsBinary: true,
}
}
err = by.Websocket.Conn.SendJSONMessage(subReq)
if err != nil {
errs = common.AppendError(errs, err)
continue
}
by.Websocket.AddSuccessfulSubscriptions(channelsToSubscribe[i])
}
return errs
}
// Unsubscribe sends a websocket message to stop receiving data from the channel
func (by *Bybit) Unsubscribe(channelsToUnsubscribe []stream.ChannelSubscription) error {
var errs error
for i := range channelsToUnsubscribe {
var unSub WsReq
unSub.Event = cancel
unSub.Topic = channelsToUnsubscribe[i].Channel
formattedPair, err := by.FormatExchangeCurrency(channelsToUnsubscribe[i].Currency, asset.Spot)
if err != nil {
errs = common.AppendError(errs, err)
continue
}
unSub.Parameters = WsParams{
Symbol: formattedPair.String(),
}
err = by.Websocket.Conn.SendJSONMessage(unSub)
if err != nil {
errs = common.AppendError(errs, err)
continue
}
by.Websocket.RemoveSubscriptions(channelsToUnsubscribe[i])
}
return errs
}
// wsReadData receives and passes on websocket messages for processing
func (by *Bybit) wsReadData(ws stream.Connection) {
defer by.Websocket.Wg.Done()
for {
resp := ws.ReadMessage()
if resp.Raw == nil {
return
}
comms <- resp
}
}
// GenerateDefaultSubscriptions generates default subscription
func (by *Bybit) GenerateDefaultSubscriptions() ([]stream.ChannelSubscription, error) {
var subscriptions []stream.ChannelSubscription
var channels = []string{wsTicker, wsTrades, wsOrderbook, wsKlines}
pairs, err := by.GetEnabledPairs(asset.Spot)
if err != nil {
return nil, err
}
for z := range pairs {
for x := range channels {
subscriptions = append(subscriptions,
stream.ChannelSubscription{
Channel: channels[x],
Currency: pairs[z],
Asset: asset.Spot,
})
}
}
return subscriptions, nil
}
func stringToOrderStatus(status string) (order.Status, error) {
switch status {
case "NEW":
return order.New, nil
case "CANCELED":
return order.Cancelled, nil
case "REJECTED":
return order.Rejected, nil
case "TRADE":
return order.PartiallyFilled, nil
case "EXPIRED":
return order.Expired, nil
default:
return order.UnknownStatus, errors.New(status + " not recognised as order status")
}
}
// WsDataHandler handles data from wsReadData
func (by *Bybit) WsDataHandler() {
defer by.Websocket.Wg.Done()
for {
select {
case <-by.Websocket.ShutdownC:
return
case resp := <-comms:
err := by.wsHandleData(resp.Raw)
if err != nil {
by.Websocket.DataHandler <- err
}
}
}
}
func (by *Bybit) wsHandleData(respRaw []byte) error {
var result interface{}
err := json.Unmarshal(respRaw, &result)
if err != nil {
return err
}
switch d := result.(type) {
case map[string]interface{}:
if method, ok := d["event"].(string); ok {
if strings.EqualFold(method, sub) {
return nil
}
if strings.EqualFold(method, cancel) {
return nil
}
}
if t, ok := d["topic"].(string); ok {
switch t {
case wsOrderbook:
var data WsOrderbook
err := json.Unmarshal(respRaw, &data)
if err != nil {
return err
}
p, enabled, err := by.MatchSymbolCheckEnabled(data.OBData.Symbol, asset.Spot, false)
if err != nil {
return err
}
if !enabled {
return nil
}
err = by.wsUpdateOrderbook(&data.OBData, p, asset.Spot)
if err != nil {
return err
}
return nil
case wsTrades:
if !by.IsSaveTradeDataEnabled() {
return nil
}
var data WsTrade
err := json.Unmarshal(respRaw, &data)
if err != nil {
return err
}
p, enabled, err := by.MatchSymbolCheckEnabled(data.Parameters.Symbol, asset.Spot, false)
if err != nil {
return err
}
if !enabled {
return nil
}
side := order.Sell
if data.TradeData.Side {
side = order.Buy
}
return trade.AddTradesToBuffer(by.Name, trade.Data{
Timestamp: data.TradeData.Time.Time(),
CurrencyPair: p,
AssetType: asset.Spot,
Exchange: by.Name,
Price: data.TradeData.Price.Float64(),
Amount: data.TradeData.Size.Float64(),
Side: side,
TID: data.TradeData.ID,
})
case wsTicker:
var data WsSpotTicker
err := json.Unmarshal(respRaw, &data)
if err != nil {
return err
}
p, enabled, err := by.MatchSymbolCheckEnabled(data.Ticker.Symbol, asset.Spot, false)
if err != nil {
return err
}
if !enabled {
return nil
}
by.Websocket.DataHandler <- &ticker.Price{
ExchangeName: by.Name,
Bid: data.Ticker.Bid.Float64(),
Ask: data.Ticker.Ask.Float64(),
LastUpdated: data.Ticker.Time.Time(),
AssetType: asset.Spot,
Pair: p,
}
return nil
case wsKlines:
var data KlineStream
err := json.Unmarshal(respRaw, &data)
if err != nil {
return err
}
p, enabled, err := by.MatchSymbolCheckEnabled(data.Kline.Symbol, asset.Spot, false)
if err != nil {
return err
}
if !enabled {
return nil
}
by.Websocket.DataHandler <- stream.KlineData{
Pair: p,
AssetType: asset.Spot,
Exchange: by.Name,
StartTime: data.Kline.StartTime.Time(),
Interval: data.Parameters.KlineType,
OpenPrice: data.Kline.OpenPrice.Float64(),
ClosePrice: data.Kline.ClosePrice.Float64(),
HighPrice: data.Kline.HighPrice.Float64(),
LowPrice: data.Kline.LowPrice.Float64(),
Volume: data.Kline.Volume.Float64(),
}
return nil
default:
by.Websocket.DataHandler <- stream.UnhandledMessageWarning{Message: by.Name + stream.UnhandledMessage + string(respRaw)}
}
}
if m, ok := d["auth"]; ok {
log.Infof(log.WebsocketMgr, "%v received auth response: %v", by.Name, m)
return nil
}
if m, ok := d["pong"]; ok {
log.Infof(log.WebsocketMgr, "%v received pong: %v", by.Name, m)
return nil
}
case []interface{}:
for i := range d {
obj, ok := d[i].(map[string]interface{})
if !ok {
return common.GetTypeAssertError("map[string]interface{}", d[i])
}
e, ok := obj["e"].(string)
if !ok {
return common.GetTypeAssertError("string", obj["e"])
}
switch e {
case wsAccountInfo:
var data []wsAccount
err := json.Unmarshal(respRaw, &data)
if err != nil {
return fmt.Errorf("%v - Could not convert to outboundAccountInfo structure %w",
by.Name,
err)
}
by.Websocket.DataHandler <- data
return nil
case wsOrderExecution:
var data []wsOrderUpdate
err := json.Unmarshal(respRaw, &data)
if err != nil {
return fmt.Errorf("%v - Could not convert to executionReport structure %w",
by.Name,
err)
}
for j := range data {
oType, err := order.StringToOrderType(data[j].OrderType)
if err != nil {
by.Websocket.DataHandler <- order.ClassificationError{
Exchange: by.Name,
OrderID: data[j].OrderID,
Err: err,
}
}
var oSide order.Side
oSide, err = order.StringToOrderSide(data[j].Side)
if err != nil {
by.Websocket.DataHandler <- order.ClassificationError{
Exchange: by.Name,
OrderID: data[j].OrderID,
Err: err,
}
}
var oStatus order.Status
oStatus, err = stringToOrderStatus(data[j].OrderStatus)
if err != nil {
by.Websocket.DataHandler <- order.ClassificationError{
Exchange: by.Name,
OrderID: data[j].OrderID,
Err: err,
}
}
p, enabled, err := by.MatchSymbolCheckEnabled(data[j].Symbol, asset.Spot, false)
if err != nil {
return err
}
if !enabled {
continue
}
by.Websocket.DataHandler <- order.Detail{
Price: data[j].Price.Float64(),
Amount: data[j].Quantity.Float64(),
ExecutedAmount: data[j].CumulativeFilledQuantity.Float64(),
RemainingAmount: data[j].Quantity.Float64() - data[j].CumulativeFilledQuantity.Float64(),
Exchange: by.Name,
OrderID: data[j].OrderID,
Type: oType,
Side: oSide,
Status: oStatus,
AssetType: asset.Spot,
Date: data[j].OrderCreationTime.Time(),
Pair: p,
ClientOrderID: data[j].ClientOrderID,
Trades: []order.TradeHistory{
{
Price: data[j].Price.Float64(),
Amount: data[j].Quantity.Float64(),
Exchange: by.Name,
Timestamp: data[j].OrderCreationTime.Time(),
},
},
}
}
return nil
case wsTickerInfo:
var data []wsOrderFilled
err := json.Unmarshal(respRaw, &data)
if err != nil {
return fmt.Errorf("%v - Could not convert to ticketInfo structure %w",
by.Name,
err)
}
for j := range data {
var oSide order.Side
oSide, err = order.StringToOrderSide(data[j].Side)
if err != nil {
by.Websocket.DataHandler <- order.ClassificationError{
Exchange: by.Name,
OrderID: data[j].OrderID,
Err: err,
}
}
p, enabled, err := by.MatchSymbolCheckEnabled(data[j].Symbol, asset.Spot, false)
if err != nil {
return err
}
if !enabled {
continue
}
by.Websocket.DataHandler <- &order.Detail{
Exchange: by.Name,
OrderID: data[j].OrderID,
Side: oSide,
AssetType: asset.Spot,
Pair: p,
Price: data[j].Price.Float64(),
Amount: data[j].Quantity.Float64(),
Date: data[j].Timestamp.Time(),
Trades: []order.TradeHistory{
{
Price: data[j].Price.Float64(),
Amount: data[j].Quantity.Float64(),
Exchange: by.Name,
Timestamp: data[j].Timestamp.Time(),
TID: data[j].TradeID,
IsMaker: data[j].IsMaker,
},
},
}
}
return nil
}
}
}
return fmt.Errorf("unhandled stream data %s", string(respRaw))
}
func (by *Bybit) wsUpdateOrderbook(update *WsOrderbookData, p currency.Pair, assetType asset.Item) error {
if update == nil || (len(update.Asks) == 0 && len(update.Bids) == 0) {
return errors.New("no orderbook data")
}
asks := make([]orderbook.Item, len(update.Asks))
for i := range update.Asks {
target, err := strconv.ParseFloat(update.Asks[i][0], 64)
if err != nil {
return err
}
amount, err := strconv.ParseFloat(update.Asks[i][1], 64)
if err != nil {
return err
}
asks[i] = orderbook.Item{Price: target, Amount: amount}
}
bids := make([]orderbook.Item, len(update.Bids))
for i := range update.Bids {
target, err := strconv.ParseFloat(update.Bids[i][0], 64)
if err != nil {
return err
}
amount, err := strconv.ParseFloat(update.Bids[i][1], 64)
if err != nil {
return err
}
bids[i] = orderbook.Item{Price: target, Amount: amount}
}
return by.Websocket.Orderbook.LoadSnapshot(&orderbook.Base{
Bids: bids,
Asks: asks,
Pair: p,
LastUpdated: update.Time.Time(),
Asset: assetType,
Exchange: by.Name,
VerifyOrderbook: by.CanVerifyOrderbook,
})
}