-
-
Notifications
You must be signed in to change notification settings - Fork 283
/
stream_message_handler.go
169 lines (147 loc) · 3.94 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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 bookTickerChannel:
h.handleBookTicker(r)
case marketTradeChannel:
h.handleMarketTrade(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) handleMarketTrade(response websocketResponse) {
if response.Type == subscribedRespType {
h.handleSubscribedMessage(response)
return
}
trades, err := response.toMarketTradeResponse()
if err != nil {
logger.WithError(err).Errorf("failed to generate market trade %v", response)
return
}
for _, trade := range trades {
h.EmitMarketTrade(trade)
}
}
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)
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 := toGlobalOrderNew(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)
}