forked from tiagosiebler/gateio-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebsocketAPIClient.ts
More file actions
377 lines (350 loc) · 9.4 KB
/
WebsocketAPIClient.ts
File metadata and controls
377 lines (350 loc) · 9.4 KB
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
import { DefaultLogger } from './lib/logger.js';
import { WS_KEY_MAP } from './lib/websocket/websocket-util.js';
import { WSClientConfigurableOptions } from './types/websockets/client.js';
import {
WSAPIFuturesOrder,
WSAPIFuturesOrderAmendReq,
WSAPIFuturesOrderBatchPlaceRespItem,
WSAPIFuturesOrderCancelCPReq,
WSAPIFuturesOrderCancelIdsRespItem,
WSAPIFuturesOrderCancelReq,
WSAPIFuturesOrderListReq,
WSAPIFuturesOrderPlaceReq,
WSAPIFuturesOrderStatusReq,
WSAPIResponse,
WSAPISpotOrder,
WSAPISpotOrderAmendReq,
WSAPISpotOrderCancelCPReq,
WSAPISpotOrderCancelIdsReq,
WSAPISpotOrderCancelIdsRespItem,
WSAPISpotOrderCancelReq,
WSAPISpotOrderListReq,
WSAPISpotOrderPlaceReq,
WSAPISpotOrderStatusReq,
WSAPIWsKey,
} from './types/websockets/wsAPI.js';
import { WebsocketClient } from './WebsocketClient.js';
/**
* Configurable options specific to only the REST-like WebsocketAPIClient
*/
export interface WSAPIClientConfigurableOptions {
/**
* Default: true
*
* Attach default event listeners, which will console log any high level
* events (opened/reconnecting/reconnected/etc).
*
* If you disable this, you should set your own event listeners
* on the embedded WS Client `wsApiClient.getWSClient().on(....)`.
*/
attachEventListeners: boolean;
}
/**
* This is a minimal Websocket API wrapper around the WebsocketClient.
*
* Some methods support passing in a custom "wsKey". This is a reference to which WS connection should
* be used to transmit that message. This is only useful if you wish to use an alternative wss
* domain that is supported by the SDK.
*
* Note: To use testnet, don't set the wsKey - use `testnet: true` in
* the constructor instead.
*
* Note: You can also directly use the sendWSAPIRequest() method to make WS API calls, but some
* may find the below methods slightly more intuitive.
*
* Refer to the WS API promises example for a more detailed example on using sendWSAPIRequest() directly:
* https://github.com/tiagosiebler/gateio-api/blob/master/examples/ws-private-spot-wsapi.ts#L119
*/
export class WebsocketAPIClient {
private wsClient: WebsocketClient;
private options: WSClientConfigurableOptions & WSAPIClientConfigurableOptions;
constructor(
options?: WSClientConfigurableOptions &
Partial<WSAPIClientConfigurableOptions>,
logger?: DefaultLogger,
) {
this.wsClient = new WebsocketClient(options, logger);
this.options = {
attachEventListeners: true,
...options,
};
this.setupDefaultEventListeners();
}
public getWSClient(): WebsocketClient {
return this.wsClient;
}
public setTimeOffsetMs(newOffset: number): void {
return this.getWSClient().setTimeOffsetMs(newOffset);
}
/*
*
* SPOT - Trading requests
*
*/
/**
* Submit a spot order
*/
submitNewSpotOrder(
params: WSAPISpotOrderPlaceReq,
wsKey?: WSAPIWsKey,
): Promise<WSAPIResponse<WSAPISpotOrder>> {
return this.wsClient.sendWSAPIRequest(
wsKey || WS_KEY_MAP.spotV4,
'spot.order_place',
params,
);
}
/**
* Cancel a spot order
*/
cancelSpotOrder(
params: WSAPISpotOrderCancelReq,
wsKey?: WSAPIWsKey,
): Promise<WSAPIResponse<WSAPISpotOrder>> {
return this.wsClient.sendWSAPIRequest(
wsKey || WS_KEY_MAP.spotV4,
'spot.order_cancel',
params,
);
}
/**
* Cancel all spot orders with the given id list
*/
cancelSpotOrderById(
params: WSAPISpotOrderCancelIdsReq[],
wsKey?: WSAPIWsKey,
): Promise<WSAPIResponse<WSAPISpotOrderCancelIdsRespItem[]>> {
return this.wsClient.sendWSAPIRequest(
wsKey || WS_KEY_MAP.spotV4,
'spot.order_cancel_ids',
params,
);
}
/**
* Cancel a spot order for a given symbol
*/
cancelSpotOrderForSymbol(
params: WSAPISpotOrderCancelCPReq,
wsKey?: WSAPIWsKey,
): Promise<WSAPIResponse<WSAPISpotOrder[]>> {
return this.wsClient.sendWSAPIRequest(
wsKey || WS_KEY_MAP.spotV4,
'spot.order_cancel_cp',
params,
);
}
/**
* Update a spot order
*/
updateSpotOrder(
params: WSAPISpotOrderAmendReq,
wsKey?: WSAPIWsKey,
): Promise<WSAPIResponse<WSAPISpotOrder>> {
return this.wsClient.sendWSAPIRequest(
wsKey || WS_KEY_MAP.spotV4,
'spot.order_amend',
params,
);
}
/**
* Get the status of a spot order
*/
getSpotOrderStatus(
params: WSAPISpotOrderStatusReq,
wsKey?: WSAPIWsKey,
): Promise<WSAPIResponse<WSAPISpotOrder>> {
return this.wsClient.sendWSAPIRequest(
wsKey || WS_KEY_MAP.spotV4,
'spot.order_status',
params,
);
}
/**
* Get all spot orders
*/
getSpotOrders(
params: WSAPISpotOrderListReq,
wsKey?: WSAPIWsKey,
): Promise<WSAPIResponse<WSAPISpotOrder[]>> {
return this.wsClient.sendWSAPIRequest(
wsKey || WS_KEY_MAP.spotV4,
'spot.order_list',
params,
);
}
/*
*
* Futures - Trading requests
*
*/
/**
* Submit a futures order.
*
* Note: without a wsKey, this defaults to the perpFuturesUSDTV4 connection
*/
submitNewFuturesOrder(
params: WSAPIFuturesOrderPlaceReq,
wsKey?: WSAPIWsKey,
): Promise<WSAPIResponse<WSAPIFuturesOrder>> {
return this.wsClient.sendWSAPIRequest(
wsKey || WS_KEY_MAP.perpFuturesUSDTV4,
'futures.order_place',
params,
);
}
/**
* Submit a batch of futures orders
*
* Note: without a wsKey, this defaults to the perpFuturesUSDTV4 connection
*/
submitNewFuturesBatchOrder(
params: WSAPIFuturesOrderPlaceReq[],
wsKey?: WSAPIWsKey,
): Promise<WSAPIResponse<WSAPIFuturesOrderBatchPlaceRespItem[]>> {
return this.wsClient.sendWSAPIRequest(
wsKey || WS_KEY_MAP.perpFuturesUSDTV4,
'futures.order_batch_place',
params,
);
}
/**
* Cancel a futures order
*
* Note: without a wsKey, this defaults to the perpFuturesUSDTV4 connection
*/
cancelFuturesOrder(
params: WSAPIFuturesOrderCancelReq,
wsKey?: WSAPIWsKey,
): Promise<WSAPIResponse<WSAPIFuturesOrder>> {
return this.wsClient.sendWSAPIRequest(
wsKey || WS_KEY_MAP.perpFuturesUSDTV4,
'futures.order_cancel',
params,
);
}
/**
* Cancel futures orders by id list
*
* Note: without a wsKey, this defaults to the perpFuturesUSDTV4 connection
*/
cancelFuturesOrderById(
params: string[],
wsKey?: WSAPIWsKey,
): Promise<WSAPIResponse<WSAPIFuturesOrderCancelIdsRespItem[]>> {
return this.wsClient.sendWSAPIRequest(
wsKey || WS_KEY_MAP.perpFuturesUSDTV4,
'futures.order_cancel_ids',
params,
);
}
/**
* Cancel all open futures orders
*
* Note: without a wsKey, this defaults to the perpFuturesUSDTV4 connection
*/
cancelFuturesAllOpenOrders(
params: WSAPIFuturesOrderCancelCPReq,
wsKey?: WSAPIWsKey,
): Promise<WSAPIResponse<WSAPIFuturesOrder[]>> {
return this.wsClient.sendWSAPIRequest(
wsKey || WS_KEY_MAP.perpFuturesUSDTV4,
'futures.order_cancel_cp',
params,
);
}
/**
* Update a futures order
*
* Note: without a wsKey, this defaults to the perpFuturesUSDTV4 connection
*/
updateFuturesOrder(
params: WSAPIFuturesOrderAmendReq,
wsKey?: WSAPIWsKey,
): Promise<WSAPIResponse<WSAPIFuturesOrder>> {
return this.wsClient.sendWSAPIRequest(
wsKey || WS_KEY_MAP.perpFuturesUSDTV4,
'futures.order_amend',
params,
);
}
/**
* Get all futures orders
*
* Note: without a wsKey, this defaults to the perpFuturesUSDTV4 connection
*/
getFuturesOrders(
params: WSAPIFuturesOrderListReq,
wsKey?: WSAPIWsKey,
): Promise<WSAPIResponse<WSAPIFuturesOrder[]>> {
return this.wsClient.sendWSAPIRequest(
wsKey || WS_KEY_MAP.perpFuturesUSDTV4,
'futures.order_list',
params,
);
}
/**
* Get futures order status
*
* Note: without a wsKey, this defaults to the perpFuturesUSDTV4 connection
*/
getFuturesOrderStatus(
params: WSAPIFuturesOrderStatusReq,
wsKey?: WSAPIWsKey,
): Promise<WSAPIResponse<WSAPIFuturesOrder>> {
return this.wsClient.sendWSAPIRequest(
wsKey || WS_KEY_MAP.perpFuturesUSDTV4,
'futures.order_status',
params,
);
}
/**
*
*
*
*
*
*
*
* Private methods for handling some of the convenience/automation provided by the WS API Client
*
*
*
*
*
*
*
*/
private setupDefaultEventListeners() {
if (this.options.attachEventListeners) {
/**
* General event handlers for monitoring the WebsocketClient
*/
this.wsClient
.on('open', (data) => {
console.log(new Date(), 'ws connected', data.wsKey);
})
.on('reconnect', ({ wsKey }) => {
console.log(new Date(), 'ws automatically reconnecting.... ', wsKey);
})
.on('reconnected', (data) => {
console.log(new Date(), 'ws has reconnected ', data?.wsKey);
})
.on('authenticated', (data) => {
console.info(new Date(), 'ws has authenticated ', data?.wsKey);
})
.on('exception', (data) => {
try {
// Blind JSON.stringify can fail on circular references
console.error(
new Date(),
'ws exception: ',
JSON.stringify(data),
// JSON.stringify({ ...data, target: 'WebSocket' }),
);
} catch {
console.error(new Date(), 'ws exception: ', data);
}
});
}
}
}