-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathwebsocket-client-v2.ts
166 lines (147 loc) · 4.29 KB
/
websocket-client-v2.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
import WebSocket from 'isomorphic-ws';
import {
BitgetInstTypeV2,
WebsocketClientOptions,
WsCoinChannelsV2,
WsInstIdChannelsV2,
WsKey,
WsPublicTopicV2,
WsTopicSubscribeEventArgsV2,
WsTopicSubscribePrivateCoinArgsV2,
WsTopicSubscribePrivateInstIdArgsV2,
WsTopicV2,
} from './types';
import {
DefaultLogger,
getMaxTopicsPerSubscribeEvent,
isPrivateChannel,
neverGuard,
WS_AUTH_ON_CONNECT_KEYS,
WS_BASE_URL_MAP,
WS_KEY_MAP,
} from './util';
import { BaseWebsocketClient } from './util/BaseWSClient';
const LOGGER_CATEGORY = { category: 'bitget-ws' };
const COIN_CHANNELS: WsTopicV2[] = [
'account',
'account-crossed',
'account-isolated',
];
export class WebsocketClientV2 extends BaseWebsocketClient<
WsKey,
WsTopicSubscribeEventArgsV2
> {
protected logger: typeof DefaultLogger;
protected options: WebsocketClientOptions;
protected getWsKeyForTopic(
subscribeEvent: WsTopicSubscribeEventArgsV2,
isPrivate?: boolean,
): WsKey {
return isPrivate || isPrivateChannel(subscribeEvent.channel)
? WS_KEY_MAP.v2Private
: WS_KEY_MAP.v2Public;
}
protected isPrivateChannel(
subscribeEvent: WsTopicSubscribeEventArgsV2,
): boolean {
return isPrivateChannel(subscribeEvent.channel);
}
protected shouldAuthOnConnect(wsKey: WsKey): boolean {
return WS_AUTH_ON_CONNECT_KEYS.includes(wsKey as WsKey);
}
protected getWsUrl(wsKey: WsKey): string {
if (this.options.wsUrl) {
return this.options.wsUrl;
}
const networkKey = 'livenet';
switch (wsKey) {
case WS_KEY_MAP.spotv1:
case WS_KEY_MAP.mixv1: {
throw new Error(
'Use the WebsocketClient instead of WebsocketClientV2 for V1 websockets',
);
}
case WS_KEY_MAP.v2Private: {
return WS_BASE_URL_MAP.v2Private.all[networkKey];
}
case WS_KEY_MAP.v2Public: {
return WS_BASE_URL_MAP.v2Public.all[networkKey];
}
default: {
this.logger.error('getWsUrl(): Unhandled wsKey: ', {
...LOGGER_CATEGORY,
wsKey,
});
throw neverGuard(wsKey, 'getWsUrl(): Unhandled wsKey');
}
}
}
protected getMaxTopicsPerSubscribeEvent(wsKey: WsKey): number | null {
return getMaxTopicsPerSubscribeEvent(wsKey);
}
/**
* Request connection of all dependent (public & private) websockets, instead of waiting for automatic connection by library
*/
public connectAll(): Promise<WebSocket | undefined>[] {
return [
this.connect(WS_KEY_MAP.v2Private),
this.connect(WS_KEY_MAP.v2Public),
];
}
/** Some private channels use `coin` instead of `instId`. This method handles building the sub/unsub request */
private getSubRequest(
instType: BitgetInstTypeV2,
topic: WsTopicV2,
coin: string = 'default',
): WsTopicSubscribeEventArgsV2 {
if (isPrivateChannel(topic)) {
if (COIN_CHANNELS.includes(topic)) {
const subscribeRequest: WsTopicSubscribePrivateCoinArgsV2 = {
instType,
channel: topic as WsCoinChannelsV2,
coin,
};
return subscribeRequest;
}
const subscribeRequest: WsTopicSubscribePrivateInstIdArgsV2 = {
instType,
channel: topic as WsInstIdChannelsV2,
instId: coin,
};
return subscribeRequest;
}
return {
instType,
channel: topic as WsPublicTopicV2,
instId: coin,
};
}
/**
* Subscribe to a PUBLIC topic
* @param instType instrument type (refer to API docs).
* @param topic topic name (e.g. "ticker").
* @param instId instrument ID (e.g. "BTCUSDT"). Use "default" for private topics.
*/
public subscribeTopic(
instType: BitgetInstTypeV2,
topic: WsTopicV2,
coin: string = 'default',
) {
const subRequest = this.getSubRequest(instType, topic, coin);
return this.subscribe(subRequest);
}
/**
* Unsubscribe from a topic
* @param instType instrument type (refer to API docs).
* @param topic topic name (e.g. "ticker").
* @param instId instrument ID (e.g. "BTCUSDT"). Use "default" for private topics to get all symbols.
*/
public unsubscribeTopic(
instType: BitgetInstTypeV2,
topic: WsTopicV2,
coin: string = 'default',
) {
const subRequest = this.getSubRequest(instType, topic, coin);
return this.unsubscribe(subRequest);
}
}