forked from binance-exchange/node-binance-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
1274 lines (1152 loc) · 46.2 KB
/
test.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
* ============================================================ */
'use strict';
const WARN_SHOULD_BE_OBJ = 'should be an object';
const WARN_SHOULD_BE_NULL = 'should be null';
const WARN_SHOULD_BE_NOT_NULL = 'should not be null';
const WARN_SHOULD_HAVE_KEY = 'should have key ';
const WARN_SHOULD_NOT_HAVE_KEY = 'should not have key ';
const WARN_SHOULD_BE_UNDEFINED = 'should be undefined';
const WARN_SHOULD_BE_TYPE = 'should be a string ';
const TIMEOUT = 10000;
let chai = require( 'chai' );
let assert = chai.assert;
let path = require( 'path' );
let binance = require( path.resolve( __dirname, 'node-binance-api.js' ) );
let util = require( 'util' );
let num_pairs = 299;
let num_currencies = 156;
let logger = {
log: function (msg){
let logLineDetails = ((new Error().stack).split('at ')[3]).trim();
let logLineNum = logLineDetails.split(':');
console.log('DEBUG', logLineNum[1] + ':' + logLineNum[2], msg);
}
}
let debug = function( x ) {
if ( typeof ( process.env.node_binance_api ) === 'undefined' ) {
return;
}
logger.log( typeof ( x ) );
logger.log( util.inspect( x ) );
}
let stopSockets = function(log = false) {
//stopSocketsRunning = true;
let endpoints = binance.websockets.subscriptions();
for ( let endpoint in endpoints ) {
if (log) console.log('Terminated ws endpoint: ' + endpoint);
binance.websockets.terminate(endpoint);
}
}
debug( 'Begin' );
/*global describe*/
/*eslint no-undef: "error"*/
describe( 'Construct', function() {
/*global it*/
/*eslint no-undef: "error"*/
it( 'Construct the binance object', function( done ) {
binance.options( {
APIKEY: 'z5RQZ9n8JcS3HLDQmPpfLQIGGQN6TTs5pCP5CTnn4nYk2ImFcew49v4ZrmP3MGl5',
APISECRET: 'ZqePF1DcLb6Oa0CfcLWH0Tva59y8qBBIqu789JEY27jq0RkOKXpNl9992By1PN9Z',
useServerTime: true,
reconnect: false,
verbose: false,
log: debug
} );
assert( typeof ( binance ) === 'object', 'Binance is not an object' );
done();
} ).timeout( TIMEOUT );
} );
describe( 'UseServerTime', function() {
it( 'Call use server time', function( done ) {
binance.useServerTime();
done();
}).timeout( TIMEOUT );
});
describe( 'Prices', function() {
it( 'Checks the price of BNBBTC', function( done ) {
binance.prices( 'BNBBTC', ( error, ticker ) => {
debug( error );
debug( ticker );
assert( typeof ( error ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( ticker ) === 'object', WARN_SHOULD_BE_OBJ );
assert( error === null, WARN_SHOULD_BE_NULL );
assert( ticker !== null, WARN_SHOULD_BE_NOT_NULL );
assert( Object.prototype.hasOwnProperty.call(ticker, 'BNBBTC' ), WARN_SHOULD_HAVE_KEY + 'BNBBTC' );
assert( Object.prototype.hasOwnProperty.call(ticker, 'ETHBTC' ) === false, WARN_SHOULD_NOT_HAVE_KEY + 'ETHBTC' );
done();
});
}).timeout( TIMEOUT );
});
describe( 'All Prices', function() {
it( 'Checks the prices of coin pairs', function( done ) {
binance.prices( ( error, ticker ) => {
debug( error );
debug( ticker );
assert( typeof ( error ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( ticker ) === 'object', WARN_SHOULD_BE_OBJ );
assert( error === null, WARN_SHOULD_BE_NULL );
assert( ticker !== null, WARN_SHOULD_BE_NOT_NULL );
assert( Object.prototype.hasOwnProperty.call(ticker, 'BNBBTC' ), WARN_SHOULD_HAVE_KEY + 'BNBBTC' );
assert( Object.keys( ticker ).length >= num_pairs, 'should at least ' + num_pairs + 'currency pairs?' );
done();
});
}).timeout( TIMEOUT );
});
describe( 'Balances', function() {
it( 'Get the balances in the account', function( done ) {
binance.balance( ( error, balances ) => {
debug( error );
debug( balances );
assert( error === null, WARN_SHOULD_BE_NULL );
assert( balances !== null, WARN_SHOULD_BE_NOT_NULL );
assert( balances );
assert( Object.prototype.hasOwnProperty.call(balances, 'BNB' ), WARN_SHOULD_HAVE_KEY + 'BNB' );
assert( Object.prototype.hasOwnProperty.call(balances.BNB, 'available' ), WARN_SHOULD_HAVE_KEY + 'available' );
assert( Object.prototype.hasOwnProperty.call(balances.BNB, 'onOrder' ), WARN_SHOULD_HAVE_KEY + 'onOrder' );
assert( Object.keys( balances ).length >= num_currencies, 'should at least ' + num_currencies + 'currencies?' );
done();
});
}).timeout( TIMEOUT );
});
describe( 'Book Ticker', function() {
it( 'Get the BNB book ticker', function( done ) {
binance.bookTickers( 'BNBBTC', ( error, ticker ) => {
debug( error );
debug( ticker );
assert( error === null, WARN_SHOULD_BE_NULL );
assert( ticker !== null, WARN_SHOULD_BE_NOT_NULL );
assert( ticker );
let members = ['symbol', 'bidPrice', 'bidQty', 'askPrice', 'askQty'];
members.forEach( function( value ) {
assert( Object.prototype.hasOwnProperty.call(ticker, value ), WARN_SHOULD_HAVE_KEY + value );
});
done();
});
}).timeout( TIMEOUT );
it( 'Get all book tickers', function( done ) {
binance.bookTickers( false, ( error, ticker ) => {
assert( ticker );
/*
assert( error === null, WARN_SHOULD_BE_NULL );
assert( ticker !== null, WARN_SHOULD_BE_NOT_NULL );
assert( ticker );
let members = ['symbol', 'bidPrice', 'bidQty', 'askPrice', 'askQty'];
members.forEach( function( value ) {
assert( Object.prototype.hasOwnProperty.call(ticker, value ), WARN_SHOULD_HAVE_KEY + value );
});
*/
done();
});
}).timeout( TIMEOUT );
});
describe( 'Booker Tickers', function() {
it( 'Get the tickers for all pairs', function( done ) {
binance.bookTickers( ( error, ticker ) => {
debug( error );
debug( ticker );
assert( typeof ( error ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( ticker ) === 'object', WARN_SHOULD_BE_OBJ );
assert( error === null, WARN_SHOULD_BE_NULL );
assert( ticker !== null, WARN_SHOULD_BE_NOT_NULL );
assert( Object.keys( ticker ).length >= num_pairs, 'should at least ' + num_pairs + 'currency pairs?' );
let members = ['symbol', 'bidPrice', 'bidQty', 'askPrice', 'askQty'];
ticker.forEach( function( obj) {
members.forEach( function( member ) {
assert( Object.prototype.hasOwnProperty.call( obj, member ), WARN_SHOULD_HAVE_KEY + member );
});
});
done();
});
}).timeout( TIMEOUT );
});
describe( 'Market', function() {
it( 'Get the market base symbol of a symbol pair', function( done ) {
let tocheck = ['TRXBNB', 'BNBBTC', 'BNBETH', 'BNBUSDT'];
tocheck.forEach( function(element) {
let mark = binance.getMarket(element);
assert( typeof ( mark ) === 'string', WARN_SHOULD_BE_TYPE + 'string' );
assert( element.endsWith( mark ), 'should end with: ' + mark );
});
assert.isNotOk( binance.getMarket('ABCDEFG' ), WARN_SHOULD_BE_UNDEFINED );
done();
}).timeout( TIMEOUT );
});
describe( 'Depth chart BNB', function() {
it( 'Get the depth chart information for BNBBTC', function( done ) {
binance.depth( 'BNBBTC', ( error, depth, symbol ) => {
debug( error );
debug( depth );
debug( symbol );
assert( error === null, WARN_SHOULD_BE_NULL );
assert( depth !== null, WARN_SHOULD_BE_NOT_NULL );
assert( symbol !== null, WARN_SHOULD_BE_NOT_NULL );
assert( typeof ( symbol ) === 'string', 'should be type of string' );
assert( symbol === 'BNBBTC', 'should be BNBBTC' );
assert( typeof ( depth ) === 'object', WARN_SHOULD_BE_OBJ );
assert( Object.keys( depth ).length === 3, 'should have length 3' );
let members = ['lastUpdateId', 'asks', 'bids'];
members.forEach( function( value ) {
assert( Object.prototype.hasOwnProperty.call( depth, value ), WARN_SHOULD_HAVE_KEY + value );
});
done();
});
}).timeout( TIMEOUT );
});
describe( 'Buy', function() {
it( 'Attempt to buy ETH', function( done ) {
let quantity = 1;
let price = 0.069;
assert( typeof ( binance.buy( 'ETHBTC', quantity, price ) ) === 'undefined', WARN_SHOULD_BE_UNDEFINED);
done();
}).timeout( TIMEOUT );
});
describe( 'Sell', function() {
it( 'Attempt to sell ETH', function( done ) {
let quantity = 1;
let price = 0.069;
assert( typeof ( binance.sell( 'ETHBTC', quantity, price ) ) === 'undefined', WARN_SHOULD_BE_UNDEFINED);
done();
}).timeout( TIMEOUT );
});
describe( 'MarketiBuy', function() {
it( 'Attempt to buy ETH at market price', function( done ) {
let quantity = 1;
assert( typeof ( binance.marketBuy( 'BNBBTC', quantity ) ) === 'undefined', WARN_SHOULD_BE_UNDEFINED);
done();
}).timeout( TIMEOUT );
});
describe( 'MarketSell', function() {
it( 'Attempt to sell ETH at market price', function( done ) {
let quantity = 1;
assert( typeof ( binance.marketSell( 'ETHBTC', quantity ) ) === 'undefined', WARN_SHOULD_BE_UNDEFINED);
done();
}).timeout( TIMEOUT );
});
describe( 'Buy order advanced', function() {
it( 'Attempt to buy BNB specifying order type', function( done ) {
let quantity = 1;
let price = 0.069;
binance.buy( 'BNBETH', quantity, price, { type: 'LIMIT' }, ( error, response ) => {
debug( error );
debug( response );
assert( typeof ( error ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( response ) === 'object', WARN_SHOULD_BE_OBJ );
assert( error !== null, WARN_SHOULD_BE_NOT_NULL );
assert( response !== null, WARN_SHOULD_BE_NOT_NULL );
assert( error.body === '{"code":-2010,"msg":"Account has insufficient balance for requested action."}' );
assert( typeof ( response.orderId ) === 'undefined', WARN_SHOULD_BE_UNDEFINED);
assert( Object.keys( response ).length === 0 );
done();
});
}).timeout( TIMEOUT );
});
describe( 'Sell Stop loess', function() {
it( 'Attempt to create a stop loss order', function( done ) {
let type = 'STOP_LOSS';
let quantity = 1;
let price = 0.069;
let stopPrice = 0.068;
assert( typeof ( binance.sell( 'ETHBTC', quantity, price, { stopPrice: stopPrice, type: type } ) ) === 'undefined', WARN_SHOULD_BE_UNDEFINED);
done();
}).timeout( TIMEOUT );
});
describe( 'Iceberg sell order', function() {
it( 'Attempt to create a sell order', function( done ) {
let quantity = 1;
let price = 0.069;
assert( typeof ( binance.sell( 'ETHBTC', quantity, price, { icebergQty: 10 } ) ) === 'undefined', WARN_SHOULD_BE_UNDEFINED);
done();
}).timeout( TIMEOUT );
});
describe( 'Cancel order', function() {
it( 'Attempt to cancel an order', function( done ) {
let orderid = '7610385';
binance.cancel( 'ETHBTC', orderid, ( error, response, symbol ) => {
debug( error );
debug( response );
debug( symbol );
assert( typeof ( error ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( response ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( symbol ) === 'string', WARN_SHOULD_BE_TYPE + 'string' );
assert( symbol === 'ETHBTC' );
assert( error !== null, WARN_SHOULD_BE_NOT_NULL );
assert( response !== null, WARN_SHOULD_BE_NOT_NULL );
assert( error.body === '{"code":-2011,"msg":"UNKNOWN_ORDER"}' );
assert( typeof ( response.orderId ) === 'undefined', WARN_SHOULD_BE_UNDEFINED);
assert( Object.keys( response ).length === 0 );
done();
});
}).timeout( TIMEOUT );
});
describe( 'Cancel orders', function() {
it( 'Attempt to cancel all orders given a symbol', function( done ) {
binance.cancelOrders( 'XMRBTC', ( error, response, symbol ) => {
debug( error );
debug( response );
debug( symbol );
assert( typeof ( error ) === 'string', WARN_SHOULD_BE_OBJ );
assert( typeof ( response ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( symbol ) === 'string', WARN_SHOULD_BE_TYPE + 'string' );
assert( symbol === 'XMRBTC' );
assert( error !== null, WARN_SHOULD_BE_NOT_NULL );
assert( error === 'No orders present for this symbol', WARN_SHOULD_BE_TYPE + 'string' );
done();
} );
} ).timeout( TIMEOUT );
} );
describe( 'Open Orders', function() {
it( 'Attempt to show all orders to ETHBTC', function( done ) {
binance.openOrders( 'ETHBTC', ( error, openOrders, symbol ) => {
debug( error );
debug( openOrders );
debug( symbol );
assert( typeof ( error ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( openOrders ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( symbol ) === 'string', WARN_SHOULD_BE_TYPE + 'string' );
assert( symbol === 'ETHBTC' );
assert( error === null, WARN_SHOULD_BE_NULL );
assert( openOrders !== null, WARN_SHOULD_BE_NOT_NULL );
assert( symbol !== null, WARN_SHOULD_BE_NOT_NULL );
assert( Object.keys( openOrders ).length === 0 );
done();
} );
} ).timeout( TIMEOUT );
} );
describe( 'Open Orders', function() {
it( 'Attempt to show all orders for all symbols', function( done ) {
binance.openOrders( false, ( error, openOrders ) => {
debug( error );
debug( openOrders );
assert( typeof ( error ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( openOrders ) === 'object', WARN_SHOULD_BE_OBJ );
assert( error === null, WARN_SHOULD_BE_NULL );
assert( openOrders !== null, WARN_SHOULD_BE_NOT_NULL );
assert( Object.keys( openOrders ).length === 0 );
done();
} );
} ).timeout( TIMEOUT );
} );
describe( 'Order status', function() {
it( 'Attempt to get the order status for a given order id', function( done ) {
binance.orderStatus( 'ETHBTC', '1234567890', ( error, orderStatus, symbol ) => {
debug( error );
debug( orderStatus );
debug( symbol );
assert( typeof ( error ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( orderStatus ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( symbol ) === 'string', WARN_SHOULD_BE_TYPE + 'string' );
assert( symbol === 'ETHBTC' );
assert( error !== null, WARN_SHOULD_BE_NOT_NULL );
assert( orderStatus !== null, WARN_SHOULD_BE_NOT_NULL );
assert( error.body === '{"code":-2013,"msg":"Order does not exist."}' );
assert( Object.keys( orderStatus ).length === 0 );
done();
} );
} ).timeout( TIMEOUT );
} );
describe( 'trades', function() {
it( 'Attempt get all trade history for given symbol', function( done ) {
binance.trades( 'SNMBTC', ( error, trades, symbol ) => {
debug( error );
debug( trades );
debug( symbol );
assert( typeof ( error ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( trades ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( symbol ) === 'string', WARN_SHOULD_BE_TYPE + 'string' );
assert( symbol === 'SNMBTC' );
assert( error === null, WARN_SHOULD_BE_NULL );
assert( trades !== null, WARN_SHOULD_BE_NOT_NULL );
assert( Object.keys( trades ).length === 0 );
done();
} );
} ).timeout( TIMEOUT );
} );
describe( 'Orders', function() {
it( 'Attempt get all orders for given symbol', function( done ) {
binance.allOrders( 'ETHBTC', ( error, orders, symbol ) => {
debug( error );
debug( orders );
debug( symbol );
assert( typeof ( error ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( orders ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( symbol ) === 'string', WARN_SHOULD_BE_TYPE + 'string' );
assert( symbol === 'ETHBTC' );
assert( error === null, WARN_SHOULD_BE_NULL );
assert( orders !== null, WARN_SHOULD_BE_NOT_NULL );
assert( Object.keys( orders ).length === 0 );
done();
} );
} ).timeout( TIMEOUT );
} );
describe( 'Prevday all symbols', function() {
it( 'Attempt get prevday trade status for all symbols', function( done ) {
binance.prevDay( false, ( error, prevDay ) => {
debug( error );
debug( prevDay );
assert( typeof ( error ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( prevDay ) === 'object', WARN_SHOULD_BE_OBJ );
assert( error === null, WARN_SHOULD_BE_NULL );
assert( prevDay !== null, WARN_SHOULD_BE_NOT_NULL );
assert( Object.keys( prevDay ).length >= num_pairs, 'should at least ' + num_pairs + 'currency pairs?' );
let members = [
'symbol', 'priceChange', 'priceChangePercent', 'weightedAvgPrice', 'prevClosePrice',
'lastPrice', 'lastQty', 'bidPrice', 'bidQty', 'askQty', 'openPrice', 'highPrice', 'lowPrice',
'volume', 'quoteVolume', 'openTime', 'closeTime', 'firstId', 'lastId', 'count'
];
prevDay.forEach( function( obj ) {
members.forEach( function( key ) {
assert( Object.prototype.hasOwnProperty.call( obj, key ), WARN_SHOULD_HAVE_KEY + key );
} );
} );
done();
} );
} ).timeout( TIMEOUT );
} );
describe( 'Prevday', function() {
it( 'Attempt get prevday trade status for given symbol', function( done ) {
binance.prevDay( 'BNBBTC', ( error, prevDay, symbol ) => {
debug( error );
debug( prevDay );
debug( symbol );
assert( typeof ( error ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( prevDay ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( symbol ) === 'string', WARN_SHOULD_BE_TYPE + 'string' );
assert( symbol === 'BNBBTC', 'Should be BNBBTC' );
assert( error === null, WARN_SHOULD_BE_NULL );
assert( prevDay !== null, WARN_SHOULD_BE_NOT_NULL );
let members = [
'symbol', 'priceChange', 'priceChangePercent', 'weightedAvgPrice', 'prevClosePrice',
'lastPrice', 'lastQty', 'bidPrice', 'bidQty', 'askQty', 'openPrice', 'highPrice', 'lowPrice',
'volume', 'quoteVolume', 'openTime', 'closeTime', 'firstId', 'lastId', 'count'
];
members.forEach( function( key ) {
assert( Object.prototype.hasOwnProperty.call( prevDay, key ), WARN_SHOULD_HAVE_KEY + key );
} );
done();
} );
} ).timeout( TIMEOUT );
} );
describe( 'Candle sticks', function() {
it( 'Attempt get candlesticks for a given symbol', function( done ) {
binance.candlesticks( 'BNBBTC', '5m', ( error, ticks, symbol ) => {
debug( error );
debug( ticks );
debug( symbol );
assert( typeof ( error ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( ticks ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( symbol ) === 'string', WARN_SHOULD_BE_TYPE + 'string' );
assert( symbol === 'BNBBTC', 'Should be BNBBTC' );
assert( error === null, WARN_SHOULD_BE_NULL );
assert( ticks !== null, WARN_SHOULD_BE_NOT_NULL );
ticks.forEach( function( tick ) {
assert( tick.length === 12 );
} );
done();
}, {
limit: 500,
endTime: 1514764800000
} );
} ).timeout( TIMEOUT );
} );
describe( 'Object keys', function() {
describe( 'First', function() {
it( 'Gets the first key', function( done ) {
let first = binance.first( { first: '1', second: '2', third: '3' } );assert.strictEqual( 'first', first, 'should be first' );
done();
}).timeout( TIMEOUT );
});
describe( 'Last', function() {
it( 'Gets the last key', function( done ) {
let last = binance.last( { first: '1', second: '2', third: '3' } );assert.strictEqual( 'third', last, 'should be third' );
done();
}).timeout( TIMEOUT );
});
describe( 'slice', function() {
it( 'Gets slice of the object keys', function( done ) {
let slice = binance.slice( { first: '1', second: '2', third: '3' }, 2 );
assert.deepEqual( ['third'], slice, 'should be ian array with the third' );
done();
}).timeout( TIMEOUT );
});
describe( 'Min', function() {
it( 'Gets the math min of object', function( done ) {
binance.min( { first: '1', second: '2', third: '3' } );
done();
}).timeout( TIMEOUT );
});
describe( 'Max', function() {
it( 'Gets the math max of object', function( done ) {
binance.max( { first: '1', second: '2', third: '3' } );
done();
}).timeout( TIMEOUT );
});
});
describe( 'Set/Get options', function() {
it( 'Sets/Gets option to specified value', function( done ) {
binance.setOption( 'test', 'value' );
assert.equal( binance.getOption( 'test' ), 'value', 'should be value' );
done();
}).timeout( TIMEOUT );
});
describe( 'Get options', function() {
it( 'Gets all options', function( done ) {
assert( typeof ( binance.getOptions() ) === 'object' , 'should be object' );
done();
}).timeout( TIMEOUT );
});
describe( 'Percent', function() {
it( 'Get Percentage of two values', function( done ) {
assert( binance.percent( 25, 100 ) === 25 , 'should be 25 percent' );
done();
}).timeout( TIMEOUT );
});
describe( 'Sum', function() {
it( 'Get sum of array of values', function( done ) {
assert( binance.sum( [1, 2, 3] ) === 6 , 'should be 6' );
done();
}).timeout( TIMEOUT );
});
describe( 'Reverse', function() {
it( 'Reverse the keys in an object', function( done ) {
assert( binance.reverse( { '3': 3, '2': 2, '1': 1 } ).toString() === {'1': 1, '2': 2, '3': 3 }.toString(), 'should be {\'1\': 1, \'2\': 2, \'3\': 3 }' );
done();
}).timeout( TIMEOUT );
});
describe( 'Array', function() {
it( 'Convert object to an array', function( done ) {
let actual = binance.array( { 'a': 1, 'b': 2,'c': 3 } );
let expected = [[NaN, 1], [NaN, 2], [NaN, 3]];
assert.isArray( actual, 'should be an array' );
assert( actual.length === 3, 'should be of lenght 3' );
assert.deepEqual( actual, expected, 'should be both arrays with same vlaues' );
done();
}).timeout( TIMEOUT );
});
describe( 'sortBids', function() {
it( 'Sorts symbols bids and returns an object', function( done ) {
/* let actual = binance.sortBids( 'BNBBTC' );
debug( actual ); */
debug( 'todo' );
done();
});
});
describe( 'sortAsks', function() {
it( 'Sorts symbols asks and returns an object', function( done ) {
//let actual = binance.sortBids( 'BNBBTC' );
debug( 'todo' );
done();
}).timeout( TIMEOUT );
});
describe( 'Exchange Info', function() {
let async_error;
let async_data;
/*global beforeEach*/
/*eslint no-undef: "error"*/
beforeEach(function (done) {
binance.exchangeInfo(function(error, data) {
async_error = error;
async_data = data;
done( error );
})
});
it( 'Gets the exchange info as an object', function() {
assert( typeof ( async_error ) === 'object', 'error should be object' );
assert( async_error === null, 'Error should be null' );
assert( typeof ( async_data ) === 'object', 'data should be object' );
assert( async_data !== null, 'data should not be null' );
assert( Object.prototype.hasOwnProperty.call( async_data, 'symbols' ), 'data should have property \'symbols\'' );
let symbolMembers = ['status', 'orderTypes', 'icebergAllowed', 'baseAsset', 'baseAssetPrecision', 'quoteAsset', 'quotePrecision'];
async_data.symbols.forEach( function( symbol ) {
symbolMembers.forEach( function( member ) {
assert( Object.prototype.hasOwnProperty.call( symbol, member ), WARN_SHOULD_HAVE_KEY + member );
});
});
}).timeout( TIMEOUT );
});
describe( 'System status', function() {
let async_error;
let async_data;
/*global beforeEach*/
beforeEach(function (done) {
binance.systemStatus(function(error, data) {
async_error = error;
async_data = data;
done( error );
})
});
it( 'Gets the system status info as an object', function() {
debug( async_error );
debug( async_data );
assert( typeof ( async_error ) === 'object', 'error should be object' );
assert( async_error === null, 'Error should be null' );
assert( typeof ( async_data ) === 'object', 'data should be object' );
assert( async_data !== null, 'data should not be null' );
assert( Object.prototype.hasOwnProperty.call( async_data, 'msg' ), WARN_SHOULD_HAVE_KEY + 'msg' );
assert( Object.prototype.hasOwnProperty.call( async_data, 'status' ), WARN_SHOULD_HAVE_KEY + 'status' );
let members = ['msg', 'status'];
members.forEach( function( member ) {
assert( Object.prototype.hasOwnProperty.call( async_data, member ), WARN_SHOULD_HAVE_KEY + member );
});
}).timeout( TIMEOUT );
});
describe( 'Withdraw', function() {
it( 'Attempt to withdraw BNB to another address', function( done ) {
binance.withdraw( 'BNBBTC', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', '5', false, ( error, result ) => {
debug( error );
debug( result );
assert( typeof ( error ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( result ) === 'object', WARN_SHOULD_BE_OBJ );
assert( error === null, WARN_SHOULD_BE_NULL );
assert( result !== null, WARN_SHOULD_BE_NOT_NULL );
assert( Object.prototype.hasOwnProperty.call( result, 'msg' ), WARN_SHOULD_HAVE_KEY + 'msg' );
assert( result.msg === 'You don\'t have permission.' );
assert( Object.prototype.hasOwnProperty.call( result, 'success' ), WARN_SHOULD_HAVE_KEY + 'success' );
assert( result.success === false );
done();
});
}).timeout( TIMEOUT );
it( 'Attempt to withdraw BNB to another address with address tag', function( done ) {
binance.withdraw( 'BNBBTC', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', '5', 'AQSWDEFRGT', ( error, result ) => {
debug( error );
debug( result );
assert( typeof ( error ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( result ) === 'object', WARN_SHOULD_BE_OBJ );
assert( error === null, WARN_SHOULD_BE_NULL );
assert( result !== null, WARN_SHOULD_BE_NOT_NULL );
assert( Object.prototype.hasOwnProperty.call( result, 'msg' ), WARN_SHOULD_HAVE_KEY + 'msg' );
assert( result.msg === 'You don\'t have permission.' );
assert( Object.prototype.hasOwnProperty.call( result, 'success' ), WARN_SHOULD_HAVE_KEY + 'success' );
assert( result.success === false );
done();
});
}).timeout( TIMEOUT );
});
describe( 'Withdraw history', function() {
it( 'Attempt to get withdraw history for BTC', function( done ) {
binance.withdrawHistory( ( error, result ) => {
debug( error );
debug( result );
assert( typeof ( error ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( result ) === 'object', WARN_SHOULD_BE_OBJ );
assert( error === null, WARN_SHOULD_BE_NULL );
assert( result !== null, WARN_SHOULD_BE_NOT_NULL );
assert( Object.prototype.hasOwnProperty.call( result, 'withdrawList' ), WARN_SHOULD_HAVE_KEY + 'withdrawList' );
assert( Array.isArray( result.withdrawList ) );
assert( Object.prototype.hasOwnProperty.call( result, 'success' ), WARN_SHOULD_HAVE_KEY + 'success' );
assert( result.success === true );
done();
}, 'BTC' );
}).timeout( TIMEOUT );
it( 'Attempt to get withdraw history for all assets', function( done ) {
binance.withdrawHistory( ( error, result ) => {
debug( error );
debug( result );
assert( typeof ( error ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( result ) === 'object', WARN_SHOULD_BE_OBJ );
assert( error === null, WARN_SHOULD_BE_NULL );
assert( result !== null, WARN_SHOULD_BE_NOT_NULL );
assert( Object.prototype.hasOwnProperty.call( result, 'withdrawList' ), WARN_SHOULD_HAVE_KEY + 'withdrawList' );
assert( Array.isArray( result.withdrawList ) );
assert( Object.prototype.hasOwnProperty.call( result, 'success' ), WARN_SHOULD_HAVE_KEY + 'success' );
assert( result.success === true );
done();
});
}).timeout( TIMEOUT );
});
describe( 'Deposit history', function() {
it( 'Attempt to get deposit history for all assets', function( done ) {
binance.depositHistory( ( error, result ) => {
debug( error );
debug( result );
assert( typeof ( error ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( result ) === 'object', WARN_SHOULD_BE_OBJ );
assert( error === null, WARN_SHOULD_BE_NULL );
assert( result !== null, WARN_SHOULD_BE_NOT_NULL );
assert( Object.prototype.hasOwnProperty.call( result, 'depositList' ), WARN_SHOULD_HAVE_KEY + 'depositList' );
assert( Array.isArray( result.depositList ) );
assert( Object.prototype.hasOwnProperty.call( result, 'success' ), WARN_SHOULD_HAVE_KEY + 'success' );
assert( result.success === true );
done();
});
}).timeout( TIMEOUT );
});
describe( 'Deposit address', function() {
it( 'Attempt to get deposit address for BNB', function( done ) {
binance.depositAddress( 'BTC', ( error, result ) => {
debug( error );
debug( result );
assert( typeof ( error ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( result ) === 'object', WARN_SHOULD_BE_OBJ );
assert( error === null, WARN_SHOULD_BE_NULL );
assert( result !== null, WARN_SHOULD_BE_NOT_NULL );
assert( Object.prototype.hasOwnProperty.call( result, 'address' ), WARN_SHOULD_HAVE_KEY + 'address' );
assert( Object.prototype.hasOwnProperty.call( result, 'success' ), WARN_SHOULD_HAVE_KEY + 'success' );
assert( Object.prototype.hasOwnProperty.call( result, 'addressTag' ), WARN_SHOULD_HAVE_KEY + 'addressTag' );
assert( Object.prototype.hasOwnProperty.call( result, 'asset' ), WARN_SHOULD_HAVE_KEY + 'asset' );
assert( result.asset === 'BTC' );
assert( result.success === true );
done();
});
}).timeout( TIMEOUT );
it( 'Attempt to get deposit address for XYZ', function( done ) {
binance.depositAddress( 'XYZ', ( error, result ) => {
debug( error );
debug( result );
assert( typeof ( error ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( result ) === 'object', WARN_SHOULD_BE_OBJ );
assert( error === null, WARN_SHOULD_BE_NULL );
assert( result !== null, WARN_SHOULD_BE_NOT_NULL );
assert( Object.prototype.hasOwnProperty.call( result, 'address' ) === false, WARN_SHOULD_NOT_HAVE_KEY + 'address' );
assert( Object.prototype.hasOwnProperty.call( result, 'success' ), WARN_SHOULD_NOT_HAVE_KEY + 'success' );
assert( Object.prototype.hasOwnProperty.call( result, 'addressTag' ) === false, WARN_SHOULD_NOT_HAVE_KEY + 'addressTag' );
assert( Object.prototype.hasOwnProperty.call( result, 'asset' ) === false, WARN_SHOULD_NOT_HAVE_KEY + 'asset' );
assert( result.success === false );
done();
});
}).timeout( TIMEOUT );
});
describe( 'Account status', function() {
it( 'Attempt to get account status', function( done ) {
binance.accountStatus( ( error, data ) => {
debug( error );
debug( data );
assert( typeof ( error ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( data ) === 'object', WARN_SHOULD_BE_OBJ );
assert( error === null, WARN_SHOULD_BE_NULL );
assert( data !== null, WARN_SHOULD_BE_NOT_NULL );
done();
} );
} ).timeout( TIMEOUT );
});
describe( 'Account', function() {
it( 'Attempt to get account information', function( done ) {
binance.account( ( error, data ) => {
debug( error );
debug( data );
assert( typeof ( error ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( data ) === 'object', WARN_SHOULD_BE_OBJ );
assert( error === null, WARN_SHOULD_BE_NULL );
assert( data !== null, WARN_SHOULD_BE_NOT_NULL );
done();
} );
} ).timeout( TIMEOUT );
});
describe( 'Use Server Time', function() {
it( 'Gets the server time and sets it ass the offset for http connections', function( done ) {
//binance.useServerTime( ( error, data ) => {
// debug( data );
done();
//});
}).timeout( TIMEOUT );
});
describe( 'Time', function() {
it( 'Attempt to get server time', function( done ) {
binance.time( ( error, data ) => {
debug( error );
debug( data );
assert( typeof ( error ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( data ) === 'object', WARN_SHOULD_BE_OBJ );
assert( error === null, WARN_SHOULD_BE_NULL );
assert( data !== null, WARN_SHOULD_BE_NOT_NULL );
assert( Object.prototype.hasOwnProperty.call( data, 'serverTime' ), WARN_SHOULD_HAVE_KEY + 'serverTime' );
done();
} );
} ).timeout( TIMEOUT );
});
describe( 'Aggtrades', function() {
it( 'Attempt to get aggTrades for given symbol', function( done ) {
binance.aggTrades('BNBBTC', {limit:500}, (error, response) => {
assert( typeof ( error ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( response ) === 'object', WARN_SHOULD_BE_OBJ );
assert( error === null, WARN_SHOULD_BE_NULL );
assert( response !== null, WARN_SHOULD_BE_NOT_NULL );
done();
} );
}).timeout( TIMEOUT );
});
describe( 'Recent Trades', function() {
it( 'Attempt get recent Trades for a given symbol', function( done ) {
binance.recentTrades( 'BNBBTC', ( error, data ) => {
debug( error );
debug( data );
assert( typeof ( error ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( data ) === 'object', WARN_SHOULD_BE_OBJ );
assert( error === null, WARN_SHOULD_BE_NULL );
assert( data !== null, WARN_SHOULD_BE_NOT_NULL );
assert( data.length > 0 );
data.forEach(function(obj) {
assert( Object.prototype.hasOwnProperty.call( obj, 'id' ), WARN_SHOULD_HAVE_KEY + 'id' );
assert( Object.prototype.hasOwnProperty.call( obj, 'price' ), WARN_SHOULD_HAVE_KEY + 'price' );
assert( Object.prototype.hasOwnProperty.call( obj, 'qty' ), WARN_SHOULD_HAVE_KEY + 'qty' );
assert( Object.prototype.hasOwnProperty.call( obj, 'time' ), WARN_SHOULD_HAVE_KEY + 'time' );
assert( Object.prototype.hasOwnProperty.call( obj, 'isBuyerMaker' ), WARN_SHOULD_HAVE_KEY + 'isBuyerMaker' );
assert( Object.prototype.hasOwnProperty.call( obj, 'isBestMatch' ), WARN_SHOULD_HAVE_KEY + 'isBestMatch' );
});
done();
} );
} ).timeout( TIMEOUT );
});
describe( 'Historical Trades', function() {
it( 'Attempt get Historical Trades for a given symbol', function( done ) {
binance.historicalTrades( 'BNBBTC', ( error, data ) => {
debug( error );
debug( data );
assert( typeof ( error ) === 'object', WARN_SHOULD_BE_OBJ );
assert( typeof ( data ) === 'object', WARN_SHOULD_BE_OBJ );
assert( error === null, WARN_SHOULD_BE_NULL );
assert( data !== null, WARN_SHOULD_BE_NOT_NULL );
assert( data.length > 0 );
data.forEach(function(obj) {
assert( Object.prototype.hasOwnProperty.call( obj, 'id' ), WARN_SHOULD_HAVE_KEY + 'id' );
assert( Object.prototype.hasOwnProperty.call( obj, 'price' ), WARN_SHOULD_HAVE_KEY + 'price' );
assert( Object.prototype.hasOwnProperty.call( obj, 'qty' ), WARN_SHOULD_HAVE_KEY + 'qty' );
assert( Object.prototype.hasOwnProperty.call( obj, 'time' ), WARN_SHOULD_HAVE_KEY + 'time' );
assert( Object.prototype.hasOwnProperty.call( obj, 'isBuyerMaker' ), WARN_SHOULD_HAVE_KEY + 'isBuyerMaker' );
assert( Object.prototype.hasOwnProperty.call( obj, 'isBestMatch' ), WARN_SHOULD_HAVE_KEY + 'isBestMatch' );
});
done();
} );
} ).timeout( TIMEOUT );
});
describe( 'getInfo', function() {
it( 'Gets the info array form the binance object', function( done ) {
assert( typeof ( binance.getInfo() ) === 'object', 'Should be of type array' )
done();
}).timeout( TIMEOUT );
});
describe( 'Websockets candlesticks', function() {
let candlesticks;
let cnt = 0;
/*global beforeEach*/
beforeEach(function (done) {
this.timeout( TIMEOUT );
binance.websockets.candlesticks(['BNBBTC'], '1m', a_candlesticks => {
cnt++;
if ( cnt > 1 ) return;
candlesticks = a_candlesticks;
stopSockets();
done();
});
});
it( 'Calls candlesticks websocket', function() {
assert( typeof ( candlesticks ) === 'object', WARN_SHOULD_BE_OBJ );
assert( candlesticks !== null, WARN_SHOULD_BE_NOT_NULL );
assert( Object.keys( candlesticks ).length >= 0, 'should at least 1 currency pairs?' );
let keys = ['t', 'T', 's', 'i', 'f', 'L', 'o', 'c', 'h', 'l', 'v', 'n', 'x', 'q', 'V', 'Q', 'B'];
assert( Object.prototype.hasOwnProperty.call( candlesticks, 'e' ), WARN_SHOULD_HAVE_KEY + 'e' );
assert( Object.prototype.hasOwnProperty.call( candlesticks, 'E' ), WARN_SHOULD_HAVE_KEY + 'E' );
assert( Object.prototype.hasOwnProperty.call( candlesticks, 's' ), WARN_SHOULD_HAVE_KEY + 's' );
assert( Object.prototype.hasOwnProperty.call( candlesticks, 's' ), WARN_SHOULD_HAVE_KEY + 'k' );
keys.forEach(function(key) {
assert( Object.prototype.hasOwnProperty.call( candlesticks.k, key ), WARN_SHOULD_HAVE_KEY + key );
});
});
});
describe( 'Websockets depth', function() {
let depth;
let cnt = 0;
/*global beforeEach*/
beforeEach(function (done) {
this.timeout( TIMEOUT );
binance.websockets.depth(['BNBBTC'], e_depth => {
cnt++;
if ( cnt > 1 ) return;
depth = e_depth;
stopSockets();
done();
});
});
it( 'Calls depth websocket', function() {
assert( typeof ( depth ) === 'object', WARN_SHOULD_BE_OBJ );
assert( depth !== null, WARN_SHOULD_BE_NOT_NULL );
});
});
describe( 'Websockets trades', function() {
let trades;
let cnt = 0;
/*global beforeEach*/
beforeEach(function (done) {
this.timeout( TIMEOUT );
binance.websockets.trades(['BNBBTC', 'ETHBTC'], e_trades => {
cnt++;
if ( cnt > 1 ) return;
trades = e_trades;
stopSockets();
done();
});
});
it( 'Calls trades websocket', function() {
assert( typeof ( trades ) === 'object', WARN_SHOULD_BE_OBJ );
assert( trades !== null, WARN_SHOULD_BE_NOT_NULL );
});
});
describe( 'depthCache', function() {
it( 'depthCache', function( done ) {
binance.depthCache( 'BNBBTC' );
done();
}).timeout( TIMEOUT );
});
describe( 'depthVolume', function() {
it( 'depthVolume', function( done ) {
binance.depthVolume( 'BNBBTC' );
done();
}).timeout( TIMEOUT );
});
describe( 'getPrecision', function() {
it( 'getPrecision', function( done ) {
binance.getPrecision( 1.9999999 );
done();
}).timeout( TIMEOUT );
});
describe( 'roundStep', function() {
it( 'roundStep', function( done ) {
binance.roundStep( 10, 0.8 );