From 7f600271780c1acd6deb464d421dbfe5b7d707ac Mon Sep 17 00:00:00 2001 From: tiagosiebler Date: Sat, 12 Nov 2022 12:26:09 +0000 Subject: [PATCH] add e2e contract write tests --- src/constants/enum.ts | 6 ++ src/types/request/contract.ts | 2 +- test/contract/private.write.test.ts | 135 ++++++++++++++++++++++++++++ 3 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 test/contract/private.write.test.ts diff --git a/src/constants/enum.ts b/src/constants/enum.ts index 3ac4d79..68d9514 100644 --- a/src/constants/enum.ts +++ b/src/constants/enum.ts @@ -54,6 +54,12 @@ export const API_ERROR_CODE = { INSUFFICIENT_BALANCE_FOR_ORDER_COST_LINEAR: 130080, SAME_SLTP_MODE_LINEAR: 130150, RISK_ID_NOT_MODIFIED: 134026, + CONTRACT_ORDER_NOT_EXISTS: 140001, + CONTRACT_INSUFFICIENT_BALANCE: 140007, + CONTRACT_POSITION_MODE_NOT_MODIFIED: 140025, + CONTRACT_MARGIN_MODE_NOT_MODIFIED: 140026, + CONTRACT_RISK_LIMIT_INFO_NOT_EXISTS: 140031, + CONTRACT_SET_LEVERAGE_NOT_MODIFIED: 140043, /** E.g. USDC Options trading, trying to access a symbol that is no longer active */ CONTRACT_NAME_NOT_EXIST: 3100111, ORDER_NOT_EXIST: 3100136, diff --git a/src/types/request/contract.ts b/src/types/request/contract.ts index 88828c5..125df40 100644 --- a/src/types/request/contract.ts +++ b/src/types/request/contract.ts @@ -68,7 +68,7 @@ export interface ContractPositionsRequest { export interface ContractSetAutoAddMarginRequest { symbol: string; - side: string; + side: 'Buy' | 'Sell'; autoAddMargin: 1 | 0; positionIdx?: 0 | 1 | 2; } diff --git a/test/contract/private.write.test.ts b/test/contract/private.write.test.ts new file mode 100644 index 0000000..9e42db3 --- /dev/null +++ b/test/contract/private.write.test.ts @@ -0,0 +1,135 @@ +import { API_ERROR_CODE, ContractClient } from '../../src'; +import { successResponseObjectV3 } from '../response.util'; + +describe('Private Contract REST API POST Endpoints', () => { + const API_KEY = process.env.API_KEY_COM; + const API_SECRET = process.env.API_SECRET_COM; + + it('should have api credentials to test with', () => { + expect(API_KEY).toStrictEqual(expect.any(String)); + expect(API_SECRET).toStrictEqual(expect.any(String)); + }); + + const api = new ContractClient({ + key: API_KEY, + secret: API_SECRET, + testnet: false, + }); + + const symbol = 'BTCUSDT'; + + /** + * While it may seem silly, these tests simply validate the request is processed at all. + * Something very wrong would be a sign error or complaints about the endpoint/request method/server error. + */ + + it('submitOrder()', async () => { + expect( + await api.submitOrder({ + symbol, + side: 'Sell', + orderType: 'Limit', + qty: '1', + price: '20000', + orderLinkId: Date.now().toString(), + timeInForce: 'GoodTillCancel', + }) + ).toMatchObject({ + retCode: API_ERROR_CODE.CONTRACT_INSUFFICIENT_BALANCE, + }); + }); + + it('cancelOrder()', async () => { + expect( + await api.cancelOrder({ + symbol, + orderId: 'somethingFake1', + }) + ).toMatchObject({ + retCode: API_ERROR_CODE.CONTRACT_ORDER_NOT_EXISTS, + }); + }); + + it('cancelAllOrders()', async () => { + expect(await api.cancelAllOrders(symbol)).toMatchObject( + successResponseObjectV3() + ); + }); + + it('modifyOrder()', async () => { + expect( + await api.modifyOrder({ + symbol, + orderId: 'somethingFake', + price: '20000', + }) + ).toMatchObject({ + retCode: API_ERROR_CODE.CONTRACT_ORDER_NOT_EXISTS, + }); + }); + + it('setAutoAddMargin()', async () => { + expect( + await api.setAutoAddMargin({ + autoAddMargin: 0, + side: 'Buy', + symbol, + }) + ).toMatchObject({ + retMsg: expect.stringMatching(/not modified/gim), + retCode: API_ERROR_CODE.PARAMS_MISSING_OR_WRONG, + }); + }); + + it('setMarginSwitch()', async () => { + expect( + await api.setMarginSwitch({ + symbol, + tradeMode: 0, + buyLeverage: '5', + sellLeverage: '5', + }) + ).toMatchObject({ + retCode: API_ERROR_CODE.CONTRACT_MARGIN_MODE_NOT_MODIFIED, + }); + }); + + it('setPositionMode()', async () => { + expect( + await api.setPositionMode({ + symbol, + mode: 0, + }) + ).toMatchObject({ + retCode: API_ERROR_CODE.CONTRACT_POSITION_MODE_NOT_MODIFIED, + }); + }); + + it('setTPSLMode()', async () => { + expect(await api.setTPSLMode(symbol, 'Full')).toMatchObject({ + retCode: API_ERROR_CODE.PARAMS_MISSING_OR_WRONG, + retMsg: expect.stringMatching(/same/gim), + }); + }); + + it('setLeverage()', async () => { + expect(await api.setLeverage(symbol, '5', '5')).toMatchObject({ + retCode: API_ERROR_CODE.CONTRACT_SET_LEVERAGE_NOT_MODIFIED, + }); + }); + + it('setTPSL()', async () => { + expect( + await api.setTPSL({ symbol, positionIdx: 0, stopLoss: '100' }) + ).toMatchObject({ + retMsg: expect.stringMatching(/zero position/gim), + retCode: API_ERROR_CODE.PARAMS_MISSING_OR_WRONG, + }); + }); + + it('setRiskLimit()', async () => { + expect(await api.setRiskLimit(symbol, 43, 0)).toMatchObject({ + retCode: API_ERROR_CODE.CONTRACT_RISK_LIMIT_INFO_NOT_EXISTS, + }); + }); +});