This repository has been archived by the owner on Mar 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcexio.go
439 lines (330 loc) · 9.04 KB
/
cexio.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
package cexio
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"log"
"os"
"sync"
"time"
"github.com/gorilla/websocket"
)
//API cex.io websocket API type
type API struct {
//Key API key
Key string
//Secret API secret
Secret string
conn *websocket.Conn
responseSubscribers map[string]chan subscriberType
subscriberMutex sync.Mutex
orderBookHandlers map[string]chan bool
stopDataCollector bool
//Dialer used to connect to WebSocket server
Dialer *websocket.Dialer
//Logger used for error logging
Logger *log.Logger
//ReceiveDone send message after Close() initiation
ReceiveDone chan bool
}
var apiURL = "wss://ws.cex.io/ws"
//NewAPI returns new API instance with default settings
func NewAPI(key string, secret string) *API {
api := &API{
Key: key,
Secret: secret,
Dialer: websocket.DefaultDialer,
Logger: log.New(os.Stderr, "", log.LstdFlags),
responseSubscribers: map[string]chan subscriberType{},
subscriberMutex: sync.Mutex{},
orderBookHandlers: map[string]chan bool{},
stopDataCollector: false,
ReceiveDone: make(chan bool),
}
return api
}
//Connect connects to cex.io websocket API server
func (a *API) Connect() error {
sub := a.subscribe("connected")
defer a.unsubscribe("connected")
conn, _, err := a.Dialer.Dial(apiURL, nil)
if err != nil {
return err
}
a.conn = conn
// run response from API server collector
go a.responseCollector()
<-sub //wait for connect response
// run authentication
err = a.auth()
if err != nil {
return err
}
return nil
}
//Close closes API connection
func (a *API) Close() error {
a.stopDataCollector = true
err := a.conn.Close()
if err != nil {
return err
}
go func() {
a.ReceiveDone <- true
}()
return nil
}
//Ticker send ticker request
func (a *API) Ticker(cCode1 string, cCode2 string) (*responseTicker, error) {
action := "ticker"
sub := a.subscribe(action)
defer a.unsubscribe(action)
timestamp := time.Now().UnixNano()
msg := requestTicker{
E: action,
Data: []string{cCode1, cCode2},
Oid: fmt.Sprintf("%d_%s:%s", timestamp, cCode1, cCode2),
}
err := a.conn.WriteJSON(msg)
if err != nil {
return nil, err
}
// wait for response from sever
respMsg := (<-sub).([]byte)
resp := &responseTicker{}
err = json.Unmarshal(respMsg, resp)
if err != nil {
return nil, err
}
// check if authentication was successfull
if resp.OK != "ok" {
return nil, errors.New(resp.Data.Error)
}
return resp, nil
}
//OrderBookSubscribe subscribes to order book updates.
//Order book snapshot will come as a first update
func (a *API) OrderBookSubscribe(cCode1 string, cCode2 string, depth int64, handler SubscriptionHandler) (int64, error) {
action := "order-book-subscribe"
currencyPair := fmt.Sprintf("%s:%s", cCode1, cCode2)
subscriptionIdentifier := fmt.Sprintf("%s_%s", action, currencyPair)
sub := a.subscribe(subscriptionIdentifier)
defer a.unsubscribe(subscriptionIdentifier)
timestamp := time.Now().UnixNano()
req := requestOrderBookSubscribe{
E: action,
Oid: fmt.Sprintf("%d_%s:%s", timestamp, cCode1, cCode2),
Data: requestOrderBookSubscribeData{
Pair: []string{cCode1, cCode2},
Subscribe: true,
Depth: depth,
},
}
err := a.conn.WriteJSON(req)
if err != nil {
return 0, err
}
bookSnapshot := (<-sub).(*responseOrderBookSubscribe)
go a.handleOrderBookSubscriptions(bookSnapshot, currencyPair, handler)
return bookSnapshot.Data.ID, nil
}
func (a *API) handleOrderBookSubscriptions(bookSnapshot *responseOrderBookSubscribe, currencyPair string, handler SubscriptionHandler) {
quit := make(chan bool)
subscriptionIdentifier := fmt.Sprintf("md_update_%s", currencyPair)
a.subscribe(subscriptionIdentifier)
a.orderBookHandlers[subscriptionIdentifier] = quit
obData := OrderBookUpdateData{
ID: bookSnapshot.Data.ID,
Pair: bookSnapshot.Data.Pair,
Timestamp: bookSnapshot.Data.Timestamp,
Bids: bookSnapshot.Data.Bids,
Asks: bookSnapshot.Data.Asks,
}
// process order book snapshot items before order book updates
go handler(obData)
sub, err := a.subscriber(subscriptionIdentifier)
if err != nil {
a.Logger.Println(err)
return
}
for {
select {
case <-quit:
return
case m := <-sub:
resp := m.(*responseOrderBookUpdate)
obData := OrderBookUpdateData{
ID: resp.Data.ID,
Pair: resp.Data.Pair,
Timestamp: resp.Data.Timestamp,
Bids: resp.Data.Bids,
Asks: resp.Data.Asks,
}
go handler(obData)
}
}
}
//OrderBookUnsubscribe unsubscribes from order book updates
func (a *API) OrderBookUnsubscribe(cCode1 string, cCode2 string) error {
action := "order-book-unsubscribe"
sub := a.subscribe(action)
defer a.unsubscribe(action)
timestamp := time.Now().UnixNano()
req := requestOrderBookUnsubscribe{
E: action,
Oid: fmt.Sprintf("%d_%s:%s", timestamp, cCode1, cCode2),
Data: orderBookPair{
Pair: []string{cCode1, cCode2},
},
}
err := a.conn.WriteJSON(req)
if err != nil {
return err
}
msg := (<-sub).([]byte)
resp := &responseOrderBookUnsubscribe{}
err = json.Unmarshal(msg, resp)
if err != nil {
return err
}
if resp.OK != "ok" {
return errors.New(resp.Data.Error)
}
handlerIdentifier := fmt.Sprintf("md_update_%s:%s", cCode1, cCode2)
// stop processing book messages
a.orderBookHandlers[handlerIdentifier] <- true
delete(a.orderBookHandlers, handlerIdentifier)
return nil
}
func (a *API) responseCollector() {
defer a.Close()
a.stopDataCollector = false
resp := &responseAction{}
for a.stopDataCollector == false {
_, msg, err := a.conn.ReadMessage()
if err != nil {
a.Logger.Println(err)
return
}
err = json.Unmarshal(msg, resp)
if err != nil {
a.Logger.Printf("responseCollector: %s\nData: %s\n", err, string(msg))
continue
}
subscriberIdentifier := resp.Action
if resp.Action == "ping" {
a.pong()
continue
}
if resp.Action == "disconnecting" {
a.Logger.Println(string(msg))
break
}
if resp.Action == "order-book-subscribe" {
ob := &responseOrderBookSubscribe{}
err = json.Unmarshal(msg, ob)
if err != nil {
a.Logger.Printf("responseCollector | order-book-subscribe: %s\nData: %s\n", err, string(msg))
continue
}
subscriberIdentifier = fmt.Sprintf("order-book-subscribe_%s", ob.Data.Pair)
sub, err := a.subscriber(subscriberIdentifier)
if err != nil {
a.Logger.Printf("No response handler for message: %s", string(msg))
continue // don't know how to handle message so just skip it
}
sub <- ob
continue
}
if resp.Action == "md_update" {
ob := &responseOrderBookUpdate{}
err = json.Unmarshal(msg, ob)
if err != nil {
a.Logger.Printf("responseCollector | md_update: %s\nData: %s\n", err, string(msg))
continue
}
subscriberIdentifier = fmt.Sprintf("md_update_%s", ob.Data.Pair)
sub, err := a.subscriber(subscriberIdentifier)
if err != nil {
a.Logger.Printf("No response handler for message: %s", string(msg))
continue // don't know how to handle message so just skip it
}
sub <- ob
continue
}
sub, err := a.subscriber(subscriberIdentifier)
if err != nil {
a.Logger.Printf("No response handler for message: %s", string(msg))
continue // don't know how to handle message so just skip it
}
sub <- msg
}
}
func (a *API) auth() error {
action := "auth"
sub := a.subscribe(action)
defer a.unsubscribe(action)
timestamp := time.Now().Unix()
// build signature string
s := fmt.Sprintf("%d%s", timestamp, a.Key)
h := hmac.New(sha256.New, []byte(a.Secret))
h.Write([]byte(s))
// generate signed signature string
signature := hex.EncodeToString(h.Sum(nil))
// build auth request
request := requestAuthAction{
E: action,
Auth: requestAuthData{
Key: a.Key,
Signature: signature,
Timestamp: timestamp,
},
}
// send auth request to API server
err := a.conn.WriteJSON(request)
if err != nil {
return err
}
// wait for auth response from sever
respMsg := (<-sub).([]byte)
resp := &responseAuth{}
err = json.Unmarshal(respMsg, resp)
if err != nil {
return err
}
// check if authentication was successfull
if resp.OK != "ok" || resp.Data.OK != "ok" {
return errors.New(resp.Data.Error)
}
return nil
}
func (a *API) pong() {
msg := requestPong{"pong"}
err := a.conn.WriteJSON(msg)
if err != nil {
a.Logger.Printf("Error while sending Pong message: %s", err)
}
}
func (a *API) subscribe(action string) chan subscriberType {
a.subscriberMutex.Lock()
defer a.subscriberMutex.Unlock()
a.responseSubscribers[action] = make(chan subscriberType)
return a.responseSubscribers[action]
}
func (a *API) unsubscribe(action string) {
a.subscriberMutex.Lock()
defer a.subscriberMutex.Unlock()
delete(a.responseSubscribers, action)
}
func (a *API) subscriber(action string) (chan subscriberType, error) {
a.subscriberMutex.Lock()
defer a.subscriberMutex.Unlock()
sub, ok := a.responseSubscribers[action]
if ok == false {
return nil, fmt.Errorf("Subscriber '%s' not found", action)
}
return sub, nil
}