bybit-api 3.6.1 → 3.7.1-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (93) hide show
  1. package/lib/contract-client.d.ts +17 -3
  2. package/lib/contract-client.js +17 -3
  3. package/lib/contract-client.js.map +1 -1
  4. package/lib/rest-client-v5.d.ts +259 -10
  5. package/lib/rest-client-v5.js +196 -7
  6. package/lib/rest-client-v5.js.map +1 -1
  7. package/lib/types/request/contract.d.ts +21 -11
  8. package/lib/types/request/index.d.ts +1 -0
  9. package/lib/types/request/index.js +1 -0
  10. package/lib/types/request/index.js.map +1 -1
  11. package/lib/types/request/v5-account.d.ts +5 -0
  12. package/lib/types/request/v5-market.d.ts +10 -2
  13. package/lib/types/request/v5-position.d.ts +13 -2
  14. package/lib/types/request/v5-pre-upgrade.d.ts +34 -0
  15. package/lib/types/request/v5-pre-upgrade.js +3 -0
  16. package/lib/types/request/v5-pre-upgrade.js.map +1 -0
  17. package/lib/types/request/v5-trade.d.ts +24 -1
  18. package/lib/types/response/contract.d.ts +22 -12
  19. package/lib/types/response/v5-account.d.ts +1 -0
  20. package/lib/types/response/v5-market.d.ts +6 -0
  21. package/lib/types/response/v5-position.d.ts +26 -0
  22. package/lib/types/response/v5-trade.d.ts +7 -0
  23. package/lib/types/v5-shared.d.ts +4 -1
  24. package/lib/util/websocket-util.d.ts +2 -1
  25. package/lib/util/websocket-util.js +2 -2
  26. package/lib/util/websocket-util.js.map +1 -1
  27. package/lib/websocket-client.js +1 -1
  28. package/lib/websocket-client.js.map +1 -1
  29. package/package.json +13 -1
  30. package/src/account-asset-client-v3.ts +310 -0
  31. package/src/account-asset-client.ts +149 -0
  32. package/src/constants/enum.ts +105 -0
  33. package/src/contract-client.ts +360 -0
  34. package/src/copy-trading-client.ts +159 -0
  35. package/src/index.ts +18 -0
  36. package/src/inverse-client.ts +337 -0
  37. package/src/inverse-futures-client.ts +403 -0
  38. package/src/linear-client.ts +387 -0
  39. package/src/rest-client-v5.ts +1638 -0
  40. package/src/spot-client-v3.ts +303 -0
  41. package/src/spot-client.ts +180 -0
  42. package/src/types/index.ts +5 -0
  43. package/src/types/request/account-asset.ts +258 -0
  44. package/src/types/request/contract.ts +139 -0
  45. package/src/types/request/copy-trading.ts +53 -0
  46. package/src/types/request/index.ts +18 -0
  47. package/src/types/request/inverse.ts +135 -0
  48. package/src/types/request/linear.ts +201 -0
  49. package/src/types/request/spot.ts +89 -0
  50. package/src/types/request/unified-margin.ts +247 -0
  51. package/src/types/request/usdc-options.ts +134 -0
  52. package/src/types/request/usdc-perp.ts +100 -0
  53. package/src/types/request/usdc-shared.ts +39 -0
  54. package/src/types/request/v5-account.ts +40 -0
  55. package/src/types/request/v5-asset.ts +133 -0
  56. package/src/types/request/v5-market.ts +127 -0
  57. package/src/types/request/v5-position.ts +107 -0
  58. package/src/types/request/v5-pre-upgrade.ts +37 -0
  59. package/src/types/request/v5-spot-leverage-token.ts +23 -0
  60. package/src/types/request/v5-trade.ts +133 -0
  61. package/src/types/request/v5-user.ts +21 -0
  62. package/src/types/response/account-asset.ts +236 -0
  63. package/src/types/response/contract.ts +74 -0
  64. package/src/types/response/index.ts +13 -0
  65. package/src/types/response/shared.ts +19 -0
  66. package/src/types/response/spot.ts +17 -0
  67. package/src/types/response/unified-margin.ts +71 -0
  68. package/src/types/response/usdt-perp.ts +58 -0
  69. package/src/types/response/v5-account.ts +122 -0
  70. package/src/types/response/v5-asset.ts +170 -0
  71. package/src/types/response/v5-market.ts +271 -0
  72. package/src/types/response/v5-position.ts +118 -0
  73. package/src/types/response/v5-spot-leverage-token.ts +70 -0
  74. package/src/types/response/v5-trade.ts +103 -0
  75. package/src/types/response/v5-user.ts +58 -0
  76. package/src/types/shared.ts +175 -0
  77. package/src/types/v5-shared.ts +213 -0
  78. package/src/types/websockets.ts +115 -0
  79. package/src/types/ws-events/failed-topic-subscription-confirmation.ts +6 -0
  80. package/src/types/ws-events/succeeded-topic-subscription-confirmation.ts +6 -0
  81. package/src/types/ws-events/topic-subscription-confirmation.ts +7 -0
  82. package/src/unified-margin-client.ts +400 -0
  83. package/src/usdc-option-client.ts +333 -0
  84. package/src/usdc-perpetual-client.ts +312 -0
  85. package/src/util/BaseRestClient.ts +502 -0
  86. package/src/util/WsStore.ts +160 -0
  87. package/src/util/browser-support.ts +29 -0
  88. package/src/util/index.ts +5 -0
  89. package/src/util/logger.ts +26 -0
  90. package/src/util/node-support.ts +9 -0
  91. package/src/util/requestUtils.ts +171 -0
  92. package/src/util/websocket-util.ts +550 -0
  93. package/src/websocket-client.ts +1262 -0
@@ -0,0 +1,303 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import {
3
+ APIResponseV3,
4
+ APIResponseWithTime,
5
+ KlineInterval,
6
+ NewSpotOrderV3,
7
+ SpotBalances,
8
+ SpotCancelOrderBatchRequest,
9
+ SpotCrossMarginBorrowingInfoRequest,
10
+ SpotCrossMarginRepaymentHistoryRequest,
11
+ SpotLeveragedTokenPRHistoryRequest,
12
+ SpotMyTradesRequest,
13
+ SpotOrderQueryById,
14
+ } from './types';
15
+ import { REST_CLIENT_TYPE_ENUM } from './util';
16
+ import BaseRestClient from './util/BaseRestClient';
17
+
18
+ /**
19
+ * REST API client for newer Spot V3 APIs.
20
+ */
21
+ export class SpotClientV3 extends BaseRestClient {
22
+ getClientType() {
23
+ // Follows the same authentication mechanism as other v3 APIs (e.g. USDC)
24
+ return REST_CLIENT_TYPE_ENUM.v3;
25
+ }
26
+
27
+ async fetchServerTime(): Promise<number> {
28
+ const res = await this.getServerTime();
29
+ return Number(res.time_now);
30
+ }
31
+
32
+ /**
33
+ *
34
+ * Market Data Endpoints
35
+ *
36
+ */
37
+
38
+ /** Get all symbols */
39
+ getSymbols(): Promise<APIResponseV3<any>> {
40
+ return this.get('/spot/v3/public/symbols');
41
+ }
42
+
43
+ /** Get orderbook for symbol */
44
+ getOrderBook(symbol: string, limit?: number): Promise<APIResponseV3<any>> {
45
+ return this.get('/spot/v3/public/quote/depth', { symbol, limit });
46
+ }
47
+
48
+ /** Get merged orderbook for symbol */
49
+ getMergedOrderBook(
50
+ symbol: string,
51
+ scale?: number,
52
+ limit?: number
53
+ ): Promise<APIResponseV3<any>> {
54
+ return this.get('/spot/v3/public/quote/depth/merged', {
55
+ symbol,
56
+ scale,
57
+ limit,
58
+ });
59
+ }
60
+
61
+ /** Get public trading records (raw trades) */
62
+ getTrades(symbol: string, limit?: number): Promise<APIResponseV3<any>> {
63
+ return this.get('/spot/v3/public/quote/trades', { symbol, limit });
64
+ }
65
+
66
+ /** Get candles/klines */
67
+ getCandles(
68
+ symbol: string,
69
+ interval: KlineInterval,
70
+ limit?: number,
71
+ startTime?: number,
72
+ endTime?: number
73
+ ): Promise<APIResponseV3<any>> {
74
+ return this.get('/spot/v3/public/quote/kline', {
75
+ symbol,
76
+ interval,
77
+ limit,
78
+ startTime,
79
+ endTime,
80
+ });
81
+ }
82
+
83
+ /** Get latest information for symbol (24hr ticker) */
84
+ get24hrTicker(symbol?: string): Promise<APIResponseV3<any>> {
85
+ return this.get('/spot/v3/public/quote/ticker/24hr', { symbol });
86
+ }
87
+
88
+ /** Get last traded price */
89
+ getLastTradedPrice(symbol?: string): Promise<APIResponseV3<any>> {
90
+ return this.get('/spot/v3/public/quote/ticker/price', { symbol });
91
+ }
92
+
93
+ /** Get best bid/ask price */
94
+ getBestBidAskPrice(symbol?: string): Promise<APIResponseV3<any>> {
95
+ return this.get('/spot/v3/public/quote/ticker/bookTicker', { symbol });
96
+ }
97
+
98
+ /**
99
+ *
100
+ * Account Data Endpoints
101
+ *
102
+ */
103
+
104
+ /** -> Order API */
105
+
106
+ /** Create order */
107
+ submitOrder(params: NewSpotOrderV3): Promise<APIResponseV3<any>> {
108
+ return this.postPrivate('/spot/v3/private/order', params);
109
+ }
110
+
111
+ /** Get active order state */
112
+ getOrder(params: SpotOrderQueryById): Promise<APIResponseV3<any>> {
113
+ return this.getPrivate('/spot/v3/private/order', params);
114
+ }
115
+
116
+ /** Cancel order */
117
+ cancelOrder(params: SpotOrderQueryById): Promise<APIResponseV3<any>> {
118
+ return this.postPrivate('/spot/v3/private/cancel-order', params);
119
+ }
120
+
121
+ /** Batch cancel orders */
122
+ cancelOrderBatch(
123
+ params: SpotCancelOrderBatchRequest
124
+ ): Promise<APIResponseV3<any>> {
125
+ const orderTypes = params.orderTypes
126
+ ? params.orderTypes.join(',')
127
+ : undefined;
128
+
129
+ return this.postPrivate('/spot/v3/private/cancel-orders', {
130
+ ...params,
131
+ orderTypes,
132
+ });
133
+ }
134
+
135
+ /** Batch cancel up to 100 orders by ID */
136
+ cancelOrderBatchIDs(orderIds: string[]): Promise<APIResponseV3<any>> {
137
+ const orderIdsCsv = orderIds.join(',');
138
+ return this.postPrivate('/spot/v3/private/cancel-orders-by-ids', {
139
+ orderIds: orderIdsCsv,
140
+ });
141
+ }
142
+
143
+ /** Get open orders */
144
+ getOpenOrders(
145
+ symbol?: string,
146
+ orderId?: string,
147
+ limit?: number,
148
+ orderCategory?: 0 | 1
149
+ ): Promise<APIResponseV3<any>> {
150
+ return this.getPrivate('/spot/v3/private/open-orders', {
151
+ symbol,
152
+ orderId,
153
+ limit,
154
+ orderCategory,
155
+ });
156
+ }
157
+
158
+ /** Get order history */
159
+ getPastOrders(
160
+ symbol?: string,
161
+ orderId?: string,
162
+ limit?: number,
163
+ orderCategory?: 0 | 1
164
+ ): Promise<APIResponseV3<any>> {
165
+ return this.getPrivate('/spot/v3/private/history-orders', {
166
+ symbol,
167
+ orderId,
168
+ limit,
169
+ orderCategory,
170
+ });
171
+ }
172
+
173
+ /**
174
+ * Get your trade history.
175
+ * If startTime is not specified, you can only query for records in the last 7 days.
176
+ * If you want to query for records older than 7 days, startTime is required.
177
+ */
178
+ getMyTrades(params?: SpotMyTradesRequest): Promise<APIResponseV3<any>> {
179
+ return this.getPrivate('/spot/v3/private/my-trades', params);
180
+ }
181
+
182
+ /**
183
+ *
184
+ * Wallet Data Endpoints
185
+ *
186
+ */
187
+
188
+ /** Get Wallet Balance */
189
+ getBalances(): Promise<APIResponseV3<SpotBalances>> {
190
+ return this.getPrivate('/spot/v3/private/account');
191
+ }
192
+
193
+ /**
194
+ *
195
+ * API Data Endpoints
196
+ *
197
+ */
198
+
199
+ getServerTime(): Promise<APIResponseWithTime> {
200
+ return this.get('/v2/public/time');
201
+ }
202
+
203
+ /**
204
+ *
205
+ * Leveraged Token Endpoints
206
+ *
207
+ */
208
+
209
+ /** Get all asset infos */
210
+ getLeveragedTokenAssetInfos(ltCode?: string): Promise<APIResponseV3<any>> {
211
+ return this.get('/spot/v3/public/infos', { ltCode });
212
+ }
213
+
214
+ /** Get leveraged token market info */
215
+ getLeveragedTokenMarketInfo(ltCode: string): Promise<APIResponseV3<any>> {
216
+ return this.getPrivate('/spot/v3/private/reference', { ltCode });
217
+ }
218
+
219
+ /** Purchase leveraged token */
220
+ purchaseLeveragedToken(
221
+ ltCode: string,
222
+ ltAmount: string,
223
+ serialNo?: string
224
+ ): Promise<APIResponseV3<any>> {
225
+ return this.postPrivate('/spot/v3/private/purchase', {
226
+ ltCode,
227
+ ltAmount,
228
+ serialNo,
229
+ });
230
+ }
231
+
232
+ /** Redeem leveraged token */
233
+ redeemLeveragedToken(
234
+ ltCode: string,
235
+ ltAmount: string,
236
+ serialNo?: string
237
+ ): Promise<APIResponseV3<any>> {
238
+ return this.postPrivate('/spot/v3/private/redeem', {
239
+ ltCode,
240
+ ltAmount,
241
+ serialNo,
242
+ });
243
+ }
244
+
245
+ /** Get leveraged token purchase/redemption history */
246
+ getLeveragedTokenPRHistory(
247
+ params?: SpotLeveragedTokenPRHistoryRequest
248
+ ): Promise<APIResponseV3<any>> {
249
+ return this.getPrivate('/spot/v3/private/record', params);
250
+ }
251
+
252
+ /**
253
+ *
254
+ * Cross Margin Trading Endpoints
255
+ *
256
+ */
257
+
258
+ /** Borrow margin loan */
259
+ borrowCrossMarginLoan(
260
+ coin: string,
261
+ qty: string
262
+ ): Promise<APIResponseV3<any>> {
263
+ return this.postPrivate('/spot/v3/private/cross-margin-loan', {
264
+ coin,
265
+ qty,
266
+ });
267
+ }
268
+
269
+ /** Repay margin loan */
270
+ repayCrossMarginLoan(coin: string, qty: string): Promise<APIResponseV3<any>> {
271
+ return this.postPrivate('/spot/v3/private/cross-margin-repay', {
272
+ coin,
273
+ qty,
274
+ });
275
+ }
276
+
277
+ /** Query borrowing info */
278
+ getCrossMarginBorrowingInfo(
279
+ params?: SpotCrossMarginBorrowingInfoRequest
280
+ ): Promise<APIResponseV3<any>> {
281
+ return this.getPrivate('/spot/v3/private/cross-margin-orders', params);
282
+ }
283
+
284
+ /** Query account info */
285
+ getCrossMarginAccountInfo(): Promise<APIResponseV3<any>> {
286
+ return this.getPrivate('/spot/v3/private/cross-margin-account');
287
+ }
288
+
289
+ /** Query interest & quota */
290
+ getCrossMarginInterestQuota(coin: string): Promise<APIResponseV3<any>> {
291
+ return this.getPrivate('/spot/v3/private/cross-margin-loan-info', { coin });
292
+ }
293
+
294
+ /** Query repayment history */
295
+ getCrossMarginRepaymentHistory(
296
+ params?: SpotCrossMarginRepaymentHistoryRequest
297
+ ): Promise<APIResponseV3<any>> {
298
+ return this.getPrivate(
299
+ '/spot/v3/private/cross-margin-repay-history',
300
+ params
301
+ );
302
+ }
303
+ }
@@ -0,0 +1,180 @@
1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import {
3
+ APIResponse,
4
+ KlineInterval,
5
+ NewSpotOrder,
6
+ OrderSide,
7
+ OrderTypeSpot,
8
+ SpotBalances,
9
+ SpotLastPrice,
10
+ SpotOrderQueryById,
11
+ SpotSymbolInfo,
12
+ } from './types';
13
+ import BaseRestClient from './util/BaseRestClient';
14
+ import { REST_CLIENT_TYPE_ENUM } from './util/requestUtils';
15
+
16
+ /**
17
+ * @deprecated Use SpotV3Client instead, which leverages the newer v3 APIs
18
+ * REST API client for Spot APIs (v1)
19
+ */
20
+ export class SpotClient extends BaseRestClient {
21
+ getClientType() {
22
+ return REST_CLIENT_TYPE_ENUM.spot;
23
+ }
24
+
25
+ fetchServerTime(): Promise<number> {
26
+ return this.getServerTime();
27
+ }
28
+
29
+ async getServerTime(): Promise<number> {
30
+ const res = await this.get('/spot/v1/time');
31
+ return res.result.serverTime;
32
+ }
33
+
34
+ /**
35
+ *
36
+ * Market Data Endpoints
37
+ *
38
+ **/
39
+
40
+ getSymbols(): Promise<APIResponse<SpotSymbolInfo[]>> {
41
+ return this.get('/spot/v1/symbols');
42
+ }
43
+
44
+ getOrderBook(symbol: string, limit?: number): Promise<APIResponse<any>> {
45
+ return this.get('/spot/quote/v1/depth', {
46
+ symbol,
47
+ limit,
48
+ });
49
+ }
50
+
51
+ getMergedOrderBook(
52
+ symbol: string,
53
+ scale?: number,
54
+ limit?: number
55
+ ): Promise<APIResponse<any>> {
56
+ return this.get('/spot/quote/v1/depth/merged', {
57
+ symbol,
58
+ scale,
59
+ limit,
60
+ });
61
+ }
62
+
63
+ getTrades(symbol: string, limit?: number): Promise<APIResponse<any[]>> {
64
+ return this.get('/spot/quote/v1/trades', {
65
+ symbol,
66
+ limit,
67
+ });
68
+ }
69
+
70
+ getCandles(
71
+ symbol: string,
72
+ interval: KlineInterval,
73
+ limit?: number,
74
+ startTime?: number,
75
+ endTime?: number
76
+ ): Promise<APIResponse<any[]>> {
77
+ return this.get('/spot/quote/v1/kline', {
78
+ symbol,
79
+ interval,
80
+ limit,
81
+ startTime,
82
+ endTime,
83
+ });
84
+ }
85
+
86
+ get24hrTicker(symbol?: string): Promise<APIResponse<any>> {
87
+ return this.get('/spot/quote/v1/ticker/24hr', { symbol });
88
+ }
89
+
90
+ getLastTradedPrice(): Promise<APIResponse<SpotLastPrice[]>>;
91
+
92
+ getLastTradedPrice(symbol: string): Promise<APIResponse<SpotLastPrice>>;
93
+
94
+ getLastTradedPrice(
95
+ symbol?: string
96
+ ): Promise<APIResponse<SpotLastPrice | SpotLastPrice[]>> {
97
+ return this.get('/spot/quote/v1/ticker/price', { symbol });
98
+ }
99
+
100
+ getBestBidAskPrice(symbol?: string): Promise<APIResponse<any>> {
101
+ return this.get('/spot/quote/v1/ticker/book_ticker', { symbol });
102
+ }
103
+
104
+ /**
105
+ * Account Data Endpoints
106
+ */
107
+
108
+ submitOrder(params: NewSpotOrder): Promise<APIResponse<any>> {
109
+ return this.postPrivate('/spot/v1/order', params);
110
+ }
111
+
112
+ getOrder(params: SpotOrderQueryById): Promise<APIResponse<any>> {
113
+ return this.getPrivate('/spot/v1/order', params);
114
+ }
115
+
116
+ cancelOrder(params: SpotOrderQueryById): Promise<APIResponse<any>> {
117
+ return this.deletePrivate('/spot/v1/order', params);
118
+ }
119
+
120
+ cancelOrderBatch(params: {
121
+ symbol: string;
122
+ side?: OrderSide;
123
+ orderTypes: OrderTypeSpot[];
124
+ }): Promise<APIResponse<any>> {
125
+ const orderTypes = params.orderTypes
126
+ ? params.orderTypes.join(',')
127
+ : undefined;
128
+
129
+ return this.deletePrivate('/spot/order/batch-cancel', {
130
+ ...params,
131
+ orderTypes,
132
+ });
133
+ }
134
+
135
+ getOpenOrders(
136
+ symbol?: string,
137
+ orderId?: string,
138
+ limit?: number
139
+ ): Promise<APIResponse<any>> {
140
+ return this.getPrivate('/spot/v1/open-orders', {
141
+ symbol,
142
+ orderId,
143
+ limit,
144
+ });
145
+ }
146
+
147
+ getPastOrders(
148
+ symbol?: string,
149
+ orderId?: string,
150
+ limit?: number
151
+ ): Promise<APIResponse<any>> {
152
+ return this.getPrivate('/spot/v1/history-orders', {
153
+ symbol,
154
+ orderId,
155
+ limit,
156
+ });
157
+ }
158
+
159
+ getMyTrades(
160
+ symbol?: string,
161
+ limit?: number,
162
+ fromId?: number,
163
+ toId?: number
164
+ ): Promise<APIResponse<any>> {
165
+ return this.getPrivate('/spot/v1/myTrades', {
166
+ symbol,
167
+ limit,
168
+ fromId,
169
+ toId,
170
+ });
171
+ }
172
+
173
+ /**
174
+ * Wallet Data Endpoints
175
+ */
176
+
177
+ getBalances(): Promise<APIResponse<SpotBalances>> {
178
+ return this.getPrivate('/spot/v1/account');
179
+ }
180
+ }
@@ -0,0 +1,5 @@
1
+ export * from './response';
2
+ export * from './request';
3
+ export * from './shared';
4
+ export * from './v5-shared';
5
+ export * from './websockets';