-
-
Notifications
You must be signed in to change notification settings - Fork 296
/
stream_message_handler.go
122 lines (105 loc) · 2.81 KB
/
stream_message_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package ftx
import (
"encoding/json"
"github.com/c9s/bbgo/pkg/types"
)
type messageHandler struct {
*types.StandardStream
}
func (h *messageHandler) handleMessage(message []byte) {
var r websocketResponse
if err := json.Unmarshal(message, &r); err != nil {
logger.WithError(err).Errorf("failed to unmarshal resp: %s", string(message))
return
}
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 privateOrdersChannel:
h.handlePrivateOrders(r)
case privateTradesChannel:
h.handleTrades(r)
default:
logger.Warnf("unsupported message type: %+v", r.Type)
}
}
// {"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
}
logger.Info(r)
}
func (h *messageHandler) handleOrderBook(response websocketResponse) {
if response.Type == subscribedRespType {
h.handleSubscribedMessage(response)
return
}
r, err := response.toPublicOrderBookResponse()
if err != nil {
logger.WithError(err).Errorf("failed to convert the public orderbook")
return
}
globalOrderBook, err := toGlobalOrderBook(r)
if err != nil {
logger.WithError(err).Errorf("failed to generate orderbook snapshot")
return
}
switch r.Type {
case partialRespType:
if err := r.verifyChecksum(); err != nil {
logger.WithError(err).Errorf("invalid orderbook snapshot")
return
}
h.EmitBookSnapshot(globalOrderBook)
case updateRespType:
// emit updates, not the whole orderbook
h.EmitBookUpdate(globalOrderBook)
default:
logger.Errorf("unsupported order book data type %s", r.Type)
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)
}
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)
}