From fc1e3945c0110b5235abd184e2911cce0ad2c9c4 Mon Sep 17 00:00:00 2001 From: tiagosiebler Date: Mon, 20 Feb 2023 12:25:54 +0000 Subject: [PATCH] misc fixes for v5 rest client. Add e2e private fetch tests for v5 rest client. --- src/constants/enum.ts | 1 + src/rest-client-v5.ts | 11 +- test/response.util.ts | 7 +- test/rest-client-v5/private.read.test.ts | 267 +++++++++++++++++++++++ 4 files changed, 279 insertions(+), 7 deletions(-) create mode 100644 test/rest-client-v5/private.read.test.ts diff --git a/src/constants/enum.ts b/src/constants/enum.ts index 58ba6ad..61bdd42 100644 --- a/src/constants/enum.ts +++ b/src/constants/enum.ts @@ -63,6 +63,7 @@ export const API_ERROR_CODE = { CONTRACT_MARGIN_MODE_NOT_MODIFIED: 140026, CONTRACT_RISK_LIMIT_INFO_NOT_EXISTS: 140031, CONTRACT_SET_LEVERAGE_NOT_MODIFIED: 140043, + SPOT_LEVERAGE_TOKEN_ORDER_NOT_FOUND: 175007, /** 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/rest-client-v5.ts b/src/rest-client-v5.ts index 29e0708..e3a4577 100644 --- a/src/rest-client-v5.ts +++ b/src/rest-client-v5.ts @@ -601,7 +601,7 @@ export class RestClientV5 extends BaseRestClient { * Unified account covers: Spot / Linear contract / Options * Normal account covers: USDT perpetual / Inverse perpetual / Inverse futures */ - getExecutionListV5( + getExecutionList( params: GetExecutionListParamsV5 ): Promise>> { return this.getPrivate('/v5/execution/list', params); @@ -764,7 +764,7 @@ export class RestClientV5 extends BaseRestClient { nextPageCursor?: string; }> > { - return this.get('/v5/asset/exchange/order-record', params); + return this.getPrivate('/v5/asset/exchange/order-record', params); } /** @@ -870,7 +870,7 @@ export class RestClientV5 extends BaseRestClient { * Query the internal transfer records between different account types under the same UID. */ getInternalTransferRecords( - params: GetInternalTransferParamsV5 + params?: GetInternalTransferParamsV5 ): Promise>> { return this.getPrivate( '/v5/asset/transfer/query-inter-transfer-list', @@ -1014,7 +1014,10 @@ export class RestClientV5 extends BaseRestClient { getCoinInfo( coin?: string ): Promise> { - return this.get('/v5/asset/coin/query-info', coin ? { coin } : undefined); + return this.getPrivate( + '/v5/asset/coin/query-info', + coin ? { coin } : undefined + ); } /** diff --git a/test/response.util.ts b/test/response.util.ts index 36adaec..9b24a7f 100644 --- a/test/response.util.ts +++ b/test/response.util.ts @@ -1,13 +1,14 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import { API_ERROR_CODE } from '../src'; -const SUCCESS_MSG_REGEX = /OK|SUCCESS|success|success\.|Request accepted|/gim; +export const SUCCESS_MSG_REGEX = + /OK|SUCCESS|success|success\.|Request accepted|/gim; export function successResponseList(successMsg: string | null = 'OK') { return { result: expect.any(Array), - ret_code: API_ERROR_CODE.SUCCESS, ret_msg: successMsg, + ret_code: API_ERROR_CODE.SUCCESS, }; } @@ -23,8 +24,8 @@ export function successResponseListV3() { export function successResponseObject() { return { result: expect.any(Object), - ret_code: API_ERROR_CODE.SUCCESS, ret_msg: expect.stringMatching(SUCCESS_MSG_REGEX), + ret_code: API_ERROR_CODE.SUCCESS, }; } diff --git a/test/rest-client-v5/private.read.test.ts b/test/rest-client-v5/private.read.test.ts new file mode 100644 index 0000000..d6009a8 --- /dev/null +++ b/test/rest-client-v5/private.read.test.ts @@ -0,0 +1,267 @@ +import { API_ERROR_CODE, RestClientV5 } from '../../src'; +import { SUCCESS_MSG_REGEX, successResponseObjectV3 } from '../response.util'; + +describe('Private V5 REST API 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 RestClientV5({ + key: API_KEY, + secret: API_SECRET, + testnet: false, + }); + + const settleCoin = 'USDT'; + const linearSymbol = 'BTCUSDT'; + + describe('misc endpoints', () => { + it('fetchServerTime()', async () => { + expect(await api.fetchServerTime()).toEqual(expect.any(Number)); + }); + }); + + describe('Trade APIs', () => { + it('getActiveOrders()', async () => { + expect( + await api.getActiveOrders({ category: 'linear', settleCoin }) + ).toMatchObject({ + ...successResponseObjectV3(), + }); + }); + + it('getHistoricOrders()', async () => { + expect(await api.getHistoricOrders({ category: 'linear' })).toMatchObject( + { ...successResponseObjectV3() } + ); + }); + + it('getSpotBorrowCheck()', async () => { + expect(await api.getSpotBorrowCheck(linearSymbol, 'Buy')).toMatchObject({ + ...successResponseObjectV3(), + }); + }); + }); + + describe('Position APIs', () => { + it('getPositionInfo()', async () => { + expect( + await api.getPositionInfo({ category: 'linear', settleCoin }) + ).toMatchObject({ + ...successResponseObjectV3(), + }); + }); + + it('getExecutionList()', async () => { + expect( + await api.getExecutionList({ category: 'linear', symbol: linearSymbol }) + ).toMatchObject({ + ...successResponseObjectV3(), + }); + }); + + it('getClosedPnL()', async () => { + expect( + await api.getClosedPnL({ category: 'linear', symbol: linearSymbol }) + ).toMatchObject({ + ...successResponseObjectV3(), + }); + }); + }); + + describe('Account APIs', () => { + it('getWalletBalance()', async () => { + expect( + await api.getWalletBalance({ accountType: 'CONTRACT' }) + ).toMatchObject({ ...successResponseObjectV3() }); + }); + + it('getBorrowHistory()', async () => { + expect(await api.getBorrowHistory()).toMatchObject({ + ...successResponseObjectV3(), + }); + }); + + it('getCollateralInfo()', async () => { + expect(await api.getCollateralInfo()).toMatchObject({ + ...successResponseObjectV3(), + }); + }); + + // Not available on this test account + it.skip('getCoinGreeks()', async () => { + expect(await api.getCoinGreeks()).toMatchObject({ + ...successResponseObjectV3(), + retMsg: '', + }); + }); + + it('getFeeRate()', async () => { + expect(await api.getFeeRate()).toMatchObject({ + ...successResponseObjectV3(), + }); + }); + + // Fails on this test account, since it's not upgraded + it.skip('getAccountInfo()', async () => { + expect(await api.getAccountInfo()).toMatchObject({ + ...successResponseObjectV3(), + retMsg: '', + }); + }); + + it('getTransactionLog()', async () => { + expect(await api.getTransactionLog()).toMatchObject({ + ...successResponseObjectV3(), + }); + }); + + // Not available on this test account + it.skip('getMMPState()', async () => { + expect(await api.getMMPState(settleCoin)).toMatchObject({ + ...successResponseObjectV3(), + retMsg: '', + }); + }); + }); + + describe('Asset APIs', () => { + it('getCoinExchangeRecords()', async () => { + expect(await api.getCoinExchangeRecords()).toMatchObject({ + ...successResponseObjectV3(), + }); + }); + + it('getDeliveryRecord()', async () => { + expect(await api.getDeliveryRecord({ category: 'option' })).toMatchObject( + { ...successResponseObjectV3() } + ); + }); + + it('getSettlementRecords()', async () => { + expect( + await api.getSettlementRecords({ category: 'linear' }) + ).toMatchObject({ ...successResponseObjectV3() }); + }); + + it('getAssetInfo()', async () => { + expect(await api.getAssetInfo({ accountType: 'SPOT' })).toMatchObject({ + ...successResponseObjectV3(), + }); + }); + + it('getAllCoinsBalance()', async () => { + expect( + await api.getAllCoinsBalance({ accountType: 'SPOT' }) + ).toMatchObject({ ...successResponseObjectV3() }); + }); + + it('getCoinBalance()', async () => { + expect( + await api.getCoinBalance({ accountType: 'SPOT', coin: settleCoin }) + ).toMatchObject({ ...successResponseObjectV3() }); + }); + + it('getTransferableCoinList()', async () => { + expect( + await api.getTransferableCoinList('SPOT', 'CONTRACT') + ).toMatchObject({ ...successResponseObjectV3() }); + }); + + it('getInternalTransferRecords()', async () => { + expect(await api.getInternalTransferRecords()).toMatchObject({ + ...successResponseObjectV3(), + }); + }); + + it('getSubUID()', async () => { + expect(await api.getSubUID()).toMatchObject({ + ...successResponseObjectV3(), + }); + }); + + it('getUniversalTransferRecords()', async () => { + expect(await api.getUniversalTransferRecords()).toMatchObject({ + ...successResponseObjectV3(), + }); + }); + + it('getAllowedDepositCoinInfo()', async () => { + expect(await api.getAllowedDepositCoinInfo()).toMatchObject({ + ...successResponseObjectV3(), + }); + }); + + it('getDepositRecords()', async () => { + expect(await api.getDepositRecords()).toMatchObject({ + ...successResponseObjectV3(), + }); + }); + + it('getSubAccountDepositRecords()', async () => { + expect( + await api.getSubAccountDepositRecords({ subMemberId: 'fakeid' }) + ).toMatchObject({ + // ...successResponseObjectV3(), + // Expected, since sub account ID is fake + retCode: API_ERROR_CODE.PARAMS_MISSING_OR_WRONG, + }); + }); + + it('getMasterDepositAddress()', async () => { + expect(await api.getMasterDepositAddress(settleCoin)).toMatchObject({ + ...successResponseObjectV3(), + }); + }); + + it('querySubMemberAddress()', async () => { + expect( + await api.querySubMemberAddress(settleCoin, 'TRC20', 'fakeid') + ).toMatchObject({ + // ...successResponseObjectV3(), + // Expected, since sub account ID is fake + retCode: API_ERROR_CODE.PARAMS_MISSING_OR_WRONG, + }); + }); + + it('getCoinInfo()', async () => { + expect(await api.getCoinInfo()).toMatchObject({ + ...successResponseObjectV3(), + }); + }); + + it('getWithdrawalRecords()', async () => { + expect(await api.getWithdrawalRecords()).toMatchObject({ + ...successResponseObjectV3(), + }); + }); + }); + + describe('User APIs', () => { + it('getSubUIDList()', async () => { + expect(await api.getSubUIDList()).toMatchObject({ + ...successResponseObjectV3(), + }); + }); + + it('getQueryApiKey()', async () => { + expect(await api.getQueryApiKey()).toMatchObject({ + ...successResponseObjectV3(), + }); + }); + }); + + describe('Spot Leverage Token APIs', () => { + it('getSpotLeveragedTokenOrderHistory()', async () => { + expect(await api.getSpotLeveragedTokenOrderHistory()).toMatchObject({ + // ...successResponseObjectV3(), + // retMsg: '', + retCode: API_ERROR_CODE.SPOT_LEVERAGE_TOKEN_ORDER_NOT_FOUND, + }); + }); + }); +});