add USDC perp client with tests

This commit is contained in:
tiagosiebler
2022-09-08 16:48:33 +01:00
parent 666720b27d
commit 5187350878
13 changed files with 583 additions and 37 deletions

View File

@@ -4,6 +4,7 @@ export * from './inverse-futures-client';
export * from './linear-client'; export * from './linear-client';
export * from './spot-client'; export * from './spot-client';
export * from './usdc-options-client'; export * from './usdc-options-client';
export * from './usdc-perpetual-client';
export * from './websocket-client'; export * from './websocket-client';
export * from './logger'; export * from './logger';
export * from './types'; export * from './types';

View File

@@ -1,2 +1,3 @@
export * from './account-asset'; export * from './account-asset';
export * from './usdt-perp'; export * from './usdt-perp';
export * from './usdc-perp';

View File

@@ -0,0 +1,19 @@
export interface USDCKlineRequest {
symbol: string;
period: string;
startTime: number;
limit?: string;
}
export interface USDCOpenInterestRequest {
symbol: string;
period: string;
limit?: number;
}
export interface USDCLast500TradesRequest {
category: string;
symbol?: string;
baseCoin?: string;
limit?: string;
}

View File

@@ -25,6 +25,12 @@ export interface APIResponse<T> {
result: T; result: T;
} }
export interface USDCAPIResponse<T> {
retCode: number;
retMsg: 'OK' | string;
result: T;
}
export interface APIResponseWithTime<T = {}> extends APIResponse<T> { export interface APIResponseWithTime<T = {}> extends APIResponse<T> {
/** UTC timestamp */ /** UTC timestamp */
time_now: numberInString; time_now: numberInString;
@@ -37,15 +43,15 @@ export interface SymbolParam {
symbol: string; symbol: string;
} }
export interface SymbolLimitParam { export interface SymbolLimitParam<TLimit = number> {
symbol: string; symbol: string;
limit?: number; limit?: TLimit;
} }
export interface SymbolPeriodLimitParam { export interface SymbolPeriodLimitParam<TLimit = number> {
symbol: string; symbol: string;
period: string; period: string;
limit?: number; limit?: TLimit;
} }
export interface SymbolFromLimitParam { export interface SymbolFromLimitParam {

View File

@@ -7,7 +7,7 @@ import BaseRestClient from './util/BaseRestClient';
*/ */
export class USDCOptionsClient extends BaseRestClient { export class USDCOptionsClient extends BaseRestClient {
getClientType() { getClientType() {
return REST_CLIENT_TYPE_ENUM.usdcOptions; return REST_CLIENT_TYPE_ENUM.usdc;
} }
async fetchServerTime(): Promise<number> { async fetchServerTime(): Promise<number> {

View File

@@ -0,0 +1,287 @@
import {
APIResponseWithTime,
SymbolLimitParam,
SymbolPeriodLimitParam,
USDCAPIResponse,
USDCKlineRequest,
USDCLast500TradesRequest,
USDCOpenInterestRequest,
} from './types';
import { REST_CLIENT_TYPE_ENUM } from './util';
import BaseRestClient from './util/BaseRestClient';
/**
* REST API client for USDC Perpetual APIs
*/
export class USDCPerpetualClient extends BaseRestClient {
getClientType() {
return REST_CLIENT_TYPE_ENUM.usdc;
}
async fetchServerTime(): Promise<number> {
const res = await this.getServerTime();
return Number(res.time_now);
}
/**
*
* Market Data Endpoints
*
*/
getOrderBook(symbol: string): Promise<USDCAPIResponse<any>> {
return this.get('/perpetual/usdc/openapi/public/v1/order-book', { symbol });
}
/** Fetch trading rules (such as min/max qty). Query for all if blank. */
getContractInfo(params?: unknown): Promise<USDCAPIResponse<any>> {
return this.get('/perpetual/usdc/openapi/public/v1/symbols', params);
}
/** Get a symbol price/statistics ticker */
getSymbolTicker(symbol: string): Promise<USDCAPIResponse<any>> {
return this.get('/perpetual/usdc/openapi/public/v1/tick', { symbol });
}
getKline(params: USDCKlineRequest): Promise<USDCAPIResponse<any>> {
return this.get('/perpetual/usdc/openapi/public/v1/kline/list', params);
}
getMarkPrice(params: USDCKlineRequest): Promise<USDCAPIResponse<any>> {
return this.get(
'/perpetual/usdc/openapi/public/v1/mark-price-kline',
params
);
}
getIndexPrice(params: USDCKlineRequest): Promise<USDCAPIResponse<any>> {
return this.get(
'/perpetual/usdc/openapi/public/v1/index-price-kline',
params
);
}
getIndexPremium(params: USDCKlineRequest): Promise<USDCAPIResponse<any>> {
return this.get(
'/perpetual/usdc/openapi/public/v1/premium-index-kline',
params
);
}
getOpenInterest(
params: USDCOpenInterestRequest
): Promise<USDCAPIResponse<any>> {
return this.get('/perpetual/usdc/openapi/public/v1/open-interest', params);
}
getLargeOrders(
params: SymbolLimitParam<string>
): Promise<USDCAPIResponse<any>> {
return this.get('/perpetual/usdc/openapi/public/v1/big-deal', params);
}
getLongShortRatio(
params: SymbolPeriodLimitParam<string>
): Promise<USDCAPIResponse<any>> {
return this.get('/perpetual/usdc/openapi/public/v1/account-ratio', params);
}
getLast500Trades(
params: USDCLast500TradesRequest
): Promise<USDCAPIResponse<any>> {
return this.get(
'/option/usdc/openapi/public/v1/query-trade-latest',
params
);
}
/**
*
* Account Data Endpoints
*
*/
/** -> Order API */
/**
* Place an order using the USDC Derivatives Account.
* The request status can be queried in real-time.
* The response parameters must be queried through a query or a WebSocket response.
*/
submitOrder(params: unknown): Promise<USDCAPIResponse<any>> {
return this.postPrivate(
'/perpetual/usdc/openapi/private/v1/place-order',
params
);
}
/** Active order parameters (such as quantity, price) and stop order parameters cannot be modified in one request at the same time. Please request modification separately. */
modifyOrder(params: unknown): Promise<USDCAPIResponse<any>> {
return this.postPrivate(
'/perpetual/usdc/openapi/private/v1/replace-order',
params
);
}
/** Cancel order */
cancelOrder(params: unknown): Promise<USDCAPIResponse<any>> {
return this.postPrivate(
'/perpetual/usdc/openapi/private/v1/cancel-order',
params
);
}
/** Cancel all active orders. The real-time response indicates whether the request is successful, depending on retCode. */
cancelActiveOrders(
symbol: string,
orderFilter: string
): Promise<USDCAPIResponse<any>> {
return this.postPrivate('/perpetual/usdc/openapi/private/v1/cancel-all', {
symbol,
orderFilter,
});
}
/** Query Unfilled/Partially Filled Orders */
getActiveOrders(params: unknown): Promise<USDCAPIResponse<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/query-active-orders',
params
);
}
/** Query order history. The endpoint only supports up to 30 days of queried records */
getHistoricOrders(params: unknown): Promise<USDCAPIResponse<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/query-order-history',
params
);
}
/** Query trade history. The endpoint only supports up to 30 days of queried records. An error will be returned if startTime is more than 30 days. */
getOrderExecutionHistory(params: unknown): Promise<USDCAPIResponse<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/execution-list',
params
);
}
/** -> Account API */
/** The endpoint only supports up to 30 days of queried records. An error will be returned if startTime is more than 30 days. */
getTransactionLog(params: unknown): Promise<USDCAPIResponse<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/query-transaction-log',
params
);
}
/** Wallet info for USDC account. */
getBalance(params?: unknown): Promise<USDCAPIResponse<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/query-wallet-balance',
params
);
}
/** Asset Info */
getAssetInfo(baseCoin?: string): Promise<USDCAPIResponse<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/query-asset-info',
{ baseCoin }
);
}
/**
* If USDC derivatives account balance is greater than X, you can open PORTFOLIO_MARGIN, and if it is less than Y, it will automatically close PORTFOLIO_MARGIN and change back to REGULAR_MARGIN. X and Y will be adjusted according to operational requirements.
* Rest API returns the result of checking prerequisites. You could get the real status of margin mode change by subscribing margin mode.
*/
setMarginMode(
newMarginMode: 'REGULAR_MARGIN' | 'PORTFOLIO_MARGIN'
): Promise<USDCAPIResponse<any>> {
return this.postPrivate(
'/option/usdc/private/asset/account/setMarginMode',
{ setMarginMode: newMarginMode }
);
}
/** Query margin mode for USDC account. */
getMarginMode(): Promise<USDCAPIResponse<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/query-margin-info'
);
}
/** -> Positions API */
/** Query my positions */
getPositions(params: unknown): Promise<USDCAPIResponse<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/query-position',
params
);
}
/** Only for REGULAR_MARGIN */
setLeverage(symbol: string, leverage: string): Promise<USDCAPIResponse<any>> {
return this.postPrivate(
'/perpetual/usdc/openapi/private/v1/position/leverage/save',
{ symbol, leverage }
);
}
/** Query Settlement History */
getSettlementHistory(params?: unknown): Promise<USDCAPIResponse<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/session-settlement',
params
);
}
/** -> Risk Limit API */
/** Query risk limit */
getRiskLimit(symbol: string): Promise<USDCAPIResponse<any>> {
return this.getPrivate(
'/perpetual/usdc/openapi/public/v1/risk-limit/list',
{
symbol,
}
);
}
/** Set risk limit */
setRiskLimit(symbol: string, riskId: number): Promise<USDCAPIResponse<any>> {
return this.postPrivate(
'/perpetual/usdc/openapi/private/v1/position/set-risk-limit',
{ symbol, riskId }
);
}
/** -> Funding API */
/** Funding settlement occurs every 8 hours at 00:00 UTC, 08:00 UTC and 16:00 UTC. The current interval's fund fee settlement is based on the previous interval's fund rate. For example, at 16:00, the settlement is based on the fund rate generated at 8:00. The fund rate generated at 16:00 will be used at 0:00 the next day. */
getLastFundingRate(symbol: string): Promise<USDCAPIResponse<any>> {
return this.get('/perpetual/usdc/openapi/public/v1/prev-funding-rate', {
symbol,
});
}
/** Get predicted funding rate and my predicted funding fee */
getPredictedFundingRate(symbol: string): Promise<USDCAPIResponse<any>> {
return this.postPrivate(
'/perpetual/usdc/openapi/private/v1/predicted-funding',
{ symbol }
);
}
/**
*
* API Data Endpoints
*
*/
getServerTime(): Promise<APIResponseWithTime> {
return this.get('/v2/public/time');
}
}

View File

@@ -124,7 +124,7 @@ export default abstract class BaseRestClient {
} }
private isUSDCClient() { private isUSDCClient() {
return this.clientType === REST_CLIENT_TYPE_ENUM.usdcOptions; return this.clientType === REST_CLIENT_TYPE_ENUM.usdc;
} }
get(endpoint: string, params?: any) { get(endpoint: string, params?: any) {

View File

@@ -61,23 +61,6 @@ export function getRestBaseUrl(
return exchangeBaseUrls.testnet; return exchangeBaseUrls.testnet;
} }
export function isPublicEndpoint(endpoint: string): boolean {
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 false;
}
export function isWsPong(response: any) { export function isWsPong(response: any) {
if (response.pong || response.ping) { if (response.pong || response.ping) {
return true; return true;
@@ -101,7 +84,7 @@ export const REST_CLIENT_TYPE_ENUM = {
inverseFutures: 'inverseFutures', inverseFutures: 'inverseFutures',
linear: 'linear', linear: 'linear',
spot: 'spot', spot: 'spot',
usdcOptions: 'usdcOptions', usdc: 'usdc',
} as const; } as const;
export type RestClientType = export type RestClientType =

View File

@@ -19,9 +19,15 @@ export function successResponseObject(successMsg: string | null = 'OK') {
export function successUSDCResponseObject() { export function successUSDCResponseObject() {
return { return {
result: expect.any(Object), result: expect.any(Object),
...successUSDCEmptyResponseObject(),
};
}
export function successUSDCEmptyResponseObject() {
return {
retCode: API_ERROR_CODE.SUCCESS, retCode: API_ERROR_CODE.SUCCESS,
retMsg: expect.stringMatching( retMsg: expect.stringMatching(
/OK|SUCCESS|success|success\.|Request accepted/gim /OK|SUCCESS|success|success\.|Request accepted|/gim
), ),
}; };
} }

View File

@@ -1,8 +1,5 @@
import { API_ERROR_CODE, USDCOptionsClient } from '../../../src'; import { API_ERROR_CODE, USDCOptionsClient } from '../../../src';
import { import { successUSDCResponseObject } from '../../response.util';
successResponseObject,
successUSDCResponseObject,
} from '../../response.util';
describe('Private Account Asset REST API Endpoints', () => { describe('Private Account Asset REST API Endpoints', () => {
const useLivenet = true; const useLivenet = true;
@@ -16,7 +13,6 @@ describe('Private Account Asset REST API Endpoints', () => {
const api = new USDCOptionsClient(API_KEY, API_SECRET, useLivenet); const api = new USDCOptionsClient(API_KEY, API_SECRET, useLivenet);
const category = 'OPTION';
const currency = 'USDC'; const currency = 'USDC';
const symbol = 'BTC-30SEP22-400000-C'; const symbol = 'BTC-30SEP22-400000-C';
@@ -159,11 +155,4 @@ describe('Private Account Asset REST API Endpoints', () => {
retCode: API_ERROR_CODE.INSTITION_MMP_PROFILE_NOT_FOUND, retCode: API_ERROR_CODE.INSTITION_MMP_PROFILE_NOT_FOUND,
}); });
}); });
/**
it('asdfasfasdfasdf()', async () => {
expect(await api.asadfasdfasdfasf()).toStrictEqual('');
});
*/
}); });

View File

@@ -0,0 +1,74 @@
import { USDCPerpetualClient } from '../../../src';
import { successUSDCResponseObject } from '../../response.util';
describe('Private Account Asset REST API Endpoints', () => {
const useLivenet = true;
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 USDCPerpetualClient(API_KEY, API_SECRET, useLivenet);
const symbol = 'BTCPERP';
const category = 'PERPETUAL';
it('getActiveOrders()', async () => {
expect(await api.getActiveOrders({ category })).toMatchObject(
successUSDCResponseObject()
);
});
it('getHistoricOrders()', async () => {
expect(await api.getHistoricOrders({ category })).toMatchObject(
successUSDCResponseObject()
);
});
it('getOrderExecutionHistory()', async () => {
expect(await api.getOrderExecutionHistory({ category })).toMatchObject(
successUSDCResponseObject()
);
});
it('getTransactionLog()', async () => {
expect(await api.getTransactionLog({ type: 'TRADE' })).toMatchObject(
successUSDCResponseObject()
);
});
it('getBalance()', async () => {
expect(await api.getBalance()).toMatchObject(successUSDCResponseObject());
});
it('getAssetInfo()', async () => {
expect(await api.getAssetInfo()).toMatchObject(successUSDCResponseObject());
});
it('getMarginMode()', async () => {
expect(await api.getMarginMode()).toMatchObject(
successUSDCResponseObject()
);
});
it('getPositions()', async () => {
expect(await api.getPositions({ category })).toMatchObject(
successUSDCResponseObject()
);
});
it('getSettlementHistory()', async () => {
expect(await api.getSettlementHistory({ symbol })).toMatchObject(
successUSDCResponseObject()
);
});
it('getPredictedFundingRate()', async () => {
expect(await api.getPredictedFundingRate(symbol)).toMatchObject(
successUSDCResponseObject()
);
});
});

View File

@@ -0,0 +1,85 @@
import { API_ERROR_CODE, USDCPerpetualClient } from '../../../src';
import {
successUSDCEmptyResponseObject,
successUSDCResponseObject,
} from '../../response.util';
describe('Private Account Asset REST API Endpoints', () => {
const useLivenet = true;
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 USDCPerpetualClient(API_KEY, API_SECRET, useLivenet);
const symbol = 'BTCPERP';
it('submitOrder()', async () => {
expect(
await api.submitOrder({
symbol,
side: 'Sell',
orderType: 'Limit',
orderFilter: 'Order',
orderQty: '1',
orderPrice: '20000',
orderLinkId: Date.now().toString(),
timeInForce: 'GoodTillCancel',
})
).toMatchObject({
retCode: API_ERROR_CODE.INSUFFICIENT_BALANCE_FOR_ORDER_COST,
});
});
it('modifyOrder()', async () => {
expect(
await api.modifyOrder({
symbol,
orderId: 'somethingFake',
orderPrice: '20000',
})
).toMatchObject({
retCode: API_ERROR_CODE.ORDER_NOT_FOUND_OR_TOO_LATE,
});
});
it('cancelOrder()', async () => {
expect(
await api.cancelOrder({
symbol,
orderId: 'somethingFake1',
orderFilter: 'Order',
})
).toMatchObject({
retCode: API_ERROR_CODE.ORDER_NOT_FOUND_OR_TOO_LATE,
});
});
it('cancelActiveOrders()', async () => {
expect(await api.cancelActiveOrders(symbol, 'Order')).toMatchObject(
successUSDCEmptyResponseObject()
);
});
it('setMarginMode()', async () => {
expect(await api.setMarginMode('REGULAR_MARGIN')).toMatchObject(
successUSDCResponseObject()
);
});
it('setLeverage()', async () => {
expect(await api.setLeverage(symbol, '10')).toMatchObject({
retCode: API_ERROR_CODE.LEVERAGE_NOT_MODIFIED,
});
});
it('setRiskLimit()', async () => {
expect(await api.setRiskLimit(symbol, 1)).toMatchObject({
retCode: API_ERROR_CODE.RISK_LIMIT_NOT_EXISTS,
});
});
});

View File

@@ -0,0 +1,95 @@
import { USDCKlineRequest, USDCPerpetualClient } from '../../../src';
import {
successResponseObject,
successUSDCResponseObject,
} from '../../response.util';
describe('Public USDC Options REST API Endpoints', () => {
const useLivenet = true;
const API_KEY = undefined;
const API_SECRET = undefined;
const api = new USDCPerpetualClient(API_KEY, API_SECRET, useLivenet);
const symbol = 'BTCPERP';
const category = 'PERPETUAL';
const startTime = Number((Date.now() / 1000).toFixed(0));
const candleRequest: USDCKlineRequest = { symbol, period: '1m', startTime };
it('getOrderBook()', async () => {
expect(await api.getOrderBook(symbol)).toMatchObject(
successUSDCResponseObject()
);
});
it('getContractInfo()', async () => {
expect(await api.getContractInfo()).toMatchObject(
successUSDCResponseObject()
);
});
it('getSymbolTicker()', async () => {
expect(await api.getSymbolTicker(symbol)).toMatchObject(
successUSDCResponseObject()
);
});
it('getKline()', async () => {
expect(await api.getKline(candleRequest)).toMatchObject(
successUSDCResponseObject()
);
});
it('getMarkPrice()', async () => {
expect(await api.getMarkPrice(candleRequest)).toMatchObject(
successUSDCResponseObject()
);
});
it('getIndexPrice()', async () => {
expect(await api.getIndexPrice(candleRequest)).toMatchObject(
successUSDCResponseObject()
);
});
it('getIndexPremium()', async () => {
expect(await api.getIndexPremium(candleRequest)).toMatchObject(
successUSDCResponseObject()
);
});
it('getOpenInterest()', async () => {
expect(await api.getOpenInterest({ symbol, period: '1m' })).toMatchObject(
successUSDCResponseObject()
);
});
it('getLargeOrders()', async () => {
expect(await api.getLargeOrders({ symbol })).toMatchObject(
successUSDCResponseObject()
);
});
it('getLongShortRatio()', async () => {
expect(await api.getLongShortRatio({ symbol, period: '1m' })).toMatchObject(
successUSDCResponseObject()
);
});
it('getLast500Trades()', async () => {
expect(await api.getLast500Trades({ category })).toMatchObject(
successUSDCResponseObject()
);
});
it('getLastFundingRate()', async () => {
expect(await api.getLastFundingRate(symbol)).toMatchObject(
successUSDCResponseObject()
);
});
it('getServerTime()', async () => {
expect(await api.getServerTime()).toMatchObject(successResponseObject());
});
});