forked from nntaoli-project/goex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OKEx_V3_Future_Ws.go
447 lines (392 loc) · 11.1 KB
/
OKEx_V3_Future_Ws.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
package okcoin
import (
"encoding/json"
"errors"
"fmt"
"log"
"strconv"
"strings"
"sync"
"time"
. "github.com/nntaoli-project/GoEx"
)
type OKExV3FutureWs struct {
*WsBuilder
sync.Once
wsConn *WsConn
loginCh chan interface{}
logined bool
loginLock *sync.Mutex
dataParser *OKExV3DataParser
contractIDProvider IContractIDProvider
apiKey string
apiSecretKey string
passphrase string
authoriedSubs []map[string]interface{}
tickerCallback func(*FutureTicker)
depthCallback func(*Depth)
tradeCallback func(*Trade, string)
klineCallback func(*FutureKline, int)
orderCallback func(*FutureOrder, string)
}
func getSign(apiSecretKey, timestamp, method, url, body string) (string, error) {
data := timestamp + method + url + body
return GetParamHmacSHA256Base64Sign(apiSecretKey, data)
}
func NewOKExV3FutureWs(contractIDProvider IContractIDProvider) *OKExV3FutureWs {
okV3Ws := &OKExV3FutureWs{}
okV3Ws.contractIDProvider = contractIDProvider
okV3Ws.dataParser = NewOKExV3DataParser(contractIDProvider)
okV3Ws.loginCh = make(chan interface{})
okV3Ws.logined = false
okV3Ws.loginLock = &sync.Mutex{}
okV3Ws.authoriedSubs = make([]map[string]interface{}, 0)
okV3Ws.WsBuilder = NewWsBuilder().
WsUrl("wss://real.okex.com:10442/ws/v3").
Heartbeat([]byte("ping"), 30*time.Second).
ReconnectIntervalTime(24 * time.Hour).
UnCompressFunc(FlateUnCompress).
ProtoHandleFunc(okV3Ws.handle)
return okV3Ws
}
func (okV3Ws *OKExV3FutureWs) TickerCallback(tickerCallback func(*FutureTicker)) *OKExV3FutureWs {
okV3Ws.tickerCallback = tickerCallback
return okV3Ws
}
func (okV3Ws *OKExV3FutureWs) DepthCallback(depthCallback func(*Depth)) *OKExV3FutureWs {
okV3Ws.depthCallback = depthCallback
return okV3Ws
}
func (okV3Ws *OKExV3FutureWs) TradeCallback(tradeCallback func(*Trade, string)) *OKExV3FutureWs {
okV3Ws.tradeCallback = tradeCallback
return okV3Ws
}
func (okV3Ws *OKExV3FutureWs) OrderCallback(orderCallback func(*FutureOrder, string)) *OKExV3FutureWs {
okV3Ws.orderCallback = orderCallback
return okV3Ws
}
func (okV3Ws *OKExV3FutureWs) SetCallbacks(tickerCallback func(*FutureTicker),
depthCallback func(*Depth),
tradeCallback func(*Trade, string),
klineCallback func(*FutureKline, int),
orderCallback func(*FutureOrder, string)) {
okV3Ws.tickerCallback = tickerCallback
okV3Ws.depthCallback = depthCallback
okV3Ws.tradeCallback = tradeCallback
okV3Ws.klineCallback = klineCallback
okV3Ws.orderCallback = orderCallback
}
func (okV3Ws *OKExV3FutureWs) Login(apiKey string, apiSecretKey string, passphrase string) error {
// already logined
if okV3Ws.logined {
return nil
}
okV3Ws.connectWs()
okV3Ws.apiKey = apiKey
okV3Ws.apiSecretKey = apiSecretKey
okV3Ws.passphrase = passphrase
err := okV3Ws.login()
if err == nil {
okV3Ws.logined = true
}
return err
}
func (okV3Ws *OKExV3FutureWs) getTimestamp() string {
seconds := float64(time.Now().UTC().UnixNano()) / float64(time.Second)
return fmt.Sprintf("%.3f", seconds)
}
func (okV3Ws *OKExV3FutureWs) clearChan(c chan interface{}) {
for {
if len(c) > 0 {
<-c
} else {
break
}
}
}
func (okV3Ws *OKExV3FutureWs) login() error {
okV3Ws.loginLock.Lock()
defer okV3Ws.loginLock.Unlock()
okV3Ws.clearChan(okV3Ws.loginCh)
apiKey := okV3Ws.apiKey
apiSecretKey := okV3Ws.apiSecretKey
passphrase := okV3Ws.passphrase
//clear last login result
timestamp := okV3Ws.getTimestamp()
method := "GET"
url := "/users/self/verify"
sign, _ := getSign(apiSecretKey, timestamp, method, url, "")
op := map[string]interface{}{
"op": "login",
"args": []string{apiKey, passphrase, timestamp, sign}}
err := okV3Ws.wsConn.SendJsonMessage(op)
if err != nil {
return err
}
event := <-okV3Ws.loginCh
if v, ok := event.(map[string]interface{}); ok {
var success = false
switch s := v["success"].(type) {
case bool:
success = s
case string:
success = s == "true"
}
if success {
log.Println("login success:", event)
return nil
}
}
log.Println("login failed:", event)
return fmt.Errorf("login failed: %v", event)
}
func (okV3Ws *OKExV3FutureWs) subscribe(sub map[string]interface{}) error {
okV3Ws.connectWs()
return okV3Ws.wsConn.Subscribe(sub)
}
func (okV3Ws *OKExV3FutureWs) getTablePrefix(currencyPair CurrencyPair, contractType string) string {
if contractType == SWAP_CONTRACT {
return "swap"
}
return "futures"
}
func (okV3Ws *OKExV3FutureWs) SubscribeDepth(currencyPair CurrencyPair, contractType string, size int) error {
if (size > 0) && (size != 5) {
return errors.New("only support depth 5")
}
if okV3Ws.depthCallback == nil {
return errors.New("please set depth callback func")
}
symbol, err := okV3Ws.contractIDProvider.GetContractID(currencyPair, contractType)
if err != nil {
return err
}
chName := fmt.Sprintf("%s/depth5:%s", okV3Ws.getTablePrefix(currencyPair, contractType), symbol)
return okV3Ws.subscribe(map[string]interface{}{
"op": "subscribe",
"args": []string{chName}})
}
func (okV3Ws *OKExV3FutureWs) SubscribeTicker(currencyPair CurrencyPair, contractType string) error {
if okV3Ws.tickerCallback == nil {
return errors.New("please set ticker callback func")
}
symbol, err := okV3Ws.contractIDProvider.GetContractID(currencyPair, contractType)
if err != nil {
return err
}
chName := fmt.Sprintf("%s/ticker:%s", okV3Ws.getTablePrefix(currencyPair, contractType), symbol)
return okV3Ws.subscribe(map[string]interface{}{
"op": "subscribe",
"args": []string{chName}})
}
func (okV3Ws *OKExV3FutureWs) SubscribeTrade(currencyPair CurrencyPair, contractType string) error {
if okV3Ws.tradeCallback == nil {
return errors.New("please set trade callback func")
}
symbol, err := okV3Ws.contractIDProvider.GetContractID(currencyPair, contractType)
if err != nil {
return err
}
chName := fmt.Sprintf("%s/trade:%s", okV3Ws.getTablePrefix(currencyPair, contractType), symbol)
return okV3Ws.subscribe(map[string]interface{}{
"op": "subscribe",
"args": []string{chName}})
}
func (okV3Ws *OKExV3FutureWs) SubscribeKline(currencyPair CurrencyPair, contractType string, period int) error {
if okV3Ws.klineCallback == nil {
return errors.New("place set kline callback func")
}
symbol, err := okV3Ws.contractIDProvider.GetContractID(currencyPair, contractType)
if err != nil {
return err
}
seconds, ok := KlineTypeSecondsMap[period]
if !ok {
return fmt.Errorf("unsupported kline period %d in okex", period)
}
chName := fmt.Sprintf("%s/candle%ds:%s", okV3Ws.getTablePrefix(currencyPair, contractType), seconds, symbol)
return okV3Ws.subscribe(map[string]interface{}{
"op": "subscribe",
"args": []string{chName}})
}
func (okV3Ws *OKExV3FutureWs) SubscribeOrder(currencyPair CurrencyPair, contractType string) error {
if okV3Ws.orderCallback == nil {
return errors.New("place set order callback func")
}
symbol, err := okV3Ws.contractIDProvider.GetContractID(currencyPair, contractType)
if err != nil {
return err
}
chName := fmt.Sprintf("%s/order:%s", okV3Ws.getTablePrefix(currencyPair, contractType), symbol)
return okV3Ws.authoriedSubscribe(map[string]interface{}{
"op": "subscribe",
"args": []string{chName}})
}
func (okV3Ws *OKExV3FutureWs) authoriedSubscribe(data map[string]interface{}) error {
okV3Ws.authoriedSubs = append(okV3Ws.authoriedSubs, data)
return okV3Ws.subscribe(data)
}
func (okV3Ws *OKExV3FutureWs) reSubscribeAuthoriedChannel() {
for _, d := range okV3Ws.authoriedSubs {
okV3Ws.wsConn.SendJsonMessage(d)
}
}
func (okV3Ws *OKExV3FutureWs) connectWs() {
okV3Ws.Do(func() {
okV3Ws.wsConn = okV3Ws.WsBuilder.Build()
okV3Ws.wsConn.ReceiveMessage()
})
}
func (okV3Ws *OKExV3FutureWs) handle(msg []byte) error {
if string(msg) == "pong" {
log.Println(string(msg))
okV3Ws.wsConn.UpdateActiveTime()
return nil
}
var resp map[string]interface{}
err := json.Unmarshal(msg, &resp)
if err != nil {
return err
}
if resp["event"] != nil {
switch resp["event"].(string) {
case "subscribe":
log.Println("subscribed:", resp["channel"].(string))
return nil
case "login":
select {
case okV3Ws.loginCh <- resp:
return nil
default:
return nil
}
case "error":
var errorCode int
switch v := resp["errorCode"].(type) {
case int:
errorCode = v
case float64:
errorCode = int(v) // float64 okex牛逼嗷
case string:
i, _ := strconv.ParseInt(v, 10, 64)
errorCode = int(i)
}
switch errorCode {
// event:error message:Already logged in errorCode:30042
case 30041:
if okV3Ws.logined { // have logined successfully
go func() {
okV3Ws.login()
okV3Ws.reSubscribeAuthoriedChannel()
}()
} // else skip, or better hanle?
return nil
case 30042:
return nil
}
// TODO: clearfy which errors should be treated as login result.
select {
case okV3Ws.loginCh <- resp:
return nil
default:
return fmt.Errorf("error in websocket: %v", resp)
}
}
return fmt.Errorf("unknown websocet message: %v", resp)
}
if resp["table"] != nil {
ch, err := okV3Ws.parseChannel(resp["table"].(string))
if err != nil {
return err
}
switch ch {
case "ticker":
data, ok := resp["data"].([]interface{})
if ok {
valid := true
for _, v := range data {
ticker, err := okV3Ws.dataParser.ParseFutureTicker(v)
if err != nil {
valid = false
break
}
okV3Ws.tickerCallback(ticker)
}
if valid {
return nil
}
}
case "depth5":
data, ok := resp["data"].([]interface{})
if ok {
valid := true
for _, v := range data {
depth, err := okV3Ws.dataParser.ParseDepth(nil, v, 5)
if err != nil {
valid = false
break
}
okV3Ws.depthCallback(depth)
}
if valid {
return nil
}
}
case "trade":
data, ok := resp["data"].([]interface{})
if ok {
valid := true
for _, v := range data {
trade, contractType, err := okV3Ws.dataParser.ParseTrade(nil, "", v)
if err != nil {
valid = false
break
}
okV3Ws.tradeCallback(trade, contractType)
}
if valid {
return nil
}
}
case "order":
data, ok := resp["data"].([]interface{})
if ok {
valid := true
for _, v := range data {
order, contractType, err := okV3Ws.dataParser.ParseFutureOrder(v)
if err != nil {
valid = false
break
}
okV3Ws.orderCallback(order, contractType)
}
if valid {
return nil
}
}
}
}
return fmt.Errorf("unknown websocet message: %v", resp)
}
func (okV3Ws *OKExV3FutureWs) parseChannel(channel string) (string, error) {
metas := strings.Split(channel, "/")
if len(metas) != 2 {
return "", fmt.Errorf("unknown channel: %s", channel)
}
return metas[1], nil
}
func (okV3Ws *OKExV3FutureWs) getKlinePeriodFormChannel(channel string) int {
metas := strings.Split(channel, ":")
if len(metas) != 2 {
return 0
}
i, _ := strconv.ParseInt(metas[1], 10, 64)
return int(i)
}
func (okV3Ws *OKExV3FutureWs) adaptTime(tm string) int64 {
format := "2006-01-02 15:04:05"
day := time.Now().Format("2006-01-02")
local, _ := time.LoadLocation("Asia/Chongqing")
t, _ := time.ParseInLocation(format, day+" "+tm, local)
return t.UnixNano() / 1e6
}