Skip to content

Commit 51f77b2

Browse files
committed
Merge branch 'master' into ed-encryption
2 parents 6006a6d + b01a876 commit 51f77b2

8 files changed

Lines changed: 734 additions & 196 deletions

File tree

.github/workflows/js.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ jobs:
3535
run: npm run static-test
3636
- name: Live Tests (TS ESM)
3737
run: npm run ts-test-live
38+
- name: Ws Live Tests (spot)
39+
run: npm run ws-tests-spot
40+
- name: Ws Live Tests (futures)
41+
run: npm run ws-tests-futures
3842
- name: CJS test
3943
run: npm run test-cjs
4044
- name: Package test

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
"test-cjs": "node ./tests/cjs-test.cjs",
3030
"crypto-tests": "mocha ./tests/crypto.test.ts",
3131
"ws-tests": "mocha ./tests/binance-class-ws.test.ts",
32+
"ws-tests-spot": "mocha ./tests/binance-ws-spot.test.ts --exit",
33+
"ws-tests-futures": "mocha ./tests/binance-ws-futures.test.ts --exit",
3234
"test-debug": "mocha --inspect-brk",
3335
"lint": "eslint src/",
3436
"cover": "istanbul cover _mocha --report lcovonly",

src/node-binance-api.ts

Lines changed: 160 additions & 123 deletions
Large diffs are not rendered by default.

src/types.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,11 @@ export interface IWebsocketsMethods {
173173
subscribeCombined(url: string, callback: Callback, reconnect?: Callback, opened_callback?: Callback);
174174
subscribe(endpoint: string, callback: Callback, reconnect?: Callback, opened_callback?: Callback);
175175
subscriptions(...args: any): any;
176+
futuresSubcriptions(...args: any): any;
177+
deliverySubcriptions(...args: any): any;
176178
terminate(endpoint: string): any;
179+
futuresTerminate(endpoint: string, reconnect?: boolean)
180+
deliveryTerminate(endpoint: string, reconnect?: boolean)
177181
depth(...args: any): any;
178182
depthCache(symbols: string[] | string, callback?: Callback, limit?: number): any;
179183
clearDepthCache(symbols: string | string[]): any;
@@ -184,7 +188,24 @@ export interface IWebsocketsMethods {
184188
candlesticks(symbols: string | string[], interval: Interval, callback: Callback)
185189
miniTicker(callback: Callback): string;
186190
bookTickers(symbol: string, callback: Callback): string;
187-
prevDay(symbols: string | string[], callback?: Callback, singleCallback?: Callback)
191+
prevDay(symbols: string | string[] | undefined, callback?: Callback, singleCallback?: Callback)
192+
futuresCandlesticks(symbols: string[] | string, interval: Interval, callback: Callback)
193+
futuresTicker(symbol?: string, callback?: Callback)
194+
futuresMiniTicker(symbol?: string, callback?: Callback)
195+
futuresAggTrades(symbols: string[] | string, callback: Callback)
196+
futuresMarkPrice(symbol?: string, callback?: Callback, speed?: string)
197+
futuresLiquidation(symbol?: string, callback?: Callback)
198+
futuresTicker(symbol?: string, callback?: Callback)
199+
futuresBookTicker(symbol?: string, callback?: Callback)
200+
futuresChart(symbols: string[] | string, interval: Interval, callback: Callback, limit?: number)
201+
deliveryAggTrade(symbols: string[] | string, callback: Callback)
202+
deliveryMarkPrice(symbol?: string, callback?: Callback, speed?: string)
203+
deliveryLiquidation(symbol?: string, callback?: Callback)
204+
deliveryTicker(symbol?: string, callback?: Callback)
205+
deliveryMiniTicker(symbol?: string, callback?: Callback)
206+
deliveryBookTicker(symbol?: string, callback?: Callback)
207+
deliveryChart(symbols: string[] | string, interval: Interval, callback: Callback, limit?: number)
208+
deliveryCandlesticks(symbols: string[] | string, interval: Interval, callback: Callback)
188209
}
189210

190211
export interface FundingRate {

tests/binance-class-live.test.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,14 @@ describe('MarketBuy/Sell WithCost', function () {
266266
describe('Limit buy Order', function () {
267267
it('Attempt to buy ETH', async function () {
268268
if (ethusdtPrice !== 0) {
269-
let quantity = 0.5;
270-
const res = await binance.order('LIMIT', 'BUY', 'ETHUSDT', quantity, Math.round(ethusdtPrice * 0.8))
271-
assert(res['orderId'] !== undefined)
272-
spotOrderId = res['orderId'];
269+
try {
270+
let quantity = 0.1;
271+
const res = await binance.order('LIMIT', 'BUY', 'ETHUSDT', quantity, Math.round(ethusdtPrice * 0.8))
272+
assert(res['orderId'] !== undefined)
273+
spotOrderId = res['orderId'];
274+
} catch (e) {
275+
assert(e.toString().includes('{"code":-2010,"msg":"Account has insufficient balance for requested action."}'));
276+
}
273277
}
274278

275279
}).timeout(TIMEOUT);
@@ -297,7 +301,7 @@ describe('Futures MarketBuy', function () {
297301
describe('Futures Limit buy Order', function () {
298302
it('Attempt to buy ETH', async function () {
299303
if (ethusdtPrice !== 0) {
300-
let quantity = 0.5;
304+
let quantity = 0.1;
301305
const res = await futuresBinance.futuresOrder('LIMIT', 'BUY', 'ETHUSDT', quantity, Math.round(ethusdtPrice * 0.8))
302306
assert(res['orderId'] !== undefined)
303307
futuresOrderId = res['orderId'];

tests/binance-class-ws.test.ts

Lines changed: 0 additions & 67 deletions
This file was deleted.

tests/binance-ws-futures.test.ts

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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 futuresBinance = new Binance().options({
16+
APIKEY: '227719da8d8499e8d3461587d19f259c0b39c2b462a77c9b748a6119abd74401',
17+
APISECRET: 'b14b935f9cfacc5dec829008733c40da0588051f29a44625c34967b45c11d73c',
18+
hedgeMode: true,
19+
test: true
20+
});
21+
22+
23+
const stopSockets = function ( log = false ) {
24+
let endpoints = futuresBinance.websockets.subscriptions();
25+
for ( let endpoint in endpoints ) {
26+
if ( log ) console.log( 'Terminated ws endpoint: ' + endpoint );
27+
futuresBinance.websockets.terminate( endpoint );
28+
}
29+
}
30+
31+
describe( 'Websockets candlesticks', function () {
32+
let candlesticks;
33+
let cnt = 0;
34+
/*global beforeEach*/
35+
beforeEach( function ( done ) {
36+
this.timeout( TIMEOUT );
37+
futuresBinance.futuresCandlesticksStream( [ 'BTCUSDT' ], '1m', a_candlesticks => {
38+
cnt++;
39+
if ( cnt > 1 ) return;
40+
candlesticks = a_candlesticks;
41+
stopSockets();
42+
done();
43+
} );
44+
} );
45+
46+
it( 'Calls spot candlesticks websocket', function () {
47+
assert( typeof ( candlesticks ) === 'object', WARN_SHOULD_BE_OBJ );
48+
assert( candlesticks !== null, WARN_SHOULD_BE_NOT_NULL );
49+
assert( Object.keys( candlesticks ).length >= 0, 'should at least 1 currency pairs?' );
50+
51+
let keys = [ 't', 'T', 's', 'i', 'f', 'L', 'o', 'c', 'h', 'l', 'v', 'n', 'x', 'q', 'V', 'Q', 'B' ];
52+
assert( Object.prototype.hasOwnProperty.call( candlesticks, 'e' ), WARN_SHOULD_HAVE_KEY + 'e' );
53+
assert( Object.prototype.hasOwnProperty.call( candlesticks, 'E' ), WARN_SHOULD_HAVE_KEY + 'E' );
54+
assert( Object.prototype.hasOwnProperty.call( candlesticks, 's' ), WARN_SHOULD_HAVE_KEY + 's' );
55+
assert( Object.prototype.hasOwnProperty.call( candlesticks, 's' ), WARN_SHOULD_HAVE_KEY + 'k' );
56+
57+
keys.forEach( function ( key ) {
58+
assert( Object.prototype.hasOwnProperty.call( candlesticks.k, key ), WARN_SHOULD_HAVE_KEY + key );
59+
} );
60+
} );
61+
} );
62+
63+
describe( 'Websockets futures ticker stream', function () {
64+
let response;
65+
let cnt = 0;
66+
67+
beforeEach( function ( done ) {
68+
this.timeout( TIMEOUT );
69+
futuresBinance.websockets.futuresTicker( undefined, a_response => {
70+
cnt++;
71+
if ( cnt > 1 ) return;
72+
stopSockets();
73+
response = a_response;
74+
done();
75+
} )
76+
} );
77+
78+
it( 'Calls prevDay websocket for symbol', function () {
79+
assert( typeof ( response ) === 'object', WARN_SHOULD_BE_OBJ );
80+
} );
81+
} );
82+
83+
84+
describe( 'Websockets miniticker', function () {
85+
let markets;
86+
let cnt = 0;
87+
beforeEach( function ( done ) {
88+
this.timeout( TIMEOUT );
89+
futuresBinance.websockets.futuresMiniTicker( undefined, tick => {
90+
cnt++;
91+
if ( cnt > 1 ) return;
92+
markets = tick;
93+
stopSockets();
94+
done();
95+
} );
96+
} );
97+
98+
it( 'check miniticker websocket', function () {
99+
assert( typeof ( markets ) === 'object', WARN_SHOULD_BE_OBJ );
100+
assert( markets !== null, WARN_SHOULD_BE_NOT_NULL );
101+
assert( Object.keys( markets ).length >= 0, 'should at least 1 currency pairs?' );
102+
103+
Object.keys( markets ).forEach( function ( symbol ) {
104+
assert( Object.prototype.hasOwnProperty.call( markets[symbol], 'close' ), WARN_SHOULD_HAVE_KEY + 'close' );
105+
assert( Object.prototype.hasOwnProperty.call( markets[symbol], 'open' ), WARN_SHOULD_HAVE_KEY + 'open' );
106+
assert( Object.prototype.hasOwnProperty.call( markets[symbol], 'high' ), WARN_SHOULD_HAVE_KEY + 'high' );
107+
assert( Object.prototype.hasOwnProperty.call( markets[symbol], 'low' ), WARN_SHOULD_HAVE_KEY + 'low' );
108+
assert( Object.prototype.hasOwnProperty.call( markets[symbol], 'volume' ), WARN_SHOULD_HAVE_KEY + 'volume' );
109+
assert( Object.prototype.hasOwnProperty.call( markets[symbol], 'quoteVolume' ), WARN_SHOULD_HAVE_KEY + 'quoteVolume' );
110+
assert( Object.prototype.hasOwnProperty.call( markets[symbol], 'eventTime' ), WARN_SHOULD_HAVE_KEY + 'eventTime' );
111+
} );
112+
} );
113+
} );
114+
115+
116+
117+
describe( 'Websockets chart', function () {
118+
let chart;
119+
let interval;
120+
let symbol;
121+
let cnt = 0;
122+
beforeEach( function ( done ) {
123+
this.timeout( TIMEOUT );
124+
futuresBinance.websockets.futuresChart( 'BTCUSDT', '1m', ( a_symbol, a_interval, a_chart ) => {
125+
cnt++;
126+
if ( cnt > 1 ) {
127+
stopSockets();
128+
return;
129+
}
130+
chart = a_chart;
131+
interval = a_interval;
132+
symbol = a_symbol;
133+
stopSockets();
134+
done();
135+
} );
136+
} );
137+
138+
it( 'Calls chart websocket', function () {
139+
assert( typeof ( chart ) === 'object', WARN_SHOULD_BE_OBJ );
140+
assert( typeof ( symbol ) === 'string', WARN_SHOULD_BE_OBJ );
141+
assert( typeof ( interval ) === 'string', WARN_SHOULD_BE_OBJ );
142+
assert( chart !== null, WARN_SHOULD_BE_NOT_NULL );
143+
assert( symbol !== null, WARN_SHOULD_BE_NOT_NULL );
144+
assert( interval !== null, WARN_SHOULD_BE_NOT_NULL );
145+
146+
let keys = [ 'open', 'high', 'open', 'close', 'volume' ];
147+
assert( Object.keys( chart ).length > 0, 'Should not be empty' );
148+
149+
Object.keys( chart ).forEach( function ( c ) {
150+
keys.forEach( function ( key ) {
151+
assert( Object.prototype.hasOwnProperty.call( chart[c], key ), WARN_SHOULD_HAVE_KEY + key );
152+
} );
153+
} );
154+
} );
155+
} );
156+
157+
158+
describe( 'Websockets aggregated trades', function () {
159+
let trades;
160+
let cnt = 0;
161+
/*global beforeEach*/
162+
beforeEach( function ( done ) {
163+
this.timeout( TIMEOUT );
164+
futuresBinance.websockets.futuresAggTrades( [ 'BTCUSDT', 'ETHBTC' ], e_trades => {
165+
cnt++;
166+
if ( cnt > 1 ) return;
167+
trades = e_trades;
168+
stopSockets();
169+
done();
170+
} );
171+
} );
172+
173+
it( 'Calls trades websocket', function () {
174+
assert( typeof ( trades ) === 'object', WARN_SHOULD_BE_OBJ );
175+
assert( trades !== null, WARN_SHOULD_BE_NOT_NULL );
176+
} );
177+
} );

0 commit comments

Comments
 (0)