1+ import Binance from '../src/node-binance-api' ;
2+ import { assert } from 'chai' ;
3+ import util from 'util' ;
4+
5+ const WARN_SHOULD_BE_OBJ = 'should be an object' ;
6+ const WARN_SHOULD_BE_NULL = 'should be null' ;
7+ const WARN_SHOULD_BE_NOT_NULL = 'should not be null' ;
8+ const WARN_SHOULD_HAVE_KEY = 'should have key ' ;
9+ const WARN_SHOULD_NOT_HAVE_KEY = 'should not have key ' ;
10+ const WARN_SHOULD_BE_UNDEFINED = 'should be undefined' ;
11+ const WARN_SHOULD_BE_TYPE = 'should be a ' ;
12+ const TIMEOUT = 40000 ;
13+
14+
15+ const binance = new Binance ( ) . options ( {
16+ APIKEY : 'X4BHNSimXOK6RKs2FcKqExquJtHjMxz5hWqF0BBeVnfa5bKFMk7X0wtkfEz0cPrJ' ,
17+ APISECRET : 'x8gLihunpNq0d46F2q0TWJmeCDahX5LMXSlv3lSFNbMI3rujSOpTDKdhbcmPSf2i' ,
18+ test : true
19+ } ) ;
20+
21+ const futuresBinance = new Binance ( ) . options ( {
22+ APIKEY : '227719da8d8499e8d3461587d19f259c0b39c2b462a77c9b748a6119abd74401' ,
23+ APISECRET : 'b14b935f9cfacc5dec829008733c40da0588051f29a44625c34967b45c11d73c' ,
24+ hedgeMode : true ,
25+ test : true
26+ } ) ;
27+
28+ const stopSockets = function ( log = false ) {
29+ let endpoints = binance . websockets . subscriptions ( ) ;
30+ for ( let endpoint in endpoints ) {
31+ if ( log ) console . log ( 'Terminated ws endpoint: ' + endpoint ) ;
32+ binance . websockets . terminate ( endpoint ) ;
33+ }
34+ }
35+
36+ describe ( 'Websockets candlesticks' , function ( ) {
37+ let candlesticks ;
38+ let cnt = 0 ;
39+ /*global beforeEach*/
40+ beforeEach ( function ( done ) {
41+ this . timeout ( TIMEOUT ) ;
42+ binance . websockets . candlesticks ( [ 'BTCUSDT' ] , '1m' , a_candlesticks => {
43+ cnt ++ ;
44+ if ( cnt > 1 ) return ;
45+ candlesticks = a_candlesticks ;
46+ stopSockets ( ) ;
47+ done ( ) ;
48+ } ) ;
49+ } ) ;
50+
51+ it ( 'Calls spot candlesticks websocket' , function ( ) {
52+ assert ( typeof ( candlesticks ) === 'object' , WARN_SHOULD_BE_OBJ ) ;
53+ assert ( candlesticks !== null , WARN_SHOULD_BE_NOT_NULL ) ;
54+ assert ( Object . keys ( candlesticks ) . length >= 0 , 'should at least 1 currency pairs?' ) ;
55+
56+ let keys = [ 't' , 'T' , 's' , 'i' , 'f' , 'L' , 'o' , 'c' , 'h' , 'l' , 'v' , 'n' , 'x' , 'q' , 'V' , 'Q' , 'B' ] ;
57+ assert ( Object . prototype . hasOwnProperty . call ( candlesticks , 'e' ) , WARN_SHOULD_HAVE_KEY + 'e' ) ;
58+ assert ( Object . prototype . hasOwnProperty . call ( candlesticks , 'E' ) , WARN_SHOULD_HAVE_KEY + 'E' ) ;
59+ assert ( Object . prototype . hasOwnProperty . call ( candlesticks , 's' ) , WARN_SHOULD_HAVE_KEY + 's' ) ;
60+ assert ( Object . prototype . hasOwnProperty . call ( candlesticks , 's' ) , WARN_SHOULD_HAVE_KEY + 'k' ) ;
61+
62+ keys . forEach ( function ( key ) {
63+ assert ( Object . prototype . hasOwnProperty . call ( candlesticks . k , key ) , WARN_SHOULD_HAVE_KEY + key ) ;
64+ } ) ;
65+ } ) ;
66+ } ) ;
67+
68+ describe ( 'Websockets prevDay' , function ( ) {
69+ let response ;
70+ let cnt = 0 ;
71+
72+ beforeEach ( function ( done ) {
73+ this . timeout ( TIMEOUT ) ;
74+ binance . websockets . prevDay ( undefined , a_response => {
75+ cnt ++ ;
76+ if ( cnt > 1 ) return ;
77+ stopSockets ( ) ;
78+ response = a_response ;
79+ done ( ) ;
80+ } )
81+ } ) ;
82+
83+ it ( 'Calls prevDay websocket for symbol' , function ( ) {
84+ assert ( typeof ( response ) === 'object' , WARN_SHOULD_BE_OBJ ) ;
85+ } ) ;
86+ } ) ;
87+
88+
89+ describe ( 'Websockets miniticker' , function ( ) {
90+ let markets ;
91+ let cnt = 0 ;
92+ beforeEach ( function ( done ) {
93+ this . timeout ( TIMEOUT ) ;
94+ binance . websockets . miniTicker ( tick => {
95+ cnt ++ ;
96+ if ( cnt > 1 ) return ;
97+ markets = tick ;
98+ stopSockets ( ) ;
99+ done ( ) ;
100+ } ) ;
101+ } ) ;
102+
103+ it ( 'check miniticker websocket' , function ( ) {
104+ assert ( typeof ( markets ) === 'object' , WARN_SHOULD_BE_OBJ ) ;
105+ assert ( markets !== null , WARN_SHOULD_BE_NOT_NULL ) ;
106+ assert ( Object . keys ( markets ) . length >= 0 , 'should at least 1 currency pairs?' ) ;
107+
108+ Object . keys ( markets ) . forEach ( function ( symbol ) {
109+ assert ( Object . prototype . hasOwnProperty . call ( markets [ symbol ] , 'close' ) , WARN_SHOULD_HAVE_KEY + 'close' ) ;
110+ assert ( Object . prototype . hasOwnProperty . call ( markets [ symbol ] , 'open' ) , WARN_SHOULD_HAVE_KEY + 'open' ) ;
111+ assert ( Object . prototype . hasOwnProperty . call ( markets [ symbol ] , 'high' ) , WARN_SHOULD_HAVE_KEY + 'high' ) ;
112+ assert ( Object . prototype . hasOwnProperty . call ( markets [ symbol ] , 'low' ) , WARN_SHOULD_HAVE_KEY + 'low' ) ;
113+ assert ( Object . prototype . hasOwnProperty . call ( markets [ symbol ] , 'volume' ) , WARN_SHOULD_HAVE_KEY + 'volume' ) ;
114+ assert ( Object . prototype . hasOwnProperty . call ( markets [ symbol ] , 'quoteVolume' ) , WARN_SHOULD_HAVE_KEY + 'quoteVolume' ) ;
115+ assert ( Object . prototype . hasOwnProperty . call ( markets [ symbol ] , 'eventTime' ) , WARN_SHOULD_HAVE_KEY + 'eventTime' ) ;
116+ } ) ;
117+ } ) ;
118+ } ) ;
119+
120+
121+ describe ( 'Websockets symbol depthcache' , function ( ) {
122+ let symbol ;
123+ let bids ;
124+ let asks ;
125+ let cnt = 0 ;
126+ beforeEach ( function ( done ) {
127+ this . timeout ( TIMEOUT ) ;
128+ binance . websockets . depthCache ( 'BTCUSDT' , ( a_symbol , a_depth ) => {
129+ cnt ++ ;
130+ if ( cnt > 1 ) return ;
131+ stopSockets ( true ) ;
132+ symbol = a_symbol ;
133+ bids = a_depth . bids ;
134+ asks = a_depth . asks ;
135+ done ( ) ;
136+ } ) ;
137+ } ) ;
138+
139+ bids = binance . sortBids ( bids ) ;
140+ asks = binance . sortAsks ( asks ) ;
141+
142+ it ( 'check result of depth cache' , function ( ) {
143+ assert ( typeof ( bids ) === 'object' , WARN_SHOULD_BE_OBJ ) ;
144+ assert ( typeof ( asks ) === 'object' , WARN_SHOULD_BE_OBJ ) ;
145+ assert ( typeof ( symbol ) === 'string' , WARN_SHOULD_BE_OBJ ) ;
146+ assert ( bids !== null , WARN_SHOULD_BE_NOT_NULL ) ;
147+ assert ( asks !== null , WARN_SHOULD_BE_NOT_NULL ) ;
148+ assert ( symbol !== null , WARN_SHOULD_BE_NOT_NULL ) ;
149+ assert ( Object . keys ( asks ) . length !== 0 , 'should not be 0' ) ;
150+ assert ( Object . keys ( bids ) . length !== 0 , 'should not be 0' ) ;
151+ } ) ;
152+ } ) ;
153+
154+
155+ describe ( 'Websockets array depthcache' , function ( ) {
156+ let symbol ;
157+ let bids ;
158+ let asks ;
159+ let cnt = 0 ;
160+ beforeEach ( function ( done ) {
161+ this . timeout ( TIMEOUT ) ;
162+ binance . websockets . depthCache ( [ 'BTCUSDT' , 'ETHUSDT' ] , ( a_symbol , a_depth ) => {
163+ cnt ++ ;
164+ if ( cnt > 1 ) return ;
165+ stopSockets ( ) ;
166+ symbol = a_symbol ;
167+ bids = a_depth . bids ;
168+ asks = a_depth . asks ;
169+ done ( ) ;
170+ } ) ;
171+ } ) ;
172+
173+ bids = binance . sortBids ( bids ) ;
174+ asks = binance . sortAsks ( asks ) ;
175+
176+ it ( 'check result of symbols array depth cache' , function ( ) {
177+ assert ( typeof ( bids ) === 'object' , WARN_SHOULD_BE_OBJ ) ;
178+ assert ( typeof ( asks ) === 'object' , WARN_SHOULD_BE_OBJ ) ;
179+ assert ( typeof ( symbol ) === 'string' , WARN_SHOULD_BE_OBJ ) ;
180+ assert ( bids !== null , WARN_SHOULD_BE_NOT_NULL ) ;
181+ assert ( asks !== null , WARN_SHOULD_BE_NOT_NULL ) ;
182+ assert ( symbol !== null , WARN_SHOULD_BE_NOT_NULL ) ;
183+ assert ( Object . keys ( asks ) . length !== 0 , 'should not be 0' ) ;
184+ assert ( Object . keys ( bids ) . length !== 0 , 'should not be 0' ) ;
185+ } ) ;
186+ } ) ;
187+
188+
189+ describe ( 'Staggered websockets symbol depthcache' , function ( ) {
190+ let symbol ;
191+ let bids ;
192+ let asks ;
193+ let cnt = 0 ;
194+ beforeEach ( function ( done ) {
195+ this . timeout ( TIMEOUT ) ;
196+ binance . websockets . depthCacheStaggered ( 'BTCUSDT' , ( a_symbol , a_depth ) => {
197+ cnt ++ ;
198+ if ( cnt > 1 ) return ;
199+ stopSockets ( true ) ;
200+ symbol = a_symbol ;
201+ bids = a_depth . bids ;
202+ asks = a_depth . asks ;
203+ done ( ) ;
204+ } ) ;
205+ } ) ;
206+
207+ bids = binance . sortBids ( bids ) ;
208+ asks = binance . sortAsks ( asks ) ;
209+
210+ it ( 'check result of depth cache' , function ( ) {
211+ assert ( typeof ( bids ) === 'object' , WARN_SHOULD_BE_OBJ ) ;
212+ assert ( typeof ( asks ) === 'object' , WARN_SHOULD_BE_OBJ ) ;
213+ assert ( typeof ( symbol ) === 'string' , WARN_SHOULD_BE_OBJ ) ;
214+ assert ( bids !== null , WARN_SHOULD_BE_NOT_NULL ) ;
215+ assert ( asks !== null , WARN_SHOULD_BE_NOT_NULL ) ;
216+ assert ( symbol !== null , WARN_SHOULD_BE_NOT_NULL ) ;
217+ assert ( Object . keys ( asks ) . length !== 0 , 'should not be 0' ) ;
218+ assert ( Object . keys ( bids ) . length !== 0 , 'should not be 0' ) ;
219+ } ) ;
220+ } ) ;
221+
222+
223+ describe ( 'Staggered Websockets array depthcache' , function ( ) {
224+ let symbol ;
225+ let bids ;
226+ let asks ;
227+ let cnt = 0 ;
228+ beforeEach ( function ( done ) {
229+ this . timeout ( TIMEOUT ) ;
230+ binance . websockets . depthCacheStaggered ( [ 'BTCUSDT' , 'ETHUSDT' ] , ( a_symbol , a_depth ) => {
231+ cnt ++ ;
232+ if ( cnt > 1 ) return ;
233+ stopSockets ( ) ;
234+ symbol = a_symbol ;
235+ bids = a_depth . bids ;
236+ asks = a_depth . asks ;
237+ done ( ) ;
238+ } ) ;
239+ } ) ;
240+
241+ bids = binance . sortBids ( bids ) ;
242+ asks = binance . sortAsks ( asks ) ;
243+
244+ it ( 'check result of symbols array depth cache' , function ( ) {
245+ assert ( typeof ( bids ) === 'object' , WARN_SHOULD_BE_OBJ ) ;
246+ assert ( typeof ( asks ) === 'object' , WARN_SHOULD_BE_OBJ ) ;
247+ assert ( typeof ( symbol ) === 'string' , WARN_SHOULD_BE_OBJ ) ;
248+ assert ( bids !== null , WARN_SHOULD_BE_NOT_NULL ) ;
249+ assert ( asks !== null , WARN_SHOULD_BE_NOT_NULL ) ;
250+ assert ( symbol !== null , WARN_SHOULD_BE_NOT_NULL ) ;
251+ assert ( Object . keys ( asks ) . length !== 0 , 'should not be 0' ) ;
252+ assert ( Object . keys ( bids ) . length !== 0 , 'should not be 0' ) ;
253+ } ) ;
254+ } ) ;
255+
256+
257+ describe ( 'Websockets chart' , function ( ) {
258+ let chart ;
259+ let interval ;
260+ let symbol ;
261+ let cnt = 0 ;
262+ beforeEach ( function ( done ) {
263+ this . timeout ( TIMEOUT ) ;
264+ binance . websockets . chart ( 'BTCUSDT' , '1m' , ( a_symbol , a_interval , a_chart ) => {
265+ cnt ++ ;
266+ if ( cnt > 1 ) {
267+ stopSockets ( ) ;
268+ return ;
269+ }
270+ chart = a_chart ;
271+ interval = a_interval ;
272+ symbol = a_symbol ;
273+ stopSockets ( ) ;
274+ done ( ) ;
275+ } ) ;
276+ } ) ;
277+
278+ it ( 'Calls chart websocket' , function ( ) {
279+ assert ( typeof ( chart ) === 'object' , WARN_SHOULD_BE_OBJ ) ;
280+ assert ( typeof ( symbol ) === 'string' , WARN_SHOULD_BE_OBJ ) ;
281+ assert ( typeof ( interval ) === 'string' , WARN_SHOULD_BE_OBJ ) ;
282+ assert ( chart !== null , WARN_SHOULD_BE_NOT_NULL ) ;
283+ assert ( symbol !== null , WARN_SHOULD_BE_NOT_NULL ) ;
284+ assert ( interval !== null , WARN_SHOULD_BE_NOT_NULL ) ;
285+
286+ let keys = [ 'open' , 'high' , 'open' , 'close' , 'volume' ] ;
287+ assert ( Object . keys ( chart ) . length > 0 , 'Should not be empty' ) ;
288+
289+ Object . keys ( chart ) . forEach ( function ( c ) {
290+ keys . forEach ( function ( key ) {
291+ assert ( Object . prototype . hasOwnProperty . call ( chart [ c ] , key ) , WARN_SHOULD_HAVE_KEY + key ) ;
292+ } ) ;
293+ } ) ;
294+ } ) ;
295+ } ) ;
296+
297+
298+ describe ( 'Websockets depth' , function ( ) {
299+ let depth ;
300+ let cnt = 0 ;
301+ /*global beforeEach*/
302+ beforeEach ( function ( done ) {
303+ this . timeout ( TIMEOUT ) ;
304+ binance . websockets . depth ( [ 'BNBBTC' ] , e_depth => {
305+ cnt ++ ;
306+ if ( cnt > 1 ) return ;
307+ depth = e_depth ;
308+ stopSockets ( ) ;
309+ done ( ) ;
310+ } ) ;
311+ } ) ;
312+
313+ it ( 'Calls depth websocket' , function ( ) {
314+ assert ( typeof ( depth ) === 'object' , WARN_SHOULD_BE_OBJ ) ;
315+ assert ( depth !== null , WARN_SHOULD_BE_NOT_NULL ) ;
316+ } ) ;
317+ } ) ;
318+
319+ describe ( 'Websockets aggregated trades' , function ( ) {
320+ let trades ;
321+ let cnt = 0 ;
322+ /*global beforeEach*/
323+ beforeEach ( function ( done ) {
324+ this . timeout ( TIMEOUT ) ;
325+ binance . websockets . aggTrades ( [ 'BNBBTC' , 'ETHBTC' ] , e_trades => {
326+ cnt ++ ;
327+ if ( cnt > 1 ) return ;
328+ trades = e_trades ;
329+ stopSockets ( ) ;
330+ done ( ) ;
331+ } ) ;
332+ } ) ;
333+
334+ it ( 'Calls trades websocket' , function ( ) {
335+ assert ( typeof ( trades ) === 'object' , WARN_SHOULD_BE_OBJ ) ;
336+ assert ( trades !== null , WARN_SHOULD_BE_NOT_NULL ) ;
337+ } ) ;
338+ } ) ;
339+
340+
341+ describe ( 'Websockets (raw) trades' , function ( ) {
342+ let trades ;
343+ let cnt = 0 ;
344+ /*global beforeEach*/
345+ beforeEach ( function ( done ) {
346+ this . timeout ( TIMEOUT ) ;
347+ binance . websockets . trades ( [ 'BNBBTC' , 'ETHBTC' ] , e_trades => {
348+ cnt ++ ;
349+ if ( cnt > 1 ) return ;
350+ trades = e_trades ;
351+ stopSockets ( ) ;
352+ done ( ) ;
353+ } ) ;
354+ } ) ;
355+
356+ it ( 'Calls trades websocket' , function ( ) {
357+ assert ( typeof ( trades ) === 'object' , WARN_SHOULD_BE_OBJ ) ;
358+ assert ( trades !== null , WARN_SHOULD_BE_NOT_NULL ) ;
359+ } ) ;
360+ } ) ;
0 commit comments