From bf602c8d588ecff98252d5da0e21a0207b1184d9 Mon Sep 17 00:00:00 2001 From: tiagosiebler Date: Tue, 21 Feb 2023 12:31:40 +0000 Subject: [PATCH] fix tests and incorrect v5 endpoint --- src/constants/enum.ts | 1 + src/rest-client-v5.ts | 2 +- src/util/BaseRestClient.ts | 12 +- test/rest-client-v5/private.read.test.ts | 4 +- test/rest-client-v5/private.write.test.ts | 152 ++++++++++++---------- test/spot/private.v3.read.test.ts | 3 +- 6 files changed, 94 insertions(+), 80 deletions(-) diff --git a/src/constants/enum.ts b/src/constants/enum.ts index 95d363f..8ea22a0 100644 --- a/src/constants/enum.ts +++ b/src/constants/enum.ts @@ -76,6 +76,7 @@ export const API_ERROR_CODE = { CONTRACT_RISK_LIMIT_INFO_NOT_EXISTS: 140031, CONTRACT_SET_LEVERAGE_NOT_MODIFIED: 140043, SUB_USER_NOT_FOUND: 141009, + SPOT_LEVERAGE_TOKEN_INSUFFICIENT_BALANCE: 175006, SPOT_LEVERAGE_TOKEN_ORDER_NOT_FOUND: 175007, SPOT_MARGIN_NOT_ENABLED: 176008, SPOT_MARGIN_QUESTIONNAIRE_NOT_SUBMIT: 176037, diff --git a/src/rest-client-v5.ts b/src/rest-client-v5.ts index 4063e44..58d4c0a 100644 --- a/src/rest-client-v5.ts +++ b/src/rest-client-v5.ts @@ -738,7 +738,7 @@ export class RestClientV5 extends BaseRestClient { * Once the mmp triggered, you can unfreeze the account via this endpoint */ resetMMP(baseCoin: string): Promise> { - return this.postPrivate('/v5/account/mmp-modify', { baseCoin }); + return this.postPrivate('/v5/account/mmp-reset', { baseCoin }); } /** diff --git a/src/util/BaseRestClient.ts b/src/util/BaseRestClient.ts index 708de7f..47051fb 100644 --- a/src/util/BaseRestClient.ts +++ b/src/util/BaseRestClient.ts @@ -62,7 +62,7 @@ interface UnsignedRequest { paramsWithSign: T; } -type SignMethod = 'keyInBody' | 'usdc'; +type SignMethod = 'v2auth' | 'v5auth'; export default abstract class BaseRestClient { private timeOffset: number | null = null; @@ -233,7 +233,7 @@ export default abstract class BaseRestClient { const signResult = await this.prepareSignParams( method, - 'usdc', + 'v5auth', params, isPublicApi ); @@ -245,11 +245,9 @@ export default abstract class BaseRestClient { options.headers['X-BAPI-RECV-WINDOW'] = signResult.recvWindow; if (method === 'GET') { - // const serialisedParams = signResult.serializedParams; return { ...options, params: signResult.originalParams, - // url: url + (serialisedParams ? '?' + serialisedParams : ''), }; } @@ -261,7 +259,7 @@ export default abstract class BaseRestClient { const signResult = await this.prepareSignParams( method, - 'keyInBody', + 'v2auth', params, isPublicApi ); @@ -376,7 +374,7 @@ export default abstract class BaseRestClient { res.recvWindow = recvWindow; // usdc is different for some reason - if (signMethod === 'usdc') { + if (signMethod === 'v5auth') { const sortProperties = false; const signRequestParams = method === 'GET' @@ -397,7 +395,7 @@ export default abstract class BaseRestClient { } // spot/v2 derivatives - if (signMethod === 'keyInBody') { + if (signMethod === 'v2auth') { res.originalParams.api_key = key; res.originalParams.timestamp = timestamp; diff --git a/test/rest-client-v5/private.read.test.ts b/test/rest-client-v5/private.read.test.ts index c2d5cb6..27fd537 100644 --- a/test/rest-client-v5/private.read.test.ts +++ b/test/rest-client-v5/private.read.test.ts @@ -258,9 +258,7 @@ describe('Private READ V5 REST API Endpoints', () => { 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, + ...successResponseObjectV3(), }); }); }); diff --git a/test/rest-client-v5/private.write.test.ts b/test/rest-client-v5/private.write.test.ts index 01d6ea3..8e56efc 100644 --- a/test/rest-client-v5/private.write.test.ts +++ b/test/rest-client-v5/private.write.test.ts @@ -1,5 +1,6 @@ import { API_ERROR_CODE, + LeverageTokenInfoV5, OrderSideV5, OrderTypeV5, RestClientV5, @@ -93,62 +94,74 @@ describe('Private WRITE V5 REST API Endpoints', () => { }); }); - it('batchSubmitOrders()', async () => { - expect( - await api.batchSubmitOrders('option', [ - { - orderLinkId: 'customOrderId1', - orderType: orderType, - qty: '1', - side: orderSide, - symbol: linearSymbol, - }, - { - orderLinkId: 'customOrderId2', - orderType: orderType, - qty: '2', - side: orderSide, - symbol: linearSymbol, - }, - ]) - ).toMatchObject({ - ...successResponseObjectV3(), + describe('options only methods', () => { + // These should use a real symbol from the options category + let optionsSymbol: string; + beforeAll(async () => { + const deliveryPriceResponse = await api.getOptionDeliveryPrice({ + category: 'option', + }); + const resultsList = deliveryPriceResponse.result.list; + optionsSymbol = resultsList[0].symbol; }); - }); - it('batchAmendOrders()', async () => { - expect( - await api.batchAmendOrders('option', [ - { - orderLinkId: 'customOrderId1', - qty: '3', - symbol: linearSymbol, - }, - { - orderLinkId: 'customOrderId2', - qty: '4', - symbol: linearSymbol, - }, - ]) - ).toMatchObject({ - ...successResponseObjectV3(), + it('batchSubmitOrders()', async () => { + expect( + await api.batchSubmitOrders('option', [ + { + orderLinkId: 'customOrderId1', + orderType: orderType, + qty: '1', + side: orderSide, + symbol: optionsSymbol, + }, + { + orderLinkId: 'customOrderId2', + orderType: orderType, + qty: '2', + side: orderSide, + symbol: optionsSymbol, + }, + ]) + ).toMatchObject({ + ...successResponseObjectV3(), + }); }); - }); - it('batchCancelOrders()', async () => { - expect( - await api.batchCancelOrders('option', [ - { - orderLinkId: 'customOrderId1', - symbol: linearSymbol, - }, - { - orderLinkId: 'customOrderId2', - symbol: linearSymbol, - }, - ]) - ).toMatchObject({ - ...successResponseObjectV3(), + it('batchAmendOrders()', async () => { + expect( + await api.batchAmendOrders('option', [ + { + orderLinkId: 'customOrderId1', + qty: '3', + symbol: optionsSymbol, + }, + { + orderLinkId: 'customOrderId2', + qty: '4', + symbol: optionsSymbol, + }, + ]) + ).toMatchObject({ + ...successResponseObjectV3(), + }); + }); + + it('batchCancelOrders()', async () => { + expect( + await api.batchCancelOrders('option', [ + { + orderLinkId: 'customOrderId1', + symbol: optionsSymbol, + }, + { + orderLinkId: 'customOrderId2', + symbol: optionsSymbol, + }, + ]) + ).toMatchObject({ + ...successResponseObjectV3(), + }); }); }); @@ -296,13 +309,11 @@ describe('Private WRITE V5 REST API Endpoints', () => { }); }); - it.skip('resetMMP()', async () => { + it('resetMMP()', async () => { expect(await api.resetMMP(settleCoin)).toMatchObject({ - ...successResponseObjectV3(), - retMsg: '', - retCode: 3500715, - // Contacted bybit for info - // + "retMsg": "Parameter window cannot be empty.", + // ...successResponseObjectV3(), + // retMsg: '', + retCode: API_ERROR_CODE.INSTITION_MMP_PROFILE_NOT_FOUND, }); }); }); @@ -397,18 +408,24 @@ describe('Private WRITE V5 REST API Endpoints', () => { }); }); - // Not currently working. In touch with bybit. - describe.skip('Spot Leverage Token APIs', () => { - it('purchaseSpotLeveragedToken()', async () => { + describe('Spot Leverage Token APIs', () => { + let leverageToken: LeverageTokenInfoV5; + + beforeAll(async () => { + const tokenResult = await api.getLeveragedTokenInfo(); + leverageToken = tokenResult.result.list[0]; + }); + + // Still failing - in contact with bybit + it.skip('purchaseSpotLeveragedToken()', async () => { expect( await api.purchaseSpotLeveragedToken({ ltAmount: '100', - ltCoin: 'EOS3L', - serialNo: 'purchase-001', + ltCoin: leverageToken.ltCoin, }) ).toMatchObject({ // ...successResponseObjectV3(), - retCode: 0, + retCode: API_ERROR_CODE.SPOT_LEVERAGE_TOKEN_INSUFFICIENT_BALANCE, retMsg: '', }); }); @@ -417,13 +434,12 @@ describe('Private WRITE V5 REST API Endpoints', () => { expect( await api.redeemSpotLeveragedToken({ quantity: '100', - ltCoin: 'EOS3L', - serialNo: 'redeem-001', + ltCoin: leverageToken.ltCoin, }) ).toMatchObject({ // ...successResponseObjectV3(), - retCode: 0, - retMsg: '', + retCode: API_ERROR_CODE.SPOT_LEVERAGE_TOKEN_INSUFFICIENT_BALANCE, + // retMsg: '', }); }); }); diff --git a/test/spot/private.v3.read.test.ts b/test/spot/private.v3.read.test.ts index ee59dc0..3994773 100644 --- a/test/spot/private.v3.read.test.ts +++ b/test/spot/private.v3.read.test.ts @@ -82,7 +82,8 @@ describe('Private Spot REST API GET Endpoints', () => { it('getLeveragedTokenPRHistory()', async () => { expect(await api.getLeveragedTokenPRHistory()).toMatchObject({ - retCode: API_ERROR_CODE.ORDER_NOT_FOUND_LEVERAGED_TOKEN, + ...successResponseObjectV3(), + // retCode: API_ERROR_CODE.ORDER_NOT_FOUND_LEVERAGED_TOKEN, }); });