2021-02-27 11:27:44 +00:00
|
|
|
package ftx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type messageHandler struct {
|
2021-03-02 14:18:41 +00:00
|
|
|
*types.StandardStream
|
2021-02-27 11:27:44 +00:00
|
|
|
}
|
|
|
|
|
2021-03-06 11:23:44 +00:00
|
|
|
func (h *messageHandler) handleMessage(message []byte) {
|
2021-03-27 11:40:23 +00:00
|
|
|
var r websocketResponse
|
2021-02-27 11:27:44 +00:00
|
|
|
if err := json.Unmarshal(message, &r); err != nil {
|
|
|
|
logger.WithError(err).Errorf("failed to unmarshal resp: %s", string(message))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-28 08:29:36 +00:00
|
|
|
if r.Type == errRespType {
|
|
|
|
logger.Errorf("receives err: %+v", r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Type == pongRespType {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-27 11:40:23 +00:00
|
|
|
switch r.Channel {
|
|
|
|
case orderBookChannel:
|
|
|
|
h.handleOrderBook(r)
|
2021-12-22 13:01:11 +00:00
|
|
|
case bookTickerChannel:
|
|
|
|
h.handleBookTicker(r)
|
2021-03-27 11:40:23 +00:00
|
|
|
case privateOrdersChannel:
|
2021-03-28 02:20:35 +00:00
|
|
|
h.handlePrivateOrders(r)
|
2021-03-28 06:42:54 +00:00
|
|
|
case privateTradesChannel:
|
|
|
|
h.handleTrades(r)
|
2021-02-27 11:27:44 +00:00
|
|
|
default:
|
2021-03-28 08:29:36 +00:00
|
|
|
logger.Warnf("unsupported message type: %+v", r.Type)
|
2021-02-27 11:27:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// {"type": "subscribed", "channel": "orderbook", "market": "BTC/USDT"}
|
2021-03-27 11:40:23 +00:00
|
|
|
func (h messageHandler) handleSubscribedMessage(response websocketResponse) {
|
|
|
|
r, err := response.toSubscribedResponse()
|
|
|
|
if err != nil {
|
|
|
|
logger.WithError(err).Errorf("failed to convert the subscribed message")
|
|
|
|
return
|
|
|
|
}
|
2021-03-28 02:20:35 +00:00
|
|
|
logger.Info(r)
|
2021-02-27 11:27:44 +00:00
|
|
|
}
|
2021-03-02 14:18:41 +00:00
|
|
|
|
2021-03-27 11:40:23 +00:00
|
|
|
func (h *messageHandler) handleOrderBook(response websocketResponse) {
|
|
|
|
if response.Type == subscribedRespType {
|
|
|
|
h.handleSubscribedMessage(response)
|
2021-03-02 14:18:41 +00:00
|
|
|
return
|
|
|
|
}
|
2021-03-27 11:40:23 +00:00
|
|
|
r, err := response.toPublicOrderBookResponse()
|
|
|
|
if err != nil {
|
|
|
|
logger.WithError(err).Errorf("failed to convert the public orderbook")
|
2021-03-04 00:55:52 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-06 11:23:44 +00:00
|
|
|
globalOrderBook, err := toGlobalOrderBook(r)
|
2021-03-03 14:33:06 +00:00
|
|
|
if err != nil {
|
2021-03-27 11:40:23 +00:00
|
|
|
logger.WithError(err).Errorf("failed to generate orderbook snapshot")
|
2021-03-03 14:33:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-04 00:55:52 +00:00
|
|
|
switch r.Type {
|
|
|
|
case partialRespType:
|
2021-03-06 11:23:44 +00:00
|
|
|
if err := r.verifyChecksum(); err != nil {
|
2021-03-27 11:40:23 +00:00
|
|
|
logger.WithError(err).Errorf("invalid orderbook snapshot")
|
2021-03-06 11:23:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
h.EmitBookSnapshot(globalOrderBook)
|
2021-03-04 00:55:52 +00:00
|
|
|
case updateRespType:
|
2021-03-06 11:23:44 +00:00
|
|
|
// emit updates, not the whole orderbook
|
|
|
|
h.EmitBookUpdate(globalOrderBook)
|
2021-03-04 00:55:52 +00:00
|
|
|
default:
|
2021-03-27 11:40:23 +00:00
|
|
|
logger.Errorf("unsupported order book data type %s", r.Type)
|
2021-12-22 13:01:11 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *messageHandler) handleBookTicker(response websocketResponse) {
|
|
|
|
if response.Type == subscribedRespType {
|
|
|
|
h.handleSubscribedMessage(response)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := response.toBookTickerResponse()
|
|
|
|
if err != nil {
|
|
|
|
logger.WithError(err).Errorf("failed to convert the book ticker")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
globalBookTicker, err := toGlobalBookTicker(r)
|
|
|
|
if err != nil {
|
|
|
|
logger.WithError(err).Errorf("failed to generate book ticker")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch r.Type {
|
|
|
|
case updateRespType:
|
|
|
|
// emit updates, not the whole orderbook
|
|
|
|
h.EmitBookTickerUpdate(globalBookTicker)
|
|
|
|
default:
|
|
|
|
logger.Errorf("unsupported book ticker data type %s", r.Type)
|
2021-03-04 00:55:52 +00:00
|
|
|
return
|
|
|
|
}
|
2021-03-02 14:18:41 +00:00
|
|
|
}
|
2021-03-28 02:20:35 +00:00
|
|
|
|
|
|
|
func (h *messageHandler) handlePrivateOrders(response websocketResponse) {
|
|
|
|
if response.Type == subscribedRespType {
|
|
|
|
h.handleSubscribedMessage(response)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := response.toOrderUpdateResponse()
|
|
|
|
if err != nil {
|
|
|
|
logger.WithError(err).Errorf("failed to convert the order update response")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-02 17:47:19 +00:00
|
|
|
globalOrder, err := toGlobalOrderNew(r.Data)
|
2021-03-28 02:20:35 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.WithError(err).Errorf("failed to convert order update to global order")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
h.EmitOrderUpdate(globalOrder)
|
|
|
|
}
|
2021-03-28 06:42:54 +00:00
|
|
|
|
|
|
|
func (h *messageHandler) handleTrades(response websocketResponse) {
|
|
|
|
if response.Type == subscribedRespType {
|
|
|
|
h.handleSubscribedMessage(response)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
r, err := response.toTradeUpdateResponse()
|
|
|
|
if err != nil {
|
|
|
|
logger.WithError(err).Errorf("failed to convert the trade update response")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t, err := toGlobalTrade(r.Data)
|
|
|
|
if err != nil {
|
|
|
|
logger.WithError(err).Errorf("failed to convert trade update to global trade ")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
h.EmitTradeUpdate(t)
|
|
|
|
}
|