forked from tiagosiebler/gateio-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebsocketClient.ts
756 lines (663 loc) · 21.3 KB
/
WebsocketClient.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
import { BaseWebsocketClient, EmittableEvent } from './lib/BaseWSClient.js';
import { neverGuard } from './lib/misc-util.js';
import { CHANNEL_ID, MessageEventLike } from './lib/requestUtils.js';
import {
SignAlgorithm,
SignEncodeMethod,
signMessage,
} from './lib/webCryptoAPI.js';
import {
getPrivateFuturesTopics,
getPrivateOptionsTopics,
getPrivateSpotTopics,
getPromiseRefForWSAPIRequest,
WS_BASE_URL_MAP,
WS_KEY_MAP,
WsKey,
WsMarket,
WsTopicRequest,
} from './lib/websocket/websocket-util.js';
import { WSConnectedResult } from './lib/websocket/WsStore.types.js';
import {
WSAPIRequest,
WsOperation,
WsRequestOperationGate,
WsRequestPing,
} from './types/websockets/requests.js';
import {
WsAPITopicRequestParamMap,
WsAPITopicResponseMap,
WsAPIWsKeyTopicMap,
} from './types/websockets/wsAPI.js';
export const WS_LOGGER_CATEGORY = { category: 'gate-ws' };
/**
* WS topics are always a string for gate. Some exchanges use complex objects
*/
export type WsTopic = string;
export class WebsocketClient extends BaseWebsocketClient<WsKey> {
/**
* Request connection of all dependent (public & private) websockets, instead of waiting for automatic connection by library.
*
* Returns array of promises that individually resolve when each connection is successfully opened.
*/
public connectAll(): Promise<WSConnectedResult | undefined>[] {
return [
this.connect(WS_KEY_MAP.spotV4),
this.connect(WS_KEY_MAP.perpFuturesUSDTV4),
this.connect(WS_KEY_MAP.deliveryFuturesUSDTV4),
this.connect(WS_KEY_MAP.optionsV4),
this.connect(WS_KEY_MAP.announcementsV4),
];
}
/**
* Request subscription to one or more topics. Pass topics as either an array of strings, or array of objects (if the topic has parameters).
* Objects should be formatted as {topic: string, params: object}.
*
* - Subscriptions are automatically routed to the correct websocket connection.
* - Authentication/connection is automatic.
* - Resubscribe after network issues is automatic.
*
* Call `unsubscribe(topics)` to remove topics
*/
public subscribe(
requests:
| (WsTopicRequest<WsTopic> | WsTopic)
| (WsTopicRequest<WsTopic> | WsTopic)[],
wsKey: WsKey,
) {
if (!Array.isArray(requests)) {
this.subscribeTopicsForWsKey([requests], wsKey);
return;
}
if (requests.length) {
this.subscribeTopicsForWsKey(requests, wsKey);
}
}
/**
* Unsubscribe from one or more topics. Similar to subscribe() but in reverse.
*
* - Requests are automatically routed to the correct websocket connection.
* - These topics will be removed from the topic cache, so they won't be subscribed to again.
*/
public unsubscribe(
requests:
| (WsTopicRequest<WsTopic> | WsTopic)
| (WsTopicRequest<WsTopic> | WsTopic)[],
wsKey: WsKey,
) {
if (!Array.isArray(requests)) {
this.unsubscribeTopicsForWsKey([requests], wsKey);
return;
}
if (requests.length) {
this.unsubscribeTopicsForWsKey(requests, wsKey);
}
}
/**
* WS API Methods - similar to the REST API, but via WebSockets
*/
/**
* Send a Websocket API event on a connection. Returns a promise that resolves on reply.
*
* Returned promise is rejected if an exception is detected in the reply OR the connection disconnects for any reason (even if automatic reconnect will happen).
*
* After a fresh connection, you should always send a login request first.
*
* If you authenticated once and you're reconnected later (e.g. connection temporarily lost), the SDK will by default automatically:
* - Detect you were authenticated to the WS API before
* - Try to re-authenticate (up to 5 times, in case something (bad timestamp) goes wrong)
* - If it succeeds, it will emit the 'authenticated' event.
* - If it fails and gives up, it will emit an 'exception' event (type: 'wsapi.auth', reason: detailed text).
*
* You can turn off the automatic re-auth WS API logic using `reauthWSAPIOnReconnect: false` in the WSClient config.
*
* @param wsKey - The connection this event is for (e.g. "spotV4" | "perpFuturesUSDTV4" | "perpFuturesBTCV4" | "deliveryFuturesUSDTV4" | "deliveryFuturesBTCV4" | "optionsV4")
* @param channel - The channel this event is for (e.g. "spot.login" to authenticate)
* @param params - Any request parameters for the payload (contents of req_param in the docs). Signature generation is automatic, only send parameters such as order ID as per the docs.
* @returns Promise - tries to resolve with async WS API response. Rejects if disconnected or exception is seen in async WS API response
*/
// This overload allows the caller to omit the 3rd param, if it isn't required (e.g. for the login call)
async sendWSAPIRequest<
TWSKey extends keyof WsAPIWsKeyTopicMap,
TWSChannel extends WsAPIWsKeyTopicMap[TWSKey] = WsAPIWsKeyTopicMap[TWSKey],
TWSParams extends
WsAPITopicRequestParamMap[TWSChannel] = WsAPITopicRequestParamMap[TWSChannel],
TWSAPIResponse extends
| WsAPITopicResponseMap[TWSChannel]
| object = WsAPITopicResponseMap[TWSChannel],
>(
wsKey: TWSKey,
channel: TWSChannel,
...params: TWSParams extends undefined ? [] : [TWSParams]
): Promise<TWSAPIResponse>;
async sendWSAPIRequest<
TWSKey extends keyof WsAPIWsKeyTopicMap = keyof WsAPIWsKeyTopicMap,
TWSChannel extends WsAPIWsKeyTopicMap[TWSKey] = WsAPIWsKeyTopicMap[TWSKey],
TWSParams extends
WsAPITopicRequestParamMap[TWSChannel] = WsAPITopicRequestParamMap[TWSChannel],
TWSAPIResponse = object,
>(wsKey: TWSKey, channel: TWSChannel, params?: TWSParams): Promise<any> {
this.logger.trace(`sendWSAPIRequest(): assert "${wsKey}" is connected`);
await this.assertIsConnected(wsKey);
const signTimestamp = Date.now() + this.options.recvWindow;
const timeInSeconds = +(signTimestamp / 1000).toFixed(0);
const requestEvent: WSAPIRequest<WsAPITopicRequestParamMap[TWSChannel]> = {
time: timeInSeconds,
// id: timeInSeconds,
channel,
event: 'api',
payload: {
req_id: this.getNewRequestId(),
req_header: {
'X-Gate-Channel-Id': CHANNEL_ID,
},
api_key: this.options.apiKey,
req_param: params ? params : '',
timestamp: `${timeInSeconds}`,
},
};
// Sign request
const signedEvent = await this.signWSAPIRequest(requestEvent);
// Store deferred promise
const promiseRef = getPromiseRefForWSAPIRequest(requestEvent);
const deferredPromise =
this.getWsStore().createDeferredPromise<TWSAPIResponse>(
wsKey,
promiseRef,
false,
);
// Send event
this.tryWsSend(wsKey, JSON.stringify(signedEvent));
this.logger.trace(`sendWSAPIRequest(): sent ${channel} event`);
// Return deferred promise, so caller can await this call
return deferredPromise.promise!;
}
/**
*
* Internal methods - not intended for public use
*
*/
protected getWsUrl(wsKey: WsKey): string {
if (this.options.wsUrl) {
return this.options.wsUrl;
}
const useTestnet = this.options.useTestnet;
const networkKey = useTestnet ? 'testnet' : 'livenet';
const baseUrl = WS_BASE_URL_MAP[wsKey][networkKey];
return baseUrl;
}
protected sendPingEvent(wsKey: WsKey) {
let pingChannel: WsRequestPing['channel'];
switch (wsKey) {
case 'deliveryFuturesBTCV4':
case 'deliveryFuturesUSDTV4':
case 'perpFuturesBTCV4':
case 'perpFuturesUSDTV4': {
pingChannel = 'futures.ping';
break;
}
case 'announcementsV4': {
pingChannel = 'announcement.ping';
break;
}
case 'optionsV4': {
pingChannel = 'options.ping';
break;
}
case 'spotV4': {
pingChannel = 'spot.ping';
break;
}
default: {
throw neverGuard(wsKey, `Unhandled WsKey "${wsKey}"`);
}
}
const signTimestamp = Date.now() + this.options.recvWindow;
const timeInS = (signTimestamp / 1000).toFixed(0);
return this.tryWsSend(
wsKey,
`{ "time": ${timeInS}, "channel": "${pingChannel}" }`,
);
}
protected sendPongEvent(wsKey: WsKey) {
try {
this.logger.trace(`Sending upstream ws PONG: `, {
...WS_LOGGER_CATEGORY,
wsMessage: 'PONG',
wsKey,
});
if (!wsKey) {
throw new Error('Cannot send PONG, no wsKey provided');
}
const wsState = this.getWsStore().get(wsKey);
if (!wsState || !wsState?.ws) {
throw new Error(`Cannot send pong, ${wsKey} socket not connected yet`);
}
// Send a protocol layer pong
wsState.ws.pong();
} catch (e) {
this.logger.error(`Failed to send WS PONG`, {
...WS_LOGGER_CATEGORY,
wsMessage: 'PONG',
wsKey,
exception: e,
});
}
}
// NOT IN USE for gate.io, pings for gate are protocol layer pings
// eslint-disable-next-line @typescript-eslint/no-unused-vars
protected isWsPing(_msg: any): boolean {
return false;
}
protected isWsPong(msg: any): boolean {
if (typeof msg?.data === 'string' && msg.data.includes('.pong"')) {
return true;
}
return false;
}
/**
* Parse incoming events into categories, before emitting to the user
*/
protected resolveEmittableEvents(
wsKey: WsKey,
event: MessageEventLike,
): EmittableEvent[] {
const results: EmittableEvent[] = [];
try {
const parsed = JSON.parse(event.data);
const responseEvents = ['subscribe', 'unsubscribe'];
const authenticatedEvents = ['auth'];
const eventHeaders = parsed?.header;
const eventChannel = eventHeaders?.channel;
const eventType = eventHeaders?.event;
const eventStatusCode = eventHeaders?.status;
const requestId = parsed?.request_id;
const promiseRef = [eventChannel, requestId].join('_');
const eventAction = parsed.event || parsed.action || parsed?.header.data;
if (eventType === 'api') {
const isError = eventStatusCode !== '200';
// WS API Exception
if (isError) {
try {
this.getWsStore().rejectDeferredPromise(
wsKey,
promiseRef,
{
wsKey,
...parsed,
},
true,
);
} catch (e) {
this.logger.error(`Exception trying to reject WSAPI promise`, {
wsKey,
promiseRef,
parsedEvent: parsed,
});
}
results.push({
eventType: 'exception',
event: parsed,
});
return results;
}
// WS API Success
try {
this.getWsStore().resolveDeferredPromise(
wsKey,
promiseRef,
{
wsKey,
...parsed,
},
true,
);
} catch (e) {
this.logger.error(`Exception trying to resolve WSAPI promise`, {
wsKey,
promiseRef,
parsedEvent: parsed,
});
}
if (eventChannel.includes('.login')) {
results.push({
eventType: 'authenticated',
event: {
...parsed,
isWSAPI: true,
WSAPIAuthChannel: eventChannel,
},
});
}
results.push({
eventType: 'response',
event: parsed,
});
return results;
}
if (typeof eventAction === 'string') {
if (parsed.success === false) {
results.push({
eventType: 'exception',
event: parsed,
});
return results;
}
if (eventAction === 'update') {
results.push({
eventType: 'update',
event: parsed,
});
return results;
}
// These are request/reply pattern events (e.g. after subscribing to topics or authenticating)
if (responseEvents.includes(eventAction)) {
results.push({
eventType: 'response',
event: parsed,
});
return results;
}
// Request/reply pattern for authentication success
if (authenticatedEvents.includes(eventAction)) {
results.push({
eventType: 'authenticated',
event: parsed,
});
return results;
}
this.logger.error(
`!! Unhandled string event type "${eventAction}. Defaulting to "update" channel...`,
parsed,
);
} else {
// TODO: test meee
this.logger.error(
`!! Unhandled non-string event type "${eventAction}. Defaulting to "update" channel...`,
parsed,
);
}
results.push({
eventType: 'update',
event: parsed,
});
} catch (e) {
results.push({
event: {
message: 'Failed to parse event data due to exception',
exception: e,
eventData: event.data,
},
eventType: 'exception',
});
this.logger.error(`Failed to parse event data due to exception: `, {
exception: e,
eventData: event.data,
});
}
return results;
}
/**
* Determines if a topic is for a private channel, using a hardcoded list of strings
*/
protected isPrivateTopicRequest(
request: WsTopicRequest<string>,
wsKey: WsKey,
): boolean {
const topicName = request?.topic?.toLowerCase();
if (!topicName) {
return false;
}
switch (wsKey) {
case 'spotV4':
return getPrivateSpotTopics().includes(topicName);
case 'perpFuturesBTCV4':
case 'perpFuturesUSDTV4':
case 'deliveryFuturesBTCV4':
case 'deliveryFuturesUSDTV4':
return getPrivateFuturesTopics().includes(topicName);
case 'optionsV4':
return getPrivateOptionsTopics().includes(topicName);
// No private announcements channels
case 'announcementsV4':
return false;
default:
throw neverGuard(wsKey, `Unhandled WsKey "${wsKey}"`);
}
return false;
}
/**
* Not in use for gate.io
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
protected getWsMarketForWsKey(_wsKey: WsKey): WsMarket {
return 'all';
}
/**
* Not in use for gate.io
*/
protected getPrivateWSKeys(): WsKey[] {
return [];
}
/** Force subscription requests to be sent in smaller batches, if a number is returned */
// eslint-disable-next-line @typescript-eslint/no-unused-vars
protected getMaxTopicsPerSubscribeEvent(_wsKey: WsKey): number | null {
return 1;
}
/**
* Map one or more topics into fully prepared "subscribe request" events (already stringified and ready to send)
*/
protected async getWsOperationEventsForTopics(
topics: WsTopicRequest<string>[],
wsKey: WsKey,
operation: WsOperation,
): Promise<string[]> {
// console.log(new Date(), `called getWsSubscribeEventsForTopics()`, topics);
// console.trace();
if (!topics.length) {
return [];
}
// Events that are ready to send (usually stringified JSON)
const jsonStringEvents: string[] = [];
const market = this.getWsMarketForWsKey(wsKey);
const maxTopicsPerEvent = this.getMaxTopicsPerSubscribeEvent(wsKey);
if (
maxTopicsPerEvent &&
maxTopicsPerEvent !== null &&
topics.length > maxTopicsPerEvent
) {
for (let i = 0; i < topics.length; i += maxTopicsPerEvent) {
const batch = topics.slice(i, i + maxTopicsPerEvent);
const subscribeRequestEvents = await this.getWsRequestEvents(
market,
operation,
batch,
wsKey,
);
for (const event of subscribeRequestEvents) {
jsonStringEvents.push(JSON.stringify(event));
}
}
return jsonStringEvents;
}
const subscribeRequestEvents = await this.getWsRequestEvents(
market,
operation,
topics,
wsKey,
);
for (const event of subscribeRequestEvents) {
jsonStringEvents.push(JSON.stringify(event));
}
return jsonStringEvents;
}
/**
* @returns one or more correctly structured request events for performing a operations over WS. This can vary per exchange spec.
*/
private async getWsRequestEvents(
market: WsMarket,
operation: WsOperation,
requests: WsTopicRequest<string>[],
wsKey: WsKey,
): Promise<WsRequestOperationGate<WsTopic>[]> {
const wsRequestEvents: WsRequestOperationGate<WsTopic>[] = [];
const wsRequestBuildingErrors: unknown[] = [];
switch (market) {
case 'all': {
for (const request of requests) {
const signTimestamp = Date.now() + this.options.recvWindow;
const timeInSeconds = +(signTimestamp / 1000).toFixed(0);
const wsEvent: WsRequestOperationGate<WsTopic> = {
time: timeInSeconds,
channel: request.topic,
event: operation,
};
if (request.payload) {
wsEvent.payload = request.payload;
}
if (!this.isPrivateTopicRequest(request, wsKey)) {
wsRequestEvents.push(wsEvent);
continue;
}
// If private topic request, build auth part for request
// No key or secret, push event as failed
if (!this.options.apiKey || !this.options.apiSecret) {
wsRequestBuildingErrors.push({
error: `apiKey or apiSecret missing from config`,
operation,
event: wsEvent,
});
continue;
}
const signAlgoritm: SignAlgorithm = 'SHA-512';
const signEncoding: SignEncodeMethod = 'hex';
const signInput = `channel=${wsEvent.channel}&event=${wsEvent.event}&time=${timeInSeconds}`;
try {
const sign = await this.signMessage(
signInput,
this.options.apiSecret,
signEncoding,
signAlgoritm,
);
wsRequestEvents.push({
...wsEvent,
auth: {
method: 'api_key',
KEY: this.options.apiKey,
SIGN: sign,
},
});
} catch (e) {
wsRequestBuildingErrors.push({
error: `exception during sign`,
errorTrace: e,
operation,
event: wsEvent,
});
}
}
break;
}
default: {
throw neverGuard(market, `Unhandled market "${market}"`);
}
}
if (wsRequestBuildingErrors.length) {
const label =
wsRequestBuildingErrors.length === requests.length ? 'all' : 'some';
this.logger.error(
`Failed to build/send ${wsRequestBuildingErrors.length} event(s) for ${label} WS requests due to exceptions`,
{
...WS_LOGGER_CATEGORY,
wsRequestBuildingErrors,
wsRequestBuildingErrorsStringified: JSON.stringify(
wsRequestBuildingErrors,
null,
2,
),
},
);
}
return wsRequestEvents;
}
private async signMessage(
paramsStr: string,
secret: string,
method: 'hex' | 'base64',
algorithm: SignAlgorithm,
): Promise<string> {
if (typeof this.options.customSignMessageFn === 'function') {
return this.options.customSignMessageFn(paramsStr, secret);
}
return await signMessage(paramsStr, secret, method, algorithm);
}
protected async getWsAuthRequestEvent(wsKey: WsKey): Promise<object> {
const market = this.getWsMarketForWsKey(wsKey);
if (!this.options.apiKey || !this.options.apiSecret) {
throw new Error(
`Cannot auth - missing api key, secret or memo in config`,
);
}
const signTimestamp = Date.now() + this.options.recvWindow;
const signMessageInput = `|${signTimestamp}`;
const signature = await this.signMessage(
signMessageInput,
this.options.apiSecret,
'hex',
'SHA-512',
);
switch (market) {
case 'all': {
const wsRequestEvent = {
id: 'auth' + signTimestamp,
event: 'auth',
params: {
apikey: this.options.apiKey,
sign: signature,
timestamp: signTimestamp,
},
};
return wsRequestEvent;
}
default: {
throw neverGuard(market, `Unhandled market "${market}"`);
}
}
}
/**
*
* @param requestEvent
* @returns A signed updated WS API request object, ready to be sent
*/
private async signWSAPIRequest<TRequestParams = object>(
requestEvent: WSAPIRequest<TRequestParams>,
): Promise<WSAPIRequest<TRequestParams>> {
if (!this.options.apiSecret) {
throw new Error(`API Secret missing`);
}
const payload = requestEvent.payload;
const toSign = [
requestEvent.event,
requestEvent.channel,
JSON.stringify(payload.req_param),
requestEvent.time,
].join('\n');
const signEncoding: SignEncodeMethod = 'hex';
const signAlgoritm: SignAlgorithm = 'SHA-512';
return {
...requestEvent,
payload: {
...requestEvent.payload,
req_header: {
'X-Gate-Channel-Id': CHANNEL_ID,
},
signature: await this.signMessage(
toSign,
this.options.apiSecret,
signEncoding,
signAlgoritm,
),
},
};
}
}