Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions src/node-binance-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ export default class Binance {

userAgent = 'Mozilla/4.0 (compatible; Node Binance API)';
contentType = 'application/x-www-form-urlencoded';
SPOT_PREFIX = "x-HNA2TXFJ";
CONTRACT_PREFIX = "x-Cb7ytekJ";
SPOT_PREFIX = "x-B3AUXNYV";
CONTRACT_PREFIX = "x-ftGmvgAN";

// Websockets Options
isAlive = false;
Expand Down Expand Up @@ -109,7 +109,7 @@ export default class Binance {
userDeliveryData: this.userDeliveryData.bind(this),
subscribeCombined: this.subscribeCombined.bind(this),
subscribe: this.subscribe.bind(this),
subscriptions: () => this.getSubscriptions.bind(this),
subscriptions: () => this.getSubscriptions.bind(this),
terminate: this.terminate.bind(this),
depth: this.depthStream.bind(this),
depthCache: this.depthCacheStream.bind(this),
Expand Down Expand Up @@ -237,7 +237,7 @@ export default class Binance {
return this.base;
}

getSapiUrl(){
getSapiUrl() {
return this.sapi;
}

Expand Down Expand Up @@ -638,6 +638,9 @@ export default class Binance {
return await this.signedRequest/**/(this.getSpotUrl() + path, data, method, noDataInSignature);
}

/**
* Create a signed SAPI request
*/
async privateSapiRequest(path: string, data: Dict = {}, method: HttpMethod = 'GET', noDataInSignature = false) {
return await this.signedRequest/**/(this.getSapiUrl() + path, data, method, noDataInSignature);
}
Expand All @@ -661,7 +664,7 @@ export default class Binance {
const query = method === 'POST' && noDataInSignature ? '' : this.makeQueryString(data);

const signature = this.generateSignature(query);

if (method === 'POST') {
const opt = this.reqObjPOST(
url,
Expand All @@ -687,7 +690,7 @@ export default class Binance {
generateSignature(query: string, encode = true) {
const secret = this.APISECRET || this.PRIVATEKEY;
let signature = '';
if (secret.includes ('PRIVATE KEY')) {
if (secret.includes('PRIVATE KEY')) {
// if less than the below length, then it can't be RSA key
let keyObject: crypto.KeyObject;
try {
Expand All @@ -699,7 +702,7 @@ export default class Binance {

keyObject = crypto.createPrivateKey(privateKeyObj);

} catch (e){
} catch (e) {
throw new Error(
'Invalid private key. Please provide a valid RSA or ED25519 private key. ' + e.toString()
);
Expand All @@ -710,7 +713,7 @@ export default class Binance {
signature = crypto
.sign('RSA-SHA256', Buffer.from(query), keyObject)
.toString('base64');
if (encode) signature = encodeURIComponent (signature);
if (encode) signature = encodeURIComponent(signature);
return signature;
} else {
// Ed25519 key
Expand Down Expand Up @@ -1089,6 +1092,16 @@ export default class Binance {
return await this.privateFuturesRequest('v1/order', params, 'POST');
}

/**
* @see https://developers.binance.com/docs/derivatives/option/trade/New-Order
* @param type type of order to create
* @param side side of the order (BUY or SELL)
* @param symbol symbol of the market to trade
* @param quantity quantity of the order to create
* @param price price of the order to create
* @param params extra parameters to be sent in the request
* @returns
*/
async deliveryOrder(type: OrderType, side: string, symbol: string, quantity: number, price?: number, params: Dict = {}): Promise<FuturesOrder> {
params.symbol = symbol;
params.side = side;
Expand Down Expand Up @@ -2216,7 +2229,7 @@ export default class Binance {
if (this.Options.verbose) {
this.Options.log(`deliverySubscribe: Subscribed to [${ws.endpoint}] ${queryParams}`);
}
ws.on('open', this.handleDeliverySocketOpen.bind(this, ws,params.openCallback));
ws.on('open', this.handleDeliverySocketOpen.bind(this, ws, params.openCallback));
ws.on('pong', this.handleDeliverySocketHeartbeat.bind(this, ws));
ws.on('error', this.handleDeliverySocketError.bind(this, ws));
ws.on('close', this.handleDeliverySocketClose.bind(this, ws, params.reconnect));
Expand Down Expand Up @@ -3851,7 +3864,7 @@ export default class Binance {
async candlesticks(symbol: string, interval: Interval = '5m', params: Dict = {}): Promise<Candle[]> {
if (!params.limit) params.limit = 500;
params = Object.assign({ symbol: symbol, interval: interval }, params);
const res = await this.publicSpotRequest('v3/klines', params);
const res = await this.publicSpotRequest('v3/klines', params);
return this.parseCandles(res);
}

Expand Down Expand Up @@ -4037,7 +4050,7 @@ export default class Binance {
async futuresCandles(symbol: string, interval: Interval = "30m", params: Dict = {}): Promise<Candle[]> {
params.symbol = symbol;
params.interval = interval;
const res = await this.publicFuturesRequest('v1/klines', params);
const res = await this.publicFuturesRequest('v1/klines', params);
return this.parseCandles(res);
}

Expand Down Expand Up @@ -4357,7 +4370,7 @@ export default class Binance {
/**
* @see https://developers.binance.com/docs/derivatives/usds-margined-futures/trade/rest-api/Cancel-Multiple-Orders
*/
async futuresCancelMultipleOrders(symbol: string, params: Dict = {}): Promise<(FuturesOrder|Response)[]> {
async futuresCancelMultipleOrders(symbol: string, params: Dict = {}): Promise<(FuturesOrder | Response)[]> {
return await this.privateFuturesRequest('v1/batchOrders', this.extend({ 'symbol': symbol }, params), 'DELETE');
}

Expand Down Expand Up @@ -4568,7 +4581,7 @@ export default class Binance {
async deliveryCandles(symbol: string, interval: Interval = "30m", params: Dict = {}): Promise<Candle[]> {
params.symbol = symbol;
params.interval = interval;
const res = await this.publicDeliveryRequest('v1/klines', params);
const res = await this.publicDeliveryRequest('v1/klines', params);
return this.parseCandles(res);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/binance-class-static.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ describe( 'Static tests', async function () {
assert.equal( obj.orderId, '34234234')
})

const SPOT_PREFIX = "x-HNA2TXFJ"
const SPOT_PREFIX = "x-B3AUXNYV"

it( 'MarketBuy', async function ( ) {
await binance.marketBuy( 'LTCUSDT', 0.5 )
Expand Down Expand Up @@ -200,7 +200,7 @@ describe( 'Static tests', async function () {
assert.equal( obj.symbol, 'LTCUSDT' )
})

const CONTRACT_PREFIX = "x-Cb7ytekJ"
const CONTRACT_PREFIX = "x-ftGmvgAN"

it( 'Futures MarketBuy', async function ( ) {
await binance.futuresMarketBuy( 'LTCUSDT', 0.5 )
Expand Down
4 changes: 2 additions & 2 deletions tests/static-tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ describe( 'Static tests', async function () {
assert.equal( obj.orderId, '34234234')
})

const SPOT_PREFIX = "x-HNA2TXFJ"
const SPOT_PREFIX = "x-B3AUXNYV"

it( 'MarketBuy', async function ( ) {
await binance.marketBuy( 'LTCUSDT', 0.5 )
Expand Down Expand Up @@ -182,7 +182,7 @@ describe( 'Static tests', async function () {
assert(obj.newClientOrderId.startsWith(SPOT_PREFIX))
})

const CONTRACT_PREFIX = "x-Cb7ytekJ"
const CONTRACT_PREFIX = "x-ftGmvgAN"

it( 'Futures MarketBuy', async function ( ) {
await binance.futuresMarketBuy( 'LTCUSDT', 0.5 )
Expand Down
Loading