-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
linear-client.ts
391 lines (331 loc) · 10.3 KB
/
linear-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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/* eslint-disable @typescript-eslint/no-explicit-any */
import { REST_CLIENT_TYPE_ENUM } from './util/requestUtils';
import {
APIResponse,
APIResponseWithTime,
AssetExchangeRecordsReq,
CoinParam,
LinearCancelConditionalOrderRequest,
LinearCancelOrderRequest,
LinearConditionalOrderRequest,
LinearGetClosedPnlRequest,
LinearGetConditionalOrderRequest,
LinearGetHistoryTradeRecordsRequest,
LinearGetOrderRequest,
LinearGetOrdersRequest,
LinearGetTradeRecordsRequest,
LinearOrder,
LinearQueryConditionalOrderRequest,
LinearReplaceConditionalOrderRequest,
LinearReplaceOrderRequest,
LinearSetAddReduceMarginRequest,
LinearSetAutoAddMarginRequest,
LinearSetMarginSwitchRequest,
LinearSetPositionModeRequest,
LinearSetPositionTpSlModeRequest,
LinearSetRiskLimitRequest,
LinearSetTradingStopRequest,
LinearSetUserLeverageRequest,
NewLinearOrder,
PerpPosition,
PerpPositionRoot,
SymbolInfo,
SymbolIntervalFromLimitParam,
SymbolLimitParam,
SymbolParam,
SymbolPeriodLimitParam,
WalletBalances,
WalletFundRecordsReq,
WithdrawRecordsReq,
} from './types';
import BaseRestClient from './util/BaseRestClient';
/**
* REST API client for linear/USD perpetual futures APIs (v2)
*
* @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 LinearClient extends BaseRestClient {
getClientType() {
return REST_CLIENT_TYPE_ENUM.linear;
}
async fetchServerTime(): Promise<number> {
const timeRes = await this.getServerTime();
return Number(timeRes.time_now);
}
/**
*
* Market Data Endpoints
*
*/
getOrderBook(params: SymbolParam): Promise<APIResponseWithTime<any[]>> {
return this.get('v2/public/orderBook/L2', params);
}
getKline(
params: SymbolIntervalFromLimitParam,
): Promise<APIResponseWithTime<any[]>> {
return this.get('public/linear/kline', params);
}
/**
* Get latest information for symbol
*/
getTickers(
params?: Partial<SymbolParam>,
): Promise<APIResponseWithTime<any[]>> {
return this.get('v2/public/tickers', params);
}
getTrades(params: SymbolLimitParam): Promise<APIResponseWithTime<any[]>> {
return this.get('public/linear/recent-trading-records', params);
}
getSymbols(): Promise<APIResponse<SymbolInfo[]>> {
return this.get('v2/public/symbols');
}
getLastFundingRate(params: SymbolParam): Promise<APIResponseWithTime<any[]>> {
return this.get('public/linear/funding/prev-funding-rate', params);
}
getMarkPriceKline(
params: SymbolIntervalFromLimitParam,
): Promise<APIResponseWithTime<any[]>> {
return this.get('public/linear/mark-price-kline', params);
}
getIndexPriceKline(
params: SymbolIntervalFromLimitParam,
): Promise<APIResponseWithTime<any[]>> {
return this.get('public/linear/index-price-kline', params);
}
getPremiumIndexKline(
params: SymbolIntervalFromLimitParam,
): Promise<APIResponseWithTime<any[]>> {
return this.get('public/linear/premium-index-kline', params);
}
/**
*
* Market Data : Advanced
*
*/
getOpenInterest(
params: SymbolPeriodLimitParam,
): Promise<APIResponseWithTime<any[]>> {
return this.get('v2/public/open-interest', params);
}
getLatestBigDeal(
params: SymbolLimitParam,
): Promise<APIResponseWithTime<any[]>> {
return this.get('v2/public/big-deal', params);
}
getLongShortRatio(
params: SymbolPeriodLimitParam,
): Promise<APIResponseWithTime<any[]>> {
return this.get('v2/public/account-ratio', params);
}
/**
*
* Account Data Endpoints
*
*/
getApiKeyInfo(): Promise<APIResponseWithTime<any>> {
return this.getPrivate('v2/private/account/api-key');
}
/**
*
* Wallet Data Endpoints
*
*/
getWalletBalance(
params?: Partial<CoinParam>,
): Promise<APIResponseWithTime<WalletBalances>> {
return this.getPrivate('v2/private/wallet/balance', params);
}
getWalletFundRecords(
params?: WalletFundRecordsReq,
): Promise<APIResponseWithTime<any>> {
return this.getPrivate('v2/private/wallet/fund/records', params);
}
getWithdrawRecords(
params?: WithdrawRecordsReq,
): Promise<APIResponseWithTime<any>> {
return this.getPrivate('v2/private/wallet/withdraw/list', params);
}
getAssetExchangeRecords(
params?: AssetExchangeRecordsReq,
): Promise<APIResponseWithTime<any>> {
return this.getPrivate('v2/private/exchange-order/list', params);
}
/**
*
* API Data Endpoints
*
*/
getServerTime(): Promise<APIResponseWithTime<{}>> {
return this.get('v2/public/time');
}
getApiAnnouncements(): Promise<APIResponseWithTime<any>> {
return this.get('v2/public/announcement');
}
/**
*
* Account Data Endpoints
*
*/
placeActiveOrder(
params: NewLinearOrder,
): Promise<APIResponseWithTime<LinearOrder | null>> {
return this.postPrivate('private/linear/order/create', params);
}
getActiveOrderList(
params: LinearGetOrdersRequest,
): Promise<APIResponseWithTime<any>> {
return this.getPrivate('private/linear/order/list', params);
}
cancelActiveOrder(
params: LinearCancelOrderRequest,
): Promise<APIResponseWithTime<any>> {
return this.postPrivate('private/linear/order/cancel', params);
}
cancelAllActiveOrders(
params: SymbolParam,
): Promise<APIResponseWithTime<any>> {
return this.postPrivate('private/linear/order/cancel-all', params);
}
replaceActiveOrder(
params: LinearReplaceOrderRequest,
): Promise<APIResponseWithTime<any>> {
return this.postPrivate('private/linear/order/replace', params);
}
queryActiveOrder(
params: LinearGetOrderRequest,
): Promise<APIResponseWithTime<any>> {
return this.getPrivate('private/linear/order/search', params);
}
/**
* Conditional orders
*/
placeConditionalOrder(
params: LinearConditionalOrderRequest,
): Promise<APIResponseWithTime<any>> {
return this.postPrivate('private/linear/stop-order/create', params);
}
getConditionalOrder(
params: LinearGetConditionalOrderRequest,
): Promise<APIResponseWithTime<any>> {
return this.getPrivate('private/linear/stop-order/list', params);
}
cancelConditionalOrder(
params: LinearCancelConditionalOrderRequest,
): Promise<APIResponseWithTime<any>> {
return this.postPrivate('private/linear/stop-order/cancel', params);
}
cancelAllConditionalOrders(
params: SymbolParam,
): Promise<APIResponseWithTime<any>> {
return this.postPrivate('private/linear/stop-order/cancel-all', params);
}
replaceConditionalOrder(
params: LinearReplaceConditionalOrderRequest,
): Promise<APIResponseWithTime<any>> {
return this.postPrivate('private/linear/stop-order/replace', params);
}
queryConditionalOrder(
params: LinearQueryConditionalOrderRequest,
): Promise<APIResponseWithTime<any>> {
return this.getPrivate('private/linear/stop-order/search', params);
}
/**
* Position
*/
getPosition(): Promise<APIResponseWithTime<PerpPositionRoot[]>>;
getPosition(
params: Partial<SymbolParam>,
): Promise<APIResponseWithTime<PerpPosition[]>>;
getPosition(
params?: Partial<SymbolParam>,
): Promise<APIResponseWithTime<PerpPosition[] | PerpPositionRoot[]>> {
return this.getPrivate('private/linear/position/list', params);
}
setAutoAddMargin(
params?: LinearSetAutoAddMarginRequest,
): Promise<APIResponseWithTime<any>> {
return this.postPrivate(
'private/linear/position/set-auto-add-margin',
params,
);
}
setMarginSwitch(
params?: LinearSetMarginSwitchRequest,
): Promise<APIResponseWithTime<any>> {
return this.postPrivate('private/linear/position/switch-isolated', params);
}
/**
* Switch between one-way vs hedge mode. Use `linearPositionModeEnum` for the mode parameter.
*/
setPositionMode(
params: LinearSetPositionModeRequest,
): Promise<APIResponseWithTime<any>> {
return this.postPrivate('private/linear/position/switch-mode', params);
}
/**
* Switch TP/SL mode between full or partial. When set to Partial, TP/SL orders may have a quantity less than the position size.
* This is set with the setTradingStop() method. Use `positionTpSlModeEnum` for the tp_sl_mode parameter.
*/
setPositionTpSlMode(
params: LinearSetPositionTpSlModeRequest,
): Promise<APIResponseWithTime<any>> {
return this.postPrivate('private/linear/tpsl/switch-mode', params);
}
setAddReduceMargin(
params?: LinearSetAddReduceMarginRequest,
): Promise<APIResponseWithTime<any>> {
return this.postPrivate('private/linear/position/add-margin', params);
}
setUserLeverage(
params: LinearSetUserLeverageRequest,
): Promise<APIResponseWithTime<any>> {
return this.postPrivate('private/linear/position/set-leverage', params);
}
setTradingStop(
params: LinearSetTradingStopRequest,
): Promise<APIResponseWithTime<any>> {
return this.postPrivate('private/linear/position/trading-stop', params);
}
getTradeRecords(
params: LinearGetTradeRecordsRequest,
): Promise<APIResponseWithTime<any>> {
return this.getPrivate('private/linear/trade/execution/list', params);
}
getHistoryTradeRecords(
params: LinearGetHistoryTradeRecordsRequest,
): Promise<APIResponseWithTime<any>> {
return this.getPrivate(
'/private/linear/trade/execution/history-list',
params,
);
}
getClosedPnl(
params: LinearGetClosedPnlRequest,
): Promise<APIResponseWithTime<any>> {
return this.getPrivate('private/linear/trade/closed-pnl/list', params);
}
/**
* Risk Limit
*/
getRiskLimitList(params: SymbolParam): Promise<APIResponseWithTime<any>> {
return this.getPrivate('public/linear/risk-limit', params);
}
setRiskLimit(
params: LinearSetRiskLimitRequest,
): Promise<APIResponseWithTime<any>> {
return this.postPrivate('private/linear/position/set-risk', params);
}
/**
* Funding
*/
getPredictedFundingFee(
params: SymbolParam,
): Promise<APIResponseWithTime<any>> {
return this.getPrivate('private/linear/funding/predicted-funding', params);
}
getLastFundingFee(params: SymbolParam): Promise<APIResponseWithTime<any>> {
return this.getPrivate('private/linear/funding/prev-funding', params);
}
}