@gmx-io/sdk 2.7.0 → 2.9.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/build/cjs/scripts/check-eip6492-relay-signature.js +116 -0
- package/build/cjs/src/clients/v2/__tests__/subaccount.spec.js +2 -2
- package/build/cjs/src/clients/v2/index.js +20 -1
- package/build/cjs/src/configs/contracts.js +49 -49
- package/build/cjs/src/utils/__tests__/numbers.spec.js +15 -0
- package/build/cjs/src/utils/__tests__/orderTransactions/exchangeRouterMulticall.spec.js +6 -4
- package/build/cjs/src/utils/express/utils/relayParamsUtils.js +9 -1
- package/build/cjs/src/utils/express/utils/relayParamsUtils.spec.js +21 -0
- package/build/cjs/src/utils/multichain/__tests__/api.spec.js +11 -7
- package/build/cjs/src/utils/numbers/utils.js +8 -2
- package/build/cjs/src/utils/prices/api.js +15 -1
- package/build/cjs/src/utils/stream/WsStreamClient.js +363 -0
- package/build/cjs/src/utils/stream/__tests__/WsStreamClient.spec.js +383 -0
- package/build/cjs/src/utils/stream/__tests__/stream.integration.spec.js +133 -0
- package/build/cjs/src/utils/stream/index.js +11 -0
- package/build/cjs/src/utils/stream/subscription.js +61 -0
- package/build/cjs/src/utils/stream/types.js +2 -0
- package/build/esm/scripts/check-eip6492-relay-signature.js +114 -0
- package/build/esm/src/clients/v2/__tests__/subaccount.spec.js +2 -2
- package/build/esm/src/clients/v2/index.js +21 -2
- package/build/esm/src/configs/contracts.js +49 -49
- package/build/esm/src/utils/__tests__/numbers.spec.js +15 -0
- package/build/esm/src/utils/__tests__/orderTransactions/exchangeRouterMulticall.spec.js +6 -4
- package/build/esm/src/utils/express/utils/relayParamsUtils.js +8 -1
- package/build/esm/src/utils/express/utils/relayParamsUtils.spec.js +19 -0
- package/build/esm/src/utils/multichain/__tests__/api.spec.js +11 -7
- package/build/esm/src/utils/numbers/utils.js +8 -2
- package/build/esm/src/utils/prices/api.js +12 -0
- package/build/esm/src/utils/stream/WsStreamClient.js +355 -0
- package/build/esm/src/utils/stream/__tests__/WsStreamClient.spec.js +381 -0
- package/build/esm/src/utils/stream/__tests__/stream.integration.spec.js +131 -0
- package/build/esm/src/utils/stream/index.js +5 -0
- package/build/esm/src/utils/stream/subscription.js +57 -0
- package/build/esm/src/utils/stream/types.js +1 -0
- package/build/tsconfig.tsbuildinfo +1 -1
- package/build/types/scripts/check-eip6492-relay-signature.d.ts +1 -0
- package/build/types/src/clients/v2/index.d.ts +23 -5
- package/build/types/src/utils/express/utils/relayParamsUtils.d.ts +1 -0
- package/build/types/src/utils/express/utils/relayParamsUtils.spec.d.ts +1 -0
- package/build/types/src/utils/prices/api.d.ts +8 -0
- package/build/types/src/utils/stream/WsStreamClient.d.ts +63 -0
- package/build/types/src/utils/stream/__tests__/WsStreamClient.spec.d.ts +1 -0
- package/build/types/src/utils/stream/__tests__/stream.integration.spec.d.ts +1 -0
- package/build/types/src/utils/stream/index.d.ts +5 -0
- package/build/types/src/utils/stream/subscription.d.ts +3 -0
- package/build/types/src/utils/stream/types.d.ts +43 -0
- package/package.json +14 -2
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import IsomorphicWebSocket from "isomorphic-ws";
|
|
2
|
+
const DEFAULT_RECONNECT_BASE_MS = 500;
|
|
3
|
+
const DEFAULT_RECONNECT_MAX_MS = 10000;
|
|
4
|
+
const DEFAULT_PROBE_INTERVAL_MS = 15000;
|
|
5
|
+
const DEFAULT_PROBE_TIMEOUT_MS = 10000;
|
|
6
|
+
const CONNECT_TIMEOUT_MS = 10000;
|
|
7
|
+
const IDLE_CLOSE_MS = 10000;
|
|
8
|
+
// a socket that stayed up this long proves the path works; one dropped sooner keeps backing off
|
|
9
|
+
const STABLE_CONNECTION_MS = 30000;
|
|
10
|
+
const REFUSED_MESSAGE = "subscription refused";
|
|
11
|
+
function resolveWebSocketCtor(injected) {
|
|
12
|
+
return injected ?? IsomorphicWebSocket;
|
|
13
|
+
}
|
|
14
|
+
export function callListener(listener, value) {
|
|
15
|
+
try {
|
|
16
|
+
listener(value);
|
|
17
|
+
}
|
|
18
|
+
catch (error) {
|
|
19
|
+
queueMicrotask(() => {
|
|
20
|
+
throw error;
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
export class WsStreamClient {
|
|
25
|
+
constructor(params) {
|
|
26
|
+
this.connectionStatus = "closed";
|
|
27
|
+
this.listeners = new Map();
|
|
28
|
+
this.connectionStatusListeners = new Set();
|
|
29
|
+
this.lastFrameByChannel = new Map();
|
|
30
|
+
this.originTsByChannel = new Map();
|
|
31
|
+
this.refusalByChannel = new Map();
|
|
32
|
+
this.pendingOps = [];
|
|
33
|
+
this.reconnectAttempt = 0;
|
|
34
|
+
this.messageCount = 0;
|
|
35
|
+
this.closedByUser = false;
|
|
36
|
+
this.url = params.url;
|
|
37
|
+
this.WebSocketImpl = resolveWebSocketCtor(params.webSocketImpl);
|
|
38
|
+
this.reconnectBaseMs = params.reconnectBaseMs ?? DEFAULT_RECONNECT_BASE_MS;
|
|
39
|
+
this.reconnectMaxMs = params.reconnectMaxMs ?? DEFAULT_RECONNECT_MAX_MS;
|
|
40
|
+
this.probeIntervalMs = params.probeIntervalMs ?? DEFAULT_PROBE_INTERVAL_MS;
|
|
41
|
+
this.probeTimeoutMs = params.probeTimeoutMs ?? DEFAULT_PROBE_TIMEOUT_MS;
|
|
42
|
+
this.random = params.random ?? Math.random;
|
|
43
|
+
}
|
|
44
|
+
subscribe(channel, listener, onError) {
|
|
45
|
+
const subscriber = { onFrame: listener, onError };
|
|
46
|
+
clearTimeout(this.idleCloseTimer);
|
|
47
|
+
const existing = this.listeners.get(channel);
|
|
48
|
+
if (existing) {
|
|
49
|
+
existing.add(subscriber);
|
|
50
|
+
const frame = this.lastFrameByChannel.get(channel);
|
|
51
|
+
const refusal = this.refusalByChannel.get(channel);
|
|
52
|
+
queueMicrotask(() => this.replay(channel, subscriber, frame, refusal));
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
this.listeners.set(channel, new Set([subscriber]));
|
|
56
|
+
}
|
|
57
|
+
this.ensureConnected();
|
|
58
|
+
if (!existing && this.connectionStatus === "live") {
|
|
59
|
+
this.sendOp("subscribe", [channel]);
|
|
60
|
+
}
|
|
61
|
+
return () => {
|
|
62
|
+
const current = this.listeners.get(channel);
|
|
63
|
+
if (!current?.delete(subscriber)) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (current.size === 0) {
|
|
67
|
+
this.listeners.delete(channel);
|
|
68
|
+
this.lastFrameByChannel.delete(channel);
|
|
69
|
+
this.originTsByChannel.delete(channel);
|
|
70
|
+
this.refusalByChannel.delete(channel);
|
|
71
|
+
if (this.connectionStatus === "live") {
|
|
72
|
+
this.sendOp("unsubscribe", [channel]);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (this.listeners.size === 0) {
|
|
76
|
+
this.idleCloseTimer = setTimeout(() => this.close(), IDLE_CLOSE_MS);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
subscribeConnectionStatus(listener) {
|
|
81
|
+
this.connectionStatusListeners.add(listener);
|
|
82
|
+
return () => {
|
|
83
|
+
this.connectionStatusListeners.delete(listener);
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
ensureConnected() {
|
|
87
|
+
if (this.ws || this.reconnectTimer) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
this.closedByUser = false;
|
|
91
|
+
this.connect();
|
|
92
|
+
}
|
|
93
|
+
canConnect() {
|
|
94
|
+
return !this.ws && !this.closedByUser && this.listeners.size > 0;
|
|
95
|
+
}
|
|
96
|
+
connect() {
|
|
97
|
+
if (!this.canConnect()) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
this.setConnectionStatus(this.connectionStatus === "closed" ? "connecting" : "reconnecting");
|
|
101
|
+
if (!this.canConnect()) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
this.pendingOps = [];
|
|
105
|
+
this.refusalByChannel.clear();
|
|
106
|
+
this.openedAt = undefined;
|
|
107
|
+
let ws;
|
|
108
|
+
try {
|
|
109
|
+
ws = new this.WebSocketImpl(this.url);
|
|
110
|
+
}
|
|
111
|
+
catch {
|
|
112
|
+
this.scheduleReconnect();
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
this.ws = ws;
|
|
116
|
+
this.connectTimeoutTimer = setTimeout(() => {
|
|
117
|
+
if (this.ws === ws && this.connectionStatus !== "live") {
|
|
118
|
+
this.dropSocket();
|
|
119
|
+
this.scheduleReconnect();
|
|
120
|
+
}
|
|
121
|
+
}, CONNECT_TIMEOUT_MS);
|
|
122
|
+
ws.onopen = () => {
|
|
123
|
+
if (this.ws !== ws) {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
clearTimeout(this.connectTimeoutTimer);
|
|
127
|
+
this.openedAt = Date.now();
|
|
128
|
+
const channels = [...this.listeners.keys()];
|
|
129
|
+
if (channels.length) {
|
|
130
|
+
this.sendOp("subscribe", channels);
|
|
131
|
+
}
|
|
132
|
+
this.setConnectionStatus("live");
|
|
133
|
+
};
|
|
134
|
+
ws.onmessage = (ev) => {
|
|
135
|
+
if (this.ws === ws) {
|
|
136
|
+
this.onMessage(ev.data);
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
ws.onclose = () => {
|
|
140
|
+
if (this.ws === ws) {
|
|
141
|
+
this.onClose();
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
ws.onerror = () => {
|
|
145
|
+
// a close event always follows; reconnection is handled there
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
onMessage(raw) {
|
|
149
|
+
this.messageCount += 1;
|
|
150
|
+
const text = typeof raw === "string" ? raw : String(raw);
|
|
151
|
+
let parsed;
|
|
152
|
+
try {
|
|
153
|
+
parsed = JSON.parse(text);
|
|
154
|
+
}
|
|
155
|
+
catch {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
const frame = parsed;
|
|
162
|
+
if ("op" in frame) {
|
|
163
|
+
this.onOp(frame);
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
if (frame.type !== "snapshot") {
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
if (frame.data === undefined) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
const set = this.listeners.get(frame.ch);
|
|
173
|
+
if (!set) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
if (typeof frame.originTs === "number") {
|
|
177
|
+
const newest = this.originTsByChannel.get(frame.ch);
|
|
178
|
+
if (newest !== undefined && frame.originTs < newest) {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
this.originTsByChannel.set(frame.ch, frame.originTs);
|
|
182
|
+
}
|
|
183
|
+
const payload = {
|
|
184
|
+
data: frame.data,
|
|
185
|
+
serverTs: frame.serverTs,
|
|
186
|
+
originTs: frame.originTs,
|
|
187
|
+
receivedAt: Date.now(),
|
|
188
|
+
};
|
|
189
|
+
this.lastFrameByChannel.set(frame.ch, payload);
|
|
190
|
+
this.refusalByChannel.delete(frame.ch);
|
|
191
|
+
for (const subscriber of set) {
|
|
192
|
+
callListener(subscriber.onFrame, payload);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
onOp(frame) {
|
|
196
|
+
if (frame.op === "error") {
|
|
197
|
+
if (!Array.isArray(frame.channels)) {
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
200
|
+
const pending = this.pendingOps[0];
|
|
201
|
+
for (const channel of frame.channels) {
|
|
202
|
+
if (pending?.op === "subscribe") {
|
|
203
|
+
pending.channels = pending.channels.filter((requested) => requested !== channel);
|
|
204
|
+
}
|
|
205
|
+
this.refuse(channel, frame.message);
|
|
206
|
+
}
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
if (frame.op !== "ack") {
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
const pending = this.pendingOps.shift();
|
|
213
|
+
if (pending?.op !== "subscribe" || !Array.isArray(frame.channels)) {
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
for (const channel of pending.channels) {
|
|
217
|
+
if (!frame.channels.includes(channel)) {
|
|
218
|
+
this.refuse(channel, REFUSED_MESSAGE);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
refuse(channel, message) {
|
|
223
|
+
const set = this.listeners.get(channel);
|
|
224
|
+
if (!set) {
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
const error = { message };
|
|
228
|
+
this.refusalByChannel.set(channel, error);
|
|
229
|
+
for (const subscriber of set) {
|
|
230
|
+
if (subscriber.onError) {
|
|
231
|
+
callListener(subscriber.onError, error);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
replay(channel, subscriber, frame, refusal) {
|
|
236
|
+
if (!this.listeners.get(channel)?.has(subscriber)) {
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
// only while still current: anything newer already reached this subscriber directly
|
|
240
|
+
if (frame && frame === this.lastFrameByChannel.get(channel)) {
|
|
241
|
+
callListener(subscriber.onFrame, frame);
|
|
242
|
+
}
|
|
243
|
+
if (refusal && refusal === this.refusalByChannel.get(channel) && subscriber.onError) {
|
|
244
|
+
callListener(subscriber.onError, refusal);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
onClose() {
|
|
248
|
+
this.ws = undefined;
|
|
249
|
+
clearTimeout(this.connectTimeoutTimer);
|
|
250
|
+
this.noteSocketEnded();
|
|
251
|
+
if (this.closedByUser || this.listeners.size === 0) {
|
|
252
|
+
this.setConnectionStatus("closed");
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
this.scheduleReconnect();
|
|
256
|
+
}
|
|
257
|
+
noteSocketEnded() {
|
|
258
|
+
if (this.openedAt !== undefined && Date.now() - this.openedAt >= STABLE_CONNECTION_MS) {
|
|
259
|
+
this.reconnectAttempt = 0;
|
|
260
|
+
}
|
|
261
|
+
this.openedAt = undefined;
|
|
262
|
+
}
|
|
263
|
+
scheduleReconnect() {
|
|
264
|
+
const ceiling = Math.min(this.reconnectMaxMs, this.reconnectBaseMs * 2 ** this.reconnectAttempt);
|
|
265
|
+
this.reconnectAttempt += 1;
|
|
266
|
+
this.reconnectTimer = setTimeout(() => {
|
|
267
|
+
this.reconnectTimer = undefined;
|
|
268
|
+
this.connect();
|
|
269
|
+
}, ceiling / 2 + (this.random() * ceiling) / 2);
|
|
270
|
+
this.setConnectionStatus("reconnecting");
|
|
271
|
+
}
|
|
272
|
+
startProbe() {
|
|
273
|
+
this.probeTimer = setTimeout(() => {
|
|
274
|
+
const messagesAtProbe = this.messageCount;
|
|
275
|
+
this.sendOp("subscribe", []);
|
|
276
|
+
if (!this.probeTimeoutTimer) {
|
|
277
|
+
this.probeTimeoutTimer = setTimeout(() => {
|
|
278
|
+
this.probeTimeoutTimer = undefined;
|
|
279
|
+
if (this.messageCount === messagesAtProbe) {
|
|
280
|
+
this.dropSocket();
|
|
281
|
+
this.noteSocketEnded();
|
|
282
|
+
this.scheduleReconnect();
|
|
283
|
+
}
|
|
284
|
+
}, this.probeTimeoutMs);
|
|
285
|
+
}
|
|
286
|
+
this.startProbe();
|
|
287
|
+
}, this.probeIntervalMs);
|
|
288
|
+
}
|
|
289
|
+
stopProbe() {
|
|
290
|
+
clearTimeout(this.probeTimer);
|
|
291
|
+
clearTimeout(this.probeTimeoutTimer);
|
|
292
|
+
this.probeTimer = undefined;
|
|
293
|
+
this.probeTimeoutTimer = undefined;
|
|
294
|
+
}
|
|
295
|
+
sendOp(op, channels) {
|
|
296
|
+
try {
|
|
297
|
+
this.ws?.send(JSON.stringify({ op, channels }));
|
|
298
|
+
this.pendingOps.push({ op, channels });
|
|
299
|
+
}
|
|
300
|
+
catch {
|
|
301
|
+
// socket raced into a non-open state; resubscribe runs on reconnect
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
setConnectionStatus(status) {
|
|
305
|
+
if (this.connectionStatus === status) {
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
this.connectionStatus = status;
|
|
309
|
+
if (status === "live") {
|
|
310
|
+
this.startProbe();
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
this.stopProbe();
|
|
314
|
+
}
|
|
315
|
+
for (const listener of this.connectionStatusListeners) {
|
|
316
|
+
if (this.connectionStatus !== status) {
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
callListener(listener, status);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
dropSocket() {
|
|
323
|
+
const ws = this.ws;
|
|
324
|
+
this.ws = undefined;
|
|
325
|
+
clearTimeout(this.connectTimeoutTimer);
|
|
326
|
+
if (!ws) {
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
// onerror stays attached: ws in Node throws on an unhandled "error" when a CONNECTING socket is closed
|
|
330
|
+
ws.onopen = null;
|
|
331
|
+
ws.onmessage = null;
|
|
332
|
+
ws.onclose = null;
|
|
333
|
+
try {
|
|
334
|
+
ws.close();
|
|
335
|
+
}
|
|
336
|
+
catch {
|
|
337
|
+
// already closing
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
close() {
|
|
341
|
+
this.closedByUser = true;
|
|
342
|
+
clearTimeout(this.idleCloseTimer);
|
|
343
|
+
if (this.reconnectTimer) {
|
|
344
|
+
clearTimeout(this.reconnectTimer);
|
|
345
|
+
this.reconnectTimer = undefined;
|
|
346
|
+
}
|
|
347
|
+
this.listeners.clear();
|
|
348
|
+
this.lastFrameByChannel.clear();
|
|
349
|
+
this.originTsByChannel.clear();
|
|
350
|
+
this.refusalByChannel.clear();
|
|
351
|
+
this.reconnectAttempt = 0;
|
|
352
|
+
this.dropSocket();
|
|
353
|
+
this.setConnectionStatus("closed");
|
|
354
|
+
}
|
|
355
|
+
}
|