forked from bitfinexcom/bitfinex-api-go
/
listener.go
366 lines (343 loc) · 9.75 KB
/
listener.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package tests
import (
"errors"
"log"
"time"
bitfinex "github.com/bitfinexcom/bitfinex-api-go/v2"
"github.com/bitfinexcom/bitfinex-api-go/v2/websocket"
)
type listener struct {
infoEvents chan *websocket.InfoEvent
authEvents chan *websocket.AuthEvent
ticks chan *bitfinex.Ticker
subscriptionEvents chan *websocket.SubscribeEvent
unsubscriptionEvents chan *websocket.UnsubscribeEvent
walletUpdates chan *bitfinex.WalletUpdate
balanceUpdates chan *bitfinex.BalanceUpdate
walletSnapshot chan *bitfinex.WalletSnapshot
positionSnapshot chan *bitfinex.PositionSnapshot
notifications chan *bitfinex.Notification
positions chan *bitfinex.PositionUpdate
tradeUpdates chan *bitfinex.TradeExecutionUpdate
tradeExecutions chan *bitfinex.TradeExecution
cancels chan *bitfinex.OrderCancel
marginBase chan *bitfinex.MarginInfoBase
marginUpdate chan *bitfinex.MarginInfoUpdate
funding chan *bitfinex.FundingInfo
orderNew chan *bitfinex.OrderNew
errors chan error
}
func newListener() *listener {
return &listener{
infoEvents: make(chan *websocket.InfoEvent, 10),
authEvents: make(chan *websocket.AuthEvent, 10),
ticks: make(chan *bitfinex.Ticker, 10),
subscriptionEvents: make(chan *websocket.SubscribeEvent, 10),
unsubscriptionEvents: make(chan *websocket.UnsubscribeEvent, 10),
walletUpdates: make(chan *bitfinex.WalletUpdate, 10),
balanceUpdates: make(chan *bitfinex.BalanceUpdate, 10),
walletSnapshot: make(chan *bitfinex.WalletSnapshot, 10),
positionSnapshot: make(chan *bitfinex.PositionSnapshot, 10),
errors: make(chan error, 10),
notifications: make(chan *bitfinex.Notification, 10),
positions: make(chan *bitfinex.PositionUpdate, 10),
tradeUpdates: make(chan *bitfinex.TradeExecutionUpdate, 10),
tradeExecutions: make(chan *bitfinex.TradeExecution, 10),
cancels: make(chan *bitfinex.OrderCancel, 10),
marginBase: make(chan *bitfinex.MarginInfoBase, 10),
marginUpdate: make(chan *bitfinex.MarginInfoUpdate, 10),
orderNew: make(chan *bitfinex.OrderNew, 10),
funding: make(chan *bitfinex.FundingInfo, 10),
}
}
func (l *listener) nextInfoEvent() (*websocket.InfoEvent, error) {
timeout := make(chan bool)
go func() {
time.Sleep(time.Second * 2)
close(timeout)
}()
select {
case ev := <-l.infoEvents:
return ev, nil
case <-timeout:
return nil, errors.New("timed out waiting for InfoEvent")
}
}
func (l *listener) nextAuthEvent() (*websocket.AuthEvent, error) {
timeout := make(chan bool)
go func() {
time.Sleep(time.Second * 2)
close(timeout)
}()
select {
case ev := <-l.authEvents:
return ev, nil
case <-timeout:
return nil, errors.New("timed out waiting for AuthEvent")
}
}
func (l *listener) nextWalletUpdate() (*bitfinex.WalletUpdate, error) {
timeout := make(chan bool)
go func() {
time.Sleep(time.Second * 2)
close(timeout)
}()
select {
case ev := <-l.walletUpdates:
return ev, nil
case <-timeout:
return nil, errors.New("timed out waiting for WalletUpdate")
}
}
func (l *listener) nextBalanceUpdate() (*bitfinex.BalanceUpdate, error) {
timeout := make(chan bool)
go func() {
time.Sleep(time.Second * 2)
close(timeout)
}()
select {
case ev := <-l.balanceUpdates:
return ev, nil
case <-timeout:
return nil, errors.New("timed out waiting for BalanceUpdate")
}
}
func (l *listener) nextWalletSnapshot() (*bitfinex.WalletSnapshot, error) {
timeout := make(chan bool)
go func() {
time.Sleep(time.Second * 2)
close(timeout)
}()
select {
case ev := <-l.walletSnapshot:
return ev, nil
case <-timeout:
return nil, errors.New("timed out waiting for WalletSnapshot")
}
}
func (l *listener) nextPositionSnapshot() (*bitfinex.PositionSnapshot, error) {
timeout := make(chan bool)
go func() {
time.Sleep(time.Second * 2)
close(timeout)
}()
select {
case ev := <-l.positionSnapshot:
return ev, nil
case <-timeout:
return nil, errors.New("timed out waiting for PositionSnapshot")
}
}
func (l *listener) nextSubscriptionEvent() (*websocket.SubscribeEvent, error) {
timeout := make(chan bool)
go func() {
time.Sleep(time.Second * 2)
close(timeout)
}()
select {
case ev := <-l.subscriptionEvents:
return ev, nil
case <-timeout:
return nil, errors.New("timed out waiting for SubscribeEvent")
}
}
func (l *listener) nextUnsubscriptionEvent() (*websocket.UnsubscribeEvent, error) {
timeout := make(chan bool)
go func() {
time.Sleep(time.Second * 2)
close(timeout)
}()
select {
case ev := <-l.unsubscriptionEvents:
return ev, nil
case <-timeout:
return nil, errors.New("timed out waiting for UnsubscribeEvent")
}
}
func (l *listener) nextTick() (*bitfinex.Ticker, error) {
timeout := make(chan bool)
go func() {
time.Sleep(time.Second * 2)
close(timeout)
}()
select {
case ev := <-l.ticks:
return ev, nil
case <-timeout:
return nil, errors.New("timed out waiting for Ticker")
}
}
func (l *listener) nextNotification() (*bitfinex.Notification, error) {
timeout := make(chan bool)
go func() {
time.Sleep(time.Second * 2)
close(timeout)
}()
select {
case ev := <-l.notifications:
return ev, nil
case <-timeout:
return nil, errors.New("timed out waiting for Notification")
}
}
func (l *listener) nextTradeExecution() (*bitfinex.TradeExecution, error) {
timeout := make(chan bool)
go func() {
time.Sleep(time.Second * 2)
close(timeout)
}()
select {
case ev := <-l.tradeExecutions:
return ev, nil
case <-timeout:
return nil, errors.New("timed out waiting for TradeExecution")
}
}
func (l *listener) nextPositionUpdate() (*bitfinex.PositionUpdate, error) {
timeout := make(chan bool)
go func() {
time.Sleep(time.Second * 2)
close(timeout)
}()
select {
case ev := <-l.positions:
return ev, nil
case <-timeout:
return nil, errors.New("timed out waiting for PositionUpdate")
}
}
func (l *listener) nextTradeUpdate() (*bitfinex.TradeExecutionUpdate, error) {
timeout := make(chan bool)
go func() {
time.Sleep(time.Second * 2)
close(timeout)
}()
select {
case ev := <-l.tradeUpdates:
return ev, nil
case <-timeout:
return nil, errors.New("timed out waiting for TradeUpdate")
}
}
func (l *listener) nextOrderCancel() (*bitfinex.OrderCancel, error) {
timeout := make(chan bool)
go func() {
time.Sleep(time.Second * 2)
close(timeout)
}()
select {
case ev := <-l.cancels:
return ev, nil
case <-timeout:
return nil, errors.New("timed out waiting for OrderCancel")
}
}
func (l *listener) nextMarginInfoBase() (*bitfinex.MarginInfoBase, error) {
timeout := make(chan bool)
go func() {
time.Sleep(time.Second * 2)
close(timeout)
}()
select {
case ev := <-l.marginBase:
return ev, nil
case <-timeout:
return nil, errors.New("timed out waiting for MarginInfoBase")
}
}
func (l *listener) nextMarginInfoUpdate() (*bitfinex.MarginInfoUpdate, error) {
timeout := make(chan bool)
go func() {
time.Sleep(time.Second * 2)
close(timeout)
}()
select {
case ev := <-l.marginUpdate:
return ev, nil
case <-timeout:
return nil, errors.New("timed out waiting for MarginInfoUpdate")
}
}
func (l *listener) nextFundingInfo() (*bitfinex.FundingInfo, error) {
timeout := make(chan bool)
go func() {
time.Sleep(time.Second * 2)
close(timeout)
}()
select {
case ev := <-l.funding:
return ev, nil
case <-timeout:
return nil, errors.New("timed out waiting for FundingInfo")
}
}
func (l *listener) nextOrderNew() (*bitfinex.OrderNew, error) {
timeout := make(chan bool)
go func() {
time.Sleep(time.Second * 2)
close(timeout)
}()
select {
case ev := <-l.orderNew:
return ev, nil
case <-timeout:
return nil, errors.New("timed out waiting for OrderNew")
}
}
// strongly types messages and places them into a channel
func (l *listener) run(ch <-chan interface{}) {
go func() {
for {
select {
case msg := <-ch:
if msg == nil {
return
}
// remove threading guarantees when mulitplexing into channels
log.Printf("[DEBUG] WsService -> WsClient: %#v", msg)
switch msg.(type) {
case error:
l.errors <- msg.(error)
case *bitfinex.Ticker:
l.ticks <- msg.(*bitfinex.Ticker)
case *websocket.InfoEvent:
l.infoEvents <- msg.(*websocket.InfoEvent)
case *websocket.SubscribeEvent:
l.subscriptionEvents <- msg.(*websocket.SubscribeEvent)
case *websocket.UnsubscribeEvent:
l.unsubscriptionEvents <- msg.(*websocket.UnsubscribeEvent)
case *websocket.AuthEvent:
l.authEvents <- msg.(*websocket.AuthEvent)
case *bitfinex.WalletUpdate:
l.walletUpdates <- msg.(*bitfinex.WalletUpdate)
case *bitfinex.BalanceUpdate:
l.balanceUpdates <- msg.(*bitfinex.BalanceUpdate)
case *bitfinex.Notification:
l.notifications <- msg.(*bitfinex.Notification)
case *bitfinex.TradeExecutionUpdate:
l.tradeUpdates <- msg.(*bitfinex.TradeExecutionUpdate)
case *bitfinex.TradeExecution:
l.tradeExecutions <- msg.(*bitfinex.TradeExecution)
case *bitfinex.PositionUpdate:
l.positions <- msg.(*bitfinex.PositionUpdate)
case *bitfinex.OrderCancel:
l.cancels <- msg.(*bitfinex.OrderCancel)
case *bitfinex.MarginInfoBase:
l.marginBase <- msg.(*bitfinex.MarginInfoBase)
case *bitfinex.MarginInfoUpdate:
l.marginUpdate <- msg.(*bitfinex.MarginInfoUpdate)
case *bitfinex.OrderNew:
l.orderNew <- msg.(*bitfinex.OrderNew)
case *bitfinex.FundingInfo:
l.funding <- msg.(*bitfinex.FundingInfo)
case *bitfinex.PositionSnapshot:
l.positionSnapshot <- msg.(*bitfinex.PositionSnapshot)
case *bitfinex.WalletSnapshot:
l.walletSnapshot <- msg.(*bitfinex.WalletSnapshot)
default:
log.Printf("COULD NOT TYPE MSG ^")
}
}
}
}()
}