-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
spot-client-v3.ts
307 lines (268 loc) · 7.68 KB
/
spot-client-v3.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
/* eslint-disable @typescript-eslint/no-explicit-any */
import {
APIResponseV3,
APIResponseWithTime,
KlineInterval,
NewSpotOrderV3,
SpotBalances,
SpotCancelOrderBatchRequest,
SpotCrossMarginBorrowingInfoRequest,
SpotCrossMarginRepaymentHistoryRequest,
SpotLeveragedTokenPRHistoryRequest,
SpotMyTradesRequest,
SpotOrderQueryById,
} from './types';
import { REST_CLIENT_TYPE_ENUM } from './util';
import BaseRestClient from './util/BaseRestClient';
/**
* REST API client for newer Spot V3 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 SpotClientV3 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 */
getMergedOrderBook(
symbol: string,
scale?: number,
limit?: number,
): Promise<APIResponseV3<any>> {
return this.get('/spot/v3/public/quote/depth/merged', {
symbol,
scale,
limit,
});
}
/** 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(
symbol: string,
interval: KlineInterval,
limit?: number,
startTime?: number,
endTime?: number,
): Promise<APIResponseV3<any>> {
return this.get('/spot/v3/public/quote/kline', {
symbol,
interval,
limit,
startTime,
endTime,
});
}
/** 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: NewSpotOrderV3): 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: SpotCancelOrderBatchRequest,
): 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,
orderCategory?: 0 | 1,
): Promise<APIResponseV3<any>> {
return this.getPrivate('/spot/v3/private/open-orders', {
symbol,
orderId,
limit,
orderCategory,
});
}
/** Get order history */
getPastOrders(
symbol?: string,
orderId?: string,
limit?: number,
orderCategory?: 0 | 1,
): Promise<APIResponseV3<any>> {
return this.getPrivate('/spot/v3/private/history-orders', {
symbol,
orderId,
limit,
orderCategory,
});
}
/**
* 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?: SpotMyTradesRequest): 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?: SpotLeveragedTokenPRHistoryRequest,
): 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?: SpotCrossMarginBorrowingInfoRequest,
): 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?: SpotCrossMarginRepaymentHistoryRequest,
): Promise<APIResponseV3<any>> {
return this.getPrivate(
'/spot/v3/private/cross-margin-repay-history',
params,
);
}
}