From 9c4ccbd6e22486754b70c180bb1a6391d297b8ad Mon Sep 17 00:00:00 2001 From: ycdesu Date: Sun, 28 Mar 2021 10:20:35 +0800 Subject: [PATCH] ftx: subscribe order update --- pkg/exchange/ftx/stream_message_handler.go | 23 +++++++++++++++++++++- pkg/exchange/ftx/websocket_messages.go | 6 +++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/pkg/exchange/ftx/stream_message_handler.go b/pkg/exchange/ftx/stream_message_handler.go index 5752f9fb7..a5b308681 100644 --- a/pkg/exchange/ftx/stream_message_handler.go +++ b/pkg/exchange/ftx/stream_message_handler.go @@ -21,6 +21,7 @@ func (h *messageHandler) handleMessage(message []byte) { case orderBookChannel: h.handleOrderBook(r) case privateOrdersChannel: + h.handlePrivateOrders(r) default: logger.Errorf("unsupported message type: %+v", r.Type) } @@ -33,7 +34,7 @@ func (h messageHandler) handleSubscribedMessage(response websocketResponse) { logger.WithError(err).Errorf("failed to convert the subscribed message") return } - logger.Infof("%s %s is subscribed", r.Market, r.Channel) + logger.Info(r) } func (h *messageHandler) handleOrderBook(response websocketResponse) { @@ -68,3 +69,23 @@ func (h *messageHandler) handleOrderBook(response websocketResponse) { return } } + +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) +} diff --git a/pkg/exchange/ftx/websocket_messages.go b/pkg/exchange/ftx/websocket_messages.go index 443964570..b263447d8 100644 --- a/pkg/exchange/ftx/websocket_messages.go +++ b/pkg/exchange/ftx/websocket_messages.go @@ -113,7 +113,7 @@ type orderUpdateResponse struct { } func (r websocketResponse) toOrderUpdateResponse() (orderUpdateResponse, error) { - if r.Type != subscribedRespType || r.Channel != privateOrdersChannel { + if r.Channel != privateOrdersChannel { return orderUpdateResponse{}, fmt.Errorf("type %s, channel %s: %w", r.Type, r.Channel, errUnsupportedConversion) } var o orderUpdateResponse @@ -138,6 +138,10 @@ type subscribedResponse struct { Market string `json:"market"` } +func (s subscribedResponse) String() string { + return fmt.Sprintf("`%s` channel is subsribed", strings.TrimSpace(fmt.Sprintf("%s %s", s.Market, s.Channel))) +} + // {"type": "subscribed", "channel": "orderbook", "market": "BTC/USDT"} func (r websocketResponse) toSubscribedResponse() (subscribedResponse, error) { if r.Type != subscribedRespType {