bbgo_origin/pkg/exchange/ftx/stream_message_handler.go

153 lines
3.5 KiB
Go
Raw Permalink Normal View History

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) {
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
}
switch r.Channel {
case orderBookChannel:
h.handleOrderBook(r)
case bookTickerChannel:
h.handleBookTicker(r)
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"}
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
func (h *messageHandler) handleOrderBook(response websocketResponse) {
if response.Type == subscribedRespType {
h.handleSubscribedMessage(response)
2021-03-02 14:18:41 +00:00
return
}
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)
if err != nil {
logger.WithError(err).Errorf("failed to generate orderbook snapshot")
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 {
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:
logger.Errorf("unsupported order book data type %s", r.Type)
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
}
globalOrder, err := toGlobalOrder(r.Data)
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)
}