Skip to content

Commit 74eca0a

Browse files
authored
feat(client): add updateOrder and futuresUpdateOrder (#692)
* fix(package): include types folder * release: cut the 0.13.3 release * release: cut the 0.13.4 release * chore: update readme * feat(client): add updateOrder and futuresUpdateOrder * add types * update updateOrder type * update readme * add tests * fix linting
1 parent 3244098 commit 74eca0a

File tree

5 files changed

+247
-1
lines changed

5 files changed

+247
-1
lines changed

README.md

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ Following examples will use the `await` form, which requires some configuration
155155
- [delivery markPrice](#delivery-markprice)
156156
- [Authenticated REST Endpoints](#authenticated-rest-endpoints)
157157
- [order](#order)
158+
- [updateOrder](#updateorder)
158159
- [orderTest](#ordertest)
159160
- [orderOco](#orderoco)
160161
- [getOrder](#getorder)
@@ -207,6 +208,8 @@ Following examples will use the `await` form, which requires some configuration
207208
- [Portfolio Margin Endpoints](#portfolio-margin-endpoints)
208209
- [getPortfolioMarginAccountInfo](#getportfoliomarginaccountinfo)
209210
- [Futures Authenticated REST endpoints](#futures-authenticated-rest-endpoints)
211+
- [futuresOrder](#futuresorder)
212+
- [futuresUpdateOrder](#futuresupdateorder)
210213
- [futuresGetOrder](#futuresgetorder)
211214
- [futuresAllOrders](#futuresallorders)
212215
- [futuresBatchOrders](#futuresbatchorders)
@@ -1377,7 +1380,8 @@ the request.
13771380

13781381
#### order
13791382

1380-
Creates a new order.
1383+
- Creates a new order.
1384+
- see https://developers.binance.com/docs/binance-spot-api-docs/rest-api/trading-endpoints#new-order-trade
13811385

13821386
```js
13831387
console.log(
@@ -1453,6 +1457,26 @@ Test new order creation and signature/recvWindow. Creates and validates a new or
14531457

14541458
Same API as above, but does not return any output on success.
14551459

1460+
1461+
#### updateOrder
1462+
1463+
- updates an order
1464+
- see https://developers.binance.com/docs/binance-spot-api-docs/rest-api/trading-endpoints#cancel-an-existing-order-and-send-a-new-order-trade
1465+
1466+
```Js
1467+
1468+
const order = await client.updateOrder({
1469+
symbol: 'LTCUSDT',
1470+
cancelOrderId: 12345678,
1471+
side: 'BUY',
1472+
type: 'LIMIT',
1473+
quantity: 1,
1474+
price: 80,
1475+
timeInForce: 'GTC',
1476+
})
1477+
1478+
```
1479+
14561480
#### orderOco
14571481

14581482
Creates a new OCO order.
@@ -3617,6 +3641,42 @@ console.log(await client.getPortfolioMarginAccountInfo())
36173641

36183642
### Futures Authenticated REST endpoints
36193643

3644+
#### futuresOrder
3645+
3646+
- Creates a futures order
3647+
- see https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/rest-api
3648+
3649+
```js
3650+
console.log(
3651+
await client.futuresOrder({
3652+
symbol: 'LTCUSDT',
3653+
side: 'BUY',
3654+
type: 'LIMIT',
3655+
quantity: 1,
3656+
price: 80,
3657+
timeInForce: 'GTC',
3658+
})
3659+
)
3660+
```
3661+
3662+
#### futuresUpdateOrder
3663+
- Updates a futures order
3664+
- see https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/rest-api/Modify-Order
3665+
3666+
```js
3667+
console.log(
3668+
await client.futuresUpdateOrder({
3669+
orderId: 23423423423,
3670+
symbol: 'LTCUSDT',
3671+
side: 'BUY',
3672+
type: 'LIMIT',
3673+
quantity: 1,
3674+
price: 80,
3675+
timeInForce: 'GTC',
3676+
})
3677+
)
3678+
```
3679+
36203680
#### futuresGetOrder
36213681

36223682
Check an order's status.

src/http-client.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,27 @@ const orderOco = (privCall, payload = {}, url) => {
404404
)
405405
}
406406

407+
const updateOrder = (privCall, payload = {}, url) => {
408+
const newPayload = { ...payload }
409+
410+
if (!newPayload.cancelReplaceMode) {
411+
newPayload.cancelReplaceMode = 'STOP_ON_FAILURE'
412+
}
413+
414+
if (!newPayload.timeInForce) {
415+
newPayload.timeInForce = 'GTC'
416+
}
417+
418+
if (!newPayload.newClientOrderId) {
419+
newPayload.newClientOrderId = spotP()
420+
}
421+
422+
return (
423+
checkParams('updateOrder', newPayload, ['symbol', 'side', 'type']) &&
424+
privCall(url, newPayload, 'POST')
425+
)
426+
}
427+
407428
/**
408429
* Zip asks and bids reponse from order book
409430
*/
@@ -486,6 +507,7 @@ export default opts => {
486507

487508
// Order endpoints
488509
order: payload => order(privCall, payload, '/api/v3/order'),
510+
updateOrder: payload => updateOrder(privCall, payload, '/api/v3/order/cancelReplace'),
489511
orderOco: payload => orderOco(privCall, payload, '/api/v3/order/oco'),
490512
orderTest: payload => order(privCall, payload, '/api/v3/order/test'),
491513
getOrder: payload => privCall('/api/v3/order', payload),
@@ -598,6 +620,15 @@ export default opts => {
598620
// Use regular order endpoint
599621
return order(privCall, payload, '/fapi/v1/order')
600622
},
623+
futuresUpdateOrder: payload => {
624+
if (payload && 'conditional' in payload) {
625+
// for now it is not supported
626+
// const payloadCopy = { ...payload }
627+
// delete payloadCopy.conditional
628+
// return privCall('/fapi/v1/algoOrder', payloadCopy, 'PUT')
629+
}
630+
return privCall('/fapi/v1/order', payload, 'PUT')
631+
},
601632
futuresBatchOrders: payload => privCall('/fapi/v1/batchOrders', payload, 'POST'),
602633
futuresGetOrder: payload => {
603634
// Check if this is a request for a conditional/algo order

test/static-tests.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ test.serial.beforeEach(t => {
3737
interceptedBody = requestBody
3838
return { success: true }
3939
})
40+
nock(/.*/)
41+
.put(/.*/)
42+
.reply(200, function (uri, requestBody) {
43+
interceptedUrl = `${this.req.options.proto}://${this.req.options.hostname}${uri}`
44+
interceptedBody = requestBody
45+
return { success: true }
46+
})
4047
nock(/.*/)
4148
.delete(/.*/)
4249
.reply(200, function (uri, requestBody) {
@@ -277,6 +284,24 @@ test.serial('[REST] Futures LimitSell', async t => {
277284
t.true(obj.newClientOrderId.startsWith(CONTRACT_PREFIX))
278285
})
279286

287+
test.serial('[REST] Futures update/edit order', async t => {
288+
await binance.futuresUpdateOrder({
289+
symbol: 'LTCUSDT',
290+
side: 'SELL',
291+
type: 'LIMIT',
292+
quantity: 0.5,
293+
price: 100,
294+
orderId: 1234,
295+
})
296+
t.true(interceptedUrl.startsWith('https://fapi.binance.com/fapi/v1/order'))
297+
const obj = urlToObject(interceptedUrl.replace('https://fapi.binance.com/fapi/v1/order?', ''))
298+
t.is(obj.symbol, 'LTCUSDT')
299+
t.is(obj.side, 'SELL')
300+
t.is(obj.type, 'LIMIT')
301+
t.is(obj.quantity, '0.5')
302+
t.is(obj.orderId, '1234')
303+
})
304+
280305
test.serial('[REST] Futures cancel order', async t => {
281306
await binance.futuresCancelOrder({ symbol: 'LTCUSDT', orderId: '34234234' })
282307
const url = 'https://fapi.binance.com/fapi/v1/order'
@@ -298,6 +323,26 @@ test.serial('[REST] MarketBuy test', async t => {
298323
t.true(obj.newClientOrderId.startsWith(SPOT_PREFIX))
299324
})
300325

326+
test.serial('[REST] update spot order', async t => {
327+
await binance.updateOrder({
328+
symbol: 'LTCUSDT',
329+
side: 'BUY',
330+
type: 'MARKET',
331+
quantity: 0.5,
332+
cancelOrderId: 1234,
333+
})
334+
t.true(interceptedUrl.startsWith('https://api.binance.com/api/v3/order/cancelReplace'))
335+
const body = interceptedUrl.replace('https://api.binance.com/api/v3/order/cancelReplace', '')
336+
const obj = urlToObject(body)
337+
t.is(obj.symbol, 'LTCUSDT')
338+
t.is(obj.side, 'BUY')
339+
t.is(obj.type, 'MARKET')
340+
t.is(obj.quantity, '0.5')
341+
t.is(obj.cancelReplaceMode, 'STOP_ON_FAILURE')
342+
t.is(obj.cancelOrderId, '1234')
343+
t.true(obj.newClientOrderId.startsWith(SPOT_PREFIX))
344+
})
345+
301346
test.serial('[REST] spot open orders', async t => {
302347
await binance.openOrders({ symbol: 'LTCUSDT' })
303348
t.true(interceptedUrl.startsWith('https://api.binance.com/api/v3/openOrders'))

types/futures.d.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,46 @@ export interface FuturesEndpoints extends BinanceRestClient {
184184
commissionAsset: string;
185185
}>;
186186
}>;
187+
futuresUpdateOrder(payload: {
188+
orderId?: number;
189+
origClientOrderId?: string;
190+
symbol: string;
191+
side: OrderSide;
192+
type: OrderType;
193+
quantity?: string;
194+
price?: string;
195+
priceMatch?: string;
196+
}): Promise<{
197+
symbol: string;
198+
orderId: number;
199+
orderListId: number;
200+
clientOrderId: string;
201+
transactTime: number;
202+
price: string;
203+
origQty: string;
204+
executedQty: string;
205+
cumQuote: string;
206+
status: OrderStatus;
207+
timeInForce: TimeInForce;
208+
type: OrderType;
209+
side: OrderSide;
210+
marginBuyBorrowAmount: string;
211+
marginBuyBorrowAsset: string;
212+
stopPrice?: string;
213+
workingType?: string;
214+
priceProtect?: string;
215+
origType?: string;
216+
priceMatch?: string;
217+
selfTradePreventionMode?: string;
218+
goodTillDate?: number;
219+
updateTime?: number;
220+
fills: Array<{
221+
price: string;
222+
qty: string;
223+
commission: string;
224+
commissionAsset: string;
225+
}>;
226+
}>;
187227
futuresBatchOrders(payload: { batchOrders: Array<{
188228
symbol: string;
189229
side: OrderSide;

types/order.d.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,76 @@ export interface OrderEndpoints extends BinanceRestClient {
3838
commissionAsset: string;
3939
}>;
4040
}>;
41+
updateOrder(payload: {
42+
symbol: string;
43+
side: OrderSide;
44+
type: OrderType;
45+
quantity?: string;
46+
cancelReplaceMode?: string;
47+
price?: string;
48+
cancelNewClientOrderId?: string;
49+
quoteOrderQty?: string;
50+
cancelOrigClientOrderId?: string;
51+
cancelOrderId?: number;
52+
newClientOrderId?: string;
53+
timeInForce?: TimeInForce;
54+
strategyId?: number;
55+
strategyType?: number;
56+
stopPrice?: string;
57+
trailingDelta?: number;
58+
trailingTime?: number;
59+
icebergQty?: string;
60+
newOrderRespType?: string;
61+
selfTradePreventionMode?: string;
62+
cancelRestrictions?: string;
63+
orderRateLimitExceededMode?: string;
64+
pegPriceType?: string;
65+
pegOffsetValue?: string;
66+
pegOffsetType?: string;
67+
}): Promise<{
68+
cancelResult: string;
69+
newOrderResult: string;
70+
cancelResponse: {
71+
symbol: string;
72+
origClientOrderId: string;
73+
orderId: number;
74+
orderListId: number;
75+
clientOrderId: string;
76+
transactTime: number;
77+
price: string;
78+
origQty: string;
79+
executedQty: string;
80+
cummulativeQuoteQty: string;
81+
status: OrderStatus;
82+
timeInForce: TimeInForce;
83+
type: OrderType;
84+
side: OrderSide;
85+
};
86+
newOrderResponse: {
87+
symbol: string;
88+
orderId: number;
89+
orderListId: number;
90+
clientOrderId: string;
91+
transactTime: number;
92+
price: string;
93+
origQty: string;
94+
executedQty: string;
95+
origQuoteOrderQty: string;
96+
cummulativeQuoteQty: string;
97+
status: OrderStatus;
98+
timeInForce: TimeInForce;
99+
type: OrderType;
100+
side: OrderSide;
101+
workingTime: number;
102+
fills: Array<{
103+
price: string;
104+
qty: string;
105+
commission: string;
106+
commissionAsset: string;
107+
}>;
108+
selfTradePreventionMode: string;
109+
};
110+
}>;
41111
orderOco(payload: {
42112
symbol: string;
43113
side: OrderSide;

0 commit comments

Comments
 (0)