add coverage for inverse futures private getter endpoints, migrate private getter methods to base class

This commit is contained in:
tiagosiebler
2022-05-06 22:06:32 +01:00
parent 749efad303
commit d11fbfa227
5 changed files with 164 additions and 57 deletions

View File

@@ -145,7 +145,7 @@ export class InverseClient extends BaseRestClient {
*/ */
getApiKeyInfo(): GenericAPIResponse { getApiKeyInfo(): GenericAPIResponse {
return this.requestWrapper.get('v2/private/account/api-key'); return this.getPrivate('v2/private/account/api-key');
} }
/** /**
@@ -155,21 +155,21 @@ export class InverseClient extends BaseRestClient {
*/ */
getWalletBalance(params?: Partial<CoinParam>): GenericAPIResponse { getWalletBalance(params?: Partial<CoinParam>): GenericAPIResponse {
return this.requestWrapper.get('v2/private/wallet/balance', params); return this.getPrivate('v2/private/wallet/balance', params);
} }
getWalletFundRecords(params?: WalletFundRecordsReq): GenericAPIResponse { getWalletFundRecords(params?: WalletFundRecordsReq): GenericAPIResponse {
return this.requestWrapper.get('v2/private/wallet/fund/records', params); return this.getPrivate('v2/private/wallet/fund/records', params);
} }
getWithdrawRecords(params?: WithdrawRecordsReq): GenericAPIResponse { getWithdrawRecords(params?: WithdrawRecordsReq): GenericAPIResponse {
return this.requestWrapper.get('v2/private/wallet/withdraw/list', params); return this.getPrivate('v2/private/wallet/withdraw/list', params);
} }
getAssetExchangeRecords( getAssetExchangeRecords(
params?: AssetExchangeRecordsReq params?: AssetExchangeRecordsReq
): GenericAPIResponse { ): GenericAPIResponse {
return this.requestWrapper.get('v2/private/exchange-order/list', params); return this.getPrivate('v2/private/exchange-order/list', params);
} }
/** /**
@@ -221,7 +221,7 @@ export class InverseClient extends BaseRestClient {
limit?: number; limit?: number;
cursor?: string; cursor?: string;
}): GenericAPIResponse { }): GenericAPIResponse {
return this.requestWrapper.get('v2/private/order/list', params); return this.getPrivate('v2/private/order/list', params);
} }
cancelActiveOrder(params: { cancelActiveOrder(params: {
@@ -255,7 +255,7 @@ export class InverseClient extends BaseRestClient {
order_link_id?: string; order_link_id?: string;
symbol: string; symbol: string;
}): GenericAPIResponse { }): GenericAPIResponse {
return this.requestWrapper.get('v2/private/order', params); return this.getPrivate('v2/private/order', params);
} }
/** /**
@@ -286,7 +286,7 @@ export class InverseClient extends BaseRestClient {
limit?: number; limit?: number;
cursor?: string; cursor?: string;
}): GenericAPIResponse { }): GenericAPIResponse {
return this.requestWrapper.get('v2/private/stop-order/list', params); return this.getPrivate('v2/private/stop-order/list', params);
} }
cancelConditionalOrder(params: { cancelConditionalOrder(params: {
@@ -317,7 +317,7 @@ export class InverseClient extends BaseRestClient {
stop_order_id?: string; stop_order_id?: string;
order_link_id?: string; order_link_id?: string;
}): GenericAPIResponse { }): GenericAPIResponse {
return this.requestWrapper.get('v2/private/stop-order', params); return this.getPrivate('v2/private/stop-order', params);
} }
/** /**
@@ -325,7 +325,7 @@ export class InverseClient extends BaseRestClient {
*/ */
getPosition(params?: Partial<SymbolParam>): GenericAPIResponse { getPosition(params?: Partial<SymbolParam>): GenericAPIResponse {
return this.requestWrapper.get('v2/private/position/list', params); return this.getPrivate('v2/private/position/list', params);
} }
changePositionMargin(params: { changePositionMargin(params: {
@@ -366,7 +366,7 @@ export class InverseClient extends BaseRestClient {
limit?: number; limit?: number;
order?: string; order?: string;
}): GenericAPIResponse { }): GenericAPIResponse {
return this.requestWrapper.get('v2/private/execution/list', params); return this.getPrivate('v2/private/execution/list', params);
} }
getClosedPnl(params: { getClosedPnl(params: {
@@ -377,7 +377,7 @@ export class InverseClient extends BaseRestClient {
page?: number; page?: number;
limit?: number; limit?: number;
}): GenericAPIResponse { }): GenericAPIResponse {
return this.requestWrapper.get('v2/private/trade/closed-pnl/list', params); return this.getPrivate('v2/private/trade/closed-pnl/list', params);
} }
setPositionMode(params: { symbol: string; mode: 0 | 3 }): GenericAPIResponse { setPositionMode(params: { symbol: string; mode: 0 | 3 }): GenericAPIResponse {
@@ -427,14 +427,11 @@ export class InverseClient extends BaseRestClient {
} }
getMyLastFundingFee(params: SymbolParam): GenericAPIResponse { getMyLastFundingFee(params: SymbolParam): GenericAPIResponse {
return this.requestWrapper.get('v2/private/funding/prev-funding', params); return this.getPrivate('v2/private/funding/prev-funding', params);
} }
getPredictedFunding(params: SymbolParam): GenericAPIResponse { getPredictedFunding(params: SymbolParam): GenericAPIResponse {
return this.requestWrapper.get( return this.getPrivate('v2/private/funding/predicted-funding', params);
'v2/private/funding/predicted-funding',
params
);
} }
/** /**
@@ -442,6 +439,6 @@ export class InverseClient extends BaseRestClient {
*/ */
getLcpInfo(params: SymbolParam): GenericAPIResponse { getLcpInfo(params: SymbolParam): GenericAPIResponse {
return this.requestWrapper.get('v2/private/account/lcp', params); return this.getPrivate('v2/private/account/lcp', params);
} }
} }

View File

@@ -6,11 +6,9 @@ import {
} from './util/requestUtils'; } from './util/requestUtils';
import RequestWrapper from './util/requestWrapper'; import RequestWrapper from './util/requestWrapper';
import { import {
APIResponse,
APIResponseWithTime, APIResponseWithTime,
AssetExchangeRecordsReq, AssetExchangeRecordsReq,
CoinParam, CoinParam,
SymbolFromLimitParam,
SymbolInfo, SymbolInfo,
SymbolIntervalFromLimitParam, SymbolIntervalFromLimitParam,
SymbolLimitParam, SymbolLimitParam,
@@ -148,7 +146,7 @@ export class InverseFuturesClient extends BaseRestClient {
*/ */
getApiKeyInfo(): GenericAPIResponse { getApiKeyInfo(): GenericAPIResponse {
return this.requestWrapper.get('v2/private/account/api-key'); return this.getPrivate('v2/private/account/api-key');
} }
/** /**
@@ -158,21 +156,21 @@ export class InverseFuturesClient extends BaseRestClient {
*/ */
getWalletBalance(params?: Partial<CoinParam>): GenericAPIResponse { getWalletBalance(params?: Partial<CoinParam>): GenericAPIResponse {
return this.requestWrapper.get('v2/private/wallet/balance', params); return this.getPrivate('v2/private/wallet/balance', params);
} }
getWalletFundRecords(params?: WalletFundRecordsReq): GenericAPIResponse { getWalletFundRecords(params?: WalletFundRecordsReq): GenericAPIResponse {
return this.requestWrapper.get('v2/private/wallet/fund/records', params); return this.getPrivate('v2/private/wallet/fund/records', params);
} }
getWithdrawRecords(params: WithdrawRecordsReq): GenericAPIResponse { getWithdrawRecords(params?: WithdrawRecordsReq): GenericAPIResponse {
return this.requestWrapper.get('v2/private/wallet/withdraw/list', params); return this.getPrivate('v2/private/wallet/withdraw/list', params);
} }
getAssetExchangeRecords( getAssetExchangeRecords(
params?: AssetExchangeRecordsReq params?: AssetExchangeRecordsReq
): GenericAPIResponse { ): GenericAPIResponse {
return this.requestWrapper.get('v2/private/exchange-order/list', params); return this.getPrivate('v2/private/exchange-order/list', params);
} }
/** /**
@@ -225,7 +223,7 @@ export class InverseFuturesClient extends BaseRestClient {
limit?: number; limit?: number;
cursor?: string; cursor?: string;
}): GenericAPIResponse { }): GenericAPIResponse {
return this.requestWrapper.get('futures/private/order/list', params); return this.getPrivate('futures/private/order/list', params);
} }
cancelActiveOrder(params: { cancelActiveOrder(params: {
@@ -255,7 +253,7 @@ export class InverseFuturesClient extends BaseRestClient {
order_link_id?: string; order_link_id?: string;
symbol: string; symbol: string;
}): GenericAPIResponse { }): GenericAPIResponse {
return this.requestWrapper.get('futures/private/order', params); return this.getPrivate('futures/private/order', params);
} }
/** /**
@@ -288,7 +286,7 @@ export class InverseFuturesClient extends BaseRestClient {
limit?: number; limit?: number;
cursor?: string; cursor?: string;
}): GenericAPIResponse { }): GenericAPIResponse {
return this.requestWrapper.get('futures/private/stop-order/list', params); return this.getPrivate('futures/private/stop-order/list', params);
} }
cancelConditionalOrder(params: { cancelConditionalOrder(params: {
@@ -328,7 +326,7 @@ export class InverseFuturesClient extends BaseRestClient {
stop_order_id?: string; stop_order_id?: string;
order_link_id?: string; order_link_id?: string;
}): GenericAPIResponse { }): GenericAPIResponse {
return this.requestWrapper.get('futures/private/stop-order', params); return this.getPrivate('futures/private/stop-order', params);
} }
/** /**
@@ -339,7 +337,7 @@ export class InverseFuturesClient extends BaseRestClient {
* Get position list * Get position list
*/ */
getPosition(params?: Partial<SymbolParam>): GenericAPIResponse { getPosition(params?: Partial<SymbolParam>): GenericAPIResponse {
return this.requestWrapper.get('futures/private/position/list', params); return this.getPrivate('futures/private/position/list', params);
} }
changePositionMargin(params: { changePositionMargin(params: {
@@ -414,7 +412,7 @@ export class InverseFuturesClient extends BaseRestClient {
limit?: number; limit?: number;
order?: string; order?: string;
}): GenericAPIResponse { }): GenericAPIResponse {
return this.requestWrapper.get('futures/private/execution/list', params); return this.getPrivate('futures/private/execution/list', params);
} }
getClosedPnl(params: { getClosedPnl(params: {
@@ -425,10 +423,7 @@ export class InverseFuturesClient extends BaseRestClient {
page?: number; page?: number;
limit?: number; limit?: number;
}): GenericAPIResponse { }): GenericAPIResponse {
return this.requestWrapper.get( return this.getPrivate('futures/private/trade/closed-pnl/list', params);
'futures/private/trade/closed-pnl/list',
params
);
} }
/** /**
@@ -439,7 +434,7 @@ export class InverseFuturesClient extends BaseRestClient {
* Risk Limit * Risk Limit
*/ */
getRiskLimitList(): GenericAPIResponse { getRiskLimitList(): GenericAPIResponse {
return this.requestWrapper.get('open-api/wallet/risk-limit/list'); return this.getPrivate('open-api/wallet/risk-limit/list');
} }
setRiskLimit(params: { setRiskLimit(params: {
@@ -454,21 +449,15 @@ export class InverseFuturesClient extends BaseRestClient {
*/ */
getLastFundingRate(params: SymbolParam): GenericAPIResponse { getLastFundingRate(params: SymbolParam): GenericAPIResponse {
return this.requestWrapper.get( return this.get('v2/public/funding/prev-funding-rate', params);
'v2/public/funding/prev-funding-rate',
params
);
} }
getMyLastFundingFee(params: SymbolParam): GenericAPIResponse { getMyLastFundingFee(params: SymbolParam): GenericAPIResponse {
return this.requestWrapper.get('v2/private/funding/prev-funding', params); return this.getPrivate('v2/private/funding/prev-funding', params);
} }
getPredictedFunding(params: SymbolParam): GenericAPIResponse { getPredictedFunding(params: SymbolParam): GenericAPIResponse {
return this.requestWrapper.get( return this.getPrivate('v2/private/funding/predicted-funding', params);
'v2/private/funding/predicted-funding',
params
);
} }
/** /**
@@ -476,6 +465,6 @@ export class InverseFuturesClient extends BaseRestClient {
*/ */
getLcpInfo(params: SymbolParam): GenericAPIResponse { getLcpInfo(params: SymbolParam): GenericAPIResponse {
return this.requestWrapper.get('v2/private/account/lcp', params); return this.getPrivate('v2/private/account/lcp', params);
} }
} }

View File

@@ -1,7 +1,8 @@
import { createHmac } from 'crypto'; import { createHmac } from 'crypto';
export async function signMessage(message: string, secret: string): Promise<string> { export async function signMessage(
return createHmac('sha256', secret) message: string,
.update(message) secret: string
.digest('hex'); ): Promise<string> {
}; return createHmac('sha256', secret).update(message).digest('hex');
}

View File

@@ -59,11 +59,18 @@ export function getRestBaseUrl(
} }
export function isPublicEndpoint(endpoint: string): boolean { export function isPublicEndpoint(endpoint: string): boolean {
if (endpoint.startsWith('v2/public')) { const publicPrefixes = [
'v2/public',
'public/linear',
'spot/quote/v1',
'spot/v1/symbols',
'spot/v1/time',
];
for (const prefix of publicPrefixes) {
if (endpoint.startsWith(prefix)) {
return true; return true;
} }
if (endpoint.startsWith('public/linear')) {
return true;
} }
return false; return false;
} }

View File

@@ -0,0 +1,113 @@
import { InverseFuturesClient } from '../../src/inverse-futures-client';
import {
notAuthenticatedError,
successResponseList,
successResponseObject,
} from '../response.util';
describe('Public Inverse Futures REST API Endpoints', () => {
const useLivenet = true;
const API_KEY = process.env.API_KEY_COM;
const API_SECRET = process.env.API_SECRET_COM;
const api = new InverseFuturesClient(API_KEY, API_SECRET, useLivenet, {
disable_time_sync: true,
});
// Warning: if some of these start to fail with 10001 params error, it's probably that this future expired and a newer one exists with a different symbol!
const symbol = 'BTCUSDU22';
describe('Inverse-Futures only private GET endpoints', () => {
it('getApiKeyInfo()', async () => {
expect(await api.getApiKeyInfo()).toMatchObject(successResponseObject());
});
it('getWalletBalance()', async () => {
expect(await api.getWalletBalance()).toMatchObject(
successResponseObject()
);
});
it('getWalletFundRecords()', async () => {
expect(await api.getWalletFundRecords()).toMatchObject(
successResponseObject()
);
});
it('getWithdrawRecords()', async () => {
expect(await api.getWithdrawRecords()).toMatchObject(
successResponseObject()
);
});
it('getAssetExchangeRecords()', async () => {
expect(await api.getAssetExchangeRecords()).toMatchObject(
successResponseList()
);
});
it('getActiveOrderList()', async () => {
expect(await api.getActiveOrderList({ symbol: symbol })).toMatchObject(
successResponseObject()
);
});
it('queryActiveOrder()', async () => {
expect(await api.queryActiveOrder({ symbol: symbol })).toMatchObject(
successResponseObject()
);
});
it('getConditionalOrder()', async () => {
expect(await api.getConditionalOrder({ symbol: symbol })).toMatchObject(
successResponseObject()
);
});
it('queryConditionalOrder()', async () => {
expect(await api.queryConditionalOrder({ symbol: symbol })).toMatchObject(
successResponseObject()
);
});
it('getPosition()', async () => {
expect(await api.getPosition()).toMatchObject(successResponseObject());
});
it('getTradeRecords()', async () => {
expect(await api.getTradeRecords({ symbol: symbol })).toMatchObject(
successResponseObject()
);
});
it('getClosedPnl()', async () => {
expect(await api.getClosedPnl({ symbol: symbol })).toMatchObject(
successResponseObject()
);
});
it('getRiskLimitList()', async () => {
expect(await api.getRiskLimitList()).toMatchObject(
successResponseList('ok')
);
});
it('getMyLastFundingFee()', async () => {
expect(await api.getMyLastFundingFee({ symbol: symbol })).toMatchObject(
successResponseObject()
);
});
it('getPredictedFunding()', async () => {
expect(await api.getPredictedFunding({ symbol: 'BTCUSD' })).toMatchObject(
successResponseObject()
);
});
it('getLcpInfo()', async () => {
expect(await api.getLcpInfo({ symbol: symbol })).toMatchObject(
successResponseObject()
);
});
});
});