add account asset & USDC options clients
This commit is contained in:
173
src/account-asset-client.ts
Normal file
173
src/account-asset-client.ts
Normal file
@@ -0,0 +1,173 @@
|
||||
import { AxiosRequestConfig } from 'axios';
|
||||
import {
|
||||
AccountAssetInformationRequest,
|
||||
APIResponseWithTime,
|
||||
DepositRecordsRequest,
|
||||
EnableUniversalTransferRequest,
|
||||
InternalTransferRequest,
|
||||
SubAccountTransferRequest,
|
||||
SupportedDepositListRequest,
|
||||
TransferQueryRequest,
|
||||
UniversalTransferRequest,
|
||||
WithdrawalRecordsRequest,
|
||||
WithdrawalRequest,
|
||||
} from './types';
|
||||
import {
|
||||
RestClientOptions,
|
||||
getRestBaseUrl,
|
||||
REST_CLIENT_TYPE_ENUM,
|
||||
} from './util';
|
||||
import BaseRestClient from './util/BaseRestClient';
|
||||
|
||||
export class AccountAssetClient extends BaseRestClient {
|
||||
/**
|
||||
* @public Creates an instance of the Account Asset REST API client.
|
||||
*
|
||||
* @param {string} key - your API key
|
||||
* @param {string} secret - your API secret
|
||||
* @param {boolean} [useLivenet=false]
|
||||
* @param {RestClientOptions} [restClientOptions={}] options to configure REST API connectivity
|
||||
* @param {AxiosRequestConfig} [requestOptions={}] HTTP networking options for axios
|
||||
*/
|
||||
constructor(
|
||||
key?: string | undefined,
|
||||
secret?: string | undefined,
|
||||
useLivenet: boolean = false,
|
||||
restClientOptions: RestClientOptions = {},
|
||||
requestOptions: AxiosRequestConfig = {}
|
||||
) {
|
||||
super(
|
||||
key,
|
||||
secret,
|
||||
getRestBaseUrl(useLivenet, restClientOptions),
|
||||
restClientOptions,
|
||||
requestOptions,
|
||||
REST_CLIENT_TYPE_ENUM.accountAsset
|
||||
);
|
||||
return this;
|
||||
}
|
||||
|
||||
async fetchServerTime(): Promise<number> {
|
||||
const res = await this.getServerTime();
|
||||
return Number(res.time_now);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Transfer Data Endpoints
|
||||
*
|
||||
*/
|
||||
|
||||
createInternalTransfer(
|
||||
params: InternalTransferRequest
|
||||
): Promise<APIResponseWithTime<any>> {
|
||||
return this.postPrivate('/asset/v1/private/transfer', params);
|
||||
}
|
||||
|
||||
createSubAccountTransfer(
|
||||
params: SubAccountTransferRequest
|
||||
): Promise<APIResponseWithTime<any>> {
|
||||
return this.postPrivate('/asset/v1/private/sub-member/transfer', params);
|
||||
}
|
||||
|
||||
getInternalTransfers(
|
||||
params?: TransferQueryRequest
|
||||
): Promise<APIResponseWithTime<any>> {
|
||||
return this.getPrivate('/asset/v1/private/transfer/list', params);
|
||||
}
|
||||
|
||||
getSubAccountTransfers(
|
||||
params?: TransferQueryRequest
|
||||
): Promise<APIResponseWithTime<any>> {
|
||||
return this.getPrivate(
|
||||
'/asset/v1/private/sub-member/transfer/list',
|
||||
params
|
||||
);
|
||||
}
|
||||
|
||||
getSubAccounts(): Promise<APIResponseWithTime<any>> {
|
||||
return this.getPrivate('/asset/v1/private/sub-member/member-ids');
|
||||
}
|
||||
|
||||
enableUniversalTransfer(
|
||||
params?: EnableUniversalTransferRequest
|
||||
): Promise<APIResponseWithTime<any>> {
|
||||
return this.postPrivate('/asset/v1/private/transferable-subs/save', params);
|
||||
}
|
||||
|
||||
createUniversalTransfer(
|
||||
params: UniversalTransferRequest
|
||||
): Promise<APIResponseWithTime<any>> {
|
||||
return this.postPrivate('/asset/v1/private/universal/transfer', params);
|
||||
}
|
||||
|
||||
getUniversalTransfers(
|
||||
params?: TransferQueryRequest
|
||||
): Promise<APIResponseWithTime<any>> {
|
||||
return this.getPrivate('/asset/v1/private/universal/transfer/list', params);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Wallet & Deposit Endpoints
|
||||
*
|
||||
*/
|
||||
|
||||
getSupportedDepositList(
|
||||
params?: SupportedDepositListRequest
|
||||
): Promise<APIResponseWithTime<any>> {
|
||||
return this.get('/asset/v1/public/deposit/allowed-deposit-list', params);
|
||||
}
|
||||
|
||||
getDepositRecords(
|
||||
params?: DepositRecordsRequest
|
||||
): Promise<APIResponseWithTime<any>> {
|
||||
return this.getPrivate('/asset/v1/private/deposit/record/query', params);
|
||||
}
|
||||
|
||||
getWithdrawRecords(
|
||||
params?: WithdrawalRecordsRequest
|
||||
): Promise<APIResponseWithTime<any>> {
|
||||
return this.getPrivate('/asset/v1/private/withdraw/record/query', params);
|
||||
}
|
||||
|
||||
getCoinInformation(coin?: string): Promise<APIResponseWithTime<any>> {
|
||||
return this.getPrivate('/asset/v1/private/coin-info/query', { coin });
|
||||
}
|
||||
|
||||
getAssetInformation(
|
||||
params?: AccountAssetInformationRequest
|
||||
): Promise<APIResponseWithTime<any>> {
|
||||
return this.getPrivate('/asset/v1/private/asset-info/query', params);
|
||||
}
|
||||
|
||||
submitWithdrawal(
|
||||
params: WithdrawalRequest
|
||||
): Promise<APIResponseWithTime<any>> {
|
||||
return this.postPrivate('/asset/v1/private/withdraw', params);
|
||||
}
|
||||
|
||||
cancelWithdrawal(withdrawalId: number): Promise<APIResponseWithTime<any>> {
|
||||
return this.postPrivate('/asset/v1/private/withdraw/cancel', {
|
||||
id: withdrawalId,
|
||||
});
|
||||
}
|
||||
|
||||
getDepositAddress(coin: string): Promise<APIResponseWithTime<any>> {
|
||||
return this.getPrivate('/asset/v1/private/deposit/address', { coin });
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* API Data Endpoints
|
||||
*
|
||||
*/
|
||||
|
||||
getServerTime(): Promise<APIResponseWithTime> {
|
||||
return this.get('/v2/public/time');
|
||||
}
|
||||
|
||||
getApiAnnouncements(): Promise<APIResponseWithTime<any[]>> {
|
||||
return this.get('/v2/public/announcement');
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
export * from './account-asset-client';
|
||||
export * from './inverse-client';
|
||||
export * from './inverse-futures-client';
|
||||
export * from './linear-client';
|
||||
export * from './spot-client';
|
||||
export * from './usdc-options-client';
|
||||
export * from './websocket-client';
|
||||
export * from './logger';
|
||||
export * from './types';
|
||||
|
||||
@@ -3,7 +3,7 @@ import {
|
||||
getRestBaseUrl,
|
||||
RestClientOptions,
|
||||
REST_CLIENT_TYPE_ENUM,
|
||||
} from './util/requestUtils';
|
||||
} from './util';
|
||||
import {
|
||||
APIResponseWithTime,
|
||||
AssetExchangeRecordsReq,
|
||||
@@ -15,7 +15,7 @@ import {
|
||||
SymbolPeriodLimitParam,
|
||||
WalletFundRecordsReq,
|
||||
WithdrawRecordsReq,
|
||||
} from './types/shared';
|
||||
} from './types';
|
||||
import BaseRestClient from './util/BaseRestClient';
|
||||
|
||||
export class InverseClient extends BaseRestClient {
|
||||
|
||||
94
src/types/request/account-asset.ts
Normal file
94
src/types/request/account-asset.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
export type TransferAccountType =
|
||||
| 'CONTRACT'
|
||||
| 'SPOT'
|
||||
| 'INVESTMENT'
|
||||
| 'OPTION'
|
||||
| 'UNIFIED';
|
||||
|
||||
export type TransferType = 'IN' | 'OUT';
|
||||
|
||||
export type TransferStatus = 'SUCCESS' | 'PENDING' | 'FAILED';
|
||||
|
||||
export type PageDirection = 'Prev' | 'Next';
|
||||
|
||||
export interface InternalTransferRequest {
|
||||
transfer_id: string;
|
||||
coin: string;
|
||||
amount: string;
|
||||
from_account_type: TransferAccountType;
|
||||
to_account_type: TransferAccountType;
|
||||
}
|
||||
|
||||
export interface SubAccountTransferRequest {
|
||||
transfer_id: string;
|
||||
coin: string;
|
||||
amount: string;
|
||||
sub_user_id: string;
|
||||
type: TransferType;
|
||||
}
|
||||
|
||||
export interface TransferQueryRequest {
|
||||
transfer_id?: string;
|
||||
coin?: string;
|
||||
status?: TransferStatus;
|
||||
start_time?: number;
|
||||
end_time?: number;
|
||||
direction?: PageDirection;
|
||||
limit?: number;
|
||||
cursor?: string;
|
||||
}
|
||||
|
||||
export interface EnableUniversalTransferRequest {
|
||||
/** A comma-separated list of subaccount UIDs, for example "123,45,14,26,46" */
|
||||
transferable_sub_ids?: string;
|
||||
}
|
||||
|
||||
export interface UniversalTransferRequest {
|
||||
transfer_id: string;
|
||||
coin: string;
|
||||
amount: string;
|
||||
from_member_id: string;
|
||||
to_member_id: string;
|
||||
from_account_type: TransferAccountType;
|
||||
to_account_type: TransferAccountType;
|
||||
}
|
||||
|
||||
export interface SupportedDepositListRequest {
|
||||
coin?: string;
|
||||
chain?: string;
|
||||
page_index?: number;
|
||||
page_size?: number;
|
||||
}
|
||||
|
||||
export interface DepositRecordsRequest {
|
||||
start_time?: number;
|
||||
end_time?: number;
|
||||
coin?: string;
|
||||
cursor?: string;
|
||||
direction?: PageDirection;
|
||||
limit?: number;
|
||||
}
|
||||
|
||||
export interface WithdrawalRecordsRequest {
|
||||
withdraw_id?: number;
|
||||
start_time?: number;
|
||||
end_time?: number;
|
||||
coin?: string;
|
||||
cursor?: string;
|
||||
direction?: PageDirection;
|
||||
limit?: number;
|
||||
}
|
||||
|
||||
export interface AccountAssetInformationRequest {
|
||||
/** Account type. Default value: ACCOUNT_TYPE_SPOT */
|
||||
account_type?: string;
|
||||
coin?: string;
|
||||
}
|
||||
|
||||
export interface WithdrawalRequest {
|
||||
address: string;
|
||||
amount: string;
|
||||
coin: string;
|
||||
chain: string;
|
||||
tag?: string;
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
export * from './account-asset';
|
||||
export * from './usdt-perp';
|
||||
|
||||
@@ -25,7 +25,7 @@ export interface APIResponse<T> {
|
||||
result: T;
|
||||
}
|
||||
|
||||
export interface APIResponseWithTime<T> extends APIResponse<T> {
|
||||
export interface APIResponseWithTime<T = {}> extends APIResponse<T> {
|
||||
/** UTC timestamp */
|
||||
time_now: numberInString;
|
||||
}
|
||||
|
||||
306
src/usdc-options-client.ts
Normal file
306
src/usdc-options-client.ts
Normal file
@@ -0,0 +1,306 @@
|
||||
import { AxiosRequestConfig } from 'axios';
|
||||
import { APIResponseWithTime } from './types';
|
||||
import {
|
||||
RestClientOptions,
|
||||
getRestBaseUrl,
|
||||
REST_CLIENT_TYPE_ENUM,
|
||||
} from './util';
|
||||
import BaseRestClient from './util/BaseRestClient';
|
||||
|
||||
export class USDCOptionsClient extends BaseRestClient {
|
||||
/**
|
||||
* @public Creates an instance of the USDC Options REST API client.
|
||||
*
|
||||
* @param {string} key - your API key
|
||||
* @param {string} secret - your API secret
|
||||
* @param {boolean} [useLivenet=false] uses testnet by default
|
||||
* @param {RestClientOptions} [restClientOptions={}] options to configure REST API connectivity
|
||||
* @param {AxiosRequestConfig} [requestOptions={}] HTTP networking options for axios
|
||||
*/
|
||||
constructor(
|
||||
key?: string | undefined,
|
||||
secret?: string | undefined,
|
||||
useLivenet: boolean = false,
|
||||
restClientOptions: RestClientOptions = {},
|
||||
requestOptions: AxiosRequestConfig = {}
|
||||
) {
|
||||
super(
|
||||
key,
|
||||
secret,
|
||||
getRestBaseUrl(useLivenet, restClientOptions),
|
||||
restClientOptions,
|
||||
requestOptions,
|
||||
REST_CLIENT_TYPE_ENUM.usdcOptions
|
||||
);
|
||||
return this;
|
||||
}
|
||||
|
||||
async fetchServerTime(): Promise<number> {
|
||||
const res = await this.getServerTime();
|
||||
return Number(res.time_now);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Market Data Endpoints
|
||||
*
|
||||
*/
|
||||
|
||||
/** Query order book info. Each side has a depth of 25 orders. */
|
||||
getOrderBook(symbol: string): Promise<APIResponseWithTime<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?: unknown): Promise<APIResponseWithTime<any>> {
|
||||
return this.get('/option/usdc/openapi/public/v1/symbols', params);
|
||||
}
|
||||
|
||||
/** Get a symbol price/statistics ticker */
|
||||
getSymbolTicker(symbol: string): Promise<APIResponseWithTime<any>> {
|
||||
return this.get('/option/usdc/openapi/public/v1/tick', { symbol });
|
||||
}
|
||||
|
||||
/** Get delivery information */
|
||||
getDeliveryPrice(params?: unknown): Promise<APIResponseWithTime<any>> {
|
||||
return this.get('/option/usdc/openapi/public/v1/delivery-price', params);
|
||||
}
|
||||
|
||||
/** Returned records are Taker Buy in default. */
|
||||
getLastTrades(params: unknown): Promise<APIResponseWithTime<any>> {
|
||||
return this.get(
|
||||
'/option/usdc/openapi/public/v1/query-trade-latest',
|
||||
params
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The data is in hourly.
|
||||
* If time field is not passed, it returns the recent 1 hour data by default.
|
||||
* It could be any timeframe by inputting startTime & endTime, but it must satisfy [endTime - startTime] <= 30 days.
|
||||
* It returns all data in 2 years when startTime & endTime are not passed.
|
||||
* Both startTime & endTime entered together or both are left blank
|
||||
*/
|
||||
getHistoricalVolatility(params?: unknown): Promise<APIResponseWithTime<any>> {
|
||||
return this.get(
|
||||
'/option/usdc/openapi/public/v1/query-historical-volatility',
|
||||
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<APIResponseWithTime<any>> {
|
||||
return this.postPrivate(
|
||||
'/option/usdc/openapi/private/v1/place-order',
|
||||
params
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Each request supports a max. of four orders. The reduceOnly parameter should be separate and unique for each order in the request.
|
||||
*/
|
||||
batchSubmitOrders(
|
||||
orderRequest: unknown[]
|
||||
): Promise<APIResponseWithTime<any>> {
|
||||
return this.postPrivate(
|
||||
'/option/usdc/openapi/private/v1/batch-place-orders',
|
||||
{ orderRequest }
|
||||
);
|
||||
}
|
||||
|
||||
/** For Options, at least one of the three parameters — price, quantity or implied volatility — must be input. */
|
||||
modifyOrder(params: unknown): Promise<APIResponseWithTime<any>> {
|
||||
return this.postPrivate(
|
||||
'/option/usdc/openapi/private/v1/replace-order',
|
||||
params
|
||||
);
|
||||
}
|
||||
|
||||
/** Each request supports a max. of four orders. The reduceOnly parameter should be separate and unique for each order in the request. */
|
||||
batchModifyOrders(
|
||||
replaceOrderRequest: unknown[]
|
||||
): Promise<APIResponseWithTime<any>> {
|
||||
return this.postPrivate(
|
||||
'/option/usdc/openapi/private/v1/batch-replace-orders',
|
||||
{ replaceOrderRequest }
|
||||
);
|
||||
}
|
||||
|
||||
/** Cancel order */
|
||||
cancelOrder(params: unknown): Promise<APIResponseWithTime<any>> {
|
||||
return this.postPrivate(
|
||||
'/option/usdc/openapi/private/v1/cancel-order',
|
||||
params
|
||||
);
|
||||
}
|
||||
|
||||
/** Batch cancel orders */
|
||||
batchCancelOrders(
|
||||
cancelRequest: unknown[]
|
||||
): Promise<APIResponseWithTime<any>> {
|
||||
return this.postPrivate(
|
||||
'/option/usdc/openapi/private/v1/batch-cancel-orders',
|
||||
{ cancelRequest }
|
||||
);
|
||||
}
|
||||
|
||||
/** This is used to cancel all active orders. The real-time response indicates whether the request is successful, depending on retCode. */
|
||||
cancelActiveOrders(params?: unknown): Promise<APIResponseWithTime<any>> {
|
||||
return this.postPrivate(
|
||||
'/option/usdc/openapi/private/v1/cancel-all',
|
||||
params
|
||||
);
|
||||
}
|
||||
|
||||
/** Query Unfilled/Partially Filled Orders(real-time), up to last 7 days of partially filled/unfilled orders */
|
||||
getActiveRealtimeOrders(params?: unknown): Promise<APIResponseWithTime<any>> {
|
||||
return this.getPrivate(
|
||||
'/option/usdc/openapi/private/v1/trade/query-active-orders',
|
||||
params
|
||||
);
|
||||
}
|
||||
|
||||
/** Query Unfilled/Partially Filled Orders */
|
||||
getActiveOrders(params: unknown): Promise<APIResponseWithTime<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<APIResponseWithTime<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<APIResponseWithTime<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<APIResponseWithTime<any>> {
|
||||
return this.postPrivate(
|
||||
'/option/usdc/openapi/private/v1/query-transaction-log',
|
||||
params
|
||||
);
|
||||
}
|
||||
|
||||
/** Wallet info for USDC account. */
|
||||
getBalance(params?: unknown): Promise<APIResponseWithTime<any>> {
|
||||
return this.postPrivate(
|
||||
'/option/usdc/openapi/private/v1/query-wallet-balance',
|
||||
params
|
||||
);
|
||||
}
|
||||
|
||||
/** Asset Info */
|
||||
getAssetInfo(baseCoin?: string): Promise<APIResponseWithTime<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<APIResponseWithTime<any>> {
|
||||
return this.postPrivate(
|
||||
'/option/usdc/private/asset/account/setMarginMode',
|
||||
{ setMarginMode: newMarginMode }
|
||||
);
|
||||
}
|
||||
|
||||
/** Query margin mode for USDC account. */
|
||||
getMarginMode(): Promise<APIResponseWithTime<any>> {
|
||||
return this.postPrivate(
|
||||
'/option/usdc/openapi/private/v1/query-margin-info'
|
||||
);
|
||||
}
|
||||
|
||||
/** -> Positions API */
|
||||
|
||||
/** Query my positions */
|
||||
getPositions(params: unknown): Promise<APIResponseWithTime<any>> {
|
||||
return this.postPrivate(
|
||||
'/option/usdc/openapi/private/v1/query-position',
|
||||
params
|
||||
);
|
||||
}
|
||||
|
||||
/** Query Delivery History */
|
||||
getDeliveryHistory(params: unknown): Promise<APIResponseWithTime<any>> {
|
||||
return this.postPrivate(
|
||||
'/option/usdc/openapi/private/v1/query-delivery-list',
|
||||
params
|
||||
);
|
||||
}
|
||||
|
||||
/** Query Positions Info Upon Expiry */
|
||||
getPositionsInfoUponExpiry(
|
||||
params?: unknown
|
||||
): Promise<APIResponseWithTime<any>> {
|
||||
return this.postPrivate(
|
||||
'/option/usdc/openapi/private/v1/query-position-exp-date',
|
||||
params
|
||||
);
|
||||
}
|
||||
|
||||
/** -> Market Maker Protection */
|
||||
|
||||
/** modifyMMP */
|
||||
modifyMMP(params?: unknown): Promise<APIResponseWithTime<any>> {
|
||||
return this.postPrivate(
|
||||
'/option/usdc/openapi/private/v1/mmp-modify',
|
||||
params
|
||||
);
|
||||
}
|
||||
|
||||
/** resetMMP */
|
||||
resetMMP(currency: string): Promise<APIResponseWithTime<any>> {
|
||||
return this.postPrivate('/option/usdc/openapi/private/v1/mmp-reset', {
|
||||
currency,
|
||||
});
|
||||
}
|
||||
|
||||
/** queryMMPState */
|
||||
queryMMPState(baseCoin: string): Promise<APIResponseWithTime<any>> {
|
||||
return this.postPrivate('/option/usdc/openapi/private/v1/get-mmp-state', {
|
||||
baseCoin,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* API Data Endpoints
|
||||
*
|
||||
*/
|
||||
|
||||
getServerTime(): Promise<APIResponseWithTime> {
|
||||
return this.get('/v2/public/time');
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,7 @@ import {
|
||||
// });
|
||||
|
||||
interface SignedRequestContext {
|
||||
timestamp: number;
|
||||
timestamp?: number;
|
||||
api_key?: string;
|
||||
recv_window?: number;
|
||||
// spot is diff from the rest...
|
||||
@@ -30,9 +30,19 @@ interface SignedRequestContext {
|
||||
interface SignedRequest<T> {
|
||||
originalParams: T & SignedRequestContext;
|
||||
paramsWithSign?: T & SignedRequestContext & { sign: string };
|
||||
serializedParams: string;
|
||||
sign: string;
|
||||
timestamp: number;
|
||||
recvWindow: number;
|
||||
}
|
||||
|
||||
interface UnsignedRequest<T> {
|
||||
originalParams: T;
|
||||
paramsWithSign: T;
|
||||
}
|
||||
|
||||
type SignMethod = 'keyInBody' | 'usdc';
|
||||
|
||||
export default abstract class BaseRestClient {
|
||||
private timeOffset: number | null;
|
||||
private syncTimePromise: null | Promise<any>;
|
||||
@@ -61,8 +71,7 @@ export default abstract class BaseRestClient {
|
||||
|
||||
this.options = {
|
||||
recv_window: 5000,
|
||||
|
||||
/** Throw errors if any params are undefined */
|
||||
/** Throw errors if any request params are empty */
|
||||
strict_param_validation: false,
|
||||
/** Disable time sync by default */
|
||||
enable_time_sync: false,
|
||||
@@ -102,6 +111,10 @@ export default abstract class BaseRestClient {
|
||||
return this.clientType === REST_CLIENT_TYPE_ENUM.spot;
|
||||
}
|
||||
|
||||
private isUSDCClient() {
|
||||
return this.clientType === REST_CLIENT_TYPE_ENUM.usdcOptions;
|
||||
}
|
||||
|
||||
get(endpoint: string, params?: any) {
|
||||
return this._call('GET', endpoint, params, true);
|
||||
}
|
||||
@@ -122,7 +135,24 @@ export default abstract class BaseRestClient {
|
||||
return this._call('DELETE', endpoint, params, false);
|
||||
}
|
||||
|
||||
private async prepareSignParams(params?: any, isPublicApi?: boolean) {
|
||||
private async prepareSignParams<TParams = any>(
|
||||
method: Method,
|
||||
signMethod: SignMethod,
|
||||
params?: TParams,
|
||||
isPublicApi?: true
|
||||
): Promise<UnsignedRequest<TParams>>;
|
||||
private async prepareSignParams<TParams = any>(
|
||||
method: Method,
|
||||
signMethod: SignMethod,
|
||||
params?: TParams,
|
||||
isPublicApi?: false | undefined
|
||||
): Promise<SignedRequest<TParams>>;
|
||||
private async prepareSignParams<TParams = any>(
|
||||
method: Method,
|
||||
signMethod: SignMethod,
|
||||
params?: TParams,
|
||||
isPublicApi?: boolean
|
||||
) {
|
||||
if (isPublicApi) {
|
||||
return {
|
||||
originalParams: params,
|
||||
@@ -138,7 +168,85 @@ export default abstract class BaseRestClient {
|
||||
await this.syncTime();
|
||||
}
|
||||
|
||||
return this.signRequest(params);
|
||||
return this.signRequest(params, method, signMethod);
|
||||
}
|
||||
|
||||
/** Returns an axios request object. Handles signing process automatically if this is a private API call */
|
||||
private async buildRequest(
|
||||
method: Method,
|
||||
url: string,
|
||||
params?: any,
|
||||
isPublicApi?: boolean
|
||||
): Promise<AxiosRequestConfig> {
|
||||
const options: AxiosRequestConfig = {
|
||||
...this.globalRequestOptions,
|
||||
url: url,
|
||||
method: method,
|
||||
};
|
||||
|
||||
for (const key in params) {
|
||||
if (typeof params[key] === 'undefined') {
|
||||
delete params[key];
|
||||
}
|
||||
}
|
||||
|
||||
if (isPublicApi) {
|
||||
return {
|
||||
...options,
|
||||
params: params,
|
||||
};
|
||||
}
|
||||
|
||||
// USDC Options uses a different way of authenticating requests (headers instead of params)
|
||||
if (this.isUSDCClient()) {
|
||||
if (!options.headers) {
|
||||
options.headers = {};
|
||||
}
|
||||
|
||||
const signResult = await this.prepareSignParams(
|
||||
method,
|
||||
'usdc',
|
||||
params,
|
||||
isPublicApi
|
||||
);
|
||||
|
||||
options.headers['X-BAPI-SIGN-TYPE'] = 2;
|
||||
options.headers['X-BAPI-API-KEY'] = this.key;
|
||||
options.headers['X-BAPI-TIMESTAMP'] = signResult.timestamp;
|
||||
options.headers['X-BAPI-SIGN'] = signResult.sign;
|
||||
options.headers['X-BAPI-RECV-WINDOW'] = signResult.recvWindow;
|
||||
|
||||
if (method === 'GET') {
|
||||
return {
|
||||
...options,
|
||||
params: signResult.originalParams,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
...options,
|
||||
data: signResult.originalParams,
|
||||
};
|
||||
}
|
||||
|
||||
const signResult = await this.prepareSignParams(
|
||||
method,
|
||||
'keyInBody',
|
||||
params,
|
||||
isPublicApi
|
||||
);
|
||||
|
||||
if (method === 'GET' || this.isSpotClient()) {
|
||||
return {
|
||||
...options,
|
||||
params: signResult.paramsWithSign,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
...options,
|
||||
data: signResult.paramsWithSign,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -150,27 +258,20 @@ export default abstract class BaseRestClient {
|
||||
params?: any,
|
||||
isPublicApi?: boolean
|
||||
): Promise<any> {
|
||||
const options = {
|
||||
...this.globalRequestOptions,
|
||||
url: [this.baseUrl, endpoint].join(endpoint.startsWith('/') ? '' : '/'),
|
||||
method: method,
|
||||
json: true,
|
||||
};
|
||||
// Sanity check to make sure it's only ever signed by
|
||||
const requestUrl = [this.baseUrl, endpoint].join(
|
||||
endpoint.startsWith('/') ? '' : '/'
|
||||
);
|
||||
|
||||
for (const key in params) {
|
||||
if (typeof params[key] === 'undefined') {
|
||||
delete params[key];
|
||||
}
|
||||
}
|
||||
|
||||
const signResult = await this.prepareSignParams(params, isPublicApi);
|
||||
|
||||
if (method === 'GET' || this.isSpotClient()) {
|
||||
options.params = signResult.paramsWithSign;
|
||||
} else {
|
||||
options.data = signResult.paramsWithSign;
|
||||
}
|
||||
// Build a request and handle signature process
|
||||
const options = await this.buildRequest(
|
||||
method,
|
||||
requestUrl,
|
||||
params,
|
||||
isPublicApi
|
||||
);
|
||||
|
||||
// Dispatch request
|
||||
return axios(options)
|
||||
.then((response) => {
|
||||
if (response.status == 200) {
|
||||
@@ -215,37 +316,70 @@ export default abstract class BaseRestClient {
|
||||
/**
|
||||
* @private sign request and set recv window
|
||||
*/
|
||||
private async signRequest<T extends Object>(
|
||||
data: T & SignedRequestContext
|
||||
private async signRequest<T = {}>(
|
||||
data: T,
|
||||
method: Method,
|
||||
signMethod: SignMethod
|
||||
): Promise<SignedRequest<T>> {
|
||||
const timestamp = Date.now() + (this.timeOffset || 0);
|
||||
|
||||
const res: SignedRequest<T> = {
|
||||
originalParams: {
|
||||
...data,
|
||||
api_key: this.key,
|
||||
timestamp: Date.now() + (this.timeOffset || 0),
|
||||
},
|
||||
sign: '',
|
||||
timestamp,
|
||||
recvWindow: 0,
|
||||
serializedParams: '',
|
||||
};
|
||||
|
||||
// Optional, set to 5000 by default. Increase if timestamp/recv_window errors are seen.
|
||||
if (this.options.recv_window && !res.originalParams.recv_window) {
|
||||
if (this.isSpotClient()) {
|
||||
res.originalParams.recvWindow = this.options.recv_window;
|
||||
} else {
|
||||
res.originalParams.recv_window = this.options.recv_window;
|
||||
}
|
||||
if (!this.key || !this.secret) {
|
||||
return res;
|
||||
}
|
||||
const key = this.key;
|
||||
const recvWindow =
|
||||
res.originalParams.recv_window || this.options.recv_window || 5000;
|
||||
const strictParamValidation = this.options.strict_param_validation;
|
||||
|
||||
// In case the parent function needs it (e.g. USDC uses a header)
|
||||
res.recvWindow = recvWindow;
|
||||
|
||||
// usdc is different for some reason
|
||||
if (signMethod === 'usdc') {
|
||||
const signRequestParams =
|
||||
method === 'GET'
|
||||
? serializeParams(res.originalParams, strictParamValidation)
|
||||
: JSON.stringify(res.originalParams);
|
||||
|
||||
const paramsStr = timestamp + key + recvWindow + signRequestParams;
|
||||
res.sign = await signMessage(paramsStr, this.secret);
|
||||
return res;
|
||||
}
|
||||
|
||||
if (this.key && this.secret) {
|
||||
const serializedParams = serializeParams(
|
||||
// spot/v2 derivatives
|
||||
if (signMethod === 'keyInBody') {
|
||||
res.originalParams.api_key = key;
|
||||
res.originalParams.timestamp = timestamp;
|
||||
|
||||
// Optional, set to 5000 by default. Increase if timestamp/recv_window errors are seen.
|
||||
if (recvWindow) {
|
||||
if (this.isSpotClient()) {
|
||||
res.originalParams.recvWindow = recvWindow;
|
||||
} else {
|
||||
res.originalParams.recv_window = recvWindow;
|
||||
}
|
||||
}
|
||||
|
||||
res.serializedParams = serializeParams(
|
||||
res.originalParams,
|
||||
this.options.strict_param_validation
|
||||
strictParamValidation
|
||||
);
|
||||
res.sign = await signMessage(serializedParams, this.secret);
|
||||
res.sign = await signMessage(res.serializedParams, this.secret);
|
||||
res.paramsWithSign = {
|
||||
...res.originalParams,
|
||||
sign: res.sign,
|
||||
};
|
||||
return res;
|
||||
}
|
||||
|
||||
return res;
|
||||
|
||||
3
src/util/index.ts
Normal file
3
src/util/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from './BaseRestClient';
|
||||
export * from './requestUtils';
|
||||
export * from './WsStore';
|
||||
@@ -45,8 +45,8 @@ export function serializeParams(
|
||||
export function getRestBaseUrl(
|
||||
useLivenet: boolean,
|
||||
restInverseOptions: RestClientOptions
|
||||
) {
|
||||
const baseUrlsInverse = {
|
||||
): string {
|
||||
const exchangeBaseUrls = {
|
||||
livenet: 'https://api.bybit.com',
|
||||
testnet: 'https://api-testnet.bybit.com',
|
||||
};
|
||||
@@ -56,9 +56,9 @@ export function getRestBaseUrl(
|
||||
}
|
||||
|
||||
if (useLivenet === true) {
|
||||
return baseUrlsInverse.livenet;
|
||||
return exchangeBaseUrls.livenet;
|
||||
}
|
||||
return baseUrlsInverse.testnet;
|
||||
return exchangeBaseUrls.testnet;
|
||||
}
|
||||
|
||||
export function isPublicEndpoint(endpoint: string): boolean {
|
||||
@@ -92,11 +92,16 @@ export function isWsPong(response: any) {
|
||||
|
||||
export const agentSource = 'bybitapinode';
|
||||
|
||||
/**
|
||||
* Used to switch how authentication/requests work under the hood (primarily for SPOT since it's different there)
|
||||
*/
|
||||
export const REST_CLIENT_TYPE_ENUM = {
|
||||
accountAsset: 'accountAsset',
|
||||
inverse: 'inverse',
|
||||
inverseFutures: 'inverseFutures',
|
||||
linear: 'linear',
|
||||
spot: 'spot',
|
||||
usdcOptions: 'usdcOptions',
|
||||
} as const;
|
||||
|
||||
export type RestClientType =
|
||||
|
||||
Reference in New Issue
Block a user