Skip to content

Commit 7835a2f

Browse files
authored
Merge pull request #932 from ccxt/margin-sapi-endpoints
fix(client): margin endpoints
2 parents f537a76 + c9b68de commit 7835a2f

File tree

2 files changed

+45
-16
lines changed

2 files changed

+45
-16
lines changed

src/node-binance-api.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ export default class Binance {
237237
return this.base;
238238
}
239239

240+
getSapiUrl(){
241+
return this.sapi;
242+
}
243+
240244
getFapiUrl() {
241245
if (this.Options.test) return this.fapiTest;
242246
return this.fapi;
@@ -623,7 +627,7 @@ export default class Binance {
623627
}
624628

625629
/**
626-
* Create a signed spot/margin request
630+
* Create a signed spot request
627631
* @param {string} path - url path
628632
* @param {object} data - The data to send
629633
* @param {string} method - the http method
@@ -634,6 +638,10 @@ export default class Binance {
634638
return await this.signedRequest/**/(this.getSpotUrl() + path, data, method, noDataInSignature);
635639
}
636640

641+
async privateSapiRequest(path: string, data: Dict = {}, method: HttpMethod = 'GET', noDataInSignature = false) {
642+
return await this.signedRequest/**/(this.getSapiUrl() + path, data, method, noDataInSignature);
643+
}
644+
637645
/**
638646
* Create a signed http request
639647
* @param {string} url - The http endpoint
@@ -1029,7 +1037,7 @@ export default class Binance {
10291037
request.stopPrice = params.stopPrice;
10301038
if (request.type === 'LIMIT') throw Error('stopPrice: Must set "type" to one of the following: STOP_LOSS, STOP_LOSS_LIMIT, TAKE_PROFIT, TAKE_PROFIT_LIMIT');
10311039
}
1032-
return await this.privateSpotRequest(endpoint, this.extend(request, params), 'POST');
1040+
return await this.privateSapiRequest(endpoint, this.extend(request, params), 'POST');
10331041
}
10341042

10351043
// Futures internal functions
@@ -4820,7 +4828,7 @@ export default class Binance {
48204828
* @return {undefined}
48214829
*/
48224830
async mgCancel(symbol: string, orderid: number | string, isIsolated = 'FALSE'): Promise<CancelOrder> {
4823-
return await this.privateSpotRequest('v1/margin/order', { symbol: symbol, orderId: orderid, isIsolated }, 'DELETE');
4831+
return await this.privateSapiRequest('v1/margin/order', { symbol: symbol, orderId: orderid, isIsolated }, 'DELETE');
48244832
}
48254833

48264834
/**
@@ -4831,7 +4839,7 @@ export default class Binance {
48314839
*/
48324840
async mgAllOrders(symbol: string, params: Dict = {}): Promise<Order[]> {
48334841
const parameters = Object.assign({ symbol: symbol }, params);
4834-
return await this.privateSpotRequest('v1/margin/allOrders', parameters);
4842+
return await this.privateSapiRequest('v1/margin/allOrders', parameters);
48354843
}
48364844

48374845
/**
@@ -4843,7 +4851,7 @@ export default class Binance {
48434851
*/
48444852
async mgOrderStatus(symbol: string, orderid: number | string, flags = {}): Promise<Order> {
48454853
const parameters = Object.assign({ symbol: symbol, orderId: orderid }, flags);
4846-
return await this.privateSpotRequest('v1/margin/order', parameters);
4854+
return await this.privateSapiRequest('v1/margin/order', parameters);
48474855
}
48484856

48494857
/**
@@ -4853,7 +4861,7 @@ export default class Binance {
48534861
*/
48544862
async mgOpenOrders(symbol?: string, params: Dict = {}): Promise<Order[]> {
48554863
if (symbol) params.symbol = symbol;
4856-
return await this.privateSpotRequest('v1/margin/openOrders', params);
4864+
return await this.privateSapiRequest('v1/margin/openOrders', params);
48574865
}
48584866

48594867
/**
@@ -4874,7 +4882,7 @@ export default class Binance {
48744882
// }, 'DELETE');
48754883
// }
48764884
// }); // to do check this
4877-
return await this.privateSpotRequest('v1/margin/openOrders', this.extend({ symbol: symbol }, params), 'DELETE');
4885+
return await this.privateSapiRequest('v1/margin/openOrders', this.extend({ symbol: symbol }, params), 'DELETE');
48784886
}
48794887

48804888
/**
@@ -4886,7 +4894,7 @@ export default class Binance {
48864894
*/
48874895
async mgTransferMainToMargin(asset: string, amount: number, params: Dict = {}) {
48884896
params = this.extend({ asset: asset, amount: amount, type: 1 }, params);
4889-
return await this.privateSpotRequest('v1/margin/transfer', params, 'POST');
4897+
return await this.privateSapiRequest('v1/margin/transfer', params, 'POST');
48904898
}
48914899

48924900
/**
@@ -4897,7 +4905,7 @@ export default class Binance {
48974905
*/
48984906
async mgTransferMarginToMain(asset: string, amount: number, params: Dict = {}) {
48994907
const parameters = Object.assign({ asset: asset, amount: amount, type: 2 });
4900-
return await this.privateSpotRequest('v1/margin/transfer', this.extend(parameters, params), 'POST');
4908+
return await this.privateSapiRequest('v1/margin/transfer', this.extend(parameters, params), 'POST');
49014909
}
49024910
// /**
49034911
// * Universal Transfer requires API permissions enabled
@@ -4918,7 +4926,7 @@ export default class Binance {
49184926
*/
49194927
async mgTrades(symbol: string, params: Dict = {}): Promise<MyTrade[]> {
49204928
const parameters = Object.assign({ symbol: symbol }, params);
4921-
return await this.privateSpotRequest('v1/margin/myTrades', parameters);
4929+
return await this.privateSapiRequest('v1/margin/myTrades', parameters);
49224930
}
49234931

49244932
/**
@@ -4989,7 +4997,7 @@ export default class Binance {
49894997
isIsolated,
49904998
symbol
49914999
} : {};
4992-
return await this.privateSpotRequest('v1/margin/loan', this.extend({ ...parameters, ...isolatedObj }, params), 'POST');
5000+
return await this.privateSapiRequest('v1/margin/loan', this.extend({ ...parameters, ...isolatedObj }, params), 'POST');
49935001
}
49945002

49955003
/**
@@ -5000,7 +5008,7 @@ export default class Binance {
50005008
*/
50015009
async mgQueryLoan(asset: string, options) {
50025010
const parameters = Object.assign({ asset: asset }, options);
5003-
return await this.privateSpotRequest('v1/margin/loan', { ...parameters }, 'GET');
5011+
return await this.privateSapiRequest('v1/margin/loan', { ...parameters }, 'GET');
50045012
}
50055013

50065014
/**
@@ -5011,7 +5019,7 @@ export default class Binance {
50115019
*/
50125020
async mgQueryRepay(asset: string, params: Dict = {}) {
50135021
const parameters = Object.assign({ asset: asset }, params);
5014-
return await this.privateSpotRequest('v1/margin/repay', { ...parameters }, 'GET');
5022+
return await this.privateSapiRequest('v1/margin/repay', { ...parameters }, 'GET');
50155023
}
50165024

50175025
/**
@@ -5029,7 +5037,7 @@ export default class Binance {
50295037
isIsolated,
50305038
symbol
50315039
} : {};
5032-
return await this.privateSpotRequest('v1/margin/repay', this.extend({ ...parameters, ...isolatedObj }, params), 'POST');
5040+
return await this.privateSapiRequest('v1/margin/repay', this.extend({ ...parameters, ...isolatedObj }, params), 'POST');
50335041
}
50345042

50355043
/**
@@ -5040,7 +5048,7 @@ export default class Binance {
50405048
async mgAccount(isIsolated = false, params: Dict = {}) {
50415049
let endpoint = 'v1/margin';
50425050
endpoint += (isIsolated) ? '/isolated' : '' + '/account';
5043-
return await this.privateSpotRequest(endpoint, params);
5051+
return await this.privateSapiRequest(endpoint, params);
50445052
}
50455053
/**
50465054
* Get maximum borrow amount of an asset
@@ -5049,7 +5057,7 @@ export default class Binance {
50495057
*/
50505058
async maxBorrowable(asset: string, params: Dict = {}) {
50515059
params.asset = asset;
5052-
return await this.privateSpotRequest('v1/margin/maxBorrowable', params);
5060+
return await this.privateSapiRequest('v1/margin/maxBorrowable', params);
50535061
}
50545062

50555063
// // Futures WebSocket Functions:

tests/binance-class-static.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,27 @@ describe( 'Static tests', async function () {
266266
assert(obj.newClientOrderId.startsWith(SPOT_PREFIX))
267267
})
268268

269+
it( 'spot open orders', async function ( ) {
270+
await binance.openOrders( 'LTCUSDT')
271+
assert.isTrue( interceptedUrl.startsWith('https://api.binance.com/api/v3/openOrders' ))
272+
})
273+
274+
it( 'margin open orders', async function ( ) {
275+
await binance.mgOpenOrders( 'LTCUSDT')
276+
assert.isTrue( interceptedUrl.startsWith('https://api.binance.com/sapi/v1/margin/openOrders' ))
277+
})
278+
279+
it( 'Margin MarketBuy order', async function ( ) {
280+
await binance.mgMarketBuy( 'LTCUSDT', 0.5)
281+
assert.equal( interceptedUrl, 'https://api.binance.com/sapi/v1/margin/order' )
282+
const obj = urlToObject( interceptedBody )
283+
assert.equal( obj.symbol, 'LTCUSDT' )
284+
assert.equal( obj.side, 'BUY' )
285+
assert.equal( obj.type, 'MARKET' )
286+
assert.equal( obj.quantity, 0.5 )
287+
assert(obj.newClientOrderId.startsWith(SPOT_PREFIX))
288+
})
289+
269290
it( 'spot order with custom clientorderId', async function ( ) {
270291
await binance.order( 'LIMIT', 'BUY', 'LTCUSDT', 0.5, 100, {'newClientOrderId': 'myid'})
271292
assert.equal( interceptedUrl, 'https://api.binance.com/api/v3/order' )

0 commit comments

Comments
 (0)