@xapy/orderbook 0.1.27 → 0.1.29
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 +2 -1
- package/dist/exchanges/aster/index.cjs +610 -0
- package/dist/exchanges/aster/index.cjs.map +1 -0
- package/dist/exchanges/aster/index.d.cts +65 -0
- package/dist/exchanges/aster/index.d.ts +65 -0
- package/dist/exchanges/aster/index.js +602 -0
- package/dist/exchanges/aster/index.js.map +1 -0
- package/dist/exchanges/binance/index.d.cts +1 -1
- package/dist/exchanges/binance/index.d.ts +1 -1
- package/dist/exchanges/bingx/index.d.cts +1 -1
- package/dist/exchanges/bingx/index.d.ts +1 -1
- package/dist/exchanges/bitget/index.d.cts +1 -1
- package/dist/exchanges/bitget/index.d.ts +1 -1
- package/dist/exchanges/bybit/index.d.cts +1 -1
- package/dist/exchanges/bybit/index.d.ts +1 -1
- package/dist/exchanges/coinex/index.d.cts +1 -1
- package/dist/exchanges/coinex/index.d.ts +1 -1
- package/dist/exchanges/deribit/index.d.cts +1 -1
- package/dist/exchanges/deribit/index.d.ts +1 -1
- package/dist/exchanges/edgex/index.d.cts +1 -1
- package/dist/exchanges/edgex/index.d.ts +1 -1
- package/dist/exchanges/gate/index.d.cts +1 -1
- package/dist/exchanges/gate/index.d.ts +1 -1
- package/dist/exchanges/hyperliquid/index.d.cts +1 -1
- package/dist/exchanges/hyperliquid/index.d.ts +1 -1
- package/dist/exchanges/kucoin/index.d.cts +1 -1
- package/dist/exchanges/kucoin/index.d.ts +1 -1
- package/dist/exchanges/lighter/index.d.cts +1 -1
- package/dist/exchanges/lighter/index.d.ts +1 -1
- package/dist/exchanges/okx/index.d.cts +1 -1
- package/dist/exchanges/okx/index.d.ts +1 -1
- package/dist/exchanges/{grvt → whitebit}/index.cjs +98 -116
- package/dist/exchanges/whitebit/index.cjs.map +1 -0
- package/dist/exchanges/whitebit/index.d.cts +57 -0
- package/dist/exchanges/whitebit/index.d.ts +57 -0
- package/dist/exchanges/{grvt → whitebit}/index.js +93 -114
- package/dist/exchanges/whitebit/index.js.map +1 -0
- package/dist/index.cjs +433 -235
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -3
- package/dist/index.d.ts +4 -3
- package/dist/index.js +432 -235
- package/dist/index.js.map +1 -1
- package/dist/{stream-YYPvjMV4.d.cts → stream-D5-FyNGW.d.cts} +1 -1
- package/dist/{stream-YYPvjMV4.d.ts → stream-D5-FyNGW.d.ts} +1 -1
- package/package.json +23 -12
- package/dist/exchanges/grvt/index.cjs.map +0 -1
- package/dist/exchanges/grvt/index.d.cts +0 -47
- package/dist/exchanges/grvt/index.d.ts +0 -47
- package/dist/exchanges/grvt/index.js.map +0 -1
|
@@ -0,0 +1,602 @@
|
|
|
1
|
+
// src/core/symbol.ts
|
|
2
|
+
function parseSymbol(symbol) {
|
|
3
|
+
const [pair, settle] = symbol.split(":");
|
|
4
|
+
const parts = (pair ?? "").split("/");
|
|
5
|
+
const base = parts[0]?.toUpperCase();
|
|
6
|
+
const quote = parts[1]?.toUpperCase();
|
|
7
|
+
if (!base || !quote) {
|
|
8
|
+
throw new Error(
|
|
9
|
+
`invalid symbol "${symbol}" \u2014 expected "BASE/QUOTE" or "BASE/QUOTE:SETTLE"`
|
|
10
|
+
);
|
|
11
|
+
}
|
|
12
|
+
if (settle && settle.toUpperCase() !== quote) {
|
|
13
|
+
throw new Error(
|
|
14
|
+
`unsupported settlement currency in "${symbol}" \u2014 only linear (settle === quote) is supported`
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
return {
|
|
18
|
+
base,
|
|
19
|
+
quote,
|
|
20
|
+
market: settle ? "perpetual" : "spot",
|
|
21
|
+
pair: `${base}/${quote}`
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// src/transport/http.ts
|
|
26
|
+
async function httpJson(req) {
|
|
27
|
+
const url = req.transformUrl ? req.transformUrl(req.url) : req.url;
|
|
28
|
+
const controller = new AbortController();
|
|
29
|
+
const timer = setTimeout(() => controller.abort(), req.timeoutMs ?? 1e4);
|
|
30
|
+
try {
|
|
31
|
+
const res = await fetch(url, {
|
|
32
|
+
method: req.method ?? "GET",
|
|
33
|
+
headers: req.headers,
|
|
34
|
+
body: req.body,
|
|
35
|
+
signal: controller.signal
|
|
36
|
+
});
|
|
37
|
+
if (!res.ok) {
|
|
38
|
+
const text = await res.text().catch(() => "");
|
|
39
|
+
throw new Error(`HTTP ${res.status} ${res.statusText}: ${text.slice(0, 200)}`);
|
|
40
|
+
}
|
|
41
|
+
return await res.json();
|
|
42
|
+
} finally {
|
|
43
|
+
clearTimeout(timer);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// src/exchanges/aster/symbols.ts
|
|
48
|
+
var QUOTES = ["USDT", "USD1", "FORM", "USDC", "BTC", "ETH", "U"];
|
|
49
|
+
function toAsterSymbol(symbol) {
|
|
50
|
+
const [base, quote] = symbol.split("/");
|
|
51
|
+
if (!base || !quote) {
|
|
52
|
+
throw new Error(`invalid symbol "${symbol}" \u2014 expected "BASE/QUOTE"`);
|
|
53
|
+
}
|
|
54
|
+
return `${base}${quote}`.toUpperCase();
|
|
55
|
+
}
|
|
56
|
+
function fromAsterSymbol(s) {
|
|
57
|
+
const upper = s.toUpperCase();
|
|
58
|
+
for (const q of QUOTES) {
|
|
59
|
+
if (upper.endsWith(q) && upper.length > q.length) {
|
|
60
|
+
return `${upper.slice(0, -q.length)}/${q}`;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return upper;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// src/exchanges/aster/rest.ts
|
|
67
|
+
var ASTER_SPOT_REST = "https://sapi.asterdex.com";
|
|
68
|
+
var ASTER_FUTURES_REST = "https://fapi.asterdex.com";
|
|
69
|
+
async function fetchAsterOrderbook(opts) {
|
|
70
|
+
const symbol = toAsterSymbol(opts.symbol);
|
|
71
|
+
const limit = opts.depth ?? 1e3;
|
|
72
|
+
const url = opts.market === "spot" ? `${ASTER_SPOT_REST}/api/v3/depth?symbol=${symbol}&limit=${limit}` : `${ASTER_FUTURES_REST}/fapi/v1/depth?symbol=${symbol}&limit=${limit}`;
|
|
73
|
+
const data = await httpJson({
|
|
74
|
+
url,
|
|
75
|
+
timeoutMs: opts.timeoutMs,
|
|
76
|
+
transformUrl: opts.transformUrl
|
|
77
|
+
});
|
|
78
|
+
return {
|
|
79
|
+
exchange: "aster",
|
|
80
|
+
symbol: fromAsterSymbol(data.symbol ?? symbol),
|
|
81
|
+
market: opts.market,
|
|
82
|
+
bids: data.bids.map(([p, s]) => ({ price: Number(p), size: Number(s) })),
|
|
83
|
+
asks: data.asks.map(([p, s]) => ({ price: Number(p), size: Number(s) })),
|
|
84
|
+
timestamp: data.E ?? data.T ?? Date.now(),
|
|
85
|
+
sequence: data.lastUpdateId
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// src/core/book-merger.ts
|
|
90
|
+
var BookMerger = class {
|
|
91
|
+
bids = /* @__PURE__ */ new Map();
|
|
92
|
+
// price -> size
|
|
93
|
+
asks = /* @__PURE__ */ new Map();
|
|
94
|
+
sequence = -1;
|
|
95
|
+
timestamp = 0;
|
|
96
|
+
hasSnapshot = false;
|
|
97
|
+
apply(event) {
|
|
98
|
+
if (event.kind === "snapshot") {
|
|
99
|
+
this.bids.clear();
|
|
100
|
+
this.asks.clear();
|
|
101
|
+
for (const { price, size } of event.bids) {
|
|
102
|
+
if (size > 0) this.bids.set(price, size);
|
|
103
|
+
}
|
|
104
|
+
for (const { price, size } of event.asks) {
|
|
105
|
+
if (size > 0) this.asks.set(price, size);
|
|
106
|
+
}
|
|
107
|
+
this.sequence = event.sequence;
|
|
108
|
+
this.timestamp = event.timestamp;
|
|
109
|
+
this.hasSnapshot = true;
|
|
110
|
+
return this.emit();
|
|
111
|
+
}
|
|
112
|
+
if (!this.hasSnapshot) {
|
|
113
|
+
return { ok: false, reason: "no-snapshot" };
|
|
114
|
+
}
|
|
115
|
+
if (event.sequence <= this.sequence) {
|
|
116
|
+
return this.emit();
|
|
117
|
+
}
|
|
118
|
+
if (event.prevSequence !== void 0) {
|
|
119
|
+
if (event.prevSequence !== this.sequence) {
|
|
120
|
+
return {
|
|
121
|
+
ok: false,
|
|
122
|
+
reason: "gap",
|
|
123
|
+
expected: this.sequence,
|
|
124
|
+
received: event.prevSequence
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
} else if (event.sequence !== this.sequence + 1) {
|
|
128
|
+
return {
|
|
129
|
+
ok: false,
|
|
130
|
+
reason: "gap",
|
|
131
|
+
expected: this.sequence + 1,
|
|
132
|
+
received: event.sequence
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
for (const { price, size } of event.bids) {
|
|
136
|
+
if (size === 0) this.bids.delete(price);
|
|
137
|
+
else this.bids.set(price, size);
|
|
138
|
+
}
|
|
139
|
+
for (const { price, size } of event.asks) {
|
|
140
|
+
if (size === 0) this.asks.delete(price);
|
|
141
|
+
else this.asks.set(price, size);
|
|
142
|
+
}
|
|
143
|
+
this.sequence = event.sequence;
|
|
144
|
+
this.timestamp = event.timestamp;
|
|
145
|
+
return this.emit();
|
|
146
|
+
}
|
|
147
|
+
reset() {
|
|
148
|
+
this.bids.clear();
|
|
149
|
+
this.asks.clear();
|
|
150
|
+
this.sequence = -1;
|
|
151
|
+
this.timestamp = 0;
|
|
152
|
+
this.hasSnapshot = false;
|
|
153
|
+
}
|
|
154
|
+
/** Returns true once the merger has a usable book. */
|
|
155
|
+
isReady() {
|
|
156
|
+
return this.hasSnapshot;
|
|
157
|
+
}
|
|
158
|
+
emit() {
|
|
159
|
+
const bids = [];
|
|
160
|
+
for (const [price, size] of this.bids) bids.push({ price, size });
|
|
161
|
+
bids.sort((a, b) => b.price - a.price);
|
|
162
|
+
const asks = [];
|
|
163
|
+
for (const [price, size] of this.asks) asks.push({ price, size });
|
|
164
|
+
asks.sort((a, b) => a.price - b.price);
|
|
165
|
+
return {
|
|
166
|
+
ok: true,
|
|
167
|
+
bids,
|
|
168
|
+
asks,
|
|
169
|
+
sequence: this.sequence,
|
|
170
|
+
timestamp: this.timestamp
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
// src/core/event-emitter.ts
|
|
176
|
+
var TypedEmitter = class {
|
|
177
|
+
listeners = /* @__PURE__ */ new Map();
|
|
178
|
+
on(event, fn) {
|
|
179
|
+
let set = this.listeners.get(event);
|
|
180
|
+
if (!set) {
|
|
181
|
+
set = /* @__PURE__ */ new Set();
|
|
182
|
+
this.listeners.set(event, set);
|
|
183
|
+
}
|
|
184
|
+
set.add(fn);
|
|
185
|
+
return this;
|
|
186
|
+
}
|
|
187
|
+
off(event, fn) {
|
|
188
|
+
this.listeners.get(event)?.delete(fn);
|
|
189
|
+
return this;
|
|
190
|
+
}
|
|
191
|
+
emit(event, ...args) {
|
|
192
|
+
const set = this.listeners.get(event);
|
|
193
|
+
if (!set || set.size === 0) return false;
|
|
194
|
+
for (const fn of set) fn(...args);
|
|
195
|
+
return true;
|
|
196
|
+
}
|
|
197
|
+
removeAllListeners() {
|
|
198
|
+
this.listeners.clear();
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
// src/core/stream.ts
|
|
203
|
+
var OrderbookStream = class extends TypedEmitter {
|
|
204
|
+
constructor(onClose) {
|
|
205
|
+
super();
|
|
206
|
+
this.onClose = onClose;
|
|
207
|
+
}
|
|
208
|
+
onClose;
|
|
209
|
+
closed = false;
|
|
210
|
+
close() {
|
|
211
|
+
if (this.closed) return;
|
|
212
|
+
this.closed = true;
|
|
213
|
+
this.onClose();
|
|
214
|
+
this.removeAllListeners();
|
|
215
|
+
}
|
|
216
|
+
/** Yields each maintained book as it becomes available. */
|
|
217
|
+
async *iter() {
|
|
218
|
+
const queue = [];
|
|
219
|
+
let resolveNext = null;
|
|
220
|
+
let pendingError = null;
|
|
221
|
+
let ended = false;
|
|
222
|
+
const onUpdate = (b) => {
|
|
223
|
+
queue.push(b);
|
|
224
|
+
resolveNext?.();
|
|
225
|
+
};
|
|
226
|
+
const onError = (e) => {
|
|
227
|
+
pendingError = e;
|
|
228
|
+
resolveNext?.();
|
|
229
|
+
};
|
|
230
|
+
const onClose = () => {
|
|
231
|
+
ended = true;
|
|
232
|
+
resolveNext?.();
|
|
233
|
+
};
|
|
234
|
+
this.on("update", onUpdate);
|
|
235
|
+
this.on("error", onError);
|
|
236
|
+
this.on("disconnected", onClose);
|
|
237
|
+
try {
|
|
238
|
+
while (true) {
|
|
239
|
+
if (pendingError) throw pendingError;
|
|
240
|
+
const next = queue.shift();
|
|
241
|
+
if (next) {
|
|
242
|
+
yield next;
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
if (ended || this.closed) return;
|
|
246
|
+
await new Promise((r) => {
|
|
247
|
+
resolveNext = r;
|
|
248
|
+
});
|
|
249
|
+
resolveNext = null;
|
|
250
|
+
}
|
|
251
|
+
} finally {
|
|
252
|
+
this.off("update", onUpdate);
|
|
253
|
+
this.off("error", onError);
|
|
254
|
+
this.off("disconnected", onClose);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
// src/core/reconnect.ts
|
|
260
|
+
var Backoff = class _Backoff {
|
|
261
|
+
attempt = 0;
|
|
262
|
+
opts;
|
|
263
|
+
constructor(opts) {
|
|
264
|
+
this.opts = opts;
|
|
265
|
+
}
|
|
266
|
+
static withDefaults(opts = {}) {
|
|
267
|
+
return new _Backoff({
|
|
268
|
+
initialMs: opts.initialMs ?? 500,
|
|
269
|
+
maxMs: opts.maxMs ?? 3e4,
|
|
270
|
+
factor: opts.factor ?? 2,
|
|
271
|
+
jitter: opts.jitter ?? 0.3,
|
|
272
|
+
maxAttempts: opts.maxAttempts ?? 0
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
next() {
|
|
276
|
+
if (this.opts.maxAttempts > 0 && this.attempt >= this.opts.maxAttempts) {
|
|
277
|
+
return null;
|
|
278
|
+
}
|
|
279
|
+
this.attempt += 1;
|
|
280
|
+
const base = Math.min(
|
|
281
|
+
this.opts.initialMs * Math.pow(this.opts.factor, this.attempt - 1),
|
|
282
|
+
this.opts.maxMs
|
|
283
|
+
);
|
|
284
|
+
const jitterRange = base * this.opts.jitter;
|
|
285
|
+
const wait = Math.max(0, base + (Math.random() * 2 - 1) * jitterRange);
|
|
286
|
+
return { wait, attempt: this.attempt };
|
|
287
|
+
}
|
|
288
|
+
reset() {
|
|
289
|
+
this.attempt = 0;
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
var hasNativeWebSocket = typeof globalThis.WebSocket !== "undefined";
|
|
293
|
+
|
|
294
|
+
// src/transport/ws.ts
|
|
295
|
+
function toError(value, fallback = "unknown error") {
|
|
296
|
+
if (value instanceof Error) {
|
|
297
|
+
return value.message ? value : new Error(`${value.name}: ${fallback}`);
|
|
298
|
+
}
|
|
299
|
+
if (typeof value === "string" && value) return new Error(value);
|
|
300
|
+
return new Error(fallback);
|
|
301
|
+
}
|
|
302
|
+
function normalizeWsError(ev) {
|
|
303
|
+
const detail = ev?.message || (ev?.error instanceof Error ? ev.error.message : void 0) || (typeof ev?.error === "string" ? ev.error : void 0);
|
|
304
|
+
return new Error(detail ? `WebSocket error: ${detail}` : "WebSocket error");
|
|
305
|
+
}
|
|
306
|
+
function describeClose(ev) {
|
|
307
|
+
if (ev?.reason) return ev.reason;
|
|
308
|
+
return ev?.code === void 0 ? "closed" : `closed (code ${ev.code})`;
|
|
309
|
+
}
|
|
310
|
+
var cachedCtor = null;
|
|
311
|
+
async function getWebSocketCtor() {
|
|
312
|
+
if (cachedCtor) return cachedCtor;
|
|
313
|
+
if (hasNativeWebSocket) {
|
|
314
|
+
cachedCtor = globalThis.WebSocket;
|
|
315
|
+
return cachedCtor;
|
|
316
|
+
}
|
|
317
|
+
try {
|
|
318
|
+
const mod = await import('ws');
|
|
319
|
+
const ctor = mod.default ?? mod.WebSocket;
|
|
320
|
+
if (!ctor) throw new Error("missing default export");
|
|
321
|
+
cachedCtor = ctor;
|
|
322
|
+
return cachedCtor;
|
|
323
|
+
} catch (err) {
|
|
324
|
+
throw new Error(
|
|
325
|
+
"WebSocket unavailable; install peer dep `ws` for Node <22. " + (err instanceof Error ? err.message : String(err))
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
var WSClient = class extends TypedEmitter {
|
|
330
|
+
constructor(cfg) {
|
|
331
|
+
super();
|
|
332
|
+
this.cfg = cfg;
|
|
333
|
+
this.backoff = Backoff.withDefaults(cfg.reconnect);
|
|
334
|
+
}
|
|
335
|
+
cfg;
|
|
336
|
+
ws = null;
|
|
337
|
+
backoff;
|
|
338
|
+
closed = false;
|
|
339
|
+
pingTimer = null;
|
|
340
|
+
async connect() {
|
|
341
|
+
if (this.closed) return;
|
|
342
|
+
const WS = await getWebSocketCtor();
|
|
343
|
+
const url = typeof this.cfg.url === "function" ? await this.cfg.url() : this.cfg.url;
|
|
344
|
+
const ws = new WS(url);
|
|
345
|
+
ws.binaryType = "arraybuffer";
|
|
346
|
+
this.ws = ws;
|
|
347
|
+
ws.onopen = async () => {
|
|
348
|
+
this.backoff.reset();
|
|
349
|
+
this.startPing();
|
|
350
|
+
this.emit("open");
|
|
351
|
+
try {
|
|
352
|
+
await this.cfg.onOpen?.({
|
|
353
|
+
send: (d) => ws.send(d),
|
|
354
|
+
close: () => this.close()
|
|
355
|
+
});
|
|
356
|
+
} catch (err) {
|
|
357
|
+
this.emit("error", toError(err, "onOpen handler failed"));
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
ws.onmessage = (e) => {
|
|
361
|
+
let result;
|
|
362
|
+
try {
|
|
363
|
+
result = this.cfg.onMessage(e.data);
|
|
364
|
+
} catch (err) {
|
|
365
|
+
this.emit("error", toError(err, "onMessage handler failed"));
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
if (result && typeof result.catch === "function") {
|
|
369
|
+
result.catch(
|
|
370
|
+
(err) => this.emit("error", toError(err, "onMessage handler failed"))
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
ws.onerror = (e) => {
|
|
375
|
+
this.emit("error", normalizeWsError(e));
|
|
376
|
+
};
|
|
377
|
+
ws.onclose = (e) => {
|
|
378
|
+
this.stopPing();
|
|
379
|
+
const reason = describeClose(e);
|
|
380
|
+
this.emit("close", reason);
|
|
381
|
+
if (!this.closed) this.scheduleReconnect();
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
send(data) {
|
|
385
|
+
this.ws?.send(data);
|
|
386
|
+
}
|
|
387
|
+
close() {
|
|
388
|
+
this.closed = true;
|
|
389
|
+
this.stopPing();
|
|
390
|
+
try {
|
|
391
|
+
this.ws?.close();
|
|
392
|
+
} catch {
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
startPing() {
|
|
396
|
+
if (!this.cfg.pingIntervalMs || !this.cfg.pingPayload) return;
|
|
397
|
+
this.stopPing();
|
|
398
|
+
this.pingTimer = setInterval(() => {
|
|
399
|
+
try {
|
|
400
|
+
this.ws?.send(this.cfg.pingPayload());
|
|
401
|
+
} catch {
|
|
402
|
+
}
|
|
403
|
+
}, this.cfg.pingIntervalMs);
|
|
404
|
+
}
|
|
405
|
+
stopPing() {
|
|
406
|
+
if (this.pingTimer) {
|
|
407
|
+
clearInterval(this.pingTimer);
|
|
408
|
+
this.pingTimer = null;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
scheduleReconnect() {
|
|
412
|
+
const next = this.backoff.next();
|
|
413
|
+
if (!next) {
|
|
414
|
+
this.emit("error", new Error("max reconnect attempts exceeded"));
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
this.emit("reconnecting", next.attempt, next.wait);
|
|
418
|
+
setTimeout(() => {
|
|
419
|
+
this.connect().catch((e) => this.emit("error", toError(e, "reconnect failed")));
|
|
420
|
+
}, next.wait);
|
|
421
|
+
}
|
|
422
|
+
};
|
|
423
|
+
|
|
424
|
+
// src/exchanges/aster/ws.ts
|
|
425
|
+
var WS_SPOT = "wss://sstream.asterdex.com/ws";
|
|
426
|
+
var WS_FUTURES = "wss://fstream.asterdex.com/ws";
|
|
427
|
+
function streamAsterOrderbook(opts) {
|
|
428
|
+
const symbol = toAsterSymbol(opts.symbol);
|
|
429
|
+
const isSpot = opts.market === "spot";
|
|
430
|
+
const wsBase = isSpot ? WS_SPOT : WS_FUTURES;
|
|
431
|
+
const wsUrl = `${wsBase}/${symbol.toLowerCase()}@depth@${opts.interval ?? "100ms"}`;
|
|
432
|
+
const merger = new BookMerger();
|
|
433
|
+
let snapshotId = -1;
|
|
434
|
+
let lastU = -1;
|
|
435
|
+
let snapshotInFlight = false;
|
|
436
|
+
let snapshotDone = false;
|
|
437
|
+
let buffered = [];
|
|
438
|
+
let recovering = false;
|
|
439
|
+
let ws = null;
|
|
440
|
+
const stream = new OrderbookStream(() => ws?.close());
|
|
441
|
+
const emit = (r) => {
|
|
442
|
+
const book = {
|
|
443
|
+
exchange: "aster",
|
|
444
|
+
symbol: fromAsterSymbol(symbol),
|
|
445
|
+
market: opts.market,
|
|
446
|
+
bids: r.bids,
|
|
447
|
+
asks: r.asks,
|
|
448
|
+
timestamp: r.timestamp,
|
|
449
|
+
sequence: r.sequence
|
|
450
|
+
};
|
|
451
|
+
stream.emit("update", book);
|
|
452
|
+
};
|
|
453
|
+
const triggerSnapshot = () => {
|
|
454
|
+
if (snapshotInFlight) return;
|
|
455
|
+
snapshotInFlight = true;
|
|
456
|
+
fetchAsterOrderbook({
|
|
457
|
+
symbol: opts.symbol,
|
|
458
|
+
market: opts.market,
|
|
459
|
+
depth: opts.depth ?? 1e3,
|
|
460
|
+
timeoutMs: opts.timeoutMs,
|
|
461
|
+
transformUrl: opts.transformUrl
|
|
462
|
+
}).then((snap) => {
|
|
463
|
+
snapshotId = snap.sequence;
|
|
464
|
+
const r = merger.apply({
|
|
465
|
+
kind: "snapshot",
|
|
466
|
+
bids: snap.bids,
|
|
467
|
+
asks: snap.asks,
|
|
468
|
+
sequence: snap.sequence,
|
|
469
|
+
timestamp: snap.timestamp
|
|
470
|
+
});
|
|
471
|
+
if (r.ok) emit(r);
|
|
472
|
+
snapshotDone = true;
|
|
473
|
+
lastU = -1;
|
|
474
|
+
const pending = buffered;
|
|
475
|
+
buffered = [];
|
|
476
|
+
for (const ev of pending) processDelta(ev);
|
|
477
|
+
}).catch((err) => stream.emit("error", err)).finally(() => {
|
|
478
|
+
snapshotInFlight = false;
|
|
479
|
+
});
|
|
480
|
+
};
|
|
481
|
+
const resync = () => {
|
|
482
|
+
if (recovering) return;
|
|
483
|
+
recovering = true;
|
|
484
|
+
merger.reset();
|
|
485
|
+
snapshotId = -1;
|
|
486
|
+
snapshotDone = false;
|
|
487
|
+
lastU = -1;
|
|
488
|
+
buffered = [];
|
|
489
|
+
setTimeout(() => {
|
|
490
|
+
recovering = false;
|
|
491
|
+
triggerSnapshot();
|
|
492
|
+
}, 100);
|
|
493
|
+
};
|
|
494
|
+
const processDelta = (ev) => {
|
|
495
|
+
if (ev.u < snapshotId) return;
|
|
496
|
+
const bids = ev.b.map(([p, s]) => ({
|
|
497
|
+
price: Number(p),
|
|
498
|
+
size: Number(s)
|
|
499
|
+
}));
|
|
500
|
+
const asks = ev.a.map(([p, s]) => ({
|
|
501
|
+
price: Number(p),
|
|
502
|
+
size: Number(s)
|
|
503
|
+
}));
|
|
504
|
+
let prevSequence;
|
|
505
|
+
if (lastU < 0) {
|
|
506
|
+
if (ev.U > snapshotId) {
|
|
507
|
+
resync();
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
prevSequence = snapshotId;
|
|
511
|
+
} else {
|
|
512
|
+
if (ev.pu === void 0) {
|
|
513
|
+
resync();
|
|
514
|
+
return;
|
|
515
|
+
}
|
|
516
|
+
prevSequence = ev.pu;
|
|
517
|
+
}
|
|
518
|
+
const r = merger.apply({
|
|
519
|
+
kind: "delta",
|
|
520
|
+
bids,
|
|
521
|
+
asks,
|
|
522
|
+
sequence: ev.u,
|
|
523
|
+
prevSequence,
|
|
524
|
+
timestamp: ev.E
|
|
525
|
+
});
|
|
526
|
+
if (r.ok) {
|
|
527
|
+
lastU = ev.u;
|
|
528
|
+
emit(r);
|
|
529
|
+
} else if (r.reason === "gap" || r.reason === "no-snapshot") {
|
|
530
|
+
resync();
|
|
531
|
+
}
|
|
532
|
+
};
|
|
533
|
+
const onMessage = (raw) => {
|
|
534
|
+
if (typeof raw !== "string") return;
|
|
535
|
+
let msg;
|
|
536
|
+
try {
|
|
537
|
+
msg = JSON.parse(raw);
|
|
538
|
+
} catch {
|
|
539
|
+
return;
|
|
540
|
+
}
|
|
541
|
+
if (msg.e !== "depthUpdate") return;
|
|
542
|
+
if (!snapshotDone) {
|
|
543
|
+
buffered.push(msg);
|
|
544
|
+
if (!snapshotInFlight) triggerSnapshot();
|
|
545
|
+
return;
|
|
546
|
+
}
|
|
547
|
+
processDelta(msg);
|
|
548
|
+
};
|
|
549
|
+
ws = new WSClient({
|
|
550
|
+
url: wsUrl,
|
|
551
|
+
onOpen: () => {
|
|
552
|
+
merger.reset();
|
|
553
|
+
snapshotId = -1;
|
|
554
|
+
snapshotDone = false;
|
|
555
|
+
lastU = -1;
|
|
556
|
+
buffered = [];
|
|
557
|
+
},
|
|
558
|
+
onMessage
|
|
559
|
+
// Server-initiated WS-level ping; auto-handled by the WS impl.
|
|
560
|
+
});
|
|
561
|
+
ws.on("open", () => stream.emit("connected"));
|
|
562
|
+
ws.on("close", (r) => stream.emit("disconnected", r));
|
|
563
|
+
ws.on("reconnecting", (a, w) => stream.emit("reconnecting", a, w));
|
|
564
|
+
ws.on("error", (e) => stream.emit("error", e));
|
|
565
|
+
ws.connect().catch((e) => stream.emit("error", e));
|
|
566
|
+
return stream;
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
// src/exchanges/aster/index.ts
|
|
570
|
+
var AsterClient = class {
|
|
571
|
+
exchange = "aster";
|
|
572
|
+
transformUrl;
|
|
573
|
+
timeoutMs;
|
|
574
|
+
constructor(opts = {}) {
|
|
575
|
+
this.transformUrl = opts.transformUrl;
|
|
576
|
+
this.timeoutMs = opts.timeoutMs;
|
|
577
|
+
}
|
|
578
|
+
fetchOrderbook(symbol, opts = {}) {
|
|
579
|
+
const { pair, market } = parseSymbol(symbol);
|
|
580
|
+
return fetchAsterOrderbook({
|
|
581
|
+
symbol: pair,
|
|
582
|
+
market,
|
|
583
|
+
depth: opts.depth,
|
|
584
|
+
timeoutMs: this.timeoutMs,
|
|
585
|
+
transformUrl: this.transformUrl
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
streamOrderbook(symbol, opts = {}) {
|
|
589
|
+
const { pair, market } = parseSymbol(symbol);
|
|
590
|
+
return streamAsterOrderbook({
|
|
591
|
+
symbol: pair,
|
|
592
|
+
market,
|
|
593
|
+
depth: opts.depth,
|
|
594
|
+
transformUrl: this.transformUrl,
|
|
595
|
+
timeoutMs: this.timeoutMs
|
|
596
|
+
});
|
|
597
|
+
}
|
|
598
|
+
};
|
|
599
|
+
|
|
600
|
+
export { ASTER_FUTURES_REST, ASTER_SPOT_REST, AsterClient, fetchAsterOrderbook, fromAsterSymbol, streamAsterOrderbook, toAsterSymbol };
|
|
601
|
+
//# sourceMappingURL=index.js.map
|
|
602
|
+
//# sourceMappingURL=index.js.map
|