refactoring in new classes around consistency. Add spotv3 REST client

This commit is contained in:
tiagosiebler
2022-09-10 12:23:32 +01:00
parent a1c0887417
commit 3a984594dc
14 changed files with 365 additions and 95 deletions

View File

@@ -9,8 +9,9 @@
[1]: https://www.npmjs.com/package/bybit-api
Node.js connector for the Bybit APIs and WebSockets:
- Complete integration with all bybit APIs.
- TypeScript support (with type declarations for most API requests & responses).
- Integration tests with real API calls validating any changes before they reach npm.
- Over 200 integration tests making real API calls, validating any changes before they reach npm.
- Robust WebSocket integration with connection heartbeats & automatic reconnection.
- Browser support (via webpack bundle - see "Browser Usage" below).
@@ -77,6 +78,8 @@ Create API credentials on Bybit's website:
All REST clients have can be used in a similar way. However, method names, parameters and responses may vary depending on the API category you're using!
Not sure which function to call or which parameters to use? Click the class name in the table above to look at all the function names (they are in the same order as the official API docs), and check the API docs for a list of endpoints/paramters/responses.
```javascript
const {
InverseClient,

View File

@@ -6,7 +6,7 @@ import {
CopyTradingOrderRequest,
CopyTradingTradingStopRequest,
CopyTradingTransferRequest,
USDCAPIResponse,
APIResponseV3,
} from './types';
import { REST_CLIENT_TYPE_ENUM } from './util';
import BaseRestClient from './util/BaseRestClient';
@@ -17,7 +17,7 @@ import BaseRestClient from './util/BaseRestClient';
export class CopyTradingClient extends BaseRestClient {
getClientType() {
// Follows the same authentication mechanism as USDC APIs
return REST_CLIENT_TYPE_ENUM.usdc;
return REST_CLIENT_TYPE_ENUM.v3;
}
async fetchServerTime(): Promise<number> {
@@ -31,7 +31,7 @@ export class CopyTradingClient extends BaseRestClient {
*
*/
getSymbolList(): Promise<USDCAPIResponse<any>> {
getSymbols(): Promise<APIResponseV3<any>> {
return this.get('/contract/v3/public/copytrading/symbol/list');
}
@@ -44,7 +44,7 @@ export class CopyTradingClient extends BaseRestClient {
/** -> Order API */
/** Create order */
submitOrder(params: CopyTradingOrderRequest): Promise<USDCAPIResponse<any>> {
submitOrder(params: CopyTradingOrderRequest): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/contract/v3/private/copytrading/order/create',
params
@@ -54,7 +54,7 @@ export class CopyTradingClient extends BaseRestClient {
/** Set Trading Stop */
setTradingStop(
params: CopyTradingTradingStopRequest
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/contract/v3/private/copytrading/order/trading-stop',
params
@@ -64,7 +64,7 @@ export class CopyTradingClient extends BaseRestClient {
/** Query Order List */
getActiveOrders(
params?: CopyTradingOrderListRequest
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.getPrivate(
'/contract/v3/private/copytrading/order/list',
params
@@ -74,7 +74,7 @@ export class CopyTradingClient extends BaseRestClient {
/** Cancel order */
cancelOrder(
params: CopyTradingCancelOrderRequest
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/contract/v3/private/copytrading/order/cancel',
params
@@ -84,7 +84,7 @@ export class CopyTradingClient extends BaseRestClient {
/** Close Order. This endpoint's rate_limit will decrease by 10 per request; ie, one request to this endpoint consumes 10 from the limit allowed per minute. */
closeOrder(
params: CopyTradingCloseOrderRequest
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.postPrivate('/contract/v3/private/copytrading/order/close', {
params,
});
@@ -93,7 +93,7 @@ export class CopyTradingClient extends BaseRestClient {
/** -> Positions API */
/** Position List */
getPositions(symbol?: string): Promise<USDCAPIResponse<any>> {
getPositions(symbol?: string): Promise<APIResponseV3<any>> {
return this.getPrivate('/contract/v3/private/copytrading/position/list', {
symbol,
});
@@ -103,7 +103,7 @@ export class CopyTradingClient extends BaseRestClient {
closePosition(
symbol: string,
positionIdx: string
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.postPrivate('/contract/v3/private/copytrading/position/close', {
symbol,
positionIdx,
@@ -115,7 +115,7 @@ export class CopyTradingClient extends BaseRestClient {
symbol: string,
buyLeverage: string,
sellLeverage: string
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/contract/v3/private/copytrading/position/set-leverage',
{ symbol, buyLeverage, sellLeverage }
@@ -129,12 +129,12 @@ export class CopyTradingClient extends BaseRestClient {
*/
/** Get Wallet Balance */
getBalance(): Promise<USDCAPIResponse<any>> {
getBalances(): Promise<APIResponseV3<any>> {
return this.getPrivate('/contract/v3/private/copytrading/wallet/balance');
}
/** Transfer */
transfer(params: CopyTradingTransferRequest): Promise<USDCAPIResponse<any>> {
transfer(params: CopyTradingTransferRequest): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/contract/v3/private/copytrading/wallet/transfer',
params

271
src/spot-client-v3.ts Normal file
View File

@@ -0,0 +1,271 @@
import {
APIResponseWithTime,
APIResponseV3,
SpotOrderQueryById,
OrderSide,
OrderTypeSpot,
SpotBalances,
} from './types';
import { REST_CLIENT_TYPE_ENUM } from './util';
import BaseRestClient from './util/BaseRestClient';
/**
* REST API client for newer Spot V3 APIs.
*/
export class SpotV3Client extends BaseRestClient {
getClientType() {
// Follows the same authentication mechanism as other v3 APIs (e.g. USDC)
return REST_CLIENT_TYPE_ENUM.v3;
}
async fetchServerTime(): Promise<number> {
const res = await this.getServerTime();
return Number(res.time_now);
}
/**
*
* Market Data Endpoints
*
*/
/** Get all symbols */
getSymbols(): Promise<APIResponseV3<any>> {
return this.get('/spot/v3/public/symbols');
}
/** Get orderbook for symbol */
getOrderBook(symbol: string, limit?: number): Promise<APIResponseV3<any>> {
return this.get('/spot/v3/public/quote/depth', { symbol, limit });
}
/** Get merged orderbook for symbol */
getOrderBookMerged(params: unknown): Promise<APIResponseV3<any>> {
return this.get('/spot/v3/public/quote/depth/merged', params);
}
/** Get public trading records (raw trades) */
getTrades(symbol: string, limit?: number): Promise<APIResponseV3<any>> {
return this.get('/spot/v3/public/quote/trades', { symbol, limit });
}
/** Get candles/klines */
getCandles(params: unknown): Promise<APIResponseV3<any>> {
return this.get('/spot/v3/public/quote/kline', params);
}
/** Get latest information for symbol (24hr ticker) */
get24hrTicker(symbol?: string): Promise<APIResponseV3<any>> {
return this.get('/spot/v3/public/quote/ticker/24hr', { symbol });
}
/** Get last traded price */
getLastTradedPrice(symbol?: string): Promise<APIResponseV3<any>> {
return this.get('/spot/v3/public/quote/ticker/price', { symbol });
}
/** Get best bid/ask price */
getBestBidAskPrice(symbol?: string): Promise<APIResponseV3<any>> {
return this.get('/spot/v3/public/quote/ticker/bookTicker', { symbol });
}
/**
*
* Account Data Endpoints
*
*/
/** -> Order API */
/** Create order */
submitOrder(params: unknown): Promise<APIResponseV3<any>> {
return this.postPrivate('/spot/v3/private/order', params);
}
/** Get active order state */
getOrder(params: SpotOrderQueryById): Promise<APIResponseV3<any>> {
return this.getPrivate('/spot/v3/private/order', params);
}
/** Cancel order */
cancelOrder(params: SpotOrderQueryById): Promise<APIResponseV3<any>> {
return this.postPrivate('/spot/v3/private/cancel-order', params);
}
/** Batch cancel orders */
cancelOrderBatch(params: {
symbol: string;
side?: OrderSide;
orderTypes: OrderTypeSpot[];
}): Promise<APIResponseV3<any>> {
const orderTypes = params.orderTypes
? params.orderTypes.join(',')
: undefined;
return this.postPrivate('/spot/v3/private/cancel-orders', {
...params,
orderTypes,
});
}
/** Batch cancel up to 100 orders by ID */
cancelOrderBatchIDs(orderIds: string[]): Promise<APIResponseV3<any>> {
const orderIdsCsv = orderIds.join(',');
return this.postPrivate('/spot/v3/private/cancel-orders-by-ids', {
orderIds: orderIdsCsv,
});
}
/** Get open orders */
getOpenOrders(
symbol?: string,
orderId?: string,
limit?: number
): Promise<APIResponseV3<any>> {
return this.getPrivate('/spot/v3/private/open-orders', {
symbol,
orderId,
limit,
});
}
/** Get order history */
getPastOrders(
symbol?: string,
orderId?: string,
limit?: number
): Promise<APIResponseV3<any>> {
return this.getPrivate('/spot/v3/private/history-orders', {
symbol,
orderId,
limit,
});
}
/**
* Get your trade history.
* If startTime is not specified, you can only query for records in the last 7 days.
* If you want to query for records older than 7 days, startTime is required.
*/
getMyTrades(params?: unknown): Promise<APIResponseV3<any>> {
return this.getPrivate('/spot/v3/private/my-trades', params);
}
/**
*
* Wallet Data Endpoints
*
*/
/** Get Wallet Balance */
getBalances(): Promise<APIResponseV3<SpotBalances>> {
return this.getPrivate('/spot/v3/private/account');
}
/**
*
* API Data Endpoints
*
*/
getServerTime(): Promise<APIResponseWithTime> {
return this.get('/v2/public/time');
}
/**
*
* Leveraged Token Endpoints
*
*/
/** Get all asset infos */
getLeveragedTokenAssetInfos(ltCode?: string): Promise<APIResponseV3<any>> {
return this.get('/spot/v3/public/infos', { ltCode });
}
/** Get leveraged token market info */
getLeveragedTokenMarketInfo(ltCode: string): Promise<APIResponseV3<any>> {
return this.getPrivate('/spot/v3/private/reference', { ltCode });
}
/** Purchase leveraged token */
purchaseLeveragedToken(
ltCode: string,
ltAmount: string,
serialNo?: string
): Promise<APIResponseV3<any>> {
return this.postPrivate('/spot/v3/private/purchase', {
ltCode,
ltAmount,
serialNo,
});
}
/** Redeem leveraged token */
redeemLeveragedToken(
ltCode: string,
ltAmount: string,
serialNo?: string
): Promise<APIResponseV3<any>> {
return this.postPrivate('/spot/v3/private/redeem', {
ltCode,
ltAmount,
serialNo,
});
}
/** Get leveraged token purchase/redemption history */
getLeveragedTokenPRHistory(params: unknown): Promise<APIResponseV3<any>> {
return this.getPrivate('/spot/v3/private/record', params);
}
/**
*
* Cross Margin Trading Endpoints
*
*/
/** Borrow margin loan */
borrowCrossMarginLoan(
coin: string,
qty: string
): Promise<APIResponseV3<any>> {
return this.postPrivate('/spot/v3/private/cross-margin-loan', {
coin,
qty,
});
}
/** Repay margin loan */
repayCrossMarginLoan(coin: string, qty: string): Promise<APIResponseV3<any>> {
return this.postPrivate('/spot/v3/private/cross-margin-repay', {
coin,
qty,
});
}
/** Query borrowing info */
getCrossMarginBorrowingInfo(params?: unknown): Promise<APIResponseV3<any>> {
return this.getPrivate('/spot/v3/private/cross-margin-orders', params);
}
/** Query account info */
getCrossMarginAccountInfo(): Promise<APIResponseV3<any>> {
return this.getPrivate('/spot/v3/private/cross-margin-account');
}
/** Query interest & quota */
getCrossMarginInterestQuota(coin: string): Promise<APIResponseV3<any>> {
return this.getPrivate('/spot/v3/private/cross-margin-loan-info', { coin });
}
/** Query repayment history */
getCrossMarginRepaymentHistory(
params?: unknown
): Promise<APIResponseV3<any>> {
return this.getPrivate(
'/spot/v3/private/cross-margin-repay-history',
params
);
}
}

View File

@@ -13,6 +13,7 @@ import BaseRestClient from './util/BaseRestClient';
import { agentSource, REST_CLIENT_TYPE_ENUM } from './util/requestUtils';
/**
* @deprecated Use SpotV3Client instead, which leverages the newer v3 APIs
* REST API client for Spot APIs (v1)
*/
export class SpotClient extends BaseRestClient {
@@ -124,6 +125,7 @@ export class SpotClient extends BaseRestClient {
const orderTypes = params.orderTypes
? params.orderTypes.join(',')
: undefined;
return this.deletePrivate('/spot/order/batch-cancel', {
...params,
orderTypes,

View File

@@ -25,7 +25,7 @@ export interface APIResponse<T> {
result: T;
}
export interface USDCAPIResponse<T> {
export interface APIResponseV3<T> {
retCode: number;
retMsg: 'OK' | string;
result: T;

View File

@@ -1,6 +1,6 @@
import {
APIResponseWithTime,
USDCAPIResponse,
APIResponseV3,
USDCOptionsActiveOrdersRealtimeRequest,
USDCOptionsActiveOrdersRequest,
USDCOptionsCancelAllOrdersRequest,
@@ -27,7 +27,7 @@ import BaseRestClient from './util/BaseRestClient';
*/
export class USDCOptionClient extends BaseRestClient {
getClientType() {
return REST_CLIENT_TYPE_ENUM.usdc;
return REST_CLIENT_TYPE_ENUM.v3;
}
async fetchServerTime(): Promise<number> {
@@ -42,33 +42,33 @@ export class USDCOptionClient extends BaseRestClient {
*/
/** Query order book info. Each side has a depth of 25 orders. */
getOrderBook(symbol: string): Promise<USDCAPIResponse<any>> {
getOrderBook(symbol: string): Promise<APIResponseV3<any>> {
return this.get('/option/usdc/openapi/public/v1/order-book', { symbol });
}
/** Fetch trading rules (such as min/max qty). Query for all if blank. */
getContractInfo(
params?: USDCOptionsContractInfoRequest
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.get('/option/usdc/openapi/public/v1/symbols', params);
}
/** Get a symbol price/statistics ticker */
getSymbolTicker(symbol: string): Promise<USDCAPIResponse<any>> {
getSymbolTicker(symbol: string): Promise<APIResponseV3<any>> {
return this.get('/option/usdc/openapi/public/v1/tick', { symbol });
}
/** Get delivery information */
getDeliveryPrice(
params?: USDCOptionsDeliveryPriceRequest
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.get('/option/usdc/openapi/public/v1/delivery-price', params);
}
/** Returned records are Taker Buy in default. */
getLast500Trades(
params: USDCOptionsRecentTradesRequest
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.get(
'/option/usdc/openapi/public/v1/query-trade-latest',
params
@@ -84,7 +84,7 @@ export class USDCOptionClient extends BaseRestClient {
*/
getHistoricalVolatility(
params?: USDCOptionsHistoricalVolatilityRequest
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.get(
'/option/usdc/openapi/public/v1/query-historical-volatility',
params
@@ -104,7 +104,7 @@ export class USDCOptionClient extends BaseRestClient {
* The request status can be queried in real-time.
* The response parameters must be queried through a query or a WebSocket response.
*/
submitOrder(params: USDCOptionsOrderRequest): Promise<USDCAPIResponse<any>> {
submitOrder(params: USDCOptionsOrderRequest): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/place-order',
params
@@ -116,7 +116,7 @@ export class USDCOptionClient extends BaseRestClient {
*/
batchSubmitOrders(
orderRequest: USDCOptionsOrderRequest[]
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/batch-place-orders',
{ orderRequest }
@@ -126,7 +126,7 @@ export class USDCOptionClient extends BaseRestClient {
/** For Options, at least one of the three parameters — price, quantity or implied volatility — must be input. */
modifyOrder(
params: USDCOptionsModifyOrderRequest
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/replace-order',
params
@@ -136,7 +136,7 @@ export class USDCOptionClient extends BaseRestClient {
/** Each request supports a max. of four orders. The reduceOnly parameter should be separate and unique for each order in the request. */
batchModifyOrders(
replaceOrderRequest: USDCOptionsModifyOrderRequest[]
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/batch-replace-orders',
{ replaceOrderRequest }
@@ -146,7 +146,7 @@ export class USDCOptionClient extends BaseRestClient {
/** Cancel order */
cancelOrder(
params: USDCOptionsCancelOrderRequest
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/cancel-order',
params
@@ -156,7 +156,7 @@ export class USDCOptionClient extends BaseRestClient {
/** Batch cancel orders */
batchCancelOrders(
cancelRequest: USDCOptionsCancelOrderRequest[]
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/batch-cancel-orders',
{ cancelRequest }
@@ -166,7 +166,7 @@ export class USDCOptionClient extends BaseRestClient {
/** This is used to cancel all active orders. The real-time response indicates whether the request is successful, depending on retCode. */
cancelActiveOrders(
params?: USDCOptionsCancelAllOrdersRequest
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/cancel-all',
params
@@ -176,7 +176,7 @@ export class USDCOptionClient extends BaseRestClient {
/** Query Unfilled/Partially Filled Orders(real-time), up to last 7 days of partially filled/unfilled orders */
getActiveRealtimeOrders(
params?: USDCOptionsActiveOrdersRealtimeRequest
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.getPrivate(
'/option/usdc/openapi/private/v1/trade/query-active-orders',
params
@@ -186,7 +186,7 @@ export class USDCOptionClient extends BaseRestClient {
/** Query Unfilled/Partially Filled Orders */
getActiveOrders(
params: USDCOptionsActiveOrdersRequest
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/query-active-orders',
params
@@ -196,7 +196,7 @@ export class USDCOptionClient extends BaseRestClient {
/** Query order history. The endpoint only supports up to 30 days of queried records */
getHistoricOrders(
params: USDCOptionsHistoricOrdersRequest
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/query-order-history',
params
@@ -206,7 +206,7 @@ export class USDCOptionClient extends BaseRestClient {
/** 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: USDCOptionsOrderExecutionRequest
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/execution-list',
params
@@ -218,7 +218,7 @@ export class USDCOptionClient extends BaseRestClient {
/** 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: USDCTransactionLogRequest
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/query-transaction-log',
params
@@ -226,14 +226,14 @@ export class USDCOptionClient extends BaseRestClient {
}
/** Wallet info for USDC account. */
getBalance(): Promise<USDCAPIResponse<any>> {
getBalances(): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/query-wallet-balance'
);
}
/** Asset Info */
getAssetInfo(baseCoin?: string): Promise<USDCAPIResponse<any>> {
getAssetInfo(baseCoin?: string): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/query-asset-info',
{ baseCoin }
@@ -246,7 +246,7 @@ export class USDCOptionClient extends BaseRestClient {
*/
setMarginMode(
newMarginMode: 'REGULAR_MARGIN' | 'PORTFOLIO_MARGIN'
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/private/asset/account/setMarginMode',
{ setMarginMode: newMarginMode }
@@ -254,7 +254,7 @@ export class USDCOptionClient extends BaseRestClient {
}
/** Query margin mode for USDC account. */
getMarginMode(): Promise<USDCAPIResponse<any>> {
getMarginMode(): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/query-margin-info'
);
@@ -263,7 +263,7 @@ export class USDCOptionClient extends BaseRestClient {
/** -> Positions API */
/** Query my positions */
getPositions(params: USDCPositionsRequest): Promise<USDCAPIResponse<any>> {
getPositions(params: USDCPositionsRequest): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/query-position',
params
@@ -273,7 +273,7 @@ export class USDCOptionClient extends BaseRestClient {
/** Query Delivery History */
getDeliveryHistory(
params: USDCOptionsDeliveryHistoryRequest
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/query-delivery-list',
params
@@ -283,7 +283,7 @@ export class USDCOptionClient extends BaseRestClient {
/** Query Positions Info Upon Expiry */
getPositionsInfoUponExpiry(
params?: USDCOptionsPositionsInfoExpiryRequest
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/query-position-exp-date',
params
@@ -293,9 +293,7 @@ export class USDCOptionClient extends BaseRestClient {
/** -> Market Maker Protection */
/** modifyMMP */
modifyMMP(
params: USDCOptionsModifyMMPRequest
): Promise<USDCAPIResponse<any>> {
modifyMMP(params: USDCOptionsModifyMMPRequest): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/mmp-modify',
params
@@ -303,14 +301,14 @@ export class USDCOptionClient extends BaseRestClient {
}
/** resetMMP */
resetMMP(currency: string): Promise<USDCAPIResponse<any>> {
resetMMP(currency: string): Promise<APIResponseV3<any>> {
return this.postPrivate('/option/usdc/openapi/private/v1/mmp-reset', {
currency,
});
}
/** queryMMPState */
queryMMPState(baseCoin: string): Promise<USDCAPIResponse<any>> {
queryMMPState(baseCoin: string): Promise<APIResponseV3<any>> {
return this.postPrivate('/option/usdc/openapi/private/v1/get-mmp-state', {
baseCoin,
});

View File

@@ -2,7 +2,7 @@ import {
APIResponseWithTime,
SymbolLimitParam,
SymbolPeriodLimitParam,
USDCAPIResponse,
APIResponseV3,
USDCKlineRequest,
USDCLast500TradesRequest,
USDCOpenInterestRequest,
@@ -25,7 +25,7 @@ import BaseRestClient from './util/BaseRestClient';
*/
export class USDCPerpetualClient extends BaseRestClient {
getClientType() {
return REST_CLIENT_TYPE_ENUM.usdc;
return REST_CLIENT_TYPE_ENUM.v3;
}
async fetchServerTime(): Promise<number> {
@@ -39,41 +39,41 @@ export class USDCPerpetualClient extends BaseRestClient {
*
*/
getOrderBook(symbol: string): Promise<USDCAPIResponse<any>> {
getOrderBook(symbol: string): Promise<APIResponseV3<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?: USDCSymbolDirectionLimit
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.get('/perpetual/usdc/openapi/public/v1/symbols', params);
}
/** Get a symbol price/statistics ticker */
getSymbolTicker(symbol: string): Promise<USDCAPIResponse<any>> {
getSymbolTicker(symbol: string): Promise<APIResponseV3<any>> {
return this.get('/perpetual/usdc/openapi/public/v1/tick', { symbol });
}
getKline(params: USDCKlineRequest): Promise<USDCAPIResponse<any>> {
getCandles(params: USDCKlineRequest): Promise<APIResponseV3<any>> {
return this.get('/perpetual/usdc/openapi/public/v1/kline/list', params);
}
getMarkPrice(params: USDCKlineRequest): Promise<USDCAPIResponse<any>> {
getMarkPrice(params: USDCKlineRequest): Promise<APIResponseV3<any>> {
return this.get(
'/perpetual/usdc/openapi/public/v1/mark-price-kline',
params
);
}
getIndexPrice(params: USDCKlineRequest): Promise<USDCAPIResponse<any>> {
getIndexPrice(params: USDCKlineRequest): Promise<APIResponseV3<any>> {
return this.get(
'/perpetual/usdc/openapi/public/v1/index-price-kline',
params
);
}
getIndexPremium(params: USDCKlineRequest): Promise<USDCAPIResponse<any>> {
getIndexPremium(params: USDCKlineRequest): Promise<APIResponseV3<any>> {
return this.get(
'/perpetual/usdc/openapi/public/v1/premium-index-kline',
params
@@ -82,25 +82,25 @@ export class USDCPerpetualClient extends BaseRestClient {
getOpenInterest(
params: USDCOpenInterestRequest
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.get('/perpetual/usdc/openapi/public/v1/open-interest', params);
}
getLargeOrders(
params: SymbolLimitParam<string>
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.get('/perpetual/usdc/openapi/public/v1/big-deal', params);
}
getLongShortRatio(
params: SymbolPeriodLimitParam<string>
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.get('/perpetual/usdc/openapi/public/v1/account-ratio', params);
}
getLast500Trades(
params: USDCLast500TradesRequest
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.get(
'/option/usdc/openapi/public/v1/query-trade-latest',
params
@@ -120,7 +120,7 @@ export class USDCPerpetualClient extends BaseRestClient {
* The request status can be queried in real-time.
* The response parameters must be queried through a query or a WebSocket response.
*/
submitOrder(params: USDCPerpOrderRequest): Promise<USDCAPIResponse<any>> {
submitOrder(params: USDCPerpOrderRequest): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/perpetual/usdc/openapi/private/v1/place-order',
params
@@ -128,9 +128,7 @@ export class USDCPerpetualClient extends BaseRestClient {
}
/** 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: USDCPerpModifyOrderRequest
): Promise<USDCAPIResponse<any>> {
modifyOrder(params: USDCPerpModifyOrderRequest): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/perpetual/usdc/openapi/private/v1/replace-order',
params
@@ -138,9 +136,7 @@ export class USDCPerpetualClient extends BaseRestClient {
}
/** Cancel order */
cancelOrder(
params: USDCPerpCancelOrderRequest
): Promise<USDCAPIResponse<any>> {
cancelOrder(params: USDCPerpCancelOrderRequest): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/perpetual/usdc/openapi/private/v1/cancel-order',
params
@@ -151,7 +147,7 @@ export class USDCPerpetualClient extends BaseRestClient {
cancelActiveOrders(
symbol: string,
orderFilter: USDCOrderFilter
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.postPrivate('/perpetual/usdc/openapi/private/v1/cancel-all', {
symbol,
orderFilter,
@@ -161,7 +157,7 @@ export class USDCPerpetualClient extends BaseRestClient {
/** Query Unfilled/Partially Filled Orders */
getActiveOrders(
params: USDCPerpActiveOrdersRequest
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/query-active-orders',
params
@@ -171,7 +167,7 @@ export class USDCPerpetualClient extends BaseRestClient {
/** Query order history. The endpoint only supports up to 30 days of queried records */
getHistoricOrders(
params: USDCPerpHistoricOrdersRequest
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/query-order-history',
params
@@ -181,7 +177,7 @@ export class USDCPerpetualClient extends BaseRestClient {
/** 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: USDCPerpActiveOrdersRequest
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/execution-list',
params
@@ -193,7 +189,7 @@ export class USDCPerpetualClient extends BaseRestClient {
/** 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: USDCTransactionLogRequest
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/query-transaction-log',
params
@@ -201,14 +197,14 @@ export class USDCPerpetualClient extends BaseRestClient {
}
/** Wallet info for USDC account. */
getBalance(): Promise<USDCAPIResponse<any>> {
getBalances(): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/query-wallet-balance'
);
}
/** Asset Info */
getAssetInfo(baseCoin?: string): Promise<USDCAPIResponse<any>> {
getAssetInfo(baseCoin?: string): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/query-asset-info',
{ baseCoin }
@@ -221,7 +217,7 @@ export class USDCPerpetualClient extends BaseRestClient {
*/
setMarginMode(
newMarginMode: 'REGULAR_MARGIN' | 'PORTFOLIO_MARGIN'
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/private/asset/account/setMarginMode',
{ setMarginMode: newMarginMode }
@@ -229,7 +225,7 @@ export class USDCPerpetualClient extends BaseRestClient {
}
/** Query margin mode for USDC account. */
getMarginMode(): Promise<USDCAPIResponse<any>> {
getMarginMode(): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/query-margin-info'
);
@@ -238,7 +234,7 @@ export class USDCPerpetualClient extends BaseRestClient {
/** -> Positions API */
/** Query my positions */
getPositions(params: USDCPositionsRequest): Promise<USDCAPIResponse<any>> {
getPositions(params: USDCPositionsRequest): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/query-position',
params
@@ -246,7 +242,7 @@ export class USDCPerpetualClient extends BaseRestClient {
}
/** Only for REGULAR_MARGIN */
setLeverage(symbol: string, leverage: string): Promise<USDCAPIResponse<any>> {
setLeverage(symbol: string, leverage: string): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/perpetual/usdc/openapi/private/v1/position/leverage/save',
{ symbol, leverage }
@@ -256,7 +252,7 @@ export class USDCPerpetualClient extends BaseRestClient {
/** Query Settlement History */
getSettlementHistory(
params?: USDCSymbolDirectionLimitCursor
): Promise<USDCAPIResponse<any>> {
): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/session-settlement',
params
@@ -266,7 +262,7 @@ export class USDCPerpetualClient extends BaseRestClient {
/** -> Risk Limit API */
/** Query risk limit */
getRiskLimit(symbol: string): Promise<USDCAPIResponse<any>> {
getRiskLimit(symbol: string): Promise<APIResponseV3<any>> {
return this.getPrivate(
'/perpetual/usdc/openapi/public/v1/risk-limit/list',
{
@@ -276,7 +272,7 @@ export class USDCPerpetualClient extends BaseRestClient {
}
/** Set risk limit */
setRiskLimit(symbol: string, riskId: number): Promise<USDCAPIResponse<any>> {
setRiskLimit(symbol: string, riskId: number): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/perpetual/usdc/openapi/private/v1/position/set-risk-limit',
{ symbol, riskId }
@@ -286,14 +282,14 @@ export class USDCPerpetualClient extends BaseRestClient {
/** -> 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>> {
getLastFundingRate(symbol: string): Promise<APIResponseV3<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>> {
getPredictedFundingRate(symbol: string): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/perpetual/usdc/openapi/private/v1/predicted-funding',
{ symbol }

View File

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

View File

@@ -84,7 +84,7 @@ export const REST_CLIENT_TYPE_ENUM = {
inverseFutures: 'inverseFutures',
linear: 'linear',
spot: 'spot',
usdc: 'usdc',
v3: 'v3',
} as const;
export type RestClientType =

View File

@@ -28,8 +28,8 @@ describe('Private Copy Trading REST API Endpoints', () => {
});
});
it('getBalance()', async () => {
expect(await api.getBalance()).toMatchObject({
it('getBalances()', async () => {
expect(await api.getBalances()).toMatchObject({
retCode: API_ERROR_CODE.INCORRECT_API_KEY_PERMISSIONS,
});
});

View File

@@ -8,8 +8,8 @@ describe('Public Copy Trading REST API Endpoints', () => {
const api = new CopyTradingClient(API_KEY, API_SECRET, useLivenet);
it('getSymbolList()', async () => {
expect(await api.getSymbolList()).toMatchObject({
it('getSymbols()', async () => {
expect(await api.getSymbols()).toMatchObject({
result: {
list: expect.any(Array),
},

View File

@@ -48,8 +48,8 @@ describe('Private Account Asset REST API Endpoints', () => {
);
});
it('getBalance()', async () => {
expect(await api.getBalance()).toMatchObject(successUSDCResponseObject());
it('getBalances()', async () => {
expect(await api.getBalances()).toMatchObject(successUSDCResponseObject());
});
it('getAssetInfo()', async () => {

View File

@@ -40,8 +40,8 @@ describe('Private Account Asset REST API Endpoints', () => {
);
});
it('getBalance()', async () => {
expect(await api.getBalance()).toMatchObject(successUSDCResponseObject());
it('getBalances()', async () => {
expect(await api.getBalances()).toMatchObject(successUSDCResponseObject());
});
it('getAssetInfo()', async () => {

View File

@@ -35,8 +35,8 @@ describe('Public USDC Options REST API Endpoints', () => {
);
});
it('getKline()', async () => {
expect(await api.getKline(candleRequest)).toMatchObject(
it('getCandles()', async () => {
expect(await api.getCandles(candleRequest)).toMatchObject(
successUSDCResponseObject()
);
});