@xapy/orderbook 0.1.24 → 0.1.25
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 -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.cjs +648 -0
- package/dist/exchanges/edgex/index.cjs.map +1 -0
- package/dist/exchanges/edgex/index.d.cts +62 -0
- package/dist/exchanges/edgex/index.d.ts +62 -0
- package/dist/exchanges/edgex/index.js +642 -0
- package/dist/exchanges/edgex/index.js.map +1 -0
- package/dist/exchanges/gate/index.d.cts +1 -1
- package/dist/exchanges/gate/index.d.ts +1 -1
- package/dist/exchanges/grvt/index.d.cts +1 -1
- package/dist/exchanges/grvt/index.d.ts +1 -1
- package/dist/exchanges/huobi/index.d.cts +1 -1
- package/dist/exchanges/huobi/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.cjs +664 -0
- package/dist/exchanges/lighter/index.cjs.map +1 -0
- package/dist/exchanges/lighter/index.d.cts +58 -0
- package/dist/exchanges/lighter/index.d.ts +58 -0
- package/dist/exchanges/lighter/index.js +658 -0
- package/dist/exchanges/lighter/index.js.map +1 -0
- package/dist/exchanges/okx/index.d.cts +1 -1
- package/dist/exchanges/okx/index.d.ts +1 -1
- package/dist/index.cjs +511 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -2
- package/dist/index.d.ts +4 -2
- package/dist/index.js +510 -4
- package/dist/index.js.map +1 -1
- package/dist/{stream-Con47OAp.d.cts → stream-CrvepB6m.d.cts} +1 -1
- package/dist/{stream-Con47OAp.d.ts → stream-CrvepB6m.d.ts} +1 -1
- package/package.json +24 -2
package/dist/index.cjs
CHANGED
|
@@ -2745,13 +2745,13 @@ function streamDeribitOrderbook(opts) {
|
|
|
2745
2745
|
rpc("public/unsubscribe", { channels: [channel] });
|
|
2746
2746
|
subscribe();
|
|
2747
2747
|
};
|
|
2748
|
-
const
|
|
2748
|
+
const toLevels4 = (entries) => entries.map(([action, price, amount]) => ({
|
|
2749
2749
|
price,
|
|
2750
2750
|
size: action === "delete" ? 0 : amount
|
|
2751
2751
|
}));
|
|
2752
2752
|
const buildEvent2 = (d) => {
|
|
2753
|
-
const bids =
|
|
2754
|
-
const asks =
|
|
2753
|
+
const bids = toLevels4(d.bids);
|
|
2754
|
+
const asks = toLevels4(d.asks);
|
|
2755
2755
|
if (d.type === "snapshot") {
|
|
2756
2756
|
return {
|
|
2757
2757
|
kind: "snapshot",
|
|
@@ -3099,6 +3099,512 @@ var GrvtClient = class {
|
|
|
3099
3099
|
}
|
|
3100
3100
|
};
|
|
3101
3101
|
|
|
3102
|
+
// src/exchanges/lighter/markets.ts
|
|
3103
|
+
var LIGHTER_REST = "https://mainnet.zklighter.elliot.ai";
|
|
3104
|
+
var cache = null;
|
|
3105
|
+
var inflight = null;
|
|
3106
|
+
var key = (name, market) => `${market}:${name.toUpperCase()}`;
|
|
3107
|
+
async function fetchMarkets(opts) {
|
|
3108
|
+
const res = await httpJson({
|
|
3109
|
+
url: `${LIGHTER_REST}/api/v1/orderBooks`,
|
|
3110
|
+
timeoutMs: opts.timeoutMs,
|
|
3111
|
+
transformUrl: opts.transformUrl
|
|
3112
|
+
});
|
|
3113
|
+
const rows = res.order_books ?? [];
|
|
3114
|
+
if (rows.length === 0) {
|
|
3115
|
+
throw new ExchangeError(
|
|
3116
|
+
"lighter",
|
|
3117
|
+
res.message ? `${res.code}: ${res.message}` : "empty orderBooks response"
|
|
3118
|
+
);
|
|
3119
|
+
}
|
|
3120
|
+
const map = /* @__PURE__ */ new Map();
|
|
3121
|
+
for (const row of rows) {
|
|
3122
|
+
map.set(key(row.symbol, row.market_type === "spot" ? "spot" : "perpetual"), row);
|
|
3123
|
+
}
|
|
3124
|
+
return map;
|
|
3125
|
+
}
|
|
3126
|
+
function loadMarkets(opts) {
|
|
3127
|
+
inflight ??= fetchMarkets(opts).then(
|
|
3128
|
+
(m) => {
|
|
3129
|
+
cache = m;
|
|
3130
|
+
inflight = null;
|
|
3131
|
+
return m;
|
|
3132
|
+
},
|
|
3133
|
+
(err) => {
|
|
3134
|
+
inflight = null;
|
|
3135
|
+
throw err;
|
|
3136
|
+
}
|
|
3137
|
+
);
|
|
3138
|
+
return inflight;
|
|
3139
|
+
}
|
|
3140
|
+
async function resolveLighterMarket(name, market, opts) {
|
|
3141
|
+
const k = key(name, market);
|
|
3142
|
+
const hit = cache?.get(k);
|
|
3143
|
+
const meta = hit ?? (await loadMarkets(opts)).get(k);
|
|
3144
|
+
if (!meta) {
|
|
3145
|
+
throw new ExchangeError(
|
|
3146
|
+
"lighter",
|
|
3147
|
+
`unknown ${market} market "${name}" \u2014 not listed on lighter`
|
|
3148
|
+
);
|
|
3149
|
+
}
|
|
3150
|
+
if (meta.status && meta.status !== "active") {
|
|
3151
|
+
throw new ExchangeError(
|
|
3152
|
+
"lighter",
|
|
3153
|
+
`market "${name}" is ${meta.status} \u2014 no book is published for it`
|
|
3154
|
+
);
|
|
3155
|
+
}
|
|
3156
|
+
return meta;
|
|
3157
|
+
}
|
|
3158
|
+
|
|
3159
|
+
// src/exchanges/lighter/symbols.ts
|
|
3160
|
+
function toLighterSymbol(symbol, market) {
|
|
3161
|
+
const [base, quote] = symbol.split("/");
|
|
3162
|
+
if (!base || !quote) {
|
|
3163
|
+
throw new Error(`invalid symbol "${symbol}" \u2014 expected "BASE/QUOTE"`);
|
|
3164
|
+
}
|
|
3165
|
+
if (quote.toUpperCase() !== "USDC") {
|
|
3166
|
+
throw new Error(`lighter only supports USDC-quoted pairs; got "${quote}"`);
|
|
3167
|
+
}
|
|
3168
|
+
return market === "perpetual" ? base.toUpperCase() : `${base.toUpperCase()}/${quote.toUpperCase()}`;
|
|
3169
|
+
}
|
|
3170
|
+
function fromLighterSymbol(name) {
|
|
3171
|
+
return name.includes("/") ? name.toUpperCase() : `${name.toUpperCase()}/USDC`;
|
|
3172
|
+
}
|
|
3173
|
+
|
|
3174
|
+
// src/exchanges/lighter/rest.ts
|
|
3175
|
+
var MAX_ORDERS = 250;
|
|
3176
|
+
function usToMs(us) {
|
|
3177
|
+
return Number.isFinite(us) && us > 0 ? Math.floor(us / 1e3) : Date.now();
|
|
3178
|
+
}
|
|
3179
|
+
function aggregate(orders, side, capped) {
|
|
3180
|
+
const byPrice = /* @__PURE__ */ new Map();
|
|
3181
|
+
for (const o of orders ?? []) {
|
|
3182
|
+
const price = Number(o.price);
|
|
3183
|
+
const size = Number(o.remaining_base_amount);
|
|
3184
|
+
if (!Number.isFinite(price) || !Number.isFinite(size)) continue;
|
|
3185
|
+
byPrice.set(price, (byPrice.get(price) ?? 0) + size);
|
|
3186
|
+
}
|
|
3187
|
+
const levels = [];
|
|
3188
|
+
for (const [price, size] of byPrice) levels.push({ price, size });
|
|
3189
|
+
levels.sort((a, b) => side === "bid" ? b.price - a.price : a.price - b.price);
|
|
3190
|
+
if (capped) levels.pop();
|
|
3191
|
+
return levels;
|
|
3192
|
+
}
|
|
3193
|
+
async function fetchLighterOrderbook(opts) {
|
|
3194
|
+
const name = toLighterSymbol(opts.symbol, opts.market);
|
|
3195
|
+
const meta = await resolveLighterMarket(name, opts.market, opts);
|
|
3196
|
+
const res = await httpJson({
|
|
3197
|
+
url: `${LIGHTER_REST}/api/v1/orderBookOrders?market_id=${meta.market_id}&limit=${MAX_ORDERS}`,
|
|
3198
|
+
timeoutMs: opts.timeoutMs,
|
|
3199
|
+
transformUrl: opts.transformUrl
|
|
3200
|
+
});
|
|
3201
|
+
if (!res.asks && !res.bids) {
|
|
3202
|
+
throw new ExchangeError(
|
|
3203
|
+
"lighter",
|
|
3204
|
+
res.message ? `${res.code}: ${res.message}` : "empty orderbook response"
|
|
3205
|
+
);
|
|
3206
|
+
}
|
|
3207
|
+
const depth = opts.depth ?? 50;
|
|
3208
|
+
const bids = aggregate(res.bids, "bid", res.total_bids === MAX_ORDERS);
|
|
3209
|
+
const asks = aggregate(res.asks, "ask", res.total_asks === MAX_ORDERS);
|
|
3210
|
+
const timestamp = Date.now();
|
|
3211
|
+
return {
|
|
3212
|
+
exchange: "lighter",
|
|
3213
|
+
symbol: fromLighterSymbol(name),
|
|
3214
|
+
market: opts.market,
|
|
3215
|
+
bids: bids.slice(0, depth),
|
|
3216
|
+
asks: asks.slice(0, depth),
|
|
3217
|
+
timestamp,
|
|
3218
|
+
// The L3 endpoint publishes no sequence of its own — the WS `nonce` chain
|
|
3219
|
+
// is where ordering is enforced.
|
|
3220
|
+
sequence: timestamp
|
|
3221
|
+
};
|
|
3222
|
+
}
|
|
3223
|
+
|
|
3224
|
+
// src/exchanges/lighter/ws.ts
|
|
3225
|
+
var WS_URL2 = "wss://mainnet.zklighter.elliot.ai/stream";
|
|
3226
|
+
var PING_INTERVAL_MS = 6e4;
|
|
3227
|
+
var toLevels2 = (rows) => (rows ?? []).map((l) => ({ price: Number(l.price), size: Number(l.size) }));
|
|
3228
|
+
function streamLighterOrderbook(opts) {
|
|
3229
|
+
const name = toLighterSymbol(opts.symbol, opts.market);
|
|
3230
|
+
const symbol = fromLighterSymbol(name);
|
|
3231
|
+
const marketId = resolveLighterMarket(name, opts.market, opts).then(
|
|
3232
|
+
(m) => m.market_id
|
|
3233
|
+
);
|
|
3234
|
+
marketId.catch(() => {
|
|
3235
|
+
});
|
|
3236
|
+
const merger = new BookMerger();
|
|
3237
|
+
let ws = null;
|
|
3238
|
+
let sock = null;
|
|
3239
|
+
let channel = null;
|
|
3240
|
+
let recovering = false;
|
|
3241
|
+
const stream = new OrderbookStream(() => ws?.close());
|
|
3242
|
+
const send = (type) => {
|
|
3243
|
+
if (channel) sock?.send(JSON.stringify({ type, channel }));
|
|
3244
|
+
};
|
|
3245
|
+
const emit = (r) => {
|
|
3246
|
+
const book = {
|
|
3247
|
+
exchange: "lighter",
|
|
3248
|
+
symbol,
|
|
3249
|
+
market: opts.market,
|
|
3250
|
+
bids: r.bids,
|
|
3251
|
+
asks: r.asks,
|
|
3252
|
+
timestamp: r.timestamp,
|
|
3253
|
+
sequence: r.sequence
|
|
3254
|
+
};
|
|
3255
|
+
stream.emit("update", book);
|
|
3256
|
+
};
|
|
3257
|
+
const resync = () => {
|
|
3258
|
+
if (recovering) return;
|
|
3259
|
+
recovering = true;
|
|
3260
|
+
merger.reset();
|
|
3261
|
+
send("unsubscribe");
|
|
3262
|
+
send("subscribe");
|
|
3263
|
+
};
|
|
3264
|
+
const buildEvent2 = (msg, snapshot) => {
|
|
3265
|
+
const ob = msg.order_book ?? {};
|
|
3266
|
+
const bids = toLevels2(ob.bids);
|
|
3267
|
+
const asks = toLevels2(ob.asks);
|
|
3268
|
+
const timestamp = msg.timestamp ?? usToMs(ob.last_updated_at);
|
|
3269
|
+
const sequence = Number(ob.nonce ?? 0);
|
|
3270
|
+
return snapshot ? { kind: "snapshot", bids, asks, sequence, timestamp } : {
|
|
3271
|
+
kind: "delta",
|
|
3272
|
+
bids,
|
|
3273
|
+
asks,
|
|
3274
|
+
sequence,
|
|
3275
|
+
prevSequence: Number(ob.begin_nonce ?? 0),
|
|
3276
|
+
timestamp
|
|
3277
|
+
};
|
|
3278
|
+
};
|
|
3279
|
+
const handleBook = (msg, snapshot) => {
|
|
3280
|
+
if (snapshot) recovering = false;
|
|
3281
|
+
else if (recovering) return;
|
|
3282
|
+
const r = merger.apply(buildEvent2(msg, snapshot));
|
|
3283
|
+
if (r.ok) {
|
|
3284
|
+
emit(r);
|
|
3285
|
+
} else if (r.reason === "gap" || r.reason === "no-snapshot") {
|
|
3286
|
+
resync();
|
|
3287
|
+
}
|
|
3288
|
+
};
|
|
3289
|
+
const onMessage = (raw) => {
|
|
3290
|
+
if (typeof raw !== "string") return;
|
|
3291
|
+
let msg;
|
|
3292
|
+
try {
|
|
3293
|
+
msg = JSON.parse(raw);
|
|
3294
|
+
} catch {
|
|
3295
|
+
return;
|
|
3296
|
+
}
|
|
3297
|
+
if (msg.error) {
|
|
3298
|
+
stream.emit(
|
|
3299
|
+
"error",
|
|
3300
|
+
new ExchangeError("lighter", `${msg.error.code}: ${msg.error.message}`)
|
|
3301
|
+
);
|
|
3302
|
+
return;
|
|
3303
|
+
}
|
|
3304
|
+
if (!msg.order_book) return;
|
|
3305
|
+
if (msg.channel && channel && msg.channel !== channel.replace("/", ":")) {
|
|
3306
|
+
return;
|
|
3307
|
+
}
|
|
3308
|
+
if (msg.type === "subscribed/order_book") handleBook(msg, true);
|
|
3309
|
+
else if (msg.type === "update/order_book") handleBook(msg, false);
|
|
3310
|
+
};
|
|
3311
|
+
ws = new WSClient({
|
|
3312
|
+
url: WS_URL2,
|
|
3313
|
+
onOpen: async (handle) => {
|
|
3314
|
+
sock = handle;
|
|
3315
|
+
merger.reset();
|
|
3316
|
+
recovering = false;
|
|
3317
|
+
channel = `order_book/${await marketId}`;
|
|
3318
|
+
send("subscribe");
|
|
3319
|
+
},
|
|
3320
|
+
onMessage,
|
|
3321
|
+
pingIntervalMs: PING_INTERVAL_MS,
|
|
3322
|
+
pingPayload: () => JSON.stringify({ type: "ping" })
|
|
3323
|
+
});
|
|
3324
|
+
ws.on("open", () => stream.emit("connected"));
|
|
3325
|
+
ws.on("close", (r) => stream.emit("disconnected", r));
|
|
3326
|
+
ws.on("reconnecting", (a, w) => stream.emit("reconnecting", a, w));
|
|
3327
|
+
ws.on("error", (e) => stream.emit("error", e));
|
|
3328
|
+
ws.connect().catch((e) => stream.emit("error", e));
|
|
3329
|
+
return stream;
|
|
3330
|
+
}
|
|
3331
|
+
|
|
3332
|
+
// src/exchanges/lighter/index.ts
|
|
3333
|
+
var LighterClient = class {
|
|
3334
|
+
exchange = "lighter";
|
|
3335
|
+
transformUrl;
|
|
3336
|
+
timeoutMs;
|
|
3337
|
+
constructor(opts = {}) {
|
|
3338
|
+
this.transformUrl = opts.transformUrl;
|
|
3339
|
+
this.timeoutMs = opts.timeoutMs;
|
|
3340
|
+
}
|
|
3341
|
+
fetchOrderbook(symbol, opts = {}) {
|
|
3342
|
+
const { pair, market } = parseSymbol(symbol);
|
|
3343
|
+
return fetchLighterOrderbook({
|
|
3344
|
+
symbol: pair,
|
|
3345
|
+
market,
|
|
3346
|
+
depth: opts.depth,
|
|
3347
|
+
timeoutMs: this.timeoutMs,
|
|
3348
|
+
transformUrl: this.transformUrl
|
|
3349
|
+
});
|
|
3350
|
+
}
|
|
3351
|
+
streamOrderbook(symbol, opts = {}) {
|
|
3352
|
+
const { pair, market } = parseSymbol(symbol);
|
|
3353
|
+
return streamLighterOrderbook({
|
|
3354
|
+
symbol: pair,
|
|
3355
|
+
market,
|
|
3356
|
+
depth: opts.depth,
|
|
3357
|
+
transformUrl: this.transformUrl,
|
|
3358
|
+
timeoutMs: this.timeoutMs
|
|
3359
|
+
});
|
|
3360
|
+
}
|
|
3361
|
+
};
|
|
3362
|
+
|
|
3363
|
+
// src/exchanges/edgex/markets.ts
|
|
3364
|
+
var EDGEX_REST = "https://edgex-prod-v2.edgex.exchange";
|
|
3365
|
+
var EDGEX_WS = "wss://edgex-quote-prod-v2.edgex.exchange";
|
|
3366
|
+
var cache2 = null;
|
|
3367
|
+
var inflight2 = null;
|
|
3368
|
+
async function fetchContracts(opts) {
|
|
3369
|
+
const res = await httpJson({
|
|
3370
|
+
url: `${EDGEX_REST}/api/v2/public/meta/getMetaData`,
|
|
3371
|
+
timeoutMs: opts.timeoutMs,
|
|
3372
|
+
transformUrl: opts.transformUrl
|
|
3373
|
+
});
|
|
3374
|
+
const rows = res.data?.contractList ?? [];
|
|
3375
|
+
if (rows.length === 0) {
|
|
3376
|
+
throw new ExchangeError(
|
|
3377
|
+
"edgex",
|
|
3378
|
+
res.msg ? `${res.code}: ${res.msg}` : "empty metadata response"
|
|
3379
|
+
);
|
|
3380
|
+
}
|
|
3381
|
+
const map = /* @__PURE__ */ new Map();
|
|
3382
|
+
for (const row of rows) map.set(row.contractName.toUpperCase(), row);
|
|
3383
|
+
return map;
|
|
3384
|
+
}
|
|
3385
|
+
function loadContracts(opts) {
|
|
3386
|
+
inflight2 ??= fetchContracts(opts).then(
|
|
3387
|
+
(m) => {
|
|
3388
|
+
cache2 = m;
|
|
3389
|
+
inflight2 = null;
|
|
3390
|
+
return m;
|
|
3391
|
+
},
|
|
3392
|
+
(err) => {
|
|
3393
|
+
inflight2 = null;
|
|
3394
|
+
throw err;
|
|
3395
|
+
}
|
|
3396
|
+
);
|
|
3397
|
+
return inflight2;
|
|
3398
|
+
}
|
|
3399
|
+
async function resolveEdgexContract(contractName, opts) {
|
|
3400
|
+
const k = contractName.toUpperCase();
|
|
3401
|
+
const hit = cache2?.get(k);
|
|
3402
|
+
const contract = hit ?? (await loadContracts(opts)).get(k);
|
|
3403
|
+
if (!contract) {
|
|
3404
|
+
throw new ExchangeError(
|
|
3405
|
+
"edgex",
|
|
3406
|
+
`unknown contract "${contractName}" \u2014 not listed on edgex`
|
|
3407
|
+
);
|
|
3408
|
+
}
|
|
3409
|
+
return contract;
|
|
3410
|
+
}
|
|
3411
|
+
|
|
3412
|
+
// src/exchanges/edgex/symbols.ts
|
|
3413
|
+
function toEdgexSymbol(symbol, market) {
|
|
3414
|
+
const [base, quote] = symbol.split("/");
|
|
3415
|
+
if (!base || !quote) {
|
|
3416
|
+
throw new Error(`invalid symbol "${symbol}" \u2014 expected "BASE/QUOTE"`);
|
|
3417
|
+
}
|
|
3418
|
+
if (market !== "perpetual") {
|
|
3419
|
+
throw new Error(
|
|
3420
|
+
`edgex only lists perpetuals; use "${base.toUpperCase()}/${quote.toUpperCase()}:${quote.toUpperCase()}"`
|
|
3421
|
+
);
|
|
3422
|
+
}
|
|
3423
|
+
if (quote.toUpperCase() !== "USDC") {
|
|
3424
|
+
throw new Error(`edgex only supports USDC-quoted pairs; got "${quote}"`);
|
|
3425
|
+
}
|
|
3426
|
+
return `${base.toUpperCase()}${quote.toUpperCase()}`;
|
|
3427
|
+
}
|
|
3428
|
+
function fromEdgexSymbol(contractName) {
|
|
3429
|
+
const name = contractName.toUpperCase();
|
|
3430
|
+
return name.endsWith("USDC") ? `${name.slice(0, -4)}/USDC` : name;
|
|
3431
|
+
}
|
|
3432
|
+
|
|
3433
|
+
// src/exchanges/edgex/rest.ts
|
|
3434
|
+
var ALLOWED_LEVELS = [15, 200];
|
|
3435
|
+
function snapLevel(depth) {
|
|
3436
|
+
return ALLOWED_LEVELS.find((l) => l >= depth) ?? ALLOWED_LEVELS[ALLOWED_LEVELS.length - 1];
|
|
3437
|
+
}
|
|
3438
|
+
var toLevels3 = (rows) => (rows ?? []).map((l) => ({ price: Number(l.price), size: Number(l.size) }));
|
|
3439
|
+
async function fetchEdgexOrderbook(opts) {
|
|
3440
|
+
const contractName = toEdgexSymbol(opts.symbol, opts.market);
|
|
3441
|
+
const contract = await resolveEdgexContract(contractName, opts);
|
|
3442
|
+
const level = snapLevel(opts.depth ?? 50);
|
|
3443
|
+
const res = await httpJson({
|
|
3444
|
+
url: `${EDGEX_REST}/api/v2/public/quote/getDepth?contractId=${contract.contractId}&level=${level}`,
|
|
3445
|
+
timeoutMs: opts.timeoutMs,
|
|
3446
|
+
transformUrl: opts.transformUrl
|
|
3447
|
+
});
|
|
3448
|
+
const depth = res.data?.[0];
|
|
3449
|
+
if (!depth) {
|
|
3450
|
+
throw new ExchangeError(
|
|
3451
|
+
"edgex",
|
|
3452
|
+
res.msg ? `${res.code}: ${res.msg}` : "empty orderbook response"
|
|
3453
|
+
);
|
|
3454
|
+
}
|
|
3455
|
+
const timestamp = Date.now();
|
|
3456
|
+
return {
|
|
3457
|
+
exchange: "edgex",
|
|
3458
|
+
symbol: fromEdgexSymbol(depth.contractName ?? contractName),
|
|
3459
|
+
market: opts.market,
|
|
3460
|
+
bids: toLevels3(depth.bids),
|
|
3461
|
+
asks: toLevels3(depth.asks),
|
|
3462
|
+
timestamp,
|
|
3463
|
+
sequence: Number(depth.endVersion ?? 0)
|
|
3464
|
+
};
|
|
3465
|
+
}
|
|
3466
|
+
|
|
3467
|
+
// src/exchanges/edgex/ws.ts
|
|
3468
|
+
var WS_URL3 = `${EDGEX_WS}/api/v1/public/ws`;
|
|
3469
|
+
function streamEdgexOrderbook(opts) {
|
|
3470
|
+
const contractName = toEdgexSymbol(opts.symbol, opts.market);
|
|
3471
|
+
const symbol = fromEdgexSymbol(contractName);
|
|
3472
|
+
const level = snapLevel(opts.depth ?? 50);
|
|
3473
|
+
const contractId = resolveEdgexContract(contractName, opts).then(
|
|
3474
|
+
(c) => c.contractId
|
|
3475
|
+
);
|
|
3476
|
+
contractId.catch(() => {
|
|
3477
|
+
});
|
|
3478
|
+
const merger = new BookMerger();
|
|
3479
|
+
let ws = null;
|
|
3480
|
+
let sock = null;
|
|
3481
|
+
let channel = null;
|
|
3482
|
+
let recovering = false;
|
|
3483
|
+
const stream = new OrderbookStream(() => ws?.close());
|
|
3484
|
+
const send = (type) => {
|
|
3485
|
+
if (channel) sock?.send(JSON.stringify({ type, channel }));
|
|
3486
|
+
};
|
|
3487
|
+
const emit = (r) => {
|
|
3488
|
+
const book = {
|
|
3489
|
+
exchange: "edgex",
|
|
3490
|
+
symbol,
|
|
3491
|
+
market: opts.market,
|
|
3492
|
+
bids: r.bids,
|
|
3493
|
+
asks: r.asks,
|
|
3494
|
+
timestamp: r.timestamp,
|
|
3495
|
+
sequence: r.sequence
|
|
3496
|
+
};
|
|
3497
|
+
stream.emit("update", book);
|
|
3498
|
+
};
|
|
3499
|
+
const resync = () => {
|
|
3500
|
+
if (recovering) return;
|
|
3501
|
+
recovering = true;
|
|
3502
|
+
merger.reset();
|
|
3503
|
+
send("unsubscribe");
|
|
3504
|
+
send("subscribe");
|
|
3505
|
+
};
|
|
3506
|
+
const buildEvent2 = (d, snapshot) => {
|
|
3507
|
+
const bids = toLevels3(d.bids);
|
|
3508
|
+
const asks = toLevels3(d.asks);
|
|
3509
|
+
const timestamp = Date.now();
|
|
3510
|
+
const sequence = Number(d.endVersion ?? 0);
|
|
3511
|
+
return snapshot ? { kind: "snapshot", bids, asks, sequence, timestamp } : {
|
|
3512
|
+
kind: "delta",
|
|
3513
|
+
bids,
|
|
3514
|
+
asks,
|
|
3515
|
+
sequence,
|
|
3516
|
+
prevSequence: Number(d.startVersion ?? 0),
|
|
3517
|
+
timestamp
|
|
3518
|
+
};
|
|
3519
|
+
};
|
|
3520
|
+
const handleDepth = (d, snapshot) => {
|
|
3521
|
+
if (snapshot) recovering = false;
|
|
3522
|
+
else if (recovering) return;
|
|
3523
|
+
const r = merger.apply(buildEvent2(d, snapshot));
|
|
3524
|
+
if (r.ok) {
|
|
3525
|
+
emit(r);
|
|
3526
|
+
} else if (r.reason === "gap" || r.reason === "no-snapshot") {
|
|
3527
|
+
resync();
|
|
3528
|
+
}
|
|
3529
|
+
};
|
|
3530
|
+
const onMessage = (raw) => {
|
|
3531
|
+
if (typeof raw !== "string") return;
|
|
3532
|
+
let msg;
|
|
3533
|
+
try {
|
|
3534
|
+
msg = JSON.parse(raw);
|
|
3535
|
+
} catch {
|
|
3536
|
+
return;
|
|
3537
|
+
}
|
|
3538
|
+
if (msg.type === "ping") {
|
|
3539
|
+
sock?.send(JSON.stringify({ type: "pong", time: msg.time }));
|
|
3540
|
+
return;
|
|
3541
|
+
}
|
|
3542
|
+
if (msg.type === "error") {
|
|
3543
|
+
const { code, msg: detail } = msg.content ?? {};
|
|
3544
|
+
stream.emit(
|
|
3545
|
+
"error",
|
|
3546
|
+
new ExchangeError(
|
|
3547
|
+
"edgex",
|
|
3548
|
+
code ? `${code}: ${detail ?? ""}`.trim() : "subscribe rejected"
|
|
3549
|
+
)
|
|
3550
|
+
);
|
|
3551
|
+
return;
|
|
3552
|
+
}
|
|
3553
|
+
if (msg.type !== "quote-event") return;
|
|
3554
|
+
if (channel && (msg.channel ?? msg.content?.channel) !== channel) return;
|
|
3555
|
+
const snapshot = msg.content?.dataType?.toLowerCase() === "snapshot";
|
|
3556
|
+
for (const d of msg.content?.data ?? []) handleDepth(d, snapshot);
|
|
3557
|
+
};
|
|
3558
|
+
ws = new WSClient({
|
|
3559
|
+
url: WS_URL3,
|
|
3560
|
+
onOpen: async (handle) => {
|
|
3561
|
+
sock = handle;
|
|
3562
|
+
merger.reset();
|
|
3563
|
+
recovering = false;
|
|
3564
|
+
channel = `depth.${await contractId}.${level}`;
|
|
3565
|
+
send("subscribe");
|
|
3566
|
+
},
|
|
3567
|
+
onMessage
|
|
3568
|
+
});
|
|
3569
|
+
ws.on("open", () => stream.emit("connected"));
|
|
3570
|
+
ws.on("close", (r) => stream.emit("disconnected", r));
|
|
3571
|
+
ws.on("reconnecting", (a, w) => stream.emit("reconnecting", a, w));
|
|
3572
|
+
ws.on("error", (e) => stream.emit("error", e));
|
|
3573
|
+
ws.connect().catch((e) => stream.emit("error", e));
|
|
3574
|
+
return stream;
|
|
3575
|
+
}
|
|
3576
|
+
|
|
3577
|
+
// src/exchanges/edgex/index.ts
|
|
3578
|
+
var EdgexClient = class {
|
|
3579
|
+
exchange = "edgex";
|
|
3580
|
+
transformUrl;
|
|
3581
|
+
timeoutMs;
|
|
3582
|
+
constructor(opts = {}) {
|
|
3583
|
+
this.transformUrl = opts.transformUrl;
|
|
3584
|
+
this.timeoutMs = opts.timeoutMs;
|
|
3585
|
+
}
|
|
3586
|
+
fetchOrderbook(symbol, opts = {}) {
|
|
3587
|
+
const { pair, market } = parseSymbol(symbol);
|
|
3588
|
+
return fetchEdgexOrderbook({
|
|
3589
|
+
symbol: pair,
|
|
3590
|
+
market,
|
|
3591
|
+
depth: opts.depth,
|
|
3592
|
+
timeoutMs: this.timeoutMs,
|
|
3593
|
+
transformUrl: this.transformUrl
|
|
3594
|
+
});
|
|
3595
|
+
}
|
|
3596
|
+
streamOrderbook(symbol, opts = {}) {
|
|
3597
|
+
const { pair, market } = parseSymbol(symbol);
|
|
3598
|
+
return streamEdgexOrderbook({
|
|
3599
|
+
symbol: pair,
|
|
3600
|
+
market,
|
|
3601
|
+
depth: opts.depth,
|
|
3602
|
+
transformUrl: this.transformUrl,
|
|
3603
|
+
timeoutMs: this.timeoutMs
|
|
3604
|
+
});
|
|
3605
|
+
}
|
|
3606
|
+
};
|
|
3607
|
+
|
|
3102
3608
|
exports.Backoff = Backoff;
|
|
3103
3609
|
exports.BinanceClient = BinanceClient;
|
|
3104
3610
|
exports.BingxClient = BingxClient;
|
|
@@ -3107,12 +3613,14 @@ exports.BookMerger = BookMerger;
|
|
|
3107
3613
|
exports.BybitClient = BybitClient;
|
|
3108
3614
|
exports.CoinexClient = CoinexClient;
|
|
3109
3615
|
exports.DeribitClient = DeribitClient;
|
|
3616
|
+
exports.EdgexClient = EdgexClient;
|
|
3110
3617
|
exports.ExchangeError = ExchangeError;
|
|
3111
3618
|
exports.GateClient = GateClient;
|
|
3112
3619
|
exports.GrvtClient = GrvtClient;
|
|
3113
3620
|
exports.HuobiClient = HuobiClient;
|
|
3114
3621
|
exports.HyperliquidClient = HyperliquidClient;
|
|
3115
3622
|
exports.KucoinClient = KucoinClient;
|
|
3623
|
+
exports.LighterClient = LighterClient;
|
|
3116
3624
|
exports.OkxClient = OkxClient;
|
|
3117
3625
|
exports.OrderbookStream = OrderbookStream;
|
|
3118
3626
|
exports.RateLimitError = RateLimitError;
|