forked from jaggedsoft/node-binance-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
node-binance-api.js
6045 lines (5663 loc) · 258 KB
/
node-binance-api.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
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ============================================================
* node-binance-api
* https://github.com/jaggedsoft/node-binance-api
* ============================================================
* Copyright 2017-, Jon Eyrick
* Released under the MIT License
* ============================================================
* @module jaggedsoft/node-binance-api
* @return {object} instance to class object */
let api = function Binance( options = {} ) {
if ( !new.target ) return new api( options ); // Legacy support for calling the constructor without 'new'
let Binance = this; // eslint-disable-line consistent-this
const WebSocket = require( 'ws' );
const request = require( 'request' );
const crypto = require( 'crypto' );
const file = require( 'fs' );
const url = require( 'url' );
const JSONbig = require( 'json-bigint' );
const HttpsProxyAgent = require( 'https-proxy-agent' );
const SocksProxyAgent = require( 'socks-proxy-agent' );
const stringHash = require( 'string-hash' );
const async = require( 'async' );
let base = 'https://api.binance.com/api/';
let wapi = 'https://api.binance.com/wapi/';
let sapi = 'https://api.binance.com/sapi/';
let fapi = 'https://fapi.binance.com/fapi/';
let dapi = 'https://dapi.binance.com/dapi/';
let fapiTest = 'https://testnet.binancefuture.com/fapi/';
let dapiTest = 'https://testnet.binancefuture.com/dapi/';
let fstream = 'wss://fstream.binance.com/stream?streams=';
let fstreamSingle = 'wss://fstream.binance.com/ws/';
let fstreamSingleTest = 'wss://stream.binancefuture.com/ws/';
let fstreamTest = 'wss://stream.binancefuture.com/stream?streams=';
let dstream = 'wss://dstream.binance.com/stream?streams=';
let dstreamSingle = 'wss://dstream.binance.com/ws/';
let dstreamSingleTest = 'wss://dstream.binancefuture.com/ws/';
let dstreamTest = 'wss://dstream.binancefuture.com/stream?streams=';
let stream = 'wss://stream.binance.com:9443/ws/';
let combineStream = 'wss://stream.binance.com:9443/stream?streams=';
const userAgent = 'Mozilla/4.0 (compatible; Node Binance API)';
const contentType = 'application/x-www-form-urlencoded';
Binance.subscriptions = {};
Binance.futuresSubscriptions = {};
Binance.futuresInfo = {};
Binance.futuresMeta = {};
Binance.futuresTicks = {};
Binance.futuresRealtime = {};
Binance.futuresKlineQueue = {};
Binance.futuresDepthCache = {};
Binance.futuresDepthCacheContext = {};
Binance.deliverySubscriptions = {};
Binance.deliveryInfo = {};
Binance.deliveryMeta = {};
Binance.deliveryTicks = {};
Binance.deliveryRealtime = {};
Binance.deliveryKlineQueue = {};
Binance.depthCache = {};
Binance.depthCacheContext = {};
Binance.ohlcLatest = {};
Binance.klineQueue = {};
Binance.ohlc = {};
const default_options = {
recvWindow: 5000,
useServerTime: false,
reconnect: true,
keepAlive: true,
verbose: false,
test: false,
hedgeMode: false,
localAddress: false,
family: false,
log: function ( ...args ) {
console.log( Array.prototype.slice.call( args ) );
}
};
Binance.options = default_options;
Binance.info = {
usedWeight: 0,
futuresLatency: false,
lastRequest: false,
lastURL: false,
statusCode: 0,
orderCount1s: 0,
orderCount1m: 0,
orderCount1h: 0,
orderCount1d: 0,
timeOffset: 0
};
Binance.socketHeartbeatInterval = null;
if ( options ) setOptions( options );
function setOptions( opt = {}, callback = false ) {
if ( typeof opt === 'string' ) { // Pass json config filename
Binance.options = JSON.parse( file.readFileSync( opt ) );
} else Binance.options = opt;
if ( typeof Binance.options.recvWindow === 'undefined' ) Binance.options.recvWindow = default_options.recvWindow;
if ( typeof Binance.options.useServerTime === 'undefined' ) Binance.options.useServerTime = default_options.useServerTime;
if ( typeof Binance.options.reconnect === 'undefined' ) Binance.options.reconnect = default_options.reconnect;
if ( typeof Binance.options.test === 'undefined' ) Binance.options.test = default_options.test;
if ( typeof Binance.options.hedgeMode === 'undefined' ) Binance.options.hedgeMode = default_options.hedgeMode;
if ( typeof Binance.options.log === 'undefined' ) Binance.options.log = default_options.log;
if ( typeof Binance.options.verbose === 'undefined' ) Binance.options.verbose = default_options.verbose;
if ( typeof Binance.options.keepAlive === 'undefined' ) Binance.options.keepAlive = default_options.keepAlive;
if ( typeof Binance.options.localAddress === 'undefined' ) Binance.options.localAddress = default_options.localAddress;
if ( typeof Binance.options.family === 'undefined' ) Binance.options.family = default_options.family;
if ( typeof Binance.options.urls !== 'undefined' ) {
const { urls } = Binance.options;
if ( typeof urls.base === 'string' ) base = urls.base;
if ( typeof urls.wapi === 'string' ) wapi = urls.wapi;
if ( typeof urls.sapi === 'string' ) sapi = urls.sapi;
if ( typeof urls.fapi === 'string' ) fapi = urls.fapi;
if ( typeof urls.fapiTest === 'string' ) fapiTest = urls.fapiTest;
if ( typeof urls.stream === 'string' ) stream = urls.stream;
if ( typeof urls.combineStream === 'string' ) combineStream = urls.combineStream;
if ( typeof urls.fstream === 'string' ) fstream = urls.fstream;
if ( typeof urls.fstreamSingle === 'string' ) fstreamSingle = urls.fstreamSingle;
if ( typeof urls.fstreamTest === 'string' ) fstreamTest = urls.fstreamTest;
if ( typeof urls.fstreamSingleTest === 'string' ) fstreamSingleTest = urls.fstreamSingleTest;
if ( typeof urls.dstream === 'string' ) dstream = urls.dstream;
if ( typeof urls.dstreamSingle === 'string' ) dstreamSingle = urls.dstreamSingle;
if ( typeof urls.dstreamTest === 'string' ) dstreamTest = urls.dstreamTest;
if ( typeof urls.dstreamSingleTest === 'string' ) dstreamSingleTest = urls.dstreamSingleTest;
}
if ( Binance.options.useServerTime ) {
publicRequest( base + 'v3/time', {}, function ( error, response ) {
Binance.info.timeOffset = response.serverTime - new Date().getTime();
//Binance.options.log("server time set: ", response.serverTime, Binance.info.timeOffset);
if ( callback ) callback();
} );
} else if ( callback ) callback();
return this;
}
/**
* Replaces socks connection uri hostname with IP address
* @param {string} connString - socks connection string
* @return {string} modified string with ip address
*/
const proxyReplacewithIp = connString => {
return connString;
}
/**
* Returns an array in the form of [host, port]
* @param {string} connString - connection string
* @return {array} array of host and port
*/
const parseProxy = connString => {
let arr = connString.split( '/' );
let host = arr[2].split( ':' )[0];
let port = arr[2].split( ':' )[1];
return [ arr[0], host, port ];
}
/**
* Checks to see of the object is iterable
* @param {object} obj - The object check
* @return {boolean} true or false is iterable
*/
const isIterable = obj => {
if ( obj === null ) return false;
return typeof obj[Symbol.iterator] === 'function';
}
const addProxy = opt => {
if ( Binance.options.proxy ) {
const proxyauth = Binance.options.proxy.auth ? `${ Binance.options.proxy.auth.username }:${ Binance.options.proxy.auth.password }@` : '';
opt.proxy = `http://${ proxyauth }${ Binance.options.proxy.host }:${ Binance.options.proxy.port }`;
}
return opt;
}
const reqHandler = cb => ( error, response, body ) => {
Binance.info.lastRequest = new Date().getTime();
if ( response ) {
Binance.info.statusCode = response.statusCode || 0;
if ( response.request ) Binance.info.lastURL = response.request.uri.href;
if ( response.headers ) {
Binance.info.usedWeight = response.headers['x-mbx-used-weight-1m'] || 0;
Binance.info.orderCount1s = response.headers['x-mbx-order-count-1s'] || 0;
Binance.info.orderCount1m = response.headers['x-mbx-order-count-1m'] || 0;
Binance.info.orderCount1h = response.headers['x-mbx-order-count-1h'] || 0;
Binance.info.orderCount1d = response.headers['x-mbx-order-count-1d'] || 0;
}
}
if ( !cb ) return;
if ( error ) return cb( error, {} );
if ( response && response.statusCode !== 200 ) return cb( response, {} );
return cb( null, JSONbig.parse( body ) );
}
const proxyRequest = ( opt, cb ) => {
const req = request( addProxy( opt ), reqHandler( cb ) ).on('error', (err) => { cb( err, {} ) });
return req;
}
const reqObj = ( url, data = {}, method = 'GET', key ) => ( {
url: url,
qs: data,
method: method,
family: Binance.options.family,
localAddress: Binance.options.localAddress,
timeout: Binance.options.recvWindow,
forever: Binance.options.keepAlive,
headers: {
'User-Agent': userAgent,
'Content-type': contentType,
'X-MBX-APIKEY': key || ''
}
} )
const reqObjPOST = ( url, data = {}, method = 'POST', key ) => ( {
url: url,
form: data,
method: method,
family: Binance.options.family,
localAddress: Binance.options.localAddress,
timeout: Binance.options.recvWindow,
forever: Binance.options.keepAlive,
qsStringifyOptions: {
arrayFormat: 'repeat'
},
headers: {
'User-Agent': userAgent,
'Content-type': contentType,
'X-MBX-APIKEY': key || ''
}
} )
/**
* Create a http request to the public API
* @param {string} url - The http endpoint
* @param {object} data - The data to send
* @param {function} callback - The callback method to call
* @param {string} method - the http method
* @return {undefined}
*/
const publicRequest = ( url, data = {}, callback, method = 'GET' ) => {
let opt = reqObj( url, data, method );
proxyRequest( opt, callback );
};
// XXX: This one works with array (e.g. for dust.transfer)
// XXX: I _guess_ we could use replace this function with the `qs` module
const makeQueryString = q =>
Object.keys( q )
.reduce( ( a, k ) => {
if ( Array.isArray( q[k] ) ) {
q[k].forEach( v => {
a.push( k + "=" + encodeURIComponent( v ) )
} )
} else if ( q[k] !== undefined ) {
a.push( k + "=" + encodeURIComponent( q[k] ) );
}
return a;
}, [] )
.join( "&" );
/**
* Create a http request to the public API
* @param {string} url - The http endpoint
* @param {object} data - The data to send
* @param {function} callback - The callback method to call
* @param {string} method - the http method
* @return {undefined}
*/
const apiRequest = ( url, data = {}, callback, method = 'GET' ) => {
requireApiKey( 'apiRequest' );
let opt = reqObj(
url,
data,
method,
Binance.options.APIKEY
);
proxyRequest( opt, callback );
};
// Check if API key is empty or invalid
const requireApiKey = function( source = 'requireApiKey', fatalError = true ) {
if ( !Binance.options.APIKEY ) {
if ( fatalError ) throw Error( `${ source }: Invalid API Key!` );
return false;
}
return true;
}
// Check if API secret is present
const requireApiSecret = function( source = 'requireApiSecret', fatalError = true ) {
if ( !Binance.options.APIKEY ) {
if ( fatalError ) throw Error( `${ source }: Invalid API Key!` );
return false;
}
if ( !Binance.options.APISECRET ) {
if ( fatalError ) throw Error( `${ source }: Invalid API Secret!` );
return false;
}
return true;
}
/**
* Make market request
* @param {string} url - The http endpoint
* @param {object} data - The data to send
* @param {function} callback - The callback method to call
* @param {string} method - the http method
* @return {undefined}
*/
const marketRequest = ( url, data = {}, callback, method = 'GET' ) => {
requireApiKey( 'marketRequest' );
let query = makeQueryString( data );
let opt = reqObj(
url + ( query ? '?' + query : '' ),
data,
method,
Binance.options.APIKEY
);
proxyRequest( opt, callback );
};
/**
* Create a signed http request
* @param {string} url - The http endpoint
* @param {object} data - The data to send
* @param {function} callback - The callback method to call
* @param {string} method - the http method
* @param {boolean} noDataInSignature - Prevents data from being added to signature
* @return {undefined}
*/
const signedRequest = ( url, data = {}, callback, method = 'GET', noDataInSignature = false ) => {
requireApiSecret( 'signedRequest' );
data.timestamp = new Date().getTime() + Binance.info.timeOffset;
if ( typeof data.recvWindow === 'undefined' ) data.recvWindow = Binance.options.recvWindow;
let query = method === 'POST' && noDataInSignature ? '' : makeQueryString( data );
let signature = crypto.createHmac( 'sha256', Binance.options.APISECRET ).update( query ).digest( 'hex' ); // set the HMAC hash header
if ( method === 'POST' ) {
let opt = reqObjPOST(
url,
data,
method,
Binance.options.APIKEY
);
opt.form.signature = signature;
proxyRequest( opt, callback );
} else {
let opt = reqObj(
url + '?' + query + '&signature=' + signature,
data,
method,
Binance.options.APIKEY
);
proxyRequest( opt, callback );
}
};
/**
* Create a signed spot order
* @param {string} side - BUY or SELL
* @param {string} symbol - The symbol to buy or sell
* @param {string} quantity - The quantity to buy or sell
* @param {string} price - The price per unit to transact each unit at
* @param {object} flags - additional order settings
* @param {function} callback - the callback function
* @return {undefined}
*/
const order = ( side, symbol, quantity, price, flags = {}, callback = false ) => {
let endpoint = flags.type === 'OCO' ? 'v3/order/oco' : 'v3/order';
if ( Binance.options.test ) endpoint += '/test';
let opt = {
symbol: symbol,
side: side,
type: 'LIMIT',
quantity: quantity
};
if ( typeof flags.type !== 'undefined' ) opt.type = flags.type;
if ( opt.type.includes( 'LIMIT' ) ) {
opt.price = price;
if ( opt.type !== 'LIMIT_MAKER' ) {
opt.timeInForce = 'GTC';
}
}
if (opt.type == 'MARKET' && typeof flags.quoteOrderQty !== 'undefined') {
opt.quoteOrderQty = flags.quoteOrderQty
delete opt.quantity;
}
if ( opt.type === 'OCO' ) {
opt.price = price;
opt.stopLimitPrice = flags.stopLimitPrice;
opt.stopLimitTimeInForce = 'GTC';
delete opt.type;
if ( typeof flags.listClientOrderId !== 'undefined' ) opt.listClientOrderId = flags.listClientOrderId;
if ( typeof flags.limitClientOrderId !== 'undefined' ) opt.limitClientOrderId = flags.limitClientOrderId;
if ( typeof flags.stopClientOrderId !== 'undefined' ) opt.stopClientOrderId = flags.stopClientOrderId;
}
if ( typeof flags.timeInForce !== 'undefined' ) opt.timeInForce = flags.timeInForce;
if ( typeof flags.newOrderRespType !== 'undefined' ) opt.newOrderRespType = flags.newOrderRespType;
if ( typeof flags.newClientOrderId !== 'undefined' ) opt.newClientOrderId = flags.newClientOrderId;
/*
* STOP_LOSS
* STOP_LOSS_LIMIT
* TAKE_PROFIT
* TAKE_PROFIT_LIMIT
* LIMIT_MAKER
*/
if ( typeof flags.icebergQty !== 'undefined' ) opt.icebergQty = flags.icebergQty;
if ( typeof flags.stopPrice !== 'undefined' ) {
opt.stopPrice = flags.stopPrice;
if ( opt.type === 'LIMIT' ) throw Error( 'stopPrice: Must set "type" to one of the following: STOP_LOSS, STOP_LOSS_LIMIT, TAKE_PROFIT, TAKE_PROFIT_LIMIT' );
}
signedRequest( base + endpoint, opt, ( error, response ) => {
if ( !response ) {
if ( callback ) callback( error, response );
else Binance.options.log( 'Order() error:', error );
return;
}
if ( typeof response.msg !== 'undefined' && response.msg === 'Filter failure: MIN_NOTIONAL' ) {
Binance.options.log( 'Order quantity too small. See exchangeInfo() for minimum amounts' );
}
if ( callback ) callback( error, response );
else Binance.options.log( side + '(' + symbol + ',' + quantity + ',' + price + ') ', response );
}, 'POST' );
};
/**
* Create a signed margin order
* @param {string} side - BUY or SELL
* @param {string} symbol - The symbol to buy or sell
* @param {string} quantity - The quantity to buy or sell
* @param {string} price - The price per unit to transact each unit at
* @param {object} flags - additional order settings
* @param {function} callback - the callback function
* @return {undefined}
*/
const marginOrder = ( side, symbol, quantity, price, flags = {}, callback = false ) => {
let endpoint = 'v1/margin/order';
if ( Binance.options.test ) endpoint += '/test';
let opt = {
symbol: symbol,
side: side,
type: 'LIMIT',
quantity: quantity
};
if ( typeof flags.type !== 'undefined' ) opt.type = flags.type;
if (typeof flags.isIsolated !== 'undefined') opt.isIsolated = flags.isIsolated;
if ( opt.type.includes( 'LIMIT' ) ) {
opt.price = price;
if ( opt.type !== 'LIMIT_MAKER' ) {
opt.timeInForce = 'GTC';
}
}
if ( typeof flags.timeInForce !== 'undefined' ) opt.timeInForce = flags.timeInForce;
if ( typeof flags.newOrderRespType !== 'undefined' ) opt.newOrderRespType = flags.newOrderRespType;
if ( typeof flags.newClientOrderId !== 'undefined' ) opt.newClientOrderId = flags.newClientOrderId;
if ( typeof flags.sideEffectType !== 'undefined' ) opt.sideEffectType = flags.sideEffectType;
/*
* STOP_LOSS
* STOP_LOSS_LIMIT
* TAKE_PROFIT
* TAKE_PROFIT_LIMIT
*/
if ( typeof flags.icebergQty !== 'undefined' ) opt.icebergQty = flags.icebergQty;
if ( typeof flags.stopPrice !== 'undefined' ) {
opt.stopPrice = flags.stopPrice;
if ( opt.type === 'LIMIT' ) throw Error( 'stopPrice: Must set "type" to one of the following: STOP_LOSS, STOP_LOSS_LIMIT, TAKE_PROFIT, TAKE_PROFIT_LIMIT' );
}
signedRequest( sapi + endpoint, opt, function ( error, response ) {
if ( !response ) {
if ( callback ) callback( error, response );
else Binance.options.log( 'Order() error:', error );
return;
}
if ( typeof response.msg !== 'undefined' && response.msg === 'Filter failure: MIN_NOTIONAL' ) {
Binance.options.log( 'Order quantity too small. See exchangeInfo() for minimum amounts' );
}
if ( callback ) callback( error, response );
else Binance.options.log( side + '(' + symbol + ',' + quantity + ',' + price + ') ', response );
}, 'POST' );
};
// Futures internal functions
const futuresOrder = async ( side, symbol, quantity, price = false, params = {} ) => {
params.symbol = symbol;
params.side = side;
if ( quantity ) params.quantity = quantity;
// if in the binance futures setting Hedged mode is active, positionSide parameter is mandatory
if( typeof params.positionSide === 'undefined' && Binance.options.hedgeMode ){
params.positionSide = side === 'BUY' ? 'LONG' : 'SHORT';
}
// LIMIT STOP MARKET STOP_MARKET TAKE_PROFIT TAKE_PROFIT_MARKET
// reduceOnly stopPrice
if ( price ) {
params.price = price;
if ( typeof params.type === 'undefined' ) params.type = 'LIMIT';
} else {
if ( typeof params.type === 'undefined' ) params.type = 'MARKET';
}
if ( !params.timeInForce && ( params.type.includes( 'LIMIT' ) || params.type === 'STOP' || params.type === 'TAKE_PROFIT' ) ) {
params.timeInForce = 'GTX'; // Post only by default. Use GTC for limit orders.
}
return promiseRequest( 'v1/order', params, { base:fapi, type:'TRADE', method:'POST' } );
};
const deliveryOrder = async ( side, symbol, quantity, price = false, params = {} ) => {
params.symbol = symbol;
params.side = side;
params.quantity = quantity;
// if in the binance futures setting Hedged mode is active, positionSide parameter is mandatory
if( Binance.options.hedgeMode ){
params.positionSide = side === 'BUY' ? 'LONG' : 'SHORT';
}
// LIMIT STOP MARKET STOP_MARKET TAKE_PROFIT TAKE_PROFIT_MARKET
// reduceOnly stopPrice
if ( price ) {
params.price = price;
if ( typeof params.type === 'undefined' ) params.type = 'LIMIT';
} else {
if ( typeof params.type === 'undefined' ) params.type = 'MARKET';
}
if ( !params.timeInForce && ( params.type.includes( 'LIMIT' ) || params.type === 'STOP' || params.type === 'TAKE_PROFIT' ) ) {
params.timeInForce = 'GTX'; // Post only by default. Use GTC for limit orders.
}
return promiseRequest( 'v1/order', params, { base:dapi, type:'TRADE', method:'POST' } );
};
const promiseRequest = async ( url, data = {}, flags = {} ) => {
return new Promise( ( resolve, reject ) => {
let query = '', headers = {
'User-Agent': userAgent,
'Content-type': 'application/x-www-form-urlencoded'
};
if ( typeof flags.method === 'undefined' ) flags.method = 'GET'; // GET POST PUT DELETE
if ( typeof flags.type === 'undefined' ) flags.type = false; // TRADE, SIGNED, MARKET_DATA, USER_DATA, USER_STREAM
else {
if ( typeof data.recvWindow === 'undefined' ) data.recvWindow = Binance.options.recvWindow;
requireApiKey( 'promiseRequest' );
headers['X-MBX-APIKEY'] = Binance.options.APIKEY;
}
let baseURL = typeof flags.base === 'undefined' ? base : flags.base;
if ( Binance.options.test && baseURL === fapi ) baseURL = fapiTest;
if ( Binance.options.test && baseURL === dapi ) baseURL = dapiTest;
let opt = {
headers,
url: baseURL + url,
method: flags.method,
timeout: Binance.options.recvWindow,
followAllRedirects: true
};
if ( flags.type === 'SIGNED' || flags.type === 'TRADE' || flags.type === 'USER_DATA' ) {
if ( !requireApiSecret( 'promiseRequest' ) ) return reject( 'promiseRequest: Invalid API Secret!' );
data.timestamp = new Date().getTime() + Binance.info.timeOffset;
query = makeQueryString( data );
data.signature = crypto.createHmac( 'sha256', Binance.options.APISECRET ).update( query ).digest( 'hex' ); // HMAC hash header
opt.url = `${ baseURL }${ url }?${ query }&signature=${ data.signature }`;
}
opt.qs = data;
/*if ( flags.method === 'POST' ) {
opt.form = data;
} else {
opt.qs = data;
}*/
try {
request( addProxy( opt ), ( error, response, body ) => {
if ( error ) return reject( error );
try {
Binance.info.lastRequest = new Date().getTime();
if ( response ) {
Binance.info.statusCode = response.statusCode || 0;
if ( response.request ) Binance.info.lastURL = response.request.uri.href;
if ( response.headers ) {
Binance.info.usedWeight = response.headers['x-mbx-used-weight-1m'] || 0;
Binance.info.futuresLatency = response.headers['x-response-time'] || 0;
}
}
if ( !error && response.statusCode == 200 ) return resolve( JSONbig.parse( body ) );
if ( typeof response.body !== 'undefined' ) {
return resolve( JSONbig.parse( response.body ) );
}
return reject( response );
} catch ( err ) {
return reject( `promiseRequest error #${ response.statusCode }` );
}
} ).on( 'error', reject );
} catch ( err ) {
return reject( err );
}
} );
};
/**
* No-operation function
* @return {undefined}
*/
const noop = () => { }; // Do nothing.
/**
* Reworked Tuitio's heartbeat code into a shared single interval tick
* @return {undefined}
*/
const socketHeartbeat = () => {
/* Sockets removed from `subscriptions` during a manual terminate()
will no longer be at risk of having functions called on them */
for ( let endpointId in Binance.subscriptions ) {
const ws = Binance.subscriptions[endpointId];
if ( ws.isAlive ) {
ws.isAlive = false;
if ( ws.readyState === WebSocket.OPEN ) ws.ping( noop );
} else {
if ( Binance.options.verbose ) Binance.options.log( 'Terminating inactive/broken WebSocket: ' + ws.endpoint );
if ( ws.readyState === WebSocket.OPEN ) ws.terminate();
}
}
};
/**
* Called when socket is opened, subscriptions are registered for later reference
* @param {function} opened_callback - a callback function
* @return {undefined}
*/
const handleSocketOpen = function ( opened_callback ) {
this.isAlive = true;
if ( Object.keys( Binance.subscriptions ).length === 0 ) {
Binance.socketHeartbeatInterval = setInterval( socketHeartbeat, 30000 );
}
Binance.subscriptions[this.endpoint] = this;
if ( typeof opened_callback === 'function' ) opened_callback( this.endpoint );
};
/**
* Called when socket is closed, subscriptions are de-registered for later reference
* @param {boolean} reconnect - true or false to reconnect the socket
* @param {string} code - code associated with the socket
* @param {string} reason - string with the response
* @return {undefined}
*/
const handleSocketClose = function ( reconnect, code, reason ) {
delete Binance.subscriptions[this.endpoint];
if ( Binance.subscriptions && Object.keys( Binance.subscriptions ).length === 0 ) {
clearInterval( Binance.socketHeartbeatInterval );
}
Binance.options.log( 'WebSocket closed: ' + this.endpoint +
( code ? ' (' + code + ')' : '' ) +
( reason ? ' ' + reason : '' ) );
if ( Binance.options.reconnect && this.reconnect && reconnect ) {
if ( this.endpoint && parseInt( this.endpoint.length, 10 ) === 60 ) Binance.options.log( 'Account data WebSocket reconnecting...' );
else Binance.options.log( 'WebSocket reconnecting: ' + this.endpoint + '...' );
try {
reconnect();
} catch ( error ) {
Binance.options.log( 'WebSocket reconnect error: ' + error.message );
}
}
};
/**
* Called when socket errors
* @param {object} error - error object message
* @return {undefined}
*/
const handleSocketError = function ( error ) {
/* Errors ultimately result in a `close` event.
see: https://github.com/websockets/ws/blob/828194044bf247af852b31c49e2800d557fedeff/lib/websocket.js#L126 */
Binance.options.log( 'WebSocket error: ' + this.endpoint +
( error.code ? ' (' + error.code + ')' : '' ) +
( error.message ? ' ' + error.message : '' ) );
};
/**
* Called on each socket heartbeat
* @return {undefined}
*/
const handleSocketHeartbeat = function () {
this.isAlive = true;
};
/**
* Used to subscribe to a single websocket endpoint
* @param {string} endpoint - endpoint to connect to
* @param {function} callback - the function to call when information is received
* @param {boolean} reconnect - whether to reconnect on disconnect
* @param {object} opened_callback - the function to call when opened
* @return {WebSocket} - websocket reference
*/
const subscribe = function ( endpoint, callback, reconnect = false, opened_callback = false ) {
let httpsproxy = process.env.https_proxy || false;
let socksproxy = process.env.socks_proxy || false;
let ws = false;
if ( socksproxy !== false ) {
socksproxy = proxyReplacewithIp( socksproxy );
if ( Binance.options.verbose ) Binance.options.log( 'using socks proxy server ' + socksproxy );
let agent = new SocksProxyAgent( {
protocol: parseProxy( socksproxy )[0],
host: parseProxy( socksproxy )[1],
port: parseProxy( socksproxy )[2]
} );
ws = new WebSocket( stream + endpoint, { agent: agent } );
} else if ( httpsproxy !== false ) {
let config = url.parse( httpsproxy );
let agent = new HttpsProxyAgent( config );
if ( Binance.options.verbose ) Binance.options.log( 'using proxy server ' + agent );
ws = new WebSocket( stream + endpoint, { agent: agent } );
} else {
ws = new WebSocket( stream + endpoint );
}
if ( Binance.options.verbose ) Binance.options.log( 'Subscribed to ' + endpoint );
ws.reconnect = Binance.options.reconnect;
ws.endpoint = endpoint;
ws.isAlive = false;
ws.on( 'open', handleSocketOpen.bind( ws, opened_callback ) );
ws.on( 'pong', handleSocketHeartbeat );
ws.on( 'error', handleSocketError );
ws.on( 'close', handleSocketClose.bind( ws, reconnect ) );
ws.on( 'message', data => {
try {
callback( JSON.parse( data ) );
} catch ( error ) {
Binance.options.log( 'Parse error: ' + error.message );
}
} );
return ws;
};
/**
* Used to subscribe to a combined websocket endpoint
* @param {string} streams - streams to connect to
* @param {function} callback - the function to call when information is received
* @param {boolean} reconnect - whether to reconnect on disconnect
* @param {object} opened_callback - the function to call when opened
* @return {WebSocket} - websocket reference
*/
const subscribeCombined = function ( streams, callback, reconnect = false, opened_callback = false ) {
let httpsproxy = process.env.https_proxy || false;
let socksproxy = process.env.socks_proxy || false;
const queryParams = streams.join( '/' );
let ws = false;
if ( socksproxy !== false ) {
socksproxy = proxyReplacewithIp( socksproxy );
if ( Binance.options.verbose ) Binance.options.log( 'using socks proxy server ' + socksproxy );
let agent = new SocksProxyAgent( {
protocol: parseProxy( socksproxy )[0],
host: parseProxy( socksproxy )[1],
port: parseProxy( socksproxy )[2]
} );
ws = new WebSocket( combineStream + queryParams, { agent: agent } );
} else if ( httpsproxy !== false ) {
if ( Binance.options.verbose ) Binance.options.log( 'using proxy server ' + httpsproxy );
let config = url.parse( httpsproxy );
let agent = new HttpsProxyAgent( config );
ws = new WebSocket( combineStream + queryParams, { agent: agent } );
} else {
ws = new WebSocket( combineStream + queryParams );
}
ws.reconnect = Binance.options.reconnect;
ws.endpoint = stringHash( queryParams );
ws.isAlive = false;
if ( Binance.options.verbose ) {
Binance.options.log( 'CombinedStream: Subscribed to [' + ws.endpoint + '] ' + queryParams );
}
ws.on( 'open', handleSocketOpen.bind( ws, opened_callback ) );
ws.on( 'pong', handleSocketHeartbeat );
ws.on( 'error', handleSocketError );
ws.on( 'close', handleSocketClose.bind( ws, reconnect ) );
ws.on( 'message', data => {
try {
callback( JSON.parse( data ).data );
} catch ( error ) {
Binance.options.log( 'CombinedStream: Parse error: ' + error.message );
}
} );
return ws;
};
/**
* Used to terminate a web socket
* @param {string} endpoint - endpoint identifier associated with the web socket
* @param {boolean} reconnect - auto reconnect after termination
* @return {undefined}
*/
const terminate = function ( endpoint, reconnect = false ) {
let ws = Binance.subscriptions[endpoint];
if ( !ws ) return;
ws.removeAllListeners( 'message' );
ws.reconnect = reconnect;
ws.terminate();
}
/**
* Futures heartbeat code with a shared single interval tick
* @return {undefined}
*/
const futuresSocketHeartbeat = () => {
/* Sockets removed from subscriptions during a manual terminate()
will no longer be at risk of having functions called on them */
for ( let endpointId in Binance.futuresSubscriptions ) {
const ws = Binance.futuresSubscriptions[endpointId];
if ( ws.isAlive ) {
ws.isAlive = false;
if ( ws.readyState === WebSocket.OPEN ) ws.ping( noop );
} else {
if ( Binance.options.verbose ) Binance.options.log( `Terminating zombie futures WebSocket: ${ ws.endpoint }` );
if ( ws.readyState === WebSocket.OPEN ) ws.terminate();
}
}
};
/**
* Called when a futures socket is opened, subscriptions are registered for later reference
* @param {function} openCallback - a callback function
* @return {undefined}
*/
const handleFuturesSocketOpen = function ( openCallback ) {
this.isAlive = true;
if ( Object.keys( Binance.futuresSubscriptions ).length === 0 ) {
Binance.socketHeartbeatInterval = setInterval( futuresSocketHeartbeat, 30000 );
}
Binance.futuresSubscriptions[this.endpoint] = this;
if ( typeof openCallback === 'function' ) openCallback( this.endpoint );
};
/**
* Called when futures websocket is closed, subscriptions are de-registered for later reference
* @param {boolean} reconnect - true or false to reconnect the socket
* @param {string} code - code associated with the socket
* @param {string} reason - string with the response
* @return {undefined}
*/
const handleFuturesSocketClose = function ( reconnect, code, reason ) {
delete Binance.futuresSubscriptions[this.endpoint];
if ( Binance.futuresSubscriptions && Object.keys( Binance.futuresSubscriptions ).length === 0 ) {
clearInterval( Binance.socketHeartbeatInterval );
}
Binance.options.log( 'Futures WebSocket closed: ' + this.endpoint +
( code ? ' (' + code + ')' : '' ) +
( reason ? ' ' + reason : '' ) );
if ( Binance.options.reconnect && this.reconnect && reconnect ) {
if ( this.endpoint && parseInt( this.endpoint.length, 10 ) === 60 ) Binance.options.log( 'Futures account data WebSocket reconnecting...' );
else Binance.options.log( 'Futures WebSocket reconnecting: ' + this.endpoint + '...' );
try {
reconnect();
} catch ( error ) {
Binance.options.log( 'Futures WebSocket reconnect error: ' + error.message );
}
}
};
/**
* Called when a futures websocket errors
* @param {object} error - error object message
* @return {undefined}
*/
const handleFuturesSocketError = function ( error ) {
Binance.options.log( 'Futures WebSocket error: ' + this.endpoint +
( error.code ? ' (' + error.code + ')' : '' ) +
( error.message ? ' ' + error.message : '' ) );
};
/**
* Called on each futures socket heartbeat
* @return {undefined}
*/
const handleFuturesSocketHeartbeat = function () {
this.isAlive = true;
};
/**
* Used to subscribe to a single futures websocket endpoint
* @param {string} endpoint - endpoint to connect to
* @param {function} callback - the function to call when information is received
* @param {object} params - Optional reconnect {boolean} (whether to reconnect on disconnect), openCallback {function}, id {string}
* @return {WebSocket} - websocket reference
*/
const futuresSubscribeSingle = function ( endpoint, callback, params = {} ) {
if ( typeof params === 'boolean' ) params = { reconnect: params };
if ( !params.reconnect ) params.reconnect = false;
if ( !params.openCallback ) params.openCallback = false;
if ( !params.id ) params.id = false;
let httpsproxy = process.env.https_proxy || false;
let socksproxy = process.env.socks_proxy || false;
let ws = false;
if ( socksproxy !== false ) {
socksproxy = proxyReplacewithIp( socksproxy );
if ( Binance.options.verbose ) Binance.options.log( `futuresSubscribeSingle: using socks proxy server: ${ socksproxy }` );
let agent = new SocksProxyAgent( {
protocol: parseProxy( socksproxy )[0],
host: parseProxy( socksproxy )[1],
port: parseProxy( socksproxy )[2]
} );
ws = new WebSocket( ( Binance.options.test ? fstreamSingleTest : fstreamSingle ) + endpoint, { agent } );
} else if ( httpsproxy !== false ) {
let config = url.parse( httpsproxy );
let agent = new HttpsProxyAgent( config );
if ( Binance.options.verbose ) Binance.options.log( `futuresSubscribeSingle: using proxy server: ${ agent }` );
ws = new WebSocket( ( Binance.options.test ? fstreamSingleTest : fstreamSingle ) + endpoint, { agent } );
} else {
ws = new WebSocket( ( Binance.options.test ? fstreamSingleTest : fstreamSingle ) + endpoint );
}
if ( Binance.options.verbose ) Binance.options.log( 'futuresSubscribeSingle: Subscribed to ' + endpoint );
ws.reconnect = Binance.options.reconnect;
ws.endpoint = endpoint;
ws.isAlive = false;
ws.on( 'open', handleFuturesSocketOpen.bind( ws, params.openCallback ) );
ws.on( 'pong', handleFuturesSocketHeartbeat );
ws.on( 'error', handleFuturesSocketError );
ws.on( 'close', handleFuturesSocketClose.bind( ws, params.reconnect ) );
ws.on( 'message', data => {
try {
callback( JSONbig.parse( data ) );
} catch ( error ) {
Binance.options.log( 'Parse error: ' + error.message );
}
} );
return ws;
};
/**
* Used to subscribe to a combined futures websocket endpoint
* @param {string} streams - streams to connect to
* @param {function} callback - the function to call when information is received
* @param {object} params - Optional reconnect {boolean} (whether to reconnect on disconnect), openCallback {function}, id {string}
* @return {WebSocket} - websocket reference
*/
const futuresSubscribe = function ( streams, callback, params = {} ) {
if ( typeof streams === 'string' ) return futuresSubscribeSingle( streams, callback, params );
if ( typeof params === 'boolean' ) params = { reconnect: params };
if ( !params.reconnect ) params.reconnect = false;
if ( !params.openCallback ) params.openCallback = false;
if ( !params.id ) params.id = false;
let httpsproxy = process.env.https_proxy || false;
let socksproxy = process.env.socks_proxy || false;
const queryParams = streams.join( '/' );
let ws = false;
if ( socksproxy !== false ) {
socksproxy = proxyReplacewithIp( socksproxy );
if ( Binance.options.verbose ) Binance.options.log( `futuresSubscribe: using socks proxy server ${ socksproxy }` );
let agent = new SocksProxyAgent( {
protocol: parseProxy( socksproxy )[0],
host: parseProxy( socksproxy )[1],
port: parseProxy( socksproxy )[2]
} );
ws = new WebSocket( ( Binance.options.test ? fstreamTest : fstream ) + queryParams, { agent } );
} else if ( httpsproxy !== false ) {
if ( Binance.options.verbose ) Binance.options.log( `futuresSubscribe: using proxy server ${ httpsproxy }` );
let config = url.parse( httpsproxy );
let agent = new HttpsProxyAgent( config );
ws = new WebSocket( ( Binance.options.test ? fstreamTest : fstream ) + queryParams, { agent } );
} else {
ws = new WebSocket( ( Binance.options.test ? fstreamTest : fstream ) + queryParams );
}
ws.reconnect = Binance.options.reconnect;
ws.endpoint = stringHash( queryParams );
ws.isAlive = false;
if ( Binance.options.verbose ) {
Binance.options.log( `futuresSubscribe: Subscribed to [${ ws.endpoint }] ${ queryParams }` );
}
ws.on( 'open', handleFuturesSocketOpen.bind( ws, params.openCallback ) );
ws.on( 'pong', handleFuturesSocketHeartbeat );
ws.on( 'error', handleFuturesSocketError );
ws.on( 'close', handleFuturesSocketClose.bind( ws, params.reconnect ) );
ws.on( 'message', data => {
try {
callback( JSON.parse( data ).data );
} catch ( error ) {
Binance.options.log( `futuresSubscribe: Parse error: ${ error.message }` );
}
} );
return ws;
};
/**
* Used to terminate a futures websocket
* @param {string} endpoint - endpoint identifier associated with the web socket
* @param {boolean} reconnect - auto reconnect after termination
* @return {undefined}
*/
const futuresTerminate = function ( endpoint, reconnect = false ) {
let ws = Binance.futuresSubscriptions[endpoint];
if ( !ws ) return;
ws.removeAllListeners( 'message' );
ws.reconnect = reconnect;
ws.terminate();
}
/**
* Combines all futures OHLC data with the latest update
* @param {string} symbol - the symbol
* @param {string} interval - time interval
* @return {array} - interval data for given symbol
*/
const futuresKlineConcat = ( symbol, interval ) => {
let output = Binance.futuresTicks[symbol][interval];
if ( typeof Binance.futuresRealtime[symbol][interval].time === 'undefined' ) return output;
const time = Binance.futuresRealtime[symbol][interval].time;
const last_updated = Object.keys( Binance.futuresTicks[symbol][interval] ).pop();
if ( time >= last_updated ) {
output[time] = Binance.futuresRealtime[symbol][interval];