-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
WebsocketClient.ts
744 lines (664 loc) · 22.6 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
import { BaseWebsocketClient, EmittableEvent } from './lib/BaseWSClient.js';
import { signWSJWT } from './lib/jwtNode.js';
import { neverGuard } from './lib/misc-util.js';
import {
isCBAdvancedTradeErrorEvent,
isCBAdvancedTradeWSEvent,
isCBExchangeWSEvent,
isCBExchangeWSRequestOperation,
isCBINTXWSRequestOperation,
isCBPrimeWSRequestOperation,
} from './lib/websocket/typeGuards.js';
import {
getCBExchangeWSSign,
getCBInternationalWSSign,
getCBPrimeWSSign,
getMergedCBExchangeWSRequestOperations,
MessageEventLike,
WS_KEY_MAP,
WS_URL_MAP,
WsKey,
WsTopicRequest,
} from './lib/websocket/websocket-util.js';
import { WSConnectedResult } from './lib/websocket/WsStore.types.js';
import { WsMarket } from './types/websockets/client.js';
import {
WsAdvTradeRequestOperation,
WsExchangeAuthenticatedRequestOperation,
WsExchangeRequestOperation,
WsInternationalAuthenticatedRequestOperation,
WsInternationalRequestOperation,
WsOperation,
WsPrimeAuthenticatedRequestOperation,
WsPrimeRequestOperation,
} from './types/websockets/requests.js';
import {
WsAPITopicRequestParamMap,
WsAPITopicResponseMap,
WsAPIWsKeyTopicMap,
} from './types/websockets/wsAPI.js';
export const WS_LOGGER_CATEGORY = { category: 'coinbase-ws' };
/**
* Any WS keys in this list will trigger automatic auth as required, if credentials are available
*/
const PRIVATE_WS_KEYS: WsKey[] = [
// Account data (fills), requires auth.
WS_KEY_MAP.advTradeUserData,
// Coinbase Direct Market Data has direct access to Coinbase Exchange servers and requires auth.
WS_KEY_MAP.exchangeDirectMarketData,
// The INTX feed always requires auth.
WS_KEY_MAP.internationalMarketData,
// The Prime feed always requires auth.
WS_KEY_MAP.primeMarketData,
];
/**
* Any WS keys in this list will ALWAYS skip the authentication process, even if credentials are available
*/
export const PUBLIC_WS_KEYS: WsKey[] = [
WS_KEY_MAP.advTradeMarketData,
WS_KEY_MAP.exchangeMarketData,
];
/**
* WS topics are always a string for this exchange. Some exchanges use complex objects.
*/
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
*/
public connectAll(): Promise<(WSConnectedResult | undefined)[]> {
return Promise.all([
this.connect(WS_KEY_MAP.advTradeMarketData),
this.connect(WS_KEY_MAP.advTradeUserData),
this.connect(WS_KEY_MAP.exchangeMarketData),
this.connect(WS_KEY_MAP.exchangeDirectMarketData),
this.connect(WS_KEY_MAP.internationalMarketData),
this.connect(WS_KEY_MAP.primeMarketData),
]);
}
/**
* 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);
}
}
/**
* Not supported by this exchange, do not use
*/
// 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],
>(
wsKey: TWSKey,
channel: TWSChannel,
params?: TWSParams,
): Promise<undefined> {
this.logger.trace(`sendWSAPIRequest(): assert "${wsKey}" is connected`, {
channel,
params,
});
return;
}
/**
*
* Internal methods
*
*/
/**
* Return a websocket URL, which is connected to as-is.
* If a token or anything else is needed in the URL, this is a good place to add it.
*/
protected async getWsUrl(wsKey: WsKey): Promise<string> {
if (this.options.wsUrl) {
return this.options.wsUrl;
}
const useSandbox = this.options.useSandbox;
const networkKey = useSandbox ? 'testnet' : 'livenet';
const baseUrl = WS_URL_MAP[wsKey][networkKey];
return baseUrl;
}
protected sendPingEvent(wsKey: WsKey) {
const wsState = this.getWsStore().get(wsKey);
const ws = wsState?.ws;
ws?.ping();
}
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,
});
}
}
protected isWsPing(msg: any): boolean {
if (msg?.data === 'ping') {
return true;
}
return false;
}
protected isWsPong(msg: any): boolean {
if (msg?.data?.includes('pong')) {
return true;
}
// this.logger.info(`Not a pong: `, msg);
return false;
}
protected resolveEmittableEvents(
wsKey: WsKey,
event: MessageEventLike,
): EmittableEvent[] {
const results: EmittableEvent[] = [];
try {
const parsed = JSON.parse(event.data);
const responseEvents = ['subscriptions'];
// E.g. {"type":"error","message":"rate limit exceeded","wsKey":"advTradeMarketData"}
if (isCBAdvancedTradeErrorEvent(parsed)) {
return [{ eventType: 'exception', event: parsed }];
}
// Parse Advanced Trade WS events (update & response)
if (isCBAdvancedTradeWSEvent(parsed)) {
const eventType = parsed.channel;
// These are request/reply pattern events (e.g. after subscribing to topics or authenticating)
if (responseEvents.includes(eventType)) {
return [
{
eventType: 'response',
event: parsed,
},
];
}
// Generic data for a channel
if (typeof eventType === 'string') {
return [
{
eventType: 'update',
event: parsed,
},
];
}
}
// Parse CB Exchange WS events
if (isCBExchangeWSEvent(parsed, wsKey)) {
const eventType = parsed.type;
if (responseEvents.includes(eventType)) {
return [
{
eventType: 'response',
event: parsed,
},
];
}
// Generic data for a channel
if (typeof eventType === 'string') {
return [
{
eventType: 'update',
event: parsed,
},
];
}
}
this.logger.error(
`!! (${wsKey}) Unhandled non-string event type... Defaulting to "update" channel...` +
JSON.stringify(parsed),
);
return [
{
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 {
return request && PRIVATE_WS_KEYS.includes(wsKey);
}
protected getWsKeyForMarket(market: WsMarket, isPrivate: boolean): WsKey {
switch (market) {
case 'advancedTrade': {
return isPrivate
? WS_KEY_MAP.advTradeUserData
: WS_KEY_MAP.advTradeMarketData;
}
case 'exchange': {
return isPrivate
? WS_KEY_MAP.exchangeDirectMarketData
: WS_KEY_MAP.exchangeMarketData;
}
case 'international': {
return isPrivate
? WS_KEY_MAP.internationalMarketData
: WS_KEY_MAP.internationalMarketData;
}
case 'prime': {
return isPrivate
? WS_KEY_MAP.primeMarketData
: WS_KEY_MAP.primeMarketData;
}
default: {
throw neverGuard(market, `Unhandled "market": "${market}"`);
}
}
}
protected getWsMarketForWsKey(wsKey: WsKey): WsMarket {
switch (wsKey) {
case WS_KEY_MAP.advTradeMarketData:
case WS_KEY_MAP.advTradeUserData: {
return 'advancedTrade';
}
case WS_KEY_MAP.exchangeMarketData:
case WS_KEY_MAP.exchangeDirectMarketData: {
return 'exchange';
}
case WS_KEY_MAP.internationalMarketData: {
return 'international';
}
case WS_KEY_MAP.primeMarketData: {
return 'prime';
}
default: {
throw neverGuard(wsKey, `Unhandled WsKey: "${wsKey}"`);
}
}
}
protected getPrivateWSKeys(): WsKey[] {
return PRIVATE_WS_KEYS;
}
/** Force subscription requests to be sent in smaller batches, if a number is returned */
protected getMaxTopicsPerSubscribeEvent(wsKey: WsKey): number | null {
switch (wsKey) {
case WS_KEY_MAP.advTradeMarketData:
case WS_KEY_MAP.advTradeUserData:
// Technically, INTX supports request batching but not with nested parameters, so we'll send one at a time
case WS_KEY_MAP.internationalMarketData:
return 1;
// Exchange supports request batching, no known limit to max topics per request
case WS_KEY_MAP.exchangeDirectMarketData:
case WS_KEY_MAP.exchangeMarketData: {
return null;
}
default: {
return null;
}
}
}
/**
* Map one or more topics into fully prepared "subscribe request" events (already stringified and ready to send)
*/
protected async getWsOperationEventsForTopics(
topicRequests: WsTopicRequest<string>[],
wsKey: WsKey,
operation: WsOperation,
): Promise<string[]> {
if (!topicRequests.length) {
return [];
}
const apiKey = this.options.apiKey;
const apiSecret = this.options.apiSecret;
const apiPassphrase = this.options.apiPassphrase;
/**
* Operations need to be structured in a way that this exchange understands.
* Parse the internal format into the format expected by the exchange. One request per operation.
*/
const operationEvents = topicRequests.map((topicRequest) => {
switch (wsKey) {
case WS_KEY_MAP.advTradeMarketData:
case WS_KEY_MAP.advTradeUserData: {
const wsRequestEvent: WsAdvTradeRequestOperation<WsTopic> = {
type: operation,
channel: topicRequest.topic,
...topicRequest.payload,
};
return wsRequestEvent;
}
case WS_KEY_MAP.exchangeMarketData:
case WS_KEY_MAP.exchangeDirectMarketData: {
const wsRequestEvent: WsExchangeRequestOperation<WsTopic> = {
type: operation,
channels: [
topicRequest.payload
? {
name: topicRequest.topic,
...topicRequest.payload,
}
: topicRequest.topic,
],
};
return wsRequestEvent;
}
case WS_KEY_MAP.internationalMarketData: {
// In case there's ever more operation types than "subscribe" and "unsubscribe"
if (!['subscribe', 'unsubscribe'].includes(operation)) {
throw new Error(
`Unhandled request operation type for CB International WS: "${operation}"`,
);
}
const wsRequestEvent: WsInternationalRequestOperation<WsTopic> = {
type: operation === 'subscribe' ? 'SUBSCRIBE' : 'UNSUBSCRIBE',
// As of Sep 2024, parameters (such as product_id[]) cannot be nested with the channels array
channels: [topicRequest.topic],
// This merges parametrs (such as product_id[]) into the top level request object
...topicRequest.payload,
};
return wsRequestEvent;
}
case WS_KEY_MAP.primeMarketData: {
const wsRequestEvent: WsPrimeRequestOperation<WsTopic> = {
type: operation,
channel: topicRequest.topic,
...topicRequest.payload,
};
return wsRequestEvent;
}
default: {
throw new Error(
`Not implemented for "${wsKey}" yet - if you need Prime or INTX, please get in touch.`,
);
}
}
});
const maxTopicsPerEvent = this.getMaxTopicsPerSubscribeEvent(wsKey);
const isPrivateChannel = PRIVATE_WS_KEYS.includes(wsKey);
/**
* - Merge commands into one if the exchange supports batch requests,
* - Apply auth/sign, if needed,
* - Apply any final formatting to return a string array, ready to be sent upstream.
*/
switch (wsKey) {
case WS_KEY_MAP.advTradeMarketData:
case WS_KEY_MAP.advTradeUserData: {
// Events that are ready to send (usually stringified JSON)
// ADV trade only supports sending one at a time, so we don't try to merge them
// These are already signed, if needed.
return operationEvents.map((evt) => {
if (!isPrivateChannel) {
return JSON.stringify(evt);
}
if (!apiKey || !apiSecret) {
throw new Error(
`"options.apiKey" (api key name) and/or "options.apiSecret" missing, unable to generate JWT`,
);
}
const jwtExpiresSeconds = this.options.jwtExpiresSeconds || 120;
const timestamp = Date.now();
/**
* No batching is supported for this product group, so we can already
* handle sign here and return it as is
*/
const sign = signWSJWT({
algorithm: 'ES256',
timestampMs: timestamp,
jwtExpiresSeconds,
apiPubKey: apiKey,
apiPrivKey: apiSecret,
});
const operationEventWithSign = {
...evt,
jwt: sign,
};
return JSON.stringify(operationEventWithSign);
});
}
case WS_KEY_MAP.exchangeMarketData:
case WS_KEY_MAP.exchangeDirectMarketData: {
if (
!operationEvents.every((evt) =>
isCBExchangeWSRequestOperation(evt, wsKey),
)
) {
// Don't expect this to ever happen, but just to please typescript...
throw new Error(
`Unexpected request schema for exchange WS request builder`,
);
}
const mergedOperationEvents =
getMergedCBExchangeWSRequestOperations(operationEvents);
// We're under the max topics per request limit.
// Send operation requests as one merged request
if (
!maxTopicsPerEvent ||
mergedOperationEvents.channels.length <= maxTopicsPerEvent
) {
if (!isPrivateChannel) {
return [JSON.stringify(mergedOperationEvents)];
}
if (!apiKey || !apiSecret || !apiPassphrase) {
throw new Error(
`One or more of apiKey, apiSecret and/or apiPassphrase are missing. These must be provided to use private channels.`,
);
}
const { sign, timestampInSeconds } =
await getCBExchangeWSSign(apiSecret);
const mergedOperationEventsWithSign: WsExchangeAuthenticatedRequestOperation<WsTopic> =
{
...mergedOperationEvents,
signature: sign,
key: apiKey,
passphrase: apiPassphrase,
timestamp: timestampInSeconds,
};
return [JSON.stringify(mergedOperationEventsWithSign)];
}
// We're over the max topics per request limit. Break into batches.
const finalOperations: string[] = [];
for (
let i = 0;
i < mergedOperationEvents.channels.length;
i += maxTopicsPerEvent
) {
const batchChannels = mergedOperationEvents.channels.slice(
i,
i + maxTopicsPerEvent,
);
const wsRequestEvent: WsExchangeRequestOperation<WsTopic> = {
type: mergedOperationEvents.type,
channels: [...batchChannels],
};
if (isPrivateChannel) {
if (!apiKey || !apiSecret || !apiPassphrase) {
throw new Error(
`One or more of apiKey, apiSecret and/or apiPassphrase are missing. These must be provided to use private channels.`,
);
}
const { sign, timestampInSeconds } =
await getCBExchangeWSSign(apiSecret);
const wsRequestEventWithSign: WsExchangeAuthenticatedRequestOperation<WsTopic> =
{
...wsRequestEvent,
signature: sign,
key: apiKey,
passphrase: apiPassphrase,
timestamp: timestampInSeconds,
};
finalOperations.push(JSON.stringify(wsRequestEventWithSign));
} else {
finalOperations.push(JSON.stringify(wsRequestEvent));
}
}
return finalOperations;
}
case WS_KEY_MAP.internationalMarketData: {
if (
!operationEvents.every((evt) =>
isCBINTXWSRequestOperation(evt, wsKey),
)
) {
// Don't expect this to ever happen, but just to please typescript...
throw new Error(
`Unexpected request schema for exchange WS request builder`,
);
}
// We're over the max topics per request limit. Break into batches.
const finalOperations: string[] = [];
for (const operationEvent of operationEvents) {
if (!apiKey || !apiSecret || !apiPassphrase) {
throw new Error(
`One or more of apiKey, apiSecret and/or apiPassphrase are missing. These must be provided to use private channels.`,
);
}
const { sign, timestampInSeconds } = await getCBInternationalWSSign(
apiKey,
apiSecret,
apiPassphrase,
);
const wsRequestEventWithSign: WsInternationalAuthenticatedRequestOperation<WsTopic> =
{
...operationEvent,
time: timestampInSeconds,
key: apiKey,
passphrase: apiPassphrase,
signature: sign,
};
finalOperations.push(JSON.stringify(wsRequestEventWithSign));
}
// throw new Error(
// 'CB INTX is not fully implemented yet - awaiting test environment... if you need this, please get in touch.',
// );
return finalOperations;
}
case WS_KEY_MAP.primeMarketData: {
if (
!operationEvents.every((evt) =>
isCBPrimeWSRequestOperation(evt, wsKey),
)
) {
// Don't expect this to ever happen, but just to please typescript...
throw new Error(
`Unexpected request schema for exchange WS request builder`,
);
}
if (!apiKey || !apiSecret || !apiPassphrase) {
throw new Error(
`One or more of apiKey, apiSecret and/or apiPassphrase are missing. These must be provided to use private channels.`,
);
}
const finalOperations: string[] = [];
for (const operationEvent of operationEvents) {
const { sign, timestampInSeconds } = await getCBPrimeWSSign({
channelName: operationEvent.channel,
svcAccountId: operationEvent.svcAccountId,
portfolioId: operationEvent.portfolio_id,
apiKey,
apiSecret,
product_ids: operationEvent.product_ids,
});
const wsRequestEventWithSign: WsPrimeAuthenticatedRequestOperation<WsTopic> =
{
...operationEvent,
api_key_id: apiKey,
access_key: apiSecret,
passphrase: apiPassphrase,
signature: sign,
timestamp: timestampInSeconds,
};
finalOperations.push(JSON.stringify(wsRequestEventWithSign));
}
// throw new Error(
// 'CB Prime is not fully implemented yet - awaiting test environment... if you need this, please get in touch.',
// );
return finalOperations;
}
default: {
throw neverGuard(wsKey, `Not implemented for "${wsKey}" yet`);
// throw new Error(`Not implemented for "${wsKey}" yet`);
}
}
}
/**
* Events are signed per-request, so this function isn't needed for Coinbase.
*/
protected async getWsAuthRequestEvent(wsKey: WsKey): Promise<object> {
return { wsKey };
}
}