From a0dc9f25197d8461c7e7839128d917ce4d3113f8 Mon Sep 17 00:00:00 2001 From: garuda Date: Wed, 29 May 2024 15:34:18 +0800 Subject: [PATCH] feat: add reset interface * add Place Order Test * add Place Multiple Orders --- README.md | 109 +++++++++++++++++++++++++++++++++ example/index.js | 58 ++++++++++++++++++ lib/dataType/order.d.ts | 21 +++++++ lib/index.d.ts | 20 +++++- lib/index.js | 42 +++++++++++++ lib/resetAPI/constants.d.ts | 2 + lib/resetAPI/constants.js | 4 +- lib/resetAPI/futuresOrder.d.ts | 3 +- lib/resetAPI/futuresOrder.js | 6 +- lib/tools/constants.d.ts | 1 - lib/tools/constants.js | 3 +- lib/tools/request.d.ts | 1 - lib/tools/request.js | 4 +- lib/tools/tools.js | 3 +- src/dataType/order.ts | 24 +++++++- src/index.ts | 84 ++++++++++++++++++++++++- src/resetAPI/constants.ts | 2 + src/resetAPI/futuresOrder.ts | 7 ++- src/tools/constants.ts | 1 - src/tools/request.ts | 6 +- src/tools/tools.ts | 3 +- 21 files changed, 380 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 06dbdb5..0660fbb 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,8 @@ - [Get Transfer-Out Request Records](#get-transfer-out-request-records) - [Trade](#trade) - [Orders](#orders) + - [Place Order Test](#place-order-test) + - [Place Multiple Orders](#place-multiple-orders) - [Fills](#fills) - [Positions](#positions) - [Risk Limit Level](#risk-limit-level) @@ -280,6 +282,113 @@ futuresSDK.futuresOrderDetail({ clientOid: 'clientOid' }, console.log); futuresSDK.futuresOrderDetail('orderId', console.log); ``` +##### Place Order Test +> Place Order Test, After placing an order, the order will not enter the matching system, and the order cannot be queried. + +```js +// Place Order Test +// symbol, price, size, leverage = 1, clientOid = uuidV4(), optional + +// Buy Limit Order +futuresSDK.futuresBuyTest( + { + symbol: 'ETHUSDTM', + price: 10000, + leverage: 5, + size: 1 + // clientOid: uuidV4(), + }, + console.log +); + +// Buy Market Order +futuresSDK.futuresBuyTest( + { + symbol: 'ETHUSDTM', + leverage: 5, + size: 1 + // clientOid: uuidV4(), + }, + console.log +); + +// Buy Stop Order +futuresSDK.futuresBuyTest( + { + symbol: 'ETHUSDTM', + price: 10000, + leverage: 5, + size: 1, + // clientOid: uuidV4(), + optional: { + stop: 'up', + stopPriceType: 'TP', + stopPrice: '10000' + // ... + } + }, + console.log +); + +// Sell Order +// futuresSDK.futuresBuyTest -> futuresSDK.futuresSellTest +futuresSDK.futuresSellTest( + { + symbol: 'ETHUSDTM', + price: 20000, + leverage: 5, + size: 1 + // clientOid: uuidV4(), + }, + console.log +); +``` + +##### Place Multiple Orders +```js +//request +[ + { + "clientOid":"5c52e11203aa677f33e491", + "side":"buy", + "symbol":"ETHUSDTM", + "type":"limit", + "price":"2150", + "size":"2" + }, + { + "clientOid":"5c52e11203aa677f33e492", + "side":"buy", + "symbol":"XBTUSDTM", + "type":"limit", + "price":"32150", + "size":"2" + } +] + +//Response +[ + { + "orderId":"80465574458560512", + "clientOid":"5c52e11203aa677f33e491", + "symbol":"ETHUSDTM", + "code":"200000", + "msg":"success" + }, + { + "orderId":"80465575289094144", + "clientOid":"5c52e11203aa677f33e492", + "symbol":"ETHUSDTM", + "code":"200000", + "msg":"success" + } +] + +futuresSDK.futuresOrderMulti([...], console.log); +``` + + + #### Fills ```js diff --git a/example/index.js b/example/index.js index 9e27812..08b63d4 100644 --- a/example/index.js +++ b/example/index.js @@ -1,4 +1,5 @@ const KuCoinFutures = require('../lib/index').default; +// const { v4: uuidV4 } = require('uuid'); const futuresSDK = new KuCoinFutures({ key: '[Your key]', @@ -29,6 +30,21 @@ futuresSDK.futuresStatus(console.log); // console.log // ); +// futuresSDK.futuresBuyTest( +// { +// symbol: 'ETHUSDTM', +// price: 10000, +// leverage: 5, +// size: 1, +// optional: { +// stop: 'up', +// stopPriceType: 'TP', +// stopPrice: '10000' +// } +// }, +// console.log +// ); + // futuresSDK.futuresSell( // { // symbol: 'ETHUSDTM', @@ -47,6 +63,24 @@ futuresSDK.futuresStatus(console.log); // console.log // ); +// futuresSDK.futuresSellTest( +// { +// symbol: 'ETHUSDTM', +// price: 3000, +// leverage: 15, +// size: 1, +// // clientOid: uuidV4(), +// optional: { +// remark: 'test', +// stop: 'up', +// stopPriceType: 'TP', +// stopPrice: '20000', +// // ... +// } +// }, +// console.log +// ); + // futuresSDK.futuresCancel('orderId', console.log); // futuresSDK.futuresCancelAllOpenOrders('ETHUSDTM', console.log); @@ -73,6 +107,30 @@ futuresSDK.futuresStatus(console.log); // futuresSDK.futuresOrderDetail('orderId', console.log); +// futuresSDK.futuresOrderMulti( +// [ +// { +// clientOid: uuidV4(), +// side: 'buy', +// symbol: 'ETHUSDTM', +// type: 'limit', +// price: '10', +// size: '1', +// leverage: 5 +// }, +// { +// clientOid: uuidV4(), +// side: 'buy', +// symbol: 'XBTUSDTM', +// type: 'limit', +// price: '10', +// size: '2', +// leverage: 10 +// } +// ], +// console.log +// ); + // futuresSDK.futuresTransactionHistory( // { // startTime: new Date().getTime() - 7 * 24 * 60 * 60 * 1000, diff --git a/lib/dataType/order.d.ts b/lib/dataType/order.d.ts index 60f709d..67c6173 100644 --- a/lib/dataType/order.d.ts +++ b/lib/dataType/order.d.ts @@ -16,6 +16,27 @@ export interface OrderBody extends BaseOrderBody { side: string; price?: number | string; } +export interface MultiOrderBody { + clientOid: string; + side: string; + symbol: string; + leverage: string; + price?: number | string; + size?: number; + type?: string; + remark?: string; + stop?: string; + stopPriceType?: string; + stopPrice?: string; + reduceOnly?: string; + closeOrder?: string; + forceHold?: string; + timeInForce?: string; + postOnly?: string; + hidden?: string; + iceberg?: string; + visibleSize?: string; +} export type OpenOrderStatusType = 'active' | 'done'; export interface OpenOrderListParams extends PageSizeParams { status?: OpenOrderStatusType; diff --git a/lib/index.d.ts b/lib/index.d.ts index 77da934..5196a49 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -1,4 +1,4 @@ -import { CreateSubApiParams, FillsParams, FundingHistoryParams, OpenOrderListParams, StopOrderListParams, TransactionHistoryParams, TransferListParams, UpdateSubApiParams, IndexListParams, klineParams, Callback, FundingRatesParams } from './dataType'; +import { CreateSubApiParams, FillsParams, FundingHistoryParams, OpenOrderListParams, StopOrderListParams, TransactionHistoryParams, TransferListParams, UpdateSubApiParams, IndexListParams, klineParams, Callback, FundingRatesParams, MultiOrderBody } from './dataType'; import { WebSocketClient } from './websocket'; export default class KuCoinFutures { private request; @@ -58,6 +58,7 @@ export default class KuCoinFutures { */ futureTransfers: (params?: TransferListParams, callback?: Function) => Promise; private order; + private orderTest; private stopOrder; futuresBuy: (params: { symbol: string; @@ -75,6 +76,23 @@ export default class KuCoinFutures { clientOid?: string | undefined; optional?: object | undefined; }, callback?: Function) => Promise; + futuresBuyTest: (params: { + symbol: string; + size: string | number; + price: string | number; + leverage?: number | undefined; + clientOid?: string | undefined; + optional?: object | undefined; + }, callback?: Function) => Promise; + futuresSellTest: (params: { + symbol: string; + size: string | number; + price: string | number; + leverage?: number | undefined; + clientOid?: string | undefined; + optional?: object | undefined; + }, callback?: Function) => Promise; + futuresOrderMulti: (params: Array, callback?: Function) => Promise; futuresCancel: (orderId: string, callback?: Function) => Promise; futuresCancelAllOpenOrders: (symbol?: string, callback?: Function) => Promise; futuresCancelAllStopOrders: (symbol?: string, callback?: Function) => Promise; diff --git a/lib/index.js b/lib/index.js index 8fd3520..afb9808 100644 --- a/lib/index.js +++ b/lib/index.js @@ -238,6 +238,16 @@ var KuCoinFutures = /** @class */ (function () { }); }); }; + this.orderTest = function (params, method, callback) { + if (method === void 0) { method = constants_1.GET; } + return __awaiter(_this_1, void 0, void 0, function () { + var _a, body, endpoint; + return __generator(this, function (_b) { + _a = (0, resetAPI_1.returnBodyAndEndpoint)(params, method, true), body = _a.body, endpoint = _a.endpoint; + return [2 /*return*/, this.makeRequest({ body: body, method: method, endpoint: endpoint, callback: callback })]; + }); + }); + }; this.stopOrder = function (params, method, callback) { if (method === void 0) { method = constants_1.GET; } return __awaiter(_this_1, void 0, void 0, function () { @@ -271,6 +281,38 @@ var KuCoinFutures = /** @class */ (function () { return [2 /*return*/, this.order({ side: 'sell', price: price, symbol: symbol, size: size, leverage: leverage, clientOid: clientOid, optional: optional }, constants_1.POST, callback)]; }); }); }; + // Place Order Test, After placing an order, the order will not enter the matching system, and the order cannot be queried. + this.futuresBuyTest = function (params, callback) { return __awaiter(_this_1, void 0, void 0, function () { + var price, symbol, size, _a, leverage, _b, clientOid, optional; + return __generator(this, function (_c) { + price = params.price, symbol = params.symbol, size = params.size, _a = params.leverage, leverage = _a === void 0 ? 1 : _a, _b = params.clientOid, clientOid = _b === void 0 ? (0, uuid_1.v4)() : _b, optional = params.optional; + if (!symbol) { + throw new TypeError('Order buy symbol must be set!'); + } + return [2 /*return*/, this.orderTest({ side: 'buy', price: price, symbol: symbol, size: size, leverage: leverage, clientOid: clientOid, optional: optional }, constants_1.POST, callback)]; + }); + }); }; + // Place Order Test, After placing an order, the order will not enter the matching system, and the order cannot be queried. + this.futuresSellTest = function (params, callback) { return __awaiter(_this_1, void 0, void 0, function () { + var price, symbol, size, _a, leverage, _b, clientOid, optional; + return __generator(this, function (_c) { + price = params.price, symbol = params.symbol, size = params.size, _a = params.leverage, leverage = _a === void 0 ? 1 : _a, _b = params.clientOid, clientOid = _b === void 0 ? (0, uuid_1.v4)() : _b, optional = params.optional; + if (!symbol) { + throw new TypeError('Order sell symbol must be set!'); + } + return [2 /*return*/, this.orderTest({ side: 'sell', price: price, symbol: symbol, size: size, leverage: leverage, clientOid: clientOid, optional: optional }, constants_1.POST, callback)]; + }); + }); }; + this.futuresOrderMulti = function (params, callback) { return __awaiter(_this_1, void 0, void 0, function () { + return __generator(this, function (_a) { + return [2 /*return*/, this.makeRequest({ + body: params, + method: constants_1.POST, + endpoint: resetAPI_1.FUTURES_ORDER_MULTI_EP, + callback: callback + })]; + }); + }); }; this.futuresCancel = function (orderId, callback) { return __awaiter(_this_1, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, this.order(orderId, constants_1.DELETE, callback)]; diff --git a/lib/resetAPI/constants.d.ts b/lib/resetAPI/constants.d.ts index c2a7672..0c51057 100644 --- a/lib/resetAPI/constants.d.ts +++ b/lib/resetAPI/constants.d.ts @@ -10,6 +10,8 @@ export declare const FUTURES_ORDER_EP = "/api/v1/orders"; export declare const FUTURES_STOP_ORDER_EP = "/api/v1/stopOrders"; export declare const FUTURES_RECENT_DONE_ORDERS_EP = "/api/v1/recentDoneOrders"; export declare const FUTURES_ORDER_CLIENT_ORDER_EP = "/api/v1/orders/client-order"; +export declare const FUTURES_ORDER_TEST_EP = "/api/v1/orders/test"; +export declare const FUTURES_ORDER_MULTI_EP = "/api/v1/orders/multi"; export declare const FUTURES_FILLS_EP = "/api/v1/fills"; export declare const FUTURES_RECENT_FILLS_EP = "/api/v1/recentFills"; export declare const FUTURES_TOTAL_OPEN_ORDERS_MARGIN_EP = "/api/v1/openOrderStatistics"; diff --git a/lib/resetAPI/constants.js b/lib/resetAPI/constants.js index 667b7b2..902b7df 100644 --- a/lib/resetAPI/constants.js +++ b/lib/resetAPI/constants.js @@ -1,6 +1,6 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.FUTURES_TRADE_STATISTICS_EP = exports.FUTURES_KLINE_EP = exports.FUTURES_SERVICE_STATUS_EP = exports.FUTURES_TIMESTAMP_EP = exports.FUTURES_PREMIUM_EP = exports.FUTURES_MARK_PRICE_EP = exports.FUTURES_INDEX_EP = exports.FUTURES_INTEREST_EP = exports.FUTURES_TRADE_HISTORY_EP = exports.FUTURES_LEVEL2_100_EP = exports.FUTURES_LEVEL2_20_EP = exports.FUTURES_LEVEL2_EP = exports.FUTURES_TICKER_EP = exports.FUTURES_CONTRACTS_DETAIL_EP = exports.FUTURES_CONTRACTS_ACTIVE_EP = exports.FUTURES_FUNDING_RATES_EP = exports.FUTURES_FUNDING_HISTORY_EP = exports.FUTURES_FUNDING_RATE_EP = exports.FUTURES_CHANGE_RISK_LIMIT_EP = exports.FUTURES_RISK_LIMIT_EP = exports.FUTURES_POSITION_MARGIN_EP = exports.FUTURES_POSITION_AUTO_DEPOSIT_STATUS_EP = exports.FUTURES_POSITIONS_EP = exports.FUTURES_POSITION_EP = exports.FUTURES_TOTAL_OPEN_ORDERS_MARGIN_EP = exports.FUTURES_RECENT_FILLS_EP = exports.FUTURES_FILLS_EP = exports.FUTURES_ORDER_CLIENT_ORDER_EP = exports.FUTURES_RECENT_DONE_ORDERS_EP = exports.FUTURES_STOP_ORDER_EP = exports.FUTURES_ORDER_EP = exports.FUTURES_TRANSFER_LIST_EP = exports.FUTURES_TRANSFER_IN_EP = exports.FUTURES_TRANSFER_OUT_EP = exports.FUTURES_UPDATE_SUB_API_EP = exports.FUTURES_SUB_API_EP = exports.FUTURES_TRANSACTION_HISTORY_EP = exports.FUTURES_ACCOUNT_OVERVIEW_ALL_EP = exports.FUTURES_ACCOUNT_OVERVIEW_EP = void 0; +exports.FUTURES_TRADE_STATISTICS_EP = exports.FUTURES_KLINE_EP = exports.FUTURES_SERVICE_STATUS_EP = exports.FUTURES_TIMESTAMP_EP = exports.FUTURES_PREMIUM_EP = exports.FUTURES_MARK_PRICE_EP = exports.FUTURES_INDEX_EP = exports.FUTURES_INTEREST_EP = exports.FUTURES_TRADE_HISTORY_EP = exports.FUTURES_LEVEL2_100_EP = exports.FUTURES_LEVEL2_20_EP = exports.FUTURES_LEVEL2_EP = exports.FUTURES_TICKER_EP = exports.FUTURES_CONTRACTS_DETAIL_EP = exports.FUTURES_CONTRACTS_ACTIVE_EP = exports.FUTURES_FUNDING_RATES_EP = exports.FUTURES_FUNDING_HISTORY_EP = exports.FUTURES_FUNDING_RATE_EP = exports.FUTURES_CHANGE_RISK_LIMIT_EP = exports.FUTURES_RISK_LIMIT_EP = exports.FUTURES_POSITION_MARGIN_EP = exports.FUTURES_POSITION_AUTO_DEPOSIT_STATUS_EP = exports.FUTURES_POSITIONS_EP = exports.FUTURES_POSITION_EP = exports.FUTURES_TOTAL_OPEN_ORDERS_MARGIN_EP = exports.FUTURES_RECENT_FILLS_EP = exports.FUTURES_FILLS_EP = exports.FUTURES_ORDER_MULTI_EP = exports.FUTURES_ORDER_TEST_EP = exports.FUTURES_ORDER_CLIENT_ORDER_EP = exports.FUTURES_RECENT_DONE_ORDERS_EP = exports.FUTURES_STOP_ORDER_EP = exports.FUTURES_ORDER_EP = exports.FUTURES_TRANSFER_LIST_EP = exports.FUTURES_TRANSFER_IN_EP = exports.FUTURES_TRANSFER_OUT_EP = exports.FUTURES_UPDATE_SUB_API_EP = exports.FUTURES_SUB_API_EP = exports.FUTURES_TRANSACTION_HISTORY_EP = exports.FUTURES_ACCOUNT_OVERVIEW_ALL_EP = exports.FUTURES_ACCOUNT_OVERVIEW_EP = void 0; // account endpoint exports.FUTURES_ACCOUNT_OVERVIEW_EP = '/api/v1/account-overview'; exports.FUTURES_ACCOUNT_OVERVIEW_ALL_EP = '/api/v1/account-overview-all'; @@ -16,6 +16,8 @@ exports.FUTURES_ORDER_EP = '/api/v1/orders'; exports.FUTURES_STOP_ORDER_EP = '/api/v1/stopOrders'; exports.FUTURES_RECENT_DONE_ORDERS_EP = '/api/v1/recentDoneOrders'; exports.FUTURES_ORDER_CLIENT_ORDER_EP = '/api/v1/orders/client-order'; +exports.FUTURES_ORDER_TEST_EP = '/api/v1/orders/test'; +exports.FUTURES_ORDER_MULTI_EP = '/api/v1/orders/multi'; // fills endpoint exports.FUTURES_FILLS_EP = '/api/v1/fills'; exports.FUTURES_RECENT_FILLS_EP = '/api/v1/recentFills'; diff --git a/lib/resetAPI/futuresOrder.d.ts b/lib/resetAPI/futuresOrder.d.ts index 20587cd..59ee0cb 100644 --- a/lib/resetAPI/futuresOrder.d.ts +++ b/lib/resetAPI/futuresOrder.d.ts @@ -17,9 +17,10 @@ export declare const makeFuturesOrderBody: ({ side, symbol, size, price, leverag * return futures order make body and endpoint * @param {any} params。 * @param {string} method - DEFAULT 'GET'。 + * @param {boolean} isTest - DEFAULT false * @returns {Object} return { body, endpoint }。 */ -declare const returnBodyAndEndpoint: (params: any, method?: string) => { +declare const returnBodyAndEndpoint: (params: any, method?: string, isTest?: boolean) => { body: any; endpoint: string; }; diff --git a/lib/resetAPI/futuresOrder.js b/lib/resetAPI/futuresOrder.js index 4b33af4..b182afe 100644 --- a/lib/resetAPI/futuresOrder.js +++ b/lib/resetAPI/futuresOrder.js @@ -98,11 +98,13 @@ exports.makeFuturesOrderBody = makeFuturesOrderBody; * return futures order make body and endpoint * @param {any} params。 * @param {string} method - DEFAULT 'GET'。 + * @param {boolean} isTest - DEFAULT false * @returns {Object} return { body, endpoint }。 */ -var returnBodyAndEndpoint = function (params, method) { +var returnBodyAndEndpoint = function (params, method, isTest) { if (method === void 0) { method = 'GET'; } - var endpoint = constants_1.FUTURES_ORDER_EP; + if (isTest === void 0) { isTest = false; } + var endpoint = isTest ? constants_1.FUTURES_ORDER_TEST_EP : constants_1.FUTURES_ORDER_EP; var body = params; switch (method) { case 'POST': { diff --git a/lib/tools/constants.d.ts b/lib/tools/constants.d.ts index c88c8a4..008f78e 100644 --- a/lib/tools/constants.d.ts +++ b/lib/tools/constants.d.ts @@ -2,5 +2,4 @@ export declare const TIME_OUT = 10000; export declare const GET = "GET"; export declare const POST = "POST"; export declare const DELETE = "DELETE"; -export declare const SANDBOX_ADDR_EP = "https://api-sandbox-futures.kucoin.com"; export declare const PROD_ADDR_EP = "https://api-futures.kucoin.com"; diff --git a/lib/tools/constants.js b/lib/tools/constants.js index d035713..4fd3189 100644 --- a/lib/tools/constants.js +++ b/lib/tools/constants.js @@ -1,9 +1,8 @@ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); -exports.PROD_ADDR_EP = exports.SANDBOX_ADDR_EP = exports.DELETE = exports.POST = exports.GET = exports.TIME_OUT = void 0; +exports.PROD_ADDR_EP = exports.DELETE = exports.POST = exports.GET = exports.TIME_OUT = void 0; exports.TIME_OUT = 10000; exports.GET = 'GET'; exports.POST = 'POST'; exports.DELETE = 'DELETE'; -exports.SANDBOX_ADDR_EP = 'https://api-sandbox-futures.kucoin.com'; exports.PROD_ADDR_EP = 'https://api-futures.kucoin.com'; diff --git a/lib/tools/request.d.ts b/lib/tools/request.d.ts index cd63fe8..0406d66 100644 --- a/lib/tools/request.d.ts +++ b/lib/tools/request.d.ts @@ -32,7 +32,6 @@ export default class Request { key: string | number; secret: string | number; passphrase: string | number; - env?: string; version?: string | number; }); setSignatureConfig(config?: any): void; diff --git a/lib/tools/request.js b/lib/tools/request.js index 84d9e86..7bf7f55 100644 --- a/lib/tools/request.js +++ b/lib/tools/request.js @@ -74,8 +74,8 @@ exports.apiAuth = apiAuth; */ var Request = /** @class */ (function () { function Request(props) { - var _a = props || {}, _b = _a.env, env = _b === void 0 ? 'prod' : _b, _c = _a.key, key = _c === void 0 ? '' : _c, secret = _a.secret, _d = _a.passphrase, passphrase = _d === void 0 ? '' : _d, _e = _a.version, version = _e === void 0 ? 2 : _e, other = __rest(_a, ["env", "key", "secret", "passphrase", "version"]); - var baseURL = env === 'prod' ? constants_1.PROD_ADDR_EP : constants_1.SANDBOX_ADDR_EP; + var _a = props || {}, _b = _a.key, key = _b === void 0 ? '' : _b, secret = _a.secret, _c = _a.passphrase, passphrase = _c === void 0 ? '' : _c, _d = _a.version, version = _d === void 0 ? 2 : _d, other = __rest(_a, ["key", "secret", "passphrase", "version"]); + var baseURL = constants_1.PROD_ADDR_EP; this.axiosInstance = axios_1.default.create(__assign({ baseURL: baseURL, timeout: constants_1.TIME_OUT, httpsAgent: httpsAgent }, other)); this.key = key; this.secret = secret; diff --git a/lib/tools/tools.js b/lib/tools/tools.js index 753cca5..00bc2ef 100644 --- a/lib/tools/tools.js +++ b/lib/tools/tools.js @@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) { Object.defineProperty(exports, "__esModule", { value: true }); exports.log = exports.cryptoHmac = exports.splitArray = exports.joinSymbol = exports.filterEmptyValues = exports.makeQueryString = void 0; var omitBy_1 = __importDefault(require("lodash/omitBy")); +var isArray_1 = __importDefault(require("lodash/isArray")); var crypto_1 = __importDefault(require("crypto")); /** * make object query to string @@ -27,7 +28,7 @@ exports.makeQueryString = makeQueryString; * @returns filterObj */ var filterEmptyValues = function (obj) { - if (typeof obj !== 'object') + if (typeof obj !== 'object' || (0, isArray_1.default)(obj)) return obj; var omitValue = (0, omitBy_1.default)(obj, function (value) { return value === null || value === undefined || value === ''; }); if (!omitValue || !Object.keys(omitValue).length) diff --git a/src/dataType/order.ts b/src/dataType/order.ts index 67788d4..c3c335e 100644 --- a/src/dataType/order.ts +++ b/src/dataType/order.ts @@ -35,6 +35,28 @@ export interface OrderBody extends BaseOrderBody { price?: number | string; } +export interface MultiOrderBody { + clientOid: string; + side: string; + symbol: string; + leverage: string; + price?: number | string; + size?: number; + type?: string; + remark?: string; + stop?: string; + stopPriceType?: string; + stopPrice?: string; + reduceOnly?: string; + closeOrder?: string; + forceHold?: string; + timeInForce?: string; + postOnly?: string; + hidden?: string; + iceberg?: string; + visibleSize?: string; +} + export type OpenOrderStatusType = 'active' | 'done'; export interface OpenOrderListParams extends PageSizeParams { status?: OpenOrderStatusType; @@ -51,7 +73,7 @@ export interface StopOrderListParams extends PageSizeParams { export interface FillsParams extends PageSizeParams { orderId?: string; // List fills for a specific order only (If you specify orderId, other parameters can be ignored) - symbol?:string; + symbol?: string; side?: string; type?: OrderType; } diff --git a/src/index.ts b/src/index.ts index 9125da5..f02a7e9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -44,7 +44,8 @@ import { FUTURES_TRANSFER_IN_EP, FUTURES_TRANSFER_LIST_EP, FUTURES_ACCOUNT_OVERVIEW_ALL_EP, - FUTURES_FUNDING_RATES_EP + FUTURES_FUNDING_RATES_EP, + FUTURES_ORDER_MULTI_EP } from './resetAPI'; import { PUBLIC_BULLET_EP, @@ -78,7 +79,8 @@ import { IndexListParams, klineParams, Callback, - FundingRatesParams + FundingRatesParams, + MultiOrderBody } from './dataType'; import { WebSocketClient, CONNECT_ID, TICKER_V2 } from './websocket'; @@ -282,6 +284,15 @@ export default class KuCoinFutures { return this.makeRequest({ body, method, endpoint, callback }); }; + private orderTest = async ( + params: any, + method = GET, + callback?: Function + ) => { + const { body, endpoint } = returnBodyAndEndpoint(params, method, true); + return this.makeRequest({ body, method, endpoint, callback }); + }; + private stopOrder = async ( params: any, method = GET, @@ -353,6 +364,75 @@ export default class KuCoinFutures { ); }; + // Place Order Test, After placing an order, the order will not enter the matching system, and the order cannot be queried. + futuresBuyTest = async ( + params: { + symbol: string; + size: string | number; + price: string | number; + leverage?: number; + clientOid?: string; + optional?: object; + }, + callback?: Function + ) => { + const { + price, + symbol, + size, + leverage = 1, + clientOid = uuidV4(), + optional + } = params; + if (!symbol) { + throw new TypeError('Order buy symbol must be set!'); + } + return this.orderTest( + { side: 'buy', price, symbol, size, leverage, clientOid, optional }, + POST, + callback + ); + }; + + // Place Order Test, After placing an order, the order will not enter the matching system, and the order cannot be queried. + futuresSellTest = async ( + params: { + symbol: string; + size: string | number; + price: string | number; + leverage?: number; + clientOid?: string; + optional?: object; + }, + callback?: Function + ) => { + const { + price, + symbol, + size, + leverage = 1, + clientOid = uuidV4(), + optional + } = params; + if (!symbol) { + throw new TypeError('Order sell symbol must be set!'); + } + return this.orderTest( + { side: 'sell', price, symbol, size, leverage, clientOid, optional }, + POST, + callback + ); + }; + + futuresOrderMulti = async (params: Array, callback?: Function) => { + return this.makeRequest({ + body: params, + method: POST, + endpoint: FUTURES_ORDER_MULTI_EP, + callback + }); + }; + futuresCancel = async (orderId: string, callback?: Function) => { return this.order(orderId, DELETE, callback); }; diff --git a/src/resetAPI/constants.ts b/src/resetAPI/constants.ts index f82415a..cfe8ecf 100644 --- a/src/resetAPI/constants.ts +++ b/src/resetAPI/constants.ts @@ -16,6 +16,8 @@ export const FUTURES_ORDER_EP = '/api/v1/orders'; export const FUTURES_STOP_ORDER_EP = '/api/v1/stopOrders'; export const FUTURES_RECENT_DONE_ORDERS_EP = '/api/v1/recentDoneOrders'; export const FUTURES_ORDER_CLIENT_ORDER_EP = '/api/v1/orders/client-order'; +export const FUTURES_ORDER_TEST_EP = '/api/v1/orders/test'; +export const FUTURES_ORDER_MULTI_EP = '/api/v1/orders/multi'; // fills endpoint export const FUTURES_FILLS_EP = '/api/v1/fills'; diff --git a/src/resetAPI/futuresOrder.ts b/src/resetAPI/futuresOrder.ts index 15cc133..672a05f 100644 --- a/src/resetAPI/futuresOrder.ts +++ b/src/resetAPI/futuresOrder.ts @@ -1,6 +1,6 @@ import pick from 'lodash/pick'; -import { FUTURES_ORDER_EP } from './constants'; +import { FUTURES_ORDER_EP, FUTURES_ORDER_TEST_EP } from './constants'; import { OrderOptionalParamsType, OrderBody } from '../dataType'; @@ -125,10 +125,11 @@ export const makeFuturesOrderBody = ({ * return futures order make body and endpoint * @param {any} params。 * @param {string} method - DEFAULT 'GET'。 + * @param {boolean} isTest - DEFAULT false * @returns {Object} return { body, endpoint }。 */ -const returnBodyAndEndpoint = (params: any, method = 'GET') => { - let endpoint = FUTURES_ORDER_EP; +const returnBodyAndEndpoint = (params: any, method = 'GET', isTest = false) => { + let endpoint = isTest ? FUTURES_ORDER_TEST_EP : FUTURES_ORDER_EP; let body = params; switch (method) { case 'POST': { diff --git a/src/tools/constants.ts b/src/tools/constants.ts index 4e29329..d7f6ec2 100644 --- a/src/tools/constants.ts +++ b/src/tools/constants.ts @@ -4,5 +4,4 @@ export const GET = 'GET'; export const POST = 'POST'; export const DELETE = 'DELETE'; -export const SANDBOX_ADDR_EP = 'https://api-sandbox-futures.kucoin.com'; export const PROD_ADDR_EP = 'https://api-futures.kucoin.com'; diff --git a/src/tools/request.ts b/src/tools/request.ts index 48346d7..615a45a 100644 --- a/src/tools/request.ts +++ b/src/tools/request.ts @@ -3,7 +3,7 @@ import axios, { AxiosRequestConfig } from 'axios'; import https from 'https'; import { makeQueryString, cryptoHmac } from './tools'; -import { TIME_OUT, PROD_ADDR_EP, SANDBOX_ADDR_EP } from './constants'; +import { TIME_OUT, PROD_ADDR_EP } from './constants'; const httpsAgent = new https.Agent({ rejectUnauthorized: false }); @@ -79,18 +79,16 @@ export default class Request { key: string | number; secret: string | number; passphrase: string | number; - env?: string; version?: string | number; }) { const { - env = 'prod', key = '', secret, passphrase = '', version = 2, ...other } = props || {}; - const baseURL = env === 'prod' ? PROD_ADDR_EP : SANDBOX_ADDR_EP; + const baseURL = PROD_ADDR_EP; this.axiosInstance = axios.create({ baseURL, diff --git a/src/tools/tools.ts b/src/tools/tools.ts index d4c2e46..5c618de 100644 --- a/src/tools/tools.ts +++ b/src/tools/tools.ts @@ -1,4 +1,5 @@ import omitBy from 'lodash/omitBy'; +import isArray from 'lodash/isArray'; import crypto from 'crypto'; /** @@ -22,7 +23,7 @@ export const makeQueryString = (query: any): string => { * @returns filterObj */ export const filterEmptyValues = (obj: any) => { - if (typeof obj !== 'object') return obj; + if (typeof obj !== 'object' || isArray(obj)) return obj; const omitValue = omitBy( obj, (value) => value === null || value === undefined || value === ''