-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
contract-client.ts
364 lines (324 loc) · 10.4 KB
/
contract-client.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/* eslint-disable @typescript-eslint/no-explicit-any */
import {
APIResponseV3,
APIResponseWithTime,
ContractActiveOrdersRequest,
ContractCancelOrderRequest,
ContractClosedPNLRequest,
ContractHistoricOrder,
ContractHistoricOrdersRequest,
ContractListResult,
ContractModifyOrderRequest,
ContractOrderRequest,
ContractPositionsRequest,
ContractSetAutoAddMarginRequest,
ContractSetMarginSwitchRequest,
ContractSetPositionModeRequest,
ContractSetTPSLRequest,
ContractSymbolTicker,
ContractUserExecutionHistoryRequest,
ContractWalletFundRecordRequest,
PaginatedResult,
UMCandlesRequest,
UMCategory,
UMFundingRateHistoryRequest,
UMInstrumentInfoRequest,
UMOpenInterestRequest,
UMOptionDeliveryPriceRequest,
UMPublicTradesRequest,
} from './types';
import { REST_CLIENT_TYPE_ENUM } from './util';
import BaseRestClient from './util/BaseRestClient';
/**
* REST API client for Derivatives V3 Contract APIs
* @deprecated WARNING
* These endpoints are being switched off gradually and are expected to be completely turned off by the end of 2024.
* They may stop working at any point before then.
* Please update your code as soon as possible to use the V5 APIs instead.
*/
export class ContractClient 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 : these seem exactly the same as the unified margin market data endpoints
*
*/
/** Query order book info. Each side has a depth of 25 orders. */
getOrderBook(
symbol: string,
category?: string,
limit?: number,
): Promise<APIResponseV3<any>> {
return this.get('/derivatives/v3/public/order-book/L2', {
category,
symbol,
limit,
});
}
/** Get candles/klines */
getCandles(params: UMCandlesRequest): Promise<APIResponseV3<any>> {
return this.get('/derivatives/v3/public/kline', params);
}
/** Get a symbol price/statistics ticker */
getSymbolTicker(
category: UMCategory | '',
symbol?: string,
): Promise<APIResponseV3<ContractListResult<ContractSymbolTicker>>> {
return this.get('/derivatives/v3/public/tickers', { category, symbol });
}
/** Get trading rules per symbol/contract, incl price/amount/value/leverage filters */
getInstrumentInfo(
params: UMInstrumentInfoRequest,
): Promise<APIResponseV3<any>> {
return this.get('/derivatives/v3/public/instruments-info', params);
}
/** Query mark price kline (like getCandles() but for mark price). */
getMarkPriceCandles(params: UMCandlesRequest): Promise<APIResponseV3<any>> {
return this.get('/derivatives/v3/public/mark-price-kline', params);
}
/** Query Index Price Kline */
getIndexPriceCandles(params: UMCandlesRequest): Promise<APIResponseV3<any>> {
return this.get('/derivatives/v3/public/index-price-kline', params);
}
/**
* The funding rate is generated every 8 hours at 00:00 UTC, 08:00 UTC and 16:00 UTC.
* For example, if a request is sent at 12:00 UTC, the funding rate generated earlier that day at 08:00 UTC will be sent.
*/
getFundingRateHistory(
params: UMFundingRateHistoryRequest,
): Promise<APIResponseV3<any>> {
return this.get(
'/derivatives/v3/public/funding/history-funding-rate',
params,
);
}
/** Get Risk Limit */
getRiskLimit(
category: UMCategory,
symbol: string,
): Promise<APIResponseV3<any>> {
return this.get('/derivatives/v3/public/risk-limit/list', {
category,
symbol,
});
}
/** Get option delivery price */
getOptionDeliveryPrice(
params: UMOptionDeliveryPriceRequest,
): Promise<APIResponseV3<any>> {
return this.get('/derivatives/v3/public/delivery-price', params);
}
/** Get public trading history */
getTrades(params: UMPublicTradesRequest): Promise<APIResponseV3<any>> {
return this.get('/derivatives/v3/public/recent-trade', params);
}
/**
* Gets the total amount of unsettled contracts.
* In other words, the total number of contracts held in open positions.
*/
getOpenInterest(params: UMOpenInterestRequest): Promise<APIResponseV3<any>> {
return this.get('/derivatives/v3/public/open-interest', params);
}
/**
*
* Contract Account Endpoints
*
*/
/** -> Order API */
/** Place an order */
submitOrder(params: ContractOrderRequest): Promise<APIResponseV3<any>> {
return this.postPrivate('/contract/v3/private/order/create', params);
}
/**
* Query order history.
*
* As order creation/cancellation is asynchronous, the data returned from the interface may be delayed.
* To access order information in real-time, call getActiveOrders().
*/
getHistoricOrders(
params: ContractHistoricOrdersRequest,
): Promise<APIResponseV3<PaginatedResult<ContractHistoricOrder>>> {
return this.getPrivate('/contract/v3/private/order/list', params);
}
/** Cancel order */
cancelOrder(params: ContractCancelOrderRequest): Promise<APIResponseV3<any>> {
return this.postPrivate('/contract/v3/private/order/cancel', params);
}
/** Cancel all orders */
cancelAllOrders(symbol: string): Promise<APIResponseV3<any>> {
return this.postPrivate('/contract/v3/private/order/cancel-all', {
symbol,
});
}
/**
* Replace order
*
* 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: ContractModifyOrderRequest): Promise<APIResponseV3<any>> {
return this.postPrivate('/contract/v3/private/order/replace', params);
}
/** Query Open Order(s) (real-time) */
getActiveOrders(
params: ContractActiveOrdersRequest,
): Promise<APIResponseV3<any>> {
return this.getPrivate(
'/contract/v3/private/order/unfilled-orders',
params,
);
}
/** -> Positions API */
/**
* Query my positions real-time. Accessing personal list of positions.
* Either symbol or settleCoin is required.
* Users can access their position holding information through this interface, such as the number of position holdings and wallet balance.
*/
getPositions(params?: ContractPositionsRequest): Promise<APIResponseV3<any>> {
return this.getPrivate('/contract/v3/private/position/list', params);
}
/** Set auto add margin, or Auto-Margin Replenishment. */
setAutoAddMargin(
params: ContractSetAutoAddMarginRequest,
): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/contract/v3/private/position/set-auto-add-margin',
params,
);
}
/** Switch cross margin mode/isolated margin mode */
setMarginSwitch(
params: ContractSetMarginSwitchRequest,
): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/contract/v3/private/position/switch-isolated',
params,
);
}
/** Supports switching between One-Way Mode and Hedge Mode at the coin level. */
setPositionMode(
params: ContractSetPositionModeRequest,
): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/contract/v3/private/position/switch-mode',
params,
);
}
/**
* Switch mode between Full or Partial
*/
setTPSLMode(
symbol: string,
tpSlMode: 'Full' | 'Partial',
): Promise<APIResponseV3<any>> {
return this.postPrivate('/contract/v3/private/position/switch-tpsl-mode', {
symbol,
tpSlMode,
});
}
/** Leverage setting. */
setLeverage(
symbol: string,
buyLeverage: string,
sellLeverage: string,
): Promise<APIResponseV3<any>> {
return this.postPrivate('/contract/v3/private/position/set-leverage', {
symbol,
buyLeverage,
sellLeverage,
});
}
/**
* Set take profit, stop loss, and trailing stop for your open position.
* If using partial mode, TP/SL/TS orders will not close your entire position.
*/
setTPSL(params: ContractSetTPSLRequest): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/contract/v3/private/position/trading-stop',
params,
);
}
/** Set risk limit */
setRiskLimit(
symbol: string,
riskId: number,
/** 0-one-way, 1-buy side, 2-sell side */
positionIdx: 0 | 1 | 2,
): Promise<APIResponseV3<any>> {
return this.postPrivate('/contract/v3/private/position/set-risk-limit', {
symbol,
riskId,
positionIdx,
});
}
/**
* Get user's trading records.
* The results are ordered in descending order (the first item is the latest). Returns records up to 2 years old.
*/
getUserExecutionHistory(
params: ContractUserExecutionHistoryRequest,
): Promise<APIResponseV3<any>> {
return this.getPrivate('/contract/v3/private/execution/list', params);
}
/**
* Get user's closed profit and loss records.
* The results are ordered in descending order (the first item is the latest).
*/
getClosedProfitAndLoss(
params: ContractClosedPNLRequest,
): Promise<APIResponseV3<any>> {
return this.getPrivate('/contract/v3/private/position/closed-pnl', params);
}
/** Get the information of open interest limit. */
getOpenInterestLimitInfo(symbol: string): Promise<APIResponseV3<any>> {
return this.getPrivate('/contract/v3/private/position/limit-info', {
symbol,
});
}
/** -> Account API */
/** Query wallet balance */
getBalances(coin?: string): Promise<APIResponseV3<any>> {
return this.getPrivate('/contract/v3/private/account/wallet/balance', {
coin,
});
}
/** Get user trading fee rate */
getTradingFeeRate(symbol?: string): Promise<APIResponseV3<any>> {
return this.getPrivate('/contract/v3/private/account/fee-rate', {
symbol,
});
}
/**
* Get wallet fund records.
* This endpoint also shows exchanges from the Asset Exchange,
* where the types for the exchange are ExchangeOrderWithdraw and ExchangeOrderDeposit.
*
* This endpoint returns incomplete information for transfers involving the derivatives wallet.
* Use the account asset API for creating and querying internal transfers.
*/
getWalletFundRecords(
params?: ContractWalletFundRecordRequest,
): Promise<APIResponseV3<any>> {
return this.getPrivate(
'/contract/v3/private/account/wallet/fund-records',
params,
);
}
/**
*
* API Data Endpoints
*
*/
getServerTime(): Promise<APIResponseWithTime> {
return this.get('/v2/public/time');
}
}