Skip to content

Commit f4852e0

Browse files
committed
more tests and other fixes
1 parent a7cd063 commit f4852e0

2 files changed

Lines changed: 96 additions & 0 deletions

File tree

src/http-client.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ const getDomainName = url => {
3333

3434
const defaultGetTime = () => Date.now()
3535

36+
const uuid22 = (a) => {
37+
return a ? (a ^ Math.random() * 16 >> a / 4).toString(16) : (([1e7]) + 1e3 + 4e3 + 8e5).replace(/[018]/g, uuid22);
38+
}
39+
3640
// Singleton holding header data like rate limits.
3741
const info = {}
3842

@@ -122,6 +126,14 @@ const checkParams = (name, payload, requires = []) => {
122126
return true
123127
}
124128

129+
const spotP = () => {
130+
return `x-B3AUXNYV${uuid22()}`
131+
}
132+
133+
const futuresP = () => {
134+
return `x-ftGmvgAN${uuid22()}`
135+
}
136+
125137
/**
126138
* Make public calls against the api
127139
*
@@ -250,6 +262,13 @@ const candles = (pubCall, payload, endpoint = '/api/v3/klines') =>
250262
),
251263
)
252264

265+
const isContractURL = (path) => {
266+
const isFutures = path.includes('/fapi') || path.includes('/futures');
267+
const isDelivery = path.includes('/dapi');
268+
const isPortfolioMargin = path.includes('/papi');
269+
return isFutures || isDelivery || isPortfolioMargin;
270+
}
271+
253272
/**
254273
* Create a new order wrapper for market order simplicity
255274
*/
@@ -274,6 +293,16 @@ const order = (privCall, payload = {}, url) => {
274293
requires.push('callbackRate')
275294
}
276295

296+
if (!newPayload.newClientOrderId) {
297+
// check if this is spot or futures/delivery
298+
const isContract = isContractURL(url);
299+
if (isContract) {
300+
newPayload.newClientOrderId = futuresP();
301+
} else {
302+
newPayload.newClientOrderId = spotP();
303+
}
304+
}
305+
277306
return (
278307
checkParams('order', newPayload, requires) &&
279308
privCall(url, { type: 'LIMIT', ...newPayload }, 'POST')
@@ -286,6 +315,10 @@ const orderOco = (privCall, payload = {}, url) => {
286315
? { stopLimitTimeInForce: 'GTC', ...payload }
287316
: payload
288317

318+
if (!newPayload.listClientOrderId) {
319+
newPayload.listClientOrderId = spotP();
320+
}
321+
289322
return (
290323
checkParams('order', newPayload, ['symbol', 'side', 'quantity', 'price', 'stopPrice']) &&
291324
privCall(url, newPayload, 'POST')

test/static-tests.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ test.serial('[REST] Futures CancelOrder', async t => {
158158
t.is(obj.orderId, '34234234')
159159
})
160160

161+
const CONTRACT_PREFIX = "x-ftGmvgAN"
162+
const SPOT_PREFIX = "x-B3AUXNYV"
161163

162164
test.serial('[REST] MarketBuy', async t => {
163165
await binance.order({ symbol: 'LTCUSDT', side: 'BUY', type: 'MARKET', quantity: 0.5 })
@@ -168,6 +170,7 @@ test.serial('[REST] MarketBuy', async t => {
168170
t.is(obj.side, 'BUY')
169171
t.is(obj.type, 'MARKET')
170172
t.is(obj.quantity, '0.5')
173+
t.true(obj.newClientOrderId.startsWith(SPOT_PREFIX))
171174
})
172175

173176
test.serial('[REST] MarketSell', async t => {
@@ -179,6 +182,8 @@ test.serial('[REST] MarketSell', async t => {
179182
t.is(obj.side, 'SELL')
180183
t.is(obj.type, 'MARKET')
181184
t.is(obj.quantity, '0.5')
185+
t.true(obj.newClientOrderId.startsWith(SPOT_PREFIX))
186+
182187
})
183188

184189
test.serial('[REST] LimitBuy', async t => {
@@ -190,6 +195,7 @@ test.serial('[REST] LimitBuy', async t => {
190195
t.is(obj.side, 'BUY')
191196
t.is(obj.type, 'LIMIT')
192197
t.is(obj.quantity, '0.5')
198+
t.true(obj.newClientOrderId.startsWith(SPOT_PREFIX))
193199
})
194200

195201
test.serial('[REST] LimitSell', async t => {
@@ -201,6 +207,7 @@ test.serial('[REST] LimitSell', async t => {
201207
t.is(obj.side, 'SELL')
202208
t.is(obj.type, 'LIMIT')
203209
t.is(obj.quantity, '0.5')
210+
t.true(obj.newClientOrderId.startsWith(SPOT_PREFIX))
204211
})
205212

206213

@@ -212,6 +219,7 @@ test.serial('[REST] Futures MarketBuy', async t => {
212219
t.is(obj.side, 'BUY')
213220
t.is(obj.type, 'MARKET')
214221
t.is(obj.quantity, '0.5')
222+
t.true(obj.newClientOrderId.startsWith(CONTRACT_PREFIX))
215223
})
216224

217225
test.serial('[REST] Futures MarketSell', async t => {
@@ -222,6 +230,7 @@ test.serial('[REST] Futures MarketSell', async t => {
222230
t.is(obj.side, 'SELL')
223231
t.is(obj.type, 'MARKET')
224232
t.is(obj.quantity, '0.5')
233+
t.true(obj.newClientOrderId.startsWith(CONTRACT_PREFIX))
225234
})
226235

227236
test.serial('[REST] Futures LimitBuy', async t => {
@@ -242,6 +251,7 @@ test.serial('[REST] Futures LimitSell', async t => {
242251
t.is(obj.side, 'SELL')
243252
t.is(obj.type, 'LIMIT')
244253
t.is(obj.quantity, '0.5')
254+
t.true(obj.newClientOrderId.startsWith(CONTRACT_PREFIX))
245255
})
246256

247257

@@ -263,6 +273,7 @@ test.serial('[REST] MarketBuy test', async t => {
263273
t.is(obj.side, 'BUY')
264274
t.is(obj.type, 'MARKET')
265275
t.is(obj.quantity, '0.5')
276+
t.true(obj.newClientOrderId.startsWith(SPOT_PREFIX))
266277
})
267278

268279
test.serial('[REST] spot open orders', async t => {
@@ -284,4 +295,56 @@ test.serial('[REST] Margin MarketBuy order', async t => {
284295
t.is(obj.side, 'BUY')
285296
t.is(obj.type, 'MARKET')
286297
t.is(obj.quantity, '0.5')
298+
t.true(obj.newClientOrderId.startsWith(SPOT_PREFIX))
299+
})
300+
301+
test.serial('[REST] spot order with custom clientorderId', async t => {
302+
await binance.order({
303+
symbol: 'LTCUSDT',
304+
side: 'BUY',
305+
type: 'LIMIT',
306+
quantity: 0.5,
307+
price: 100,
308+
newClientOrderId: 'myid'
309+
})
310+
t.true(interceptedUrl.startsWith('https://api.binance.com/api/v3/order'))
311+
const body = interceptedUrl.replace('https://api.binance.com/api/v3/order', '')
312+
const obj = urlToObject(body)
313+
t.is(obj.symbol, 'LTCUSDT')
314+
t.is(obj.side, 'BUY')
315+
t.is(obj.type, 'LIMIT')
316+
t.is(obj.quantity, '0.5')
317+
t.is(obj.price, '100')
318+
t.is(obj.newClientOrderId, 'myid')
287319
})
320+
321+
322+
test.serial('[REST] delivery OrderBook', async t => {
323+
try {
324+
await binance.deliveryBook({ symbol: 'BTCUSD_PERP' })
325+
} catch (e) {
326+
// it can throw an error because of the mocked response
327+
}
328+
t.is(interceptedUrl, 'https://dapi.binance.com/dapi/v1/depth?symbol=BTCUSD_PERP')
329+
})
330+
331+
test.serial('[REST] futures set leverage', async t => {
332+
try {
333+
await binance.futuresLeverage({ symbol: 'BTCUSDT', leverage: 5 })
334+
} catch (e) {
335+
// it can throw an error because of the mocked response
336+
}
337+
t.true(interceptedUrl.startsWith('https://fapi.binance.com/fapi/v1/leverage?symbol=BTCUSDT&leverage=5'))
338+
})
339+
340+
341+
test.serial('[REST] delivery MarketBuy', async t => {
342+
await binance.deliveryOrder({ symbol: 'BTCUSD_PERP', side: 'BUY', type: 'MARKET', quantity: 0.1 })
343+
t.true(interceptedUrl.startsWith('https://dapi.binance.com/dapi/v1/order'))
344+
const obj = urlToObject(interceptedUrl.replace('https://dapi.binance.com/dapi/v1/order', ''))
345+
t.is(obj.symbol, 'BTCUSD_PERP')
346+
t.is(obj.side, 'BUY')
347+
t.is(obj.type, 'MARKET')
348+
t.is(obj.quantity, '0.1')
349+
t.true(obj.newClientOrderId.startsWith(CONTRACT_PREFIX))
350+
})

0 commit comments

Comments
 (0)