-
Notifications
You must be signed in to change notification settings - Fork 762
Expand file tree
/
Copy pathstatic-tests.mjs
More file actions
264 lines (225 loc) · 10.2 KB
/
static-tests.mjs
File metadata and controls
264 lines (225 loc) · 10.2 KB
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
import chai from 'chai';
import path from 'path';
import utils from 'util';
import Binance from '../dist/cjs/node-binance-api.cjs';
import nock from 'nock';
const assert = chai.assert;
const binance = new Binance({
APIKEY: 'XXXXXXXXXXXXXXXXXXXXXXX',
APISECRET: 'YYYYYYYYYYYYYYYYYYYYYY',
})
const debug = function ( x ) {
if ( typeof ( process.env.node_binance_api ) === 'undefined' ) {
return;
}
logger.log( typeof ( x ) );
logger.log( util.inspect( x ) );
}
function urlToObject(queryString) {
const params = new URLSearchParams(queryString);
const obj = Object.fromEntries(params.entries());
return obj;
}
describe( 'Static tests', async function () {
let interceptedUrl = null;
let interceptedBody = null;
beforeEach(() => {
interceptedUrl = null;
interceptedBody = null;
nock(/.*/)
.get(/.*/)
.reply(200, function (uri, requestBody) {
interceptedUrl = `${this.req.options.proto}://${this.req.options.hostname}${uri}`;
interceptedBody = requestBody; // Capture the request body
return { success: true };
});
nock(/.*/)
.post(/.*/)
.reply(200, function (uri, requestBody) {
interceptedUrl = `${this.req.options.proto}://${this.req.options.hostname}${uri}`;
interceptedBody = requestBody; // Capture the request body
return { success: true };
});
nock(/.*/)
.delete(/.*/)
.reply(200, function (uri, requestBody) {
interceptedUrl = `${this.req.options.proto}://${this.req.options.hostname}${uri}`;
interceptedBody = requestBody; // Capture the request body
return { success: true };
});
});
it( 'FetchTicker', async function ( ) {
await binance.prices( 'BNBBTC' )
assert.equal( interceptedUrl, 'https://api.binance.com/api/v3/ticker/price?symbol=BNBBTC' )
})
it( 'FetchOrderBook', async function ( ) {
await binance.depth( 'BTCUSDT' )
assert.equal( interceptedUrl, 'https://api.binance.com/api/v3/depth?symbol=BTCUSDT&limit=100' )
})
it( 'Futures OrderBook', async function ( ) {
await binance.futuresDepth( 'BTCUSDT' )
assert.equal( interceptedUrl, 'https://fapi.binance.com/fapi/v1/depth?symbol=BTCUSDT' )
})
it( 'OHLCVS', async function ( ) {
try {
await binance.candlesticks( 'BTCUSDT' )
} catch (e) {
// console.log(e)
}
assert.equal( interceptedUrl, 'https://api.binance.com/api/v3/klines?symbol=BTCUSDT&interval=5m&limit=500' )
})
it( 'Futures OHLCVS', async function ( ) {
try {
await binance.futuresCandles( 'BTCUSDT' )
} catch (e) {
// console.log(e)
}
assert.equal( interceptedUrl, 'https://fapi.binance.com/fapi/v1/klines?symbol=BTCUSDT&interval=30m' )
})
it( 'Trades', async function ( ) {
try {
await binance.aggTrades( 'BTCUSDT' )
} catch (e) {
// console.log(e)
}
assert.equal( interceptedUrl, 'https://api.binance.com/api/v3/aggTrades?symbol=BTCUSDT' )
})
it( 'FuturesTrades', async function ( ) {
await binance.futuresTrades( 'BTCUSDT' )
assert.equal( interceptedUrl, 'https://fapi.binance.com/fapi/v1/trades?symbol=BTCUSDT' )
})
it( 'PositionRisk V3', async function ( ) {
await binance.futuresPositionRisk()
assert.isTrue( interceptedUrl.startsWith('https://fapi.binance.com/fapi/v3/positionRisk') )
})
it( 'PositionRisk V2', async function ( ) {
await binance.futuresPositionRiskV2()
assert.isTrue( interceptedUrl.startsWith('https://fapi.binance.com/fapi/v2/positionRisk') )
})
it( 'CancelOrder', async function ( ) {
await binance.cancel( 'LTCUSDT', '34234234' )
assert( interceptedUrl.startsWith('https://api.binance.com/api/v3/order' ))
const obj = urlToObject( interceptedUrl.replace('https://api.binance.com/api/v3/order', '') )
assert.equal( obj.symbol, 'LTCUSDT' )
assert.equal( obj.orderId, '34234234')
})
it( 'Futures CancelOrder', async function ( ) {
await binance.futuresCancel( 'LTCUSDT', '34234234')
assert( interceptedUrl.startsWith('https://fapi.binance.com/fapi/v1/order'))
const obj = urlToObject( interceptedUrl.replace('https://fapi.binance.com/fapi/v1/order', '') )
assert.equal( obj.symbol, 'LTCUSDT' )
assert.equal( obj.orderId, '34234234')
})
const SPOT_PREFIX = "x-HNA2TXFJ"
it( 'MarketBuy', async function ( ) {
await binance.marketBuy( 'LTCUSDT', 0.5 )
assert.equal( interceptedUrl, 'https://api.binance.com/api/v3/order' )
const obj = urlToObject( interceptedBody )
assert.equal( obj.symbol, 'LTCUSDT' )
assert.equal( obj.side, 'BUY' )
assert.equal( obj.type, 'MARKET' )
assert.equal( obj.quantity, 0.5 )
assert(obj.newClientOrderId.startsWith(SPOT_PREFIX))
})
it( 'MarketSell', async function ( ) {
await binance.marketSell( 'LTCUSDT', 0.5 )
assert.equal( interceptedUrl, 'https://api.binance.com/api/v3/order' )
const obj = urlToObject( interceptedBody )
assert.equal( obj.symbol, 'LTCUSDT' )
assert.equal( obj.side, 'SELL' )
assert.equal( obj.type, 'MARKET' )
assert.equal( obj.quantity, 0.5 )
assert(obj.newClientOrderId.startsWith(SPOT_PREFIX))
})
it( 'LimitBuy', async function ( ) {
await binance.order('LIMIT', 'BUY', 'LTCUSDT', 0.5 )
assert.equal( interceptedUrl, 'https://api.binance.com/api/v3/order' )
const obj = urlToObject( interceptedBody )
assert.equal( obj.symbol, 'LTCUSDT' )
assert.equal( obj.side, 'BUY' )
assert.equal( obj.type, 'LIMIT' )
assert.equal( obj.quantity, 0.5 )
assert(obj.newClientOrderId.startsWith(SPOT_PREFIX))
})
it( 'LimitSell', async function ( ) {
await binance.order('LIMIT', 'SELL', 'LTCUSDT', 0.5 )
assert.equal( interceptedUrl, 'https://api.binance.com/api/v3/order' )
const obj = urlToObject( interceptedBody )
assert.equal( obj.symbol, 'LTCUSDT' )
assert.equal( obj.side, 'SELL' )
assert.equal( obj.type, 'LIMIT' )
assert.equal( obj.quantity, 0.5 )
assert(obj.newClientOrderId.startsWith(SPOT_PREFIX))
})
const CONTRACT_PREFIX = "x-Cb7ytekJ"
it( 'Futures MarketBuy', async function ( ) {
await binance.futuresMarketBuy( 'LTCUSDT', 0.5 )
assert.isTrue( interceptedUrl.startsWith('https://fapi.binance.com/fapi/v1/order' ))
const obj = urlToObject( interceptedUrl.replace('https://fapi.binance.com/fapi/v1/order?', '') )
assert.equal( obj.symbol, 'LTCUSDT' )
assert.equal( obj.side, 'BUY' )
assert.equal( obj.type, 'MARKET' )
assert.equal( obj.quantity, 0.5 )
assert(obj.newClientOrderId.startsWith(CONTRACT_PREFIX))
})
it( 'Futures MarketSell', async function ( ) {
await binance.futuresMarketSell( 'LTCUSDT', 0.5 )
assert.isTrue( interceptedUrl.startsWith('https://fapi.binance.com/fapi/v1/order' ))
const obj = urlToObject( interceptedUrl.replace('https://fapi.binance.com/fapi/v1/order?', '') )
assert.equal( obj.symbol, 'LTCUSDT' )
assert.equal( obj.side, 'SELL' )
assert.equal( obj.type, 'MARKET' )
assert.equal( obj.quantity, 0.5 )
assert(obj.newClientOrderId.startsWith(CONTRACT_PREFIX))
})
it( 'Futures LimitBuy', async function ( ) {
await binance.futuresOrder( 'LIMIT', 'BUY', 'LTCUSDT', 0.5, 100 )
assert.isTrue( interceptedUrl.startsWith('https://fapi.binance.com/fapi/v1/order' ))
const obj = urlToObject( interceptedUrl.replace('https://fapi.binance.com/fapi/v1/order?', '') )
assert.equal( obj.symbol, 'LTCUSDT' )
assert.equal( obj.side, 'BUY' )
assert.equal( obj.type, 'LIMIT' )
assert.equal( obj.quantity, 0.5 )
assert(obj.newClientOrderId.startsWith(CONTRACT_PREFIX))
})
it( 'Futures LimitSell', async function ( ) {
await binance.futuresOrder( 'LIMIT','SELL', 'LTCUSDT', 0.5, 100 )
assert.isTrue( interceptedUrl.startsWith('https://fapi.binance.com/fapi/v1/order' ))
const obj = urlToObject( interceptedUrl.replace('https://fapi.binance.com/fapi/v1/order?', '') )
assert.equal( obj.symbol, 'LTCUSDT' )
assert.equal( obj.side, 'SELL' )
assert.equal( obj.type, 'LIMIT' )
assert.equal( obj.quantity, 0.5 )
assert(obj.newClientOrderId.startsWith(CONTRACT_PREFIX))
})
it( 'cancel order', async function ( ) {
await binance.futuresCancel( 'LTCUSDT', '34234234' )
const url = 'https://fapi.binance.com/fapi/v1/order'
assert.isTrue( interceptedUrl.startsWith(url) )
const obj = urlToObject( interceptedUrl.replace(url, '') )
assert.equal( obj.orderId, '34234234' )
assert.equal( obj.symbol, 'LTCUSDT' )
})
it( 'cancel order', async function ( ) {
await binance.cancel( 'LTCUSDT', '34234234' )
const url = 'https://api.binance.com/api/v3/order'
assert.isTrue( interceptedUrl.startsWith(url) )
const obj = urlToObject( interceptedUrl.replace(url, '') )
assert.equal( obj.orderId, '34234234' )
assert.equal( obj.symbol, 'LTCUSDT' )
})
it( 'delivery OrderBook', async function ( ) {
await binance.deliveryDepth( 'BTCUSD_PERP' )
assert.equal( interceptedUrl, 'https://dapi.binance.com/dapi/v1/depth?symbol=BTCUSD_PERP' )
})
it( 'delivery MarketBuy', async function ( ) {
await binance.deliveryOrder( 'MARKET', 'BUY', 'BTCUSD_PERP', 0.1 )
assert.isTrue( interceptedUrl.startsWith('https://dapi.binance.com/dapi/v1/order' ))
const obj = urlToObject( interceptedUrl.replace('https://dapi.binance.com/dapi/v1/order', '') )
assert.equal( obj.symbol, 'BTCUSD_PERP' )
assert.equal( obj.side, 'BUY' )
assert.equal( obj.type, 'MARKET' )
assert.equal( obj.quantity, 0.1 )
assert(obj.newClientOrderId.startsWith(CONTRACT_PREFIX))
})
})