bitget-api 1.1.4 → 2.0.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.
- package/README.md +41 -33
- package/lib/broker-client.d.ts +4 -1
- package/lib/broker-client.js +4 -1
- package/lib/broker-client.js.map +1 -1
- package/lib/futures-client.d.ts +4 -1
- package/lib/futures-client.js +4 -1
- package/lib/futures-client.js.map +1 -1
- package/lib/index.d.ts +2 -0
- package/lib/index.js +2 -0
- package/lib/index.js.map +1 -1
- package/lib/rest-client-v2.d.ts +268 -0
- package/lib/rest-client-v2.js +573 -0
- package/lib/rest-client-v2.js.map +1 -0
- package/lib/spot-client.d.ts +4 -1
- package/lib/spot-client.js +4 -1
- package/lib/spot-client.js.map +1 -1
- package/lib/types/request/index.d.ts +5 -3
- package/lib/types/request/index.js +5 -3
- package/lib/types/request/index.js.map +1 -1
- package/lib/types/request/shared.d.ts +4 -0
- package/lib/types/request/v1/brokerV1.js +3 -0
- package/lib/types/request/v1/brokerV1.js.map +1 -0
- package/lib/types/request/{futures.d.ts → v1/futuresV1.d.ts} +1 -1
- package/lib/types/request/v1/futuresV1.js +3 -0
- package/lib/types/request/v1/futuresV1.js.map +1 -0
- package/lib/types/request/{spot.d.ts → v1/spotV1.d.ts} +1 -1
- package/lib/types/request/{broker.js → v1/spotV1.js} +1 -1
- package/lib/types/request/v1/spotV1.js.map +1 -0
- package/lib/types/request/v2/futures.d.ts +22 -0
- package/lib/types/request/v2/futures.js.map +1 -0
- package/lib/types/request/v2/spot.d.ts +8 -0
- package/lib/types/request/{spot.js.map → v2/spot.js.map} +1 -1
- package/lib/types/response/shared.d.ts +2 -2
- package/lib/types/websockets/client.d.ts +40 -1
- package/lib/util/BaseWSClient.d.ts +112 -0
- package/lib/util/BaseWSClient.js +395 -0
- package/lib/util/BaseWSClient.js.map +1 -0
- package/lib/util/WsStore.d.ts +10 -46
- package/lib/util/WsStore.js +2 -11
- package/lib/util/WsStore.js.map +1 -1
- package/lib/util/WsStore.types.d.ts +26 -0
- package/lib/util/WsStore.types.js +13 -0
- package/lib/util/WsStore.types.js.map +1 -0
- package/lib/util/requestUtils.d.ts +1 -0
- package/lib/util/requestUtils.js +1 -0
- package/lib/util/requestUtils.js.map +1 -1
- package/lib/util/type-guards.d.ts +5 -1
- package/lib/util/type-guards.js +11 -1
- package/lib/util/type-guards.js.map +1 -1
- package/lib/util/websocket-util.d.ts +5 -2
- package/lib/util/websocket-util.js +36 -3
- package/lib/util/websocket-util.js.map +1 -1
- package/lib/websocket-client-v2.d.ts +33 -0
- package/lib/websocket-client-v2.js +104 -0
- package/lib/websocket-client-v2.js.map +1 -0
- package/lib/websocket-client.d.ts +8 -2
- package/lib/websocket-client.js +27 -15
- package/lib/websocket-client.js.map +1 -1
- package/package.json +3 -3
- package/lib/types/request/broker.js.map +0 -1
- package/lib/types/request/futures.js.map +0 -1
- /package/lib/types/request/{broker.d.ts → v1/brokerV1.d.ts} +0 -0
- /package/lib/types/request/{futures.js → v2/futures.js} +0 -0
- /package/lib/types/request/{spot.js → v2/spot.js} +0 -0
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.BaseWebsocketClient = void 0;
|
|
16
|
+
const events_1 = __importDefault(require("events"));
|
|
17
|
+
const isomorphic_ws_1 = __importDefault(require("isomorphic-ws"));
|
|
18
|
+
const WsStore_1 = __importDefault(require("./WsStore"));
|
|
19
|
+
const WsStore_types_1 = require("./WsStore.types");
|
|
20
|
+
const logger_1 = require("./logger");
|
|
21
|
+
const requestUtils_1 = require("./requestUtils");
|
|
22
|
+
const websocket_util_1 = require("./websocket-util");
|
|
23
|
+
const LOGGER_CATEGORY = { category: 'bitget-ws' };
|
|
24
|
+
class BaseWebsocketClient extends events_1.default {
|
|
25
|
+
constructor(options, logger) {
|
|
26
|
+
super();
|
|
27
|
+
this.logger = logger || logger_1.DefaultLogger;
|
|
28
|
+
this.wsStore = new WsStore_1.default(this.logger);
|
|
29
|
+
this.options = Object.assign({ pongTimeout: 1000, pingInterval: 10000, reconnectTimeout: 500, recvWindow: 0 }, options);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Subscribe to topics & track/persist them. They will be automatically resubscribed to if the connection drops/reconnects.
|
|
33
|
+
* @param wsTopics topic or list of topics
|
|
34
|
+
* @param isPrivateTopic optional - the library will try to detect private topics, you can use this to mark a topic as private (if the topic isn't recognised yet)
|
|
35
|
+
*/
|
|
36
|
+
subscribe(wsTopics, isPrivateTopic) {
|
|
37
|
+
const topics = Array.isArray(wsTopics) ? wsTopics : [wsTopics];
|
|
38
|
+
topics.forEach((topic) => {
|
|
39
|
+
var _a;
|
|
40
|
+
const wsKey = this.getWsKeyForTopic(topic, isPrivateTopic);
|
|
41
|
+
// Persist this topic to the expected topics list
|
|
42
|
+
this.wsStore.addTopic(wsKey, topic);
|
|
43
|
+
// TODO: tidy up unsubscribe too, also in other connectors
|
|
44
|
+
// if connected, send subscription request
|
|
45
|
+
if (this.wsStore.isConnectionState(wsKey, WsStore_types_1.WsConnectionStateEnum.CONNECTED)) {
|
|
46
|
+
// if not authenticated, dont sub to private topics yet.
|
|
47
|
+
// This'll happen automatically once authenticated
|
|
48
|
+
const isAuthenticated = (_a = this.wsStore.get(wsKey)) === null || _a === void 0 ? void 0 : _a.isAuthenticated;
|
|
49
|
+
if (!isAuthenticated) {
|
|
50
|
+
return this.requestSubscribeTopics(wsKey, topics.filter((topic) => !this.isPrivateChannel(topic)));
|
|
51
|
+
}
|
|
52
|
+
return this.requestSubscribeTopics(wsKey, topics);
|
|
53
|
+
}
|
|
54
|
+
// start connection process if it hasn't yet begun. Topics are automatically subscribed to on-connect
|
|
55
|
+
if (!this.wsStore.isConnectionState(wsKey, WsStore_types_1.WsConnectionStateEnum.CONNECTING) &&
|
|
56
|
+
!this.wsStore.isConnectionState(wsKey, WsStore_types_1.WsConnectionStateEnum.RECONNECTING)) {
|
|
57
|
+
return this.connect(wsKey);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Unsubscribe from topics & remove them from memory. They won't be re-subscribed to if the connection reconnects.
|
|
63
|
+
* @param wsTopics topic or list of topics
|
|
64
|
+
* @param isPrivateTopic optional - the library will try to detect private topics, you can use this to mark a topic as private (if the topic isn't recognised yet)
|
|
65
|
+
*/
|
|
66
|
+
unsubscribe(wsTopics, isPrivateTopic) {
|
|
67
|
+
const topics = Array.isArray(wsTopics) ? wsTopics : [wsTopics];
|
|
68
|
+
topics.forEach((topic) => {
|
|
69
|
+
this.wsStore.deleteTopic(this.getWsKeyForTopic(topic, isPrivateTopic), topic);
|
|
70
|
+
const wsKey = this.getWsKeyForTopic(topic, isPrivateTopic);
|
|
71
|
+
// unsubscribe request only necessary if active connection exists
|
|
72
|
+
if (this.wsStore.isConnectionState(wsKey, WsStore_types_1.WsConnectionStateEnum.CONNECTED)) {
|
|
73
|
+
this.requestUnsubscribeTopics(wsKey, [topic]);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
/** Get the WsStore that tracks websockets & topics */
|
|
78
|
+
getWsStore() {
|
|
79
|
+
return this.wsStore;
|
|
80
|
+
}
|
|
81
|
+
close(wsKey, force) {
|
|
82
|
+
this.logger.info('Closing connection', Object.assign(Object.assign({}, LOGGER_CATEGORY), { wsKey }));
|
|
83
|
+
this.setWsState(wsKey, WsStore_types_1.WsConnectionStateEnum.CLOSING);
|
|
84
|
+
this.clearTimers(wsKey);
|
|
85
|
+
const ws = this.getWs(wsKey);
|
|
86
|
+
ws === null || ws === void 0 ? void 0 : ws.close();
|
|
87
|
+
if (force) {
|
|
88
|
+
ws === null || ws === void 0 ? void 0 : ws.terminate();
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
closeAll(force) {
|
|
92
|
+
this.wsStore.getKeys().forEach((key) => {
|
|
93
|
+
this.close(key, force);
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Request connection to a specific websocket, instead of waiting for automatic connection.
|
|
98
|
+
*/
|
|
99
|
+
connect(wsKey) {
|
|
100
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
101
|
+
try {
|
|
102
|
+
if (this.wsStore.isWsOpen(wsKey)) {
|
|
103
|
+
this.logger.error('Refused to connect to ws with existing active connection', Object.assign(Object.assign({}, LOGGER_CATEGORY), { wsKey }));
|
|
104
|
+
return this.wsStore.getWs(wsKey);
|
|
105
|
+
}
|
|
106
|
+
if (this.wsStore.isConnectionState(wsKey, WsStore_types_1.WsConnectionStateEnum.CONNECTING)) {
|
|
107
|
+
this.logger.error('Refused to connect to ws, connection attempt already active', Object.assign(Object.assign({}, LOGGER_CATEGORY), { wsKey }));
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
if (!this.wsStore.getConnectionState(wsKey) ||
|
|
111
|
+
this.wsStore.isConnectionState(wsKey, WsStore_types_1.WsConnectionStateEnum.INITIAL)) {
|
|
112
|
+
this.setWsState(wsKey, WsStore_types_1.WsConnectionStateEnum.CONNECTING);
|
|
113
|
+
}
|
|
114
|
+
const url = this.getWsUrl(wsKey); // + authParams;
|
|
115
|
+
const ws = this.connectToWsUrl(url, wsKey);
|
|
116
|
+
return this.wsStore.setWs(wsKey, ws);
|
|
117
|
+
}
|
|
118
|
+
catch (err) {
|
|
119
|
+
this.parseWsError('Connection failed', err, wsKey);
|
|
120
|
+
this.reconnectWithDelay(wsKey, this.options.reconnectTimeout);
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
parseWsError(context, error, wsKey) {
|
|
125
|
+
if (!error.message) {
|
|
126
|
+
this.logger.error(`${context} due to unexpected error: `, error);
|
|
127
|
+
this.emit('response', Object.assign(Object.assign({}, error), { wsKey }));
|
|
128
|
+
this.emit('exception', Object.assign(Object.assign({}, error), { wsKey }));
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
switch (error.message) {
|
|
132
|
+
case 'Unexpected server response: 401':
|
|
133
|
+
this.logger.error(`${context} due to 401 authorization failure.`, Object.assign(Object.assign({}, LOGGER_CATEGORY), { wsKey }));
|
|
134
|
+
break;
|
|
135
|
+
default:
|
|
136
|
+
this.logger.error(`${context} due to unexpected response error: "${(error === null || error === void 0 ? void 0 : error.msg) || (error === null || error === void 0 ? void 0 : error.message) || error}"`, Object.assign(Object.assign({}, LOGGER_CATEGORY), { wsKey, error }));
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
this.emit('response', Object.assign(Object.assign({}, error), { wsKey }));
|
|
140
|
+
this.emit('exception', Object.assign(Object.assign({}, error), { wsKey }));
|
|
141
|
+
}
|
|
142
|
+
/** Get a signature, build the auth request and send it */
|
|
143
|
+
sendAuthRequest(wsKey) {
|
|
144
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
145
|
+
try {
|
|
146
|
+
const { apiKey, apiSecret, apiPass, recvWindow } = this.options;
|
|
147
|
+
const { signature, expiresAt } = yield (0, websocket_util_1.getWsAuthSignature)(apiKey, apiSecret, apiPass, recvWindow);
|
|
148
|
+
this.logger.info(`Sending auth request...`, Object.assign(Object.assign({}, LOGGER_CATEGORY), { wsKey }));
|
|
149
|
+
const request = {
|
|
150
|
+
op: 'login',
|
|
151
|
+
args: [
|
|
152
|
+
{
|
|
153
|
+
apiKey: this.options.apiKey,
|
|
154
|
+
passphrase: this.options.apiPass,
|
|
155
|
+
timestamp: expiresAt,
|
|
156
|
+
sign: signature,
|
|
157
|
+
},
|
|
158
|
+
],
|
|
159
|
+
};
|
|
160
|
+
// console.log('ws auth req', request);
|
|
161
|
+
return this.tryWsSend(wsKey, JSON.stringify(request));
|
|
162
|
+
}
|
|
163
|
+
catch (e) {
|
|
164
|
+
this.logger.silly(e, Object.assign(Object.assign({}, LOGGER_CATEGORY), { wsKey }));
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
reconnectWithDelay(wsKey, connectionDelayMs) {
|
|
169
|
+
this.clearTimers(wsKey);
|
|
170
|
+
if (this.wsStore.getConnectionState(wsKey) !==
|
|
171
|
+
WsStore_types_1.WsConnectionStateEnum.CONNECTING) {
|
|
172
|
+
this.setWsState(wsKey, WsStore_types_1.WsConnectionStateEnum.RECONNECTING);
|
|
173
|
+
}
|
|
174
|
+
this.wsStore.get(wsKey, true).activeReconnectTimer = setTimeout(() => {
|
|
175
|
+
this.logger.info('Reconnecting to websocket', Object.assign(Object.assign({}, LOGGER_CATEGORY), { wsKey }));
|
|
176
|
+
this.connect(wsKey);
|
|
177
|
+
}, connectionDelayMs);
|
|
178
|
+
}
|
|
179
|
+
ping(wsKey) {
|
|
180
|
+
if (this.wsStore.get(wsKey, true).activePongTimer) {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
this.clearPongTimer(wsKey);
|
|
184
|
+
this.logger.silly('Sending ping', Object.assign(Object.assign({}, LOGGER_CATEGORY), { wsKey }));
|
|
185
|
+
this.tryWsSend(wsKey, 'ping');
|
|
186
|
+
this.wsStore.get(wsKey, true).activePongTimer = setTimeout(() => {
|
|
187
|
+
var _a;
|
|
188
|
+
this.logger.info('Pong timeout - closing socket to reconnect', Object.assign(Object.assign({}, LOGGER_CATEGORY), { wsKey }));
|
|
189
|
+
(_a = this.getWs(wsKey)) === null || _a === void 0 ? void 0 : _a.terminate();
|
|
190
|
+
delete this.wsStore.get(wsKey, true).activePongTimer;
|
|
191
|
+
}, this.options.pongTimeout);
|
|
192
|
+
}
|
|
193
|
+
clearTimers(wsKey) {
|
|
194
|
+
this.clearPingTimer(wsKey);
|
|
195
|
+
this.clearPongTimer(wsKey);
|
|
196
|
+
const wsState = this.wsStore.get(wsKey);
|
|
197
|
+
if (wsState === null || wsState === void 0 ? void 0 : wsState.activeReconnectTimer) {
|
|
198
|
+
clearTimeout(wsState.activeReconnectTimer);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
// Send a ping at intervals
|
|
202
|
+
clearPingTimer(wsKey) {
|
|
203
|
+
const wsState = this.wsStore.get(wsKey);
|
|
204
|
+
if (wsState === null || wsState === void 0 ? void 0 : wsState.activePingTimer) {
|
|
205
|
+
clearInterval(wsState.activePingTimer);
|
|
206
|
+
wsState.activePingTimer = undefined;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
// Expect a pong within a time limit
|
|
210
|
+
clearPongTimer(wsKey) {
|
|
211
|
+
const wsState = this.wsStore.get(wsKey);
|
|
212
|
+
if (wsState === null || wsState === void 0 ? void 0 : wsState.activePongTimer) {
|
|
213
|
+
clearTimeout(wsState.activePongTimer);
|
|
214
|
+
wsState.activePongTimer = undefined;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* @private Use the `subscribe(topics)` method to subscribe to topics. Send WS message to subscribe to topics.
|
|
219
|
+
*/
|
|
220
|
+
requestSubscribeTopics(wsKey, topics) {
|
|
221
|
+
if (!topics.length) {
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
const maxTopicsPerEvent = this.getMaxTopicsPerSubscribeEvent(wsKey);
|
|
225
|
+
if (maxTopicsPerEvent && topics.length > maxTopicsPerEvent) {
|
|
226
|
+
this.logger.silly(`Subscribing to topics in batches of ${maxTopicsPerEvent}`);
|
|
227
|
+
for (var i = 0; i < topics.length; i += maxTopicsPerEvent) {
|
|
228
|
+
const batch = topics.slice(i, i + maxTopicsPerEvent);
|
|
229
|
+
this.logger.silly(`Subscribing to batch of ${batch.length}`);
|
|
230
|
+
this.requestSubscribeTopics(wsKey, batch);
|
|
231
|
+
}
|
|
232
|
+
this.logger.silly(`Finished batch subscribing to ${topics.length} topics`);
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
const wsMessage = JSON.stringify({
|
|
236
|
+
op: 'subscribe',
|
|
237
|
+
args: topics,
|
|
238
|
+
});
|
|
239
|
+
this.tryWsSend(wsKey, wsMessage);
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* @private Use the `unsubscribe(topics)` method to unsubscribe from topics. Send WS message to unsubscribe from topics.
|
|
243
|
+
*/
|
|
244
|
+
requestUnsubscribeTopics(wsKey, topics) {
|
|
245
|
+
if (!topics.length) {
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
const maxTopicsPerEvent = this.getMaxTopicsPerSubscribeEvent(wsKey);
|
|
249
|
+
if (maxTopicsPerEvent && topics.length > maxTopicsPerEvent) {
|
|
250
|
+
this.logger.silly(`Unsubscribing to topics in batches of ${maxTopicsPerEvent}`);
|
|
251
|
+
for (var i = 0; i < topics.length; i += maxTopicsPerEvent) {
|
|
252
|
+
const batch = topics.slice(i, i + maxTopicsPerEvent);
|
|
253
|
+
this.logger.silly(`Unsubscribing to batch of ${batch.length}`);
|
|
254
|
+
this.requestUnsubscribeTopics(wsKey, batch);
|
|
255
|
+
}
|
|
256
|
+
this.logger.silly(`Finished batch unsubscribing to ${topics.length} topics`);
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
const wsMessage = JSON.stringify({
|
|
260
|
+
op: 'unsubscribe',
|
|
261
|
+
args: topics,
|
|
262
|
+
});
|
|
263
|
+
this.tryWsSend(wsKey, wsMessage);
|
|
264
|
+
}
|
|
265
|
+
tryWsSend(wsKey, wsMessage) {
|
|
266
|
+
try {
|
|
267
|
+
this.logger.silly(`Sending upstream ws message: `, Object.assign(Object.assign({}, LOGGER_CATEGORY), { wsMessage,
|
|
268
|
+
wsKey }));
|
|
269
|
+
if (!wsKey) {
|
|
270
|
+
throw new Error('Cannot send message due to no known websocket for this wsKey');
|
|
271
|
+
}
|
|
272
|
+
const ws = this.getWs(wsKey);
|
|
273
|
+
if (!ws) {
|
|
274
|
+
throw new Error(`${wsKey} socket not connected yet, call "connectAll()" first then try again when the "open" event arrives`);
|
|
275
|
+
}
|
|
276
|
+
ws.send(wsMessage);
|
|
277
|
+
}
|
|
278
|
+
catch (e) {
|
|
279
|
+
this.logger.error(`Failed to send WS message`, Object.assign(Object.assign({}, LOGGER_CATEGORY), { wsMessage,
|
|
280
|
+
wsKey, exception: e }));
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
connectToWsUrl(url, wsKey) {
|
|
284
|
+
var _a;
|
|
285
|
+
this.logger.silly(`Opening WS connection to URL: ${url}`, Object.assign(Object.assign({}, LOGGER_CATEGORY), { wsKey }));
|
|
286
|
+
const agent = (_a = this.options.requestOptions) === null || _a === void 0 ? void 0 : _a.agent;
|
|
287
|
+
const ws = new isomorphic_ws_1.default(url, undefined, agent ? { agent } : undefined);
|
|
288
|
+
ws.onopen = (event) => this.onWsOpen(event, wsKey);
|
|
289
|
+
ws.onmessage = (event) => this.onWsMessage(event, wsKey);
|
|
290
|
+
ws.onerror = (event) => this.parseWsError('websocket error', event, wsKey);
|
|
291
|
+
ws.onclose = (event) => this.onWsClose(event, wsKey);
|
|
292
|
+
return ws;
|
|
293
|
+
}
|
|
294
|
+
onWsOpen(event, wsKey) {
|
|
295
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
296
|
+
if (this.wsStore.isConnectionState(wsKey, WsStore_types_1.WsConnectionStateEnum.CONNECTING)) {
|
|
297
|
+
this.logger.info('Websocket connected', Object.assign(Object.assign({}, LOGGER_CATEGORY), { wsKey }));
|
|
298
|
+
this.emit('open', { wsKey, event });
|
|
299
|
+
}
|
|
300
|
+
else if (this.wsStore.isConnectionState(wsKey, WsStore_types_1.WsConnectionStateEnum.RECONNECTING)) {
|
|
301
|
+
this.logger.info('Websocket reconnected', Object.assign(Object.assign({}, LOGGER_CATEGORY), { wsKey }));
|
|
302
|
+
this.emit('reconnected', { wsKey, event });
|
|
303
|
+
}
|
|
304
|
+
this.setWsState(wsKey, WsStore_types_1.WsConnectionStateEnum.CONNECTED);
|
|
305
|
+
// Some websockets require an auth packet to be sent after opening the connection
|
|
306
|
+
if (this.shouldAuthOnConnect(wsKey)) {
|
|
307
|
+
yield this.sendAuthRequest(wsKey);
|
|
308
|
+
}
|
|
309
|
+
// Reconnect to topics known before it connected
|
|
310
|
+
// Private topics will be resubscribed to once reconnected
|
|
311
|
+
const topics = [...this.wsStore.getTopics(wsKey)];
|
|
312
|
+
const publicTopics = topics.filter((topic) => !this.isPrivateChannel(topic));
|
|
313
|
+
this.requestSubscribeTopics(wsKey, publicTopics);
|
|
314
|
+
this.wsStore.get(wsKey, true).activePingTimer = setInterval(() => this.ping(wsKey), this.options.pingInterval);
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
/** Handle subscription to private topics _after_ authentication successfully completes asynchronously */
|
|
318
|
+
onWsAuthenticated(wsKey) {
|
|
319
|
+
const wsState = this.wsStore.get(wsKey, true);
|
|
320
|
+
wsState.isAuthenticated = true;
|
|
321
|
+
const topics = [...this.wsStore.getTopics(wsKey)];
|
|
322
|
+
const privateTopics = topics.filter((topic) => this.isPrivateChannel(topic));
|
|
323
|
+
if (privateTopics.length) {
|
|
324
|
+
this.subscribe(privateTopics, true);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
onWsMessage(event, wsKey) {
|
|
328
|
+
try {
|
|
329
|
+
// any message can clear the pong timer - wouldn't get a message if the ws wasn't working
|
|
330
|
+
this.clearPongTimer(wsKey);
|
|
331
|
+
if ((0, requestUtils_1.isWsPong)(event)) {
|
|
332
|
+
this.logger.silly('Received pong', Object.assign(Object.assign({}, LOGGER_CATEGORY), { wsKey }));
|
|
333
|
+
return;
|
|
334
|
+
}
|
|
335
|
+
const msg = JSON.parse((event && event['data']) || event);
|
|
336
|
+
const emittableEvent = Object.assign(Object.assign({}, msg), { wsKey });
|
|
337
|
+
if (typeof msg === 'object') {
|
|
338
|
+
if (typeof msg['code'] === 'number') {
|
|
339
|
+
if (msg.event === 'login' && msg.code === 0) {
|
|
340
|
+
this.logger.info(`Successfully authenticated WS client`, Object.assign(Object.assign({}, LOGGER_CATEGORY), { wsKey }));
|
|
341
|
+
this.emit('response', emittableEvent);
|
|
342
|
+
this.emit('authenticated', emittableEvent);
|
|
343
|
+
this.onWsAuthenticated(wsKey);
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
if (msg['event']) {
|
|
348
|
+
if (msg.event === 'error') {
|
|
349
|
+
this.logger.error(`WS Error received`, Object.assign(Object.assign({}, LOGGER_CATEGORY), { wsKey, message: msg || 'no message',
|
|
350
|
+
// messageType: typeof msg,
|
|
351
|
+
// messageString: JSON.stringify(msg),
|
|
352
|
+
event }));
|
|
353
|
+
this.emit('exception', emittableEvent);
|
|
354
|
+
this.emit('response', emittableEvent);
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
return this.emit('response', emittableEvent);
|
|
358
|
+
}
|
|
359
|
+
if (msg['arg']) {
|
|
360
|
+
return this.emit('update', emittableEvent);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
this.logger.warning('Unhandled/unrecognised ws event message', Object.assign(Object.assign({}, LOGGER_CATEGORY), { message: msg || 'no message',
|
|
364
|
+
// messageType: typeof msg,
|
|
365
|
+
// messageString: JSON.stringify(msg),
|
|
366
|
+
event,
|
|
367
|
+
wsKey }));
|
|
368
|
+
// fallback emit anyway
|
|
369
|
+
return this.emit('update', emittableEvent);
|
|
370
|
+
}
|
|
371
|
+
catch (e) {
|
|
372
|
+
this.logger.error('Failed to parse ws event message', Object.assign(Object.assign({}, LOGGER_CATEGORY), { error: e, event,
|
|
373
|
+
wsKey }));
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
onWsClose(event, wsKey) {
|
|
377
|
+
this.logger.info('Websocket connection closed', Object.assign(Object.assign({}, LOGGER_CATEGORY), { wsKey }));
|
|
378
|
+
if (this.wsStore.getConnectionState(wsKey) !== WsStore_types_1.WsConnectionStateEnum.CLOSING) {
|
|
379
|
+
this.reconnectWithDelay(wsKey, this.options.reconnectTimeout);
|
|
380
|
+
this.emit('reconnect', { wsKey, event });
|
|
381
|
+
}
|
|
382
|
+
else {
|
|
383
|
+
this.setWsState(wsKey, WsStore_types_1.WsConnectionStateEnum.INITIAL);
|
|
384
|
+
this.emit('close', { wsKey, event });
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
getWs(wsKey) {
|
|
388
|
+
return this.wsStore.getWs(wsKey);
|
|
389
|
+
}
|
|
390
|
+
setWsState(wsKey, state) {
|
|
391
|
+
this.wsStore.setConnectionState(wsKey, state);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
exports.BaseWebsocketClient = BaseWebsocketClient;
|
|
395
|
+
//# sourceMappingURL=BaseWSClient.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BaseWSClient.js","sourceRoot":"","sources":["../../src/util/BaseWSClient.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,oDAAkC;AAClC,kEAAsC;AAGtC,wDAAgC;AAChC,mDAAwD;AACxD,qCAAyC;AACzC,iDAA0C;AAC1C,qDAAsD;AAuCtD,MAAM,eAAe,GAAG,EAAE,QAAQ,EAAE,WAAW,EAAE,CAAC;AAElD,MAAsB,mBAGpB,SAAQ,gBAAY;IAMpB,YACE,OAAoC,EACpC,MAA6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K,CAAC,KAAK,CAAC,CAAC;IACnC,CAAC;IAEO,UAAU,CAAC,KAAa,EAAE,KAA4B;QAC5D,IAAI,CAAC,OAAO,CAAC,kBAAkB,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IAChD,CAAC;CACF;AApkBD,kDAokBC"}
|
package/lib/util/WsStore.d.ts
CHANGED
|
@@ -1,50 +1,15 @@
|
|
|
1
1
|
import WebSocket from 'isomorphic-ws';
|
|
2
|
-
import { WsTopic } from '../types';
|
|
3
2
|
import { DefaultLogger } from './logger';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
CONNECTING = 1,
|
|
7
|
-
CONNECTED = 2,
|
|
8
|
-
CLOSING = 3,
|
|
9
|
-
RECONNECTING = 4
|
|
10
|
-
}
|
|
11
|
-
/** A "topic" is always a string */
|
|
12
|
-
export declare type BitgetInstType = 'SP' | 'SPBL' | 'MC' | 'UMCBL' | 'DMCBL';
|
|
13
|
-
export interface WsTopicSubscribeEventArgs {
|
|
14
|
-
instType: BitgetInstType;
|
|
15
|
-
channel: WsTopic;
|
|
16
|
-
/** The symbol, e.g. "BTCUSDT" */
|
|
17
|
-
instId: string;
|
|
18
|
-
}
|
|
19
|
-
declare type WsTopicList = Set<WsTopicSubscribeEventArgs>;
|
|
20
|
-
interface WsStoredState {
|
|
21
|
-
/** The currently active websocket connection */
|
|
22
|
-
ws?: WebSocket;
|
|
23
|
-
/** The current lifecycle state of the connection (enum) */
|
|
24
|
-
connectionState?: WsConnectionStateEnum;
|
|
25
|
-
/** A timer that will send an upstream heartbeat (ping) when it expires */
|
|
26
|
-
activePingTimer?: ReturnType<typeof setTimeout> | undefined;
|
|
27
|
-
/** A timer tracking that an upstream heartbeat was sent, expecting a reply before it expires */
|
|
28
|
-
activePongTimer?: ReturnType<typeof setTimeout> | undefined;
|
|
29
|
-
/** If a reconnection is in progress, this will have the timer for the delayed reconnect */
|
|
30
|
-
activeReconnectTimer?: ReturnType<typeof setTimeout> | undefined;
|
|
31
|
-
/**
|
|
32
|
-
* All the topics we are expected to be subscribed to (and we automatically resubscribe to if the connection drops)
|
|
33
|
-
*
|
|
34
|
-
* A "Set" and a deep object match are used to ensure we only subscribe to a topic once (tracking a list of unique topics we're expected to be connected to)
|
|
35
|
-
*/
|
|
36
|
-
subscribedTopics: WsTopicList;
|
|
37
|
-
isAuthenticated?: boolean;
|
|
38
|
-
}
|
|
39
|
-
export default class WsStore<WsKey extends string> {
|
|
3
|
+
import { WsConnectionStateEnum, WsStoredState } from './WsStore.types';
|
|
4
|
+
export default class WsStore<WsKey extends string, TWSTopicSubscribeEventArgs extends object> {
|
|
40
5
|
private wsState;
|
|
41
6
|
private logger;
|
|
42
7
|
constructor(logger: typeof DefaultLogger);
|
|
43
8
|
/** Get WS stored state for key, optionally create if missing */
|
|
44
|
-
get(key: WsKey, createIfMissing?: true): WsStoredState
|
|
45
|
-
get(key: WsKey, createIfMissing?: false): WsStoredState | undefined;
|
|
9
|
+
get(key: WsKey, createIfMissing?: true): WsStoredState<TWSTopicSubscribeEventArgs>;
|
|
10
|
+
get(key: WsKey, createIfMissing?: false): WsStoredState<TWSTopicSubscribeEventArgs> | undefined;
|
|
46
11
|
getKeys(): WsKey[];
|
|
47
|
-
create(key: WsKey): WsStoredState | undefined;
|
|
12
|
+
create(key: WsKey): WsStoredState<TWSTopicSubscribeEventArgs> | undefined;
|
|
48
13
|
delete(key: WsKey): void;
|
|
49
14
|
hasExistingActiveConnection(key: WsKey): boolean;
|
|
50
15
|
getWs(key: WsKey): WebSocket | undefined;
|
|
@@ -53,10 +18,9 @@ export default class WsStore<WsKey extends string> {
|
|
|
53
18
|
getConnectionState(key: WsKey): WsConnectionStateEnum;
|
|
54
19
|
setConnectionState(key: WsKey, state: WsConnectionStateEnum): void;
|
|
55
20
|
isConnectionState(key: WsKey, state: WsConnectionStateEnum): boolean;
|
|
56
|
-
getTopics(key: WsKey):
|
|
57
|
-
getTopicsByKey(): Record<string,
|
|
58
|
-
getMatchingTopic(key: WsKey, topic:
|
|
59
|
-
addTopic(key: WsKey, topic:
|
|
60
|
-
deleteTopic(key: WsKey, topic:
|
|
21
|
+
getTopics(key: WsKey): Set<TWSTopicSubscribeEventArgs>;
|
|
22
|
+
getTopicsByKey(): Record<string, Set<TWSTopicSubscribeEventArgs>>;
|
|
23
|
+
getMatchingTopic(key: WsKey, topic: TWSTopicSubscribeEventArgs): TWSTopicSubscribeEventArgs | undefined;
|
|
24
|
+
addTopic(key: WsKey, topic: TWSTopicSubscribeEventArgs): Set<TWSTopicSubscribeEventArgs>;
|
|
25
|
+
deleteTopic(key: WsKey, topic: TWSTopicSubscribeEventArgs): Set<TWSTopicSubscribeEventArgs>;
|
|
61
26
|
}
|
|
62
|
-
export {};
|
package/lib/util/WsStore.js
CHANGED
|
@@ -1,16 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.WsConnectionStateEnum = void 0;
|
|
4
3
|
const logger_1 = require("./logger");
|
|
5
|
-
|
|
6
|
-
(function (WsConnectionStateEnum) {
|
|
7
|
-
WsConnectionStateEnum[WsConnectionStateEnum["INITIAL"] = 0] = "INITIAL";
|
|
8
|
-
WsConnectionStateEnum[WsConnectionStateEnum["CONNECTING"] = 1] = "CONNECTING";
|
|
9
|
-
WsConnectionStateEnum[WsConnectionStateEnum["CONNECTED"] = 2] = "CONNECTED";
|
|
10
|
-
WsConnectionStateEnum[WsConnectionStateEnum["CLOSING"] = 3] = "CLOSING";
|
|
11
|
-
WsConnectionStateEnum[WsConnectionStateEnum["RECONNECTING"] = 4] = "RECONNECTING";
|
|
12
|
-
// ERROR = 5,
|
|
13
|
-
})(WsConnectionStateEnum = exports.WsConnectionStateEnum || (exports.WsConnectionStateEnum = {}));
|
|
4
|
+
const WsStore_types_1 = require("./WsStore.types");
|
|
14
5
|
function isDeepObjectMatch(object1, object2) {
|
|
15
6
|
for (const key in object1) {
|
|
16
7
|
if (object1[key] !== object2[key]) {
|
|
@@ -41,7 +32,7 @@ class WsStore {
|
|
|
41
32
|
}
|
|
42
33
|
this.wsState[key] = {
|
|
43
34
|
subscribedTopics: new Set(),
|
|
44
|
-
connectionState: WsConnectionStateEnum.INITIAL,
|
|
35
|
+
connectionState: WsStore_types_1.WsConnectionStateEnum.INITIAL,
|
|
45
36
|
};
|
|
46
37
|
return this.get(key);
|
|
47
38
|
}
|
package/lib/util/WsStore.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WsStore.js","sourceRoot":"","sources":["../../src/util/WsStore.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"WsStore.js","sourceRoot":"","sources":["../../src/util/WsStore.ts"],"names":[],"mappings":";;AACA,qCAAyC;AACzC,mDAAuE;AAEvE,SAAS,iBAAiB,CAAC,OAAe,EAAE,OAAe;IACzD,KAAK,MAAM,GAAG,IAAI,OAAO,EAAE;QACzB,IAAI,OAAO,CAAC,GAAG,CAAC,KAAK,OAAO,CAAC,GAAG,CAAC,EAAE;YACjC,OAAO,KAAK,CAAC;SACd;KACF;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED,MAAqB,OAAO;IAQ1B,YAAY,MAA4B;QAJhC,YAAO,GACb,EAAE,CAAC;QAIH,IAAI,CAAC,MAAM,GAAG,MAAM,IAAI,sBAAa,CAAC;IACxC,CAAC;IAWD,GAAG,CACD,GAAU,EACV,eAAyB;QAEzB,IAAI,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;YACrB,OAAO,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;SAC1B;QAED,IAAI,eAAe,EAAE;YACnB,OAAO,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;SACzB;IACH,CAAC;IAED,OAAO;QACL,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAY,CAAC;IAC9C,CAAC;IAED,MAAM,CAAC,GAAU;QACf,IAAI,IAAI,CAAC,2BAA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YACnC,IAAI,iBAAiB,CAAC,KAAK,EAAE,WAAW,CAAC,EAAE;gBACzC,OAAO,WAAW,CAAC;aACpB;SACF;IACH,CAAC;IAED,QAAQ,CAAC,GAAU,EAAE,KAAiC;QACpD,mCAAmC;QACnC,gCAAgC;QAChC,sBAAsB;QACtB,sBAAsB;QACtB,yBAAyB;QACzB,OAAO;QACP,IAAI;QACJ,sEAAsE;QACtE,MAAM,aAAa,GAAG,IAAI,CAAC,gBAAgB,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QACxD,IAAI,aAAa,EAAE;YACjB,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;SAC5B;QACD,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IACxC,CAAC;IAED,WAAW,CAAC,GAAU,EAAE,KAAiC;QACvD,iDAAiD;QACjD,MAAM,WAAW,GAAG,IAAI,CAAC,gBAAgB,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;QACtD,IAAI,WAAW,EAAE;YACf,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;SACzC;QAED,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC;IAC7B,CAAC;CACF;AAlKD,0BAkKC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export declare enum WsConnectionStateEnum {
|
|
2
|
+
INITIAL = 0,
|
|
3
|
+
CONNECTING = 1,
|
|
4
|
+
CONNECTED = 2,
|
|
5
|
+
CLOSING = 3,
|
|
6
|
+
RECONNECTING = 4
|
|
7
|
+
}
|
|
8
|
+
export interface WsStoredState<TWSTopicSubscribeEvent extends object> {
|
|
9
|
+
/** The currently active websocket connection */
|
|
10
|
+
ws?: WebSocket;
|
|
11
|
+
/** The current lifecycle state of the connection (enum) */
|
|
12
|
+
connectionState?: WsConnectionStateEnum;
|
|
13
|
+
/** A timer that will send an upstream heartbeat (ping) when it expires */
|
|
14
|
+
activePingTimer?: ReturnType<typeof setTimeout> | undefined;
|
|
15
|
+
/** A timer tracking that an upstream heartbeat was sent, expecting a reply before it expires */
|
|
16
|
+
activePongTimer?: ReturnType<typeof setTimeout> | undefined;
|
|
17
|
+
/** If a reconnection is in progress, this will have the timer for the delayed reconnect */
|
|
18
|
+
activeReconnectTimer?: ReturnType<typeof setTimeout> | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* All the topics we are expected to be subscribed to on this connection (and we automatically resubscribe to if the connection drops)
|
|
21
|
+
*
|
|
22
|
+
* A "Set" and a deep-object-match are used to ensure we only subscribe to a topic once (tracking a list of unique topics we're expected to be connected to)
|
|
23
|
+
*/
|
|
24
|
+
subscribedTopics: Set<TWSTopicSubscribeEvent>;
|
|
25
|
+
isAuthenticated?: boolean;
|
|
26
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WsConnectionStateEnum = void 0;
|
|
4
|
+
var WsConnectionStateEnum;
|
|
5
|
+
(function (WsConnectionStateEnum) {
|
|
6
|
+
WsConnectionStateEnum[WsConnectionStateEnum["INITIAL"] = 0] = "INITIAL";
|
|
7
|
+
WsConnectionStateEnum[WsConnectionStateEnum["CONNECTING"] = 1] = "CONNECTING";
|
|
8
|
+
WsConnectionStateEnum[WsConnectionStateEnum["CONNECTED"] = 2] = "CONNECTED";
|
|
9
|
+
WsConnectionStateEnum[WsConnectionStateEnum["CLOSING"] = 3] = "CLOSING";
|
|
10
|
+
WsConnectionStateEnum[WsConnectionStateEnum["RECONNECTING"] = 4] = "RECONNECTING";
|
|
11
|
+
// ERROR = 5,
|
|
12
|
+
})(WsConnectionStateEnum = exports.WsConnectionStateEnum || (exports.WsConnectionStateEnum = {}));
|
|
13
|
+
//# sourceMappingURL=WsStore.types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"WsStore.types.js","sourceRoot":"","sources":["../../src/util/WsStore.types.ts"],"names":[],"mappings":";;;AAAA,IAAY,qBAOX;AAPD,WAAY,qBAAqB;IAC/B,uEAAW,CAAA;IACX,6EAAc,CAAA;IACd,2EAAa,CAAA;IACb,uEAAW,CAAA;IACX,iFAAgB,CAAA;IAChB,aAAa;AACf,CAAC,EAPW,qBAAqB,GAArB,6BAAqB,KAArB,6BAAqB,QAOhC"}
|
package/lib/util/requestUtils.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"requestUtils.js","sourceRoot":"","sources":["../../src/util/requestUtils.ts"],"names":[],"mappings":";;;AAoCA,SAAgB,eAAe,CAC7B,MAAS,EACT,iBAAiB,GAAG,KAAK,EACzB,eAAwB,IAAI,EAC5B,aAAqB,EAAE;IAEvB,IAAI,CAAC,MAAM,EAAE;QACX,OAAO,EAAE,CAAC;KACX;IAED,MAAM,WAAW,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC;SACpC,IAAI,EAAE;SACN,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE;QACX,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;QAC1B,IAAI,iBAAiB,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,WAAW,EAAE;YAC9D,MAAM,IAAI,KAAK,CACb,uDAAuD,CACxD,CAAC;SACH;QACD,MAAM,YAAY,GAAG,YAAY,CAAC,CAAC,CAAC,kBAAkB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;QACtE,OAAO,GAAG,GAAG,IAAI,YAAY,EAAE,CAAC;IAClC,CAAC,CAAC;SACD,IAAI,CAAC,GAAG,CAAC,CAAC;IAEb,iCAAiC;IACjC,OAAO,WAAW,CAAC,CAAC,CAAC,UAAU,GAAG,WAAW,CAAC,CAAC,CAAC,WAAW,CAAC;AAC9D,CAAC;AA1BD,0CA0BC;AAED,SAAgB,cAAc,CAC5B,UAAmB,EACnB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C,CAAC,kBAAkB,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;QACtE,OAAO,GAAG,GAAG,IAAI,YAAY,EAAE,CAAC;IAClC,CAAC,CAAC;SACD,IAAI,CAAC,GAAG,CAAC,CAAC;IAEb,iCAAiC;IACjC,OAAO,WAAW,CAAC,CAAC,CAAC,UAAU,GAAG,WAAW,CAAC,CAAC,CAAC,WAAW,CAAC;AAC9D,CAAC;AA1BD,0CA0BC;AAED,SAAgB,cAAc,CAC5B,UAAmB,EACnB,kBAAqC;IAErC,MAAM,gBAAgB,GAAG;QACvB,OAAO,EAAE,wBAAwB;QACjC,QAAQ,EAAE,yBAAyB;QACnC,OAAO,EAAE,mBAAmB;KAC7B,CAAC;IAEF,IAAI,kBAAkB,CAAC,OAAO,EAAE;QAC9B,OAAO,kBAAkB,CAAC,OAAO,CAAC;KACnC;IAED,IAAI,UAAU,EAAE;QACd,OAAO,gBAAgB,CAAC,OAAO,CAAC;KACjC;IAED,OAAO,gBAAgB,CAAC,OAAO,CAAC;AAClC,CAAC;AAnBD,wCAmBC;AAED,SAAgB,QAAQ,CAAC,GAAQ;IAC/B,SAAS;IACT,IAAI,CAAA,GAAG,aAAH,GAAG,uBAAH,GAAG,CAAE,IAAI,MAAK,MAAM,EAAE;QACxB,OAAO,IAAI,CAAC;KACb;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAND,4BAMC;AAED;;GAEG;AACU,QAAA,qBAAqB,GAAG;IACnC,IAAI,EAAE,MAAM;IACZ,OAAO,EAAE,SAAS;IAClB,MAAM,EAAE,QAAQ;IAChB,EAAE,EAAE,IAAI;CACA,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { WsAccountSnapshotUMCBL, WSPositionSnapshotUMCBL, WsSnapshotAccountEvent, WsSnapshotPositionsEvent } from '../types';
|
|
1
|
+
import { MarginType, WsAccountSnapshotUMCBL, WSPositionSnapshotUMCBL, WsSnapshotAccountEvent, WsSnapshotPositionsEvent } from '../types';
|
|
2
2
|
/** TypeGuard: event is an account update (balance) */
|
|
3
3
|
export declare function isWsAccountSnapshotEvent(event: unknown): event is WsSnapshotAccountEvent;
|
|
4
4
|
/** TypeGuard: event is a positions update */
|
|
@@ -7,3 +7,7 @@ export declare function isWsPositionsSnapshotEvent(event: unknown): event is WsS
|
|
|
7
7
|
export declare function isWsFuturesAccountSnapshotEvent(event: unknown): event is WsAccountSnapshotUMCBL;
|
|
8
8
|
/** TypeGuard: event is a UMCBL positions update */
|
|
9
9
|
export declare function isWsFuturesPositionsSnapshotEvent(event: unknown): event is WSPositionSnapshotUMCBL;
|
|
10
|
+
/**
|
|
11
|
+
* Simple guard for non-TypeScript users, throw a runtime error if value doesn't match type
|
|
12
|
+
*/
|
|
13
|
+
export declare function assertMarginType(marginType: string): marginType is MarginType;
|