-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
usdc-option-client.ts
337 lines (299 loc) · 9.82 KB
/
usdc-option-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
/* eslint-disable @typescript-eslint/no-explicit-any */
import {
APIResponseV3,
APIResponseWithTime,
USDCOptionsActiveOrdersRealtimeRequest,
USDCOptionsActiveOrdersRequest,
USDCOptionsCancelAllOrdersRequest,
USDCOptionsCancelOrderRequest,
USDCOptionsContractInfoRequest,
USDCOptionsDeliveryHistoryRequest,
USDCOptionsDeliveryPriceRequest,
USDCOptionsHistoricOrdersRequest,
USDCOptionsHistoricalVolatilityRequest,
USDCOptionsModifyMMPRequest,
USDCOptionsModifyOrderRequest,
USDCOptionsOrderExecutionRequest,
USDCOptionsOrderRequest,
USDCOptionsPositionsInfoExpiryRequest,
USDCOptionsRecentTradesRequest,
USDCPositionsRequest,
USDCTransactionLogRequest,
} from './types';
import { REST_CLIENT_TYPE_ENUM } from './util';
import BaseRestClient from './util/BaseRestClient';
/**
* REST API client for USDC Option APIs
*
* @deprecated WARNING: V1/V2 private endpoints (Rest API & Websocket Stream) for mainnet
* will be switched off gradually from 30 Oct 2023 UTC, so they are not promised a stability.
* Please note that you are at your own risk of using old endpoints going forward, and please move to V5 ASAP.
*/
export class USDCOptionClient extends BaseRestClient {
getClientType() {
return REST_CLIENT_TYPE_ENUM.v3;
}
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<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<APIResponseV3<any>> {
return this.get('/option/usdc/openapi/public/v1/symbols', params);
}
/** Get a symbol price/statistics ticker */
getSymbolTicker(symbol: string): Promise<APIResponseV3<any>> {
return this.get('/option/usdc/openapi/public/v1/tick', { symbol });
}
/** Get delivery information */
getDeliveryPrice(
params?: USDCOptionsDeliveryPriceRequest,
): 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<APIResponseV3<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?: USDCOptionsHistoricalVolatilityRequest,
): Promise<APIResponseV3<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: USDCOptionsOrderRequest): Promise<APIResponseV3<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: USDCOptionsOrderRequest[],
): Promise<APIResponseV3<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: USDCOptionsModifyOrderRequest,
): Promise<APIResponseV3<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: USDCOptionsModifyOrderRequest[],
): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/batch-replace-orders',
{ replaceOrderRequest },
);
}
/** Cancel order */
cancelOrder(
params: USDCOptionsCancelOrderRequest,
): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/cancel-order',
params,
);
}
/** Batch cancel orders */
batchCancelOrders(
cancelRequest: USDCOptionsCancelOrderRequest[],
): Promise<APIResponseV3<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?: USDCOptionsCancelAllOrdersRequest,
): Promise<APIResponseV3<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?: USDCOptionsActiveOrdersRealtimeRequest,
): Promise<APIResponseV3<any>> {
return this.getPrivate(
'/option/usdc/openapi/private/v1/trade/query-active-orders',
params,
);
}
/** Query Unfilled/Partially Filled Orders */
getActiveOrders(
params: USDCOptionsActiveOrdersRequest,
): Promise<APIResponseV3<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: USDCOptionsHistoricOrdersRequest,
): Promise<APIResponseV3<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: USDCOptionsOrderExecutionRequest,
): Promise<APIResponseV3<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: USDCTransactionLogRequest,
): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/query-transaction-log',
params,
);
}
/** Wallet info for USDC account. */
getBalances(): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/query-wallet-balance',
);
}
/** Asset Info */
getAssetInfo(baseCoin?: string): Promise<APIResponseV3<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<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/private/asset/account/setMarginMode',
{ setMarginMode: newMarginMode },
);
}
/** Query margin mode for USDC account. */
getMarginMode(): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/query-margin-info',
);
}
/** -> Positions API */
/** Query my positions */
getPositions(params: USDCPositionsRequest): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/query-position',
params,
);
}
/** Query Delivery History */
getDeliveryHistory(
params: USDCOptionsDeliveryHistoryRequest,
): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/query-delivery-list',
params,
);
}
/** Query Positions Info Upon Expiry */
getPositionsInfoUponExpiry(
params?: USDCOptionsPositionsInfoExpiryRequest,
): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/query-position-exp-date',
params,
);
}
/** -> Market Maker Protection */
/** modifyMMP */
modifyMMP(params: USDCOptionsModifyMMPRequest): Promise<APIResponseV3<any>> {
return this.postPrivate(
'/option/usdc/openapi/private/v1/mmp-modify',
params,
);
}
/** resetMMP */
resetMMP(currency: string): Promise<APIResponseV3<any>> {
return this.postPrivate('/option/usdc/openapi/private/v1/mmp-reset', {
currency,
});
}
/** queryMMPState */
queryMMPState(baseCoin: string): Promise<APIResponseV3<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');
}
}