-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnection.js
654 lines (539 loc) · 19.4 KB
/
connection.js
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
/*
* Copyright (c) 2020. Christopher Queen Consulting LLC (http://www.ChristopherQueenConsulting.com/)
*/
const WebSocket = require('ws');
const EventEmitter = require('events');
class Connection extends EventEmitter {
constructor({key, secret, domain = 'www.deribit.com', debug = false}) {
super();
this.reconnectingCount = 0;
this.DEBUG = debug;
this.heartBeat = 60 * 1; //1 minutes in seconds
//this.heartBeat = 10; //1 minutes in seconds
this.key = key;
this.secret = secret;
this.WSdomain = domain;
//this.log(`Key: ${key} | Secret: ${secret} | Domain: ${domain} | Debug: ${debug}`);
this.connected = false;
this.isReadyHook = false;
this.isReady = new Promise((r => this.isReadyHook = r));
this.authenticated = false;
this.reconnecting = false;
this.afterReconnect = false;
this.inflightQueue = [];
this.subscriptions = [];
this.id = +new Date;
}
log(message, variable = false) {
if (variable !== false) {
message = message + JSON.stringify(variable);
}
if (this.DEBUG) {
console.log(message);
}
}
nextId() {
return ++this.id;
}
handleError(e) {
if (this.DEBUG) {
this.log(new Date, `Handle ERROR: ${JSON.stringify(e)}`);
}
throw new Error(e);
}
handleOnOpen() {
this.connected = true;
this.pingInterval = setInterval(this.ping, (this.heartBeat * 1000) * 5); // 5X the heart beat without a ping means connection is dead
this.isReadyHook();
}
_connect() {
if (this.connected) {
return;
}
let promise = new Promise((resolve, reject) => {
this.ws = new WebSocket(`wss://${this.WSdomain}/ws/api/v2`);
this.ws.onmessage = (message) => {
return this.handleWSMessage(message);
}
this.ws.onopen = () => {
this.handleOnOpen();
resolve();
}
//this.ws.onerror = this.handleError;
//this.ws.on('error', this.handleError);
this.ws.onerror = (error) => {
this.handleError(error);
}
this.ws.onclose = async () => {
if (this.DEBUG)
this.log(new Date + '-> CLOSED CON');
this.inflightQueue.forEach((queueElement) => {
//queueElement.connectionAborted();
queueElement.connectionAborted(new Error('Deribit Connection Closed'));
//queueElement.connectionAborted('Deribit Connection Closed');
});
//throw(new Error("Deribit Connection Closed. We will reconnect when we catch this error"));
this.inflightQueue = [];
this.authenticated = false;
this.connected = false;
clearInterval(this.pingInterval);
if (this.reconnectingCount < 3) {
await this.reconnect();
} else {
this.log(`Cannot properly reconnect to Deribit. Exiting Node and restarting Docker container.`);
this.end();
reject();
process.exit(1);
}
}
});
promise.catch((error) => {
this.log("Error:");
this.log(error.message);
this.log(error.stack);
this.inflightQueue = [];
this.authenticated = false;
this.connected = false;
clearInterval(this.pingInterval);
return this.reconnect();
});
return promise;
}
ping() {
let start = new Date;
const timeout = setTimeout(() => {
if (this.DEBUG)
this.log(new Date, ' NO PING RESPONSE');
this.terminate();
}, (this.heartBeat * 1000)); // If 5X the Heartbeat goes by then we will terminate the connection because it is dead
(async () => await this.request('public/test'))();
clearInterval(timeout);
}
// terminate a connection and immediately try to reconnect
terminate() {
if (this.DEBUG)
this.log(new Date, ' TERMINATED WS CON');
this.ws.terminate();
this.authenticated = false;
this.connected = false;
}
// end a connection
end() {
if (this.DEBUG)
this.log(new Date, ' ENDED WS CON');
this.subscriptions.forEach(sub => {
this.unsubscribe(sub.type, sub.channel);
});
clearInterval(this.pingInterval);
this.ws.onclose = undefined;
(async () => await this.request('private/logout', {access_token: this.token}))();
this.authenticated = false;
this.connected = false;
this.ws.terminate();
}
wait(n) {
return new Promise(r => setTimeout(r, n))
};
async reconnect() {
if (this.reconnectingCount >= 3) {
this.log(`Cannot properly reconnect to Deribit. Exiting Node and restarting Docker container.`);
this.end();
process.exit(1);
}
this.reconnecting = true;
this.reconnectingCount++;
let hook;
this.afterReconnect = new Promise(r => hook = r);
this.isReady = new Promise((r => this.isReadyHook = r));
await this.wait(5000);
if (this.DEBUG)
this.log(new Date, ' RECONNECTING...');
this.connect();
hook();
this.isReadyHook();
this.subscriptions.forEach(sub => {
this.subscribe(sub.type, sub.channel);
});
return this.isReady;
}
connect() {
this.connected = (async () => await this._connect())();
if (this.key) {
this.authenticate()
}
// Set the heartbeat (in seconds)
(async () => await this.request("public/set_heartbeat", {interval: this.heartBeat}))();
//.then(async () => {
//this.on('test_request', this.handleWSMessage);
//});
}
authenticate() {
if (!this.connected) {
this.connect();
}
let today = new Date();
let date = today.getFullYear() + '_' + (today.getMonth() + 1) + '_' + today.getDate();
const resp = (async () => await this.sendMessage({
'jsonrpc': '2.0',
'method': 'public/auth',
'id': this.nextId(),
'params': {
'grant_type': 'client_credentials',
'client_id': this.key,
'client_secret': this.secret,
'scope': 'session:tradingapp_docker_nodejs' + date
}
}))();
if (resp.error) {
throw new Error(resp.error.message);
}
this.token = resp.result.access_token;
this.refreshToken = resp.result.refresh_token;
this.authenticated = true;
if (!resp.result.expires_in) {
throw new Error('Deribit did not provide expiry details');
}
/*
wait(resp.result.expires_in - 10 * 60 * 1000).then(() => this.refreshTokenFn()).catch(error => {
this.log(`Error while refreshing token: ${error.message}`)
})
*/
let refreshTime = (resp.result.expires_in - (10 * 60)) * 1000; // (ExpireTime (seconds) - 10 Minutes (in seconds)) converted back to Milliseconds
//let today = Date.now();
let expireDateMilli = today + refreshTime;
let expireDate = new Date(expireDateMilli);
this.log(`Refresh Token Expires On: ${expireDate.toString()}`);
let safeRefresh = Math.min(refreshTime, (Math.pow(2, 31) - 1));
setTimeout(this.refreshTokenFn, safeRefresh);
//setTimeout(this.refreshTokenFn, resp.result.expires_in - 10 * 60 * 1000);
}
refreshTokenFn() {
this.log(`Refreshing Token Now.`);
const resp = (async () => await this.sendMessage({
'jsonrpc': '2.0',
'method': 'public/auth',
'id': this.nextId(),
'params': {
'grant_type': 'refresh_token',
'refresh_token': this.refreshToken
}
}))();
this.token = resp.result.access_token;
this.refreshToken = resp.result.refresh_token;
if (!resp.result.expires_in) {
throw new Error('Deribit did not provide expiry details');
}
/*
wait(resp.result.expires_in - 10 * 60 * 1000).then(() => this.refreshTokenFn()).catch(error => {
this.log(`Error while refreshing token: ${error.message}`)
})
*/
let refreshTime = (resp.result.expires_in - (10 * 60)) * 1000; // (ExpireTime (seconds) - 10 Minutes (in seconds)) converted back to Milliseconds
let today = Date.now();
let expireDateMilli = today + refreshTime;
let expireDate = new Date(expireDateMilli);
this.log(`Refresh Token Expires On: ${expireDate.toString()}`);
let safeRefresh = Math.min(refreshTime, (Math.pow(2, 31) - 1));
setTimeout(this.refreshTokenFn, safeRefresh);
}
findRequest(id) {
let foundReq = false;
for (let i = 0; i < this.inflightQueue.length; i++) {
let req = this.inflightQueue[i];
if (id === req.id) {
this.inflightQueue.splice(i, 1);
foundReq = req;
break;
}
}
return foundReq;
}
handleWSMessage(e) {
let payload;
try {
payload = JSON.parse(e.data);
} catch (e) {
console.error('deribit sent bad json', e);
}
if (payload.method === 'subscription') {
clearInterval(this.pingInterval);
return this.emit(payload.params.channel, payload.params.data);
}
if (payload.method === 'heartbeat' || payload.method === 'test_request' || payload.method === 'ping') {
if (this.DEBUG) {
this.log(new Date + ' -> Responding to Heartbeat Request')
}
clearInterval(this.pingInterval);
return this.sendMessage({
'jsonrpc': '2.0',
'method': 'public/test',
'id': this.nextId(),
'param': {}
})
}
let request = this.findRequest(payload.id);
if (!request) {
return console.error('received response to request not send:', payload);
}
payload.requestedAt = request.requestedAt;
payload.receivedAt = +new Date;
request.onDone(payload);
}
async sendMessage(payload, fireAndForget) {
if (!this.connected) {
if (!this.reconnecting) {
throw new Error('Not connected.')
}
await this.reconnect();
}
let p;
if (!fireAndForget) {
let onDone;
let connectionAborted;
p = new Promise((r, rj) => {
onDone = r;
connectionAborted = rj;
this.inflightQueue.push({
requestedAt: +new Date,
id: payload.id,
onDone,
connectionAborted
});
});
}
try {
this.ws.send(JSON.stringify(payload));
} catch (error) {
this.log(error);
await this.reconnect();
setTimeout(() => {
this.sendMessage(payload, fireAndForget);
}, 5000);
}
/*
.catch((e) => {
const reason = new Error(`failed sending message: ${JSON.stringify(payload)}`);
reason.stack += `\nCaused By:\n` + e.stack;
return reason;
});
*/
/*
// CDQ - added retry for message that may fail
try {
this.ws.send(JSON.stringify(payload));
} catch (error) {
this.log(error);
await this.reconnect();
setTimeout(() => {
this.sendMessage(payload, fireAndForget)
}, 5 * 1000);
}
*/
//clearInterval(this.pingInterval);
return p;
}
async request(path, params) {
if (!this.connected) {
if (!this.reconnecting) {
throw new Error('Not connected.');
}
await this.reconnect();
}
if (path.startsWith('private')) {
if (!this.authenticated) {
throw new Error('Not authenticated.');
}
}
const message = {
'jsonrpc': '2.0',
'method': path,
'params': params,
'id': this.nextId()
};
//this.log(`Sending Message: `, message);
return this.sendMessage(message);
}
unsubscribe(type, channel) {
if (!this.connected) {
throw new Error('Not connected.');
} else if (type === 'private' && !this.authenticated) {
throw new Error('Not authenticated.');
}
const message = {
'jsonrpc': '2.0',
'method': `${type}/unsubscribe`,
'params': {
'channels': [channel]
},
'id': this.nextId()
};
return this.sendMessage(message);
}
async subscribe(type, channel) {
this.subscriptions.push({type, channel});
if (!this.connected) {
throw new Error('Not connected.');
} else if (type === 'private' && !this.authenticated) {
throw new Error('Not authenticated.');
}
const message = {
'jsonrpc': '2.0',
'method': `${type}/subscribe`,
'params': {
'channels': [channel]
},
'id': this.nextId()
};
return await this.sendMessage(message);
}
async cancel_order_by_label(label) {
return await this.request(`private/cancel_by_label`,
{
'label': label
})
.catch((e) => {
this.log(`Could not return after cancel_order_by_label() Error : `, e.message);
//throw new Error(`Could not return after cancel_order_by_label()`);
return Promise.reject(e);
});
}
async close_position(instrument, type) {
return await this.request(`private/close_position`,
{
'instrument_name': instrument,
'type': type
})
.catch((e) => {
this.log(`Could not return after close_position() Error: `, e.message);
return Promise.reject(e);
});
}
async getPosition(instrument) {
return await this.request(`private/get_position`,
{
'instrument_name': instrument
})
.catch((e) => {
this.log(`Could not return after getPosition() : `, e.message);
return Promise.reject(e);
});
}
async get_tradingview_chart_data(instrument, start, end, resolution) {
return await this.request('public/get_tradingview_chart_data', {
'instrument_name': instrument,
'start_timestamp': start,
'end_timestamp': end,
'resolution': resolution
})
.catch((e) => {
this.log(`Could not return after get_tradingview_chart_data() Error: `, e.message)
return Promise.reject(e);
});
}
async buy(options) {
return await this.request('private/buy', options)
.catch((e) => {
this.log(`Could not return after buy() Error: `, e.message);
return Promise.reject(e);
});
}
async sell(options) {
return await this.request('private/sell', options)
.catch((e) => {
this.log(`Could not return after sell() Error: `, e.message);
return Promise.reject(e);
});
}
async get_open_orders_by_instrument(instrument, type = "all") {
return await this.request('private/get_open_orders_by_instrument', {
'instrument_name': instrument,
'type': type
})
.catch((e) => {
this.log(`Could not return after get_open_orders_by_instrument() Error: `, e.message);
return Promise.reject(e);
});
}
async get_stop_order_history(instrument, currency = "BTC", count = 30) {
return await this.request('private/get_stop_order_history', {
'instrument_name': instrument,
'currency': currency,
'count': count
})
.catch(e => {
this.log(`Could not return after get_stop_order_history() Error: `, e.message);
return Promise.reject(e);
});
}
async editOrder(orderId, orderSizeUSD, price = false, stopPrice = false) {
let orderEditOptions = {
"order_id": orderId,
"amount": orderSizeUSD,
};
if (price) {
orderEditOptions['price'] = price;
}
if (stopPrice) {
orderEditOptions['stop_price'] = stopPrice;
}
return await this.request(`private/edit`, orderEditOptions)
.catch((e) => {
this.log(`Could not return after editOrder() : `, e.message);
return Promise.reject(e);
});
}
async enable_cancel_on_disconnect() {
return await this.request('private/enable_cancel_on_disconnect')
.catch((e) => {
this.log(`Could not return after enable_cancel_on_disconnect() Error: `, e.message);
return Promise.reject(e);
});
}
async disable_cancel_on_disconnect() {
return await this.request('private/disable_cancel_on_disconnect')
.catch((e) => {
this.log(`Could not return after disable_cancel_on_disconnect() Error: `, e.message);
return Promise.reject(e);
});
}
async get_account_summary(currency, extended) {
return await this.request('private/get_account_summary',
{
'currency': currency,
'extended': extended
}).catch((e) => {
this.log(`Could not return after get_account_summary() Error: `, e.message);
return Promise.reject(e);
});
}
async get_instruments(currency, kind, expired) {
return await this.request('public/get_instruments',
{
'currency': currency,
'kind': kind,
'expired': expired
}).catch((e) => {
this.log(`Could not return after get_instruments() Error: `, e.message);
return Promise.reject(e);
});
}
async get_book_summary_by_instrument(instrument) {
return await this.request('public/get_book_summary_by_instrument',
{
'instrument_name': instrument
}).catch((e) => {
this.log(`Could not return after get_book_summary_by_instrument() Error: `, e.message);
return Promise.reject(e);
});
}
async get_ticker(instrument) {
return await this.request('public/ticker',
{
'instrument_name': instrument
}).catch((e) => {
this.log(`Could not return after get_ticker() Error: `, e.message);
return Promise.reject(e);
});
}
}
module.exports = Connection;