@xapy/orderbook 0.1.30 → 0.1.31

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/dist/index.js CHANGED
@@ -1248,48 +1248,14 @@ function streamBingxOrderbook(opts) {
1248
1248
  const level = opts.depth ?? 20;
1249
1249
  const interval = opts.interval ?? "500ms";
1250
1250
  const depthDataType = isSpot ? `${wsSymbol}@depth${level}` : `${wsSymbol}@depth${level}@${interval}`;
1251
- const tickerDataType = `${wsSymbol}@bookTicker`;
1252
1251
  const merger = new BookMerger();
1253
1252
  let ws = null;
1254
1253
  const stream = new OrderbookStream(() => ws?.close());
1255
- const subId = Date.now();
1256
1254
  const subscribeDepth = JSON.stringify({
1257
- id: `sub-depth-${subId}`,
1255
+ id: `sub-depth-${Date.now()}`,
1258
1256
  reqType: "sub",
1259
1257
  dataType: depthDataType
1260
1258
  });
1261
- const subscribeTicker = JSON.stringify({
1262
- id: `sub-ticker-${subId}`,
1263
- reqType: "sub",
1264
- dataType: tickerDataType
1265
- });
1266
- let lastMerged = null;
1267
- let lastTicker = null;
1268
- const emitOverlayed = () => {
1269
- if (!lastMerged) return;
1270
- let bids = lastMerged.bids;
1271
- let asks = lastMerged.asks;
1272
- let timestamp = lastMerged.timestamp;
1273
- let sequence = lastMerged.sequence;
1274
- if (lastTicker) {
1275
- bids = overlayBid(bids, lastTicker.bid);
1276
- asks = overlayAsk(asks, lastTicker.ask);
1277
- if (lastTicker.ts > timestamp) {
1278
- timestamp = lastTicker.ts;
1279
- sequence = Math.max(sequence, lastTicker.ts);
1280
- }
1281
- }
1282
- const book = {
1283
- exchange: "bingx",
1284
- symbol: opts.symbol,
1285
- market: opts.market,
1286
- bids,
1287
- asks,
1288
- timestamp,
1289
- sequence
1290
- };
1291
- stream.emit("update", book);
1292
- };
1293
1259
  const handleDepth = (d, frameTs) => {
1294
1260
  const ts = d.T ?? d.ts ?? frameTs ?? Date.now();
1295
1261
  const event = {
@@ -1301,26 +1267,16 @@ function streamBingxOrderbook(opts) {
1301
1267
  };
1302
1268
  const r = merger.apply(event);
1303
1269
  if (!r.ok) return;
1304
- lastMerged = {
1270
+ const book = {
1271
+ exchange: "bingx",
1272
+ symbol: opts.symbol,
1273
+ market: opts.market,
1305
1274
  bids: r.bids,
1306
1275
  asks: r.asks,
1307
1276
  timestamp: r.timestamp,
1308
1277
  sequence: r.sequence
1309
1278
  };
1310
- emitOverlayed();
1311
- };
1312
- const handleTicker = (t) => {
1313
- const bidPrice = Number(t.b);
1314
- const askPrice = Number(t.a);
1315
- const bidSize = Number(t.B);
1316
- const askSize = Number(t.A);
1317
- if (!Number.isFinite(bidPrice) || !Number.isFinite(askPrice)) return;
1318
- lastTicker = {
1319
- bid: { price: bidPrice, size: bidSize },
1320
- ask: { price: askPrice, size: askSize },
1321
- ts: t.T ?? t.E ?? Date.now()
1322
- };
1323
- emitOverlayed();
1279
+ stream.emit("update", book);
1324
1280
  };
1325
1281
  const handlePayload = (text) => {
1326
1282
  if (text === "Ping" || text === "ping") {
@@ -1346,12 +1302,8 @@ function streamBingxOrderbook(opts) {
1346
1302
  return;
1347
1303
  }
1348
1304
  if (!msg.dataType || !msg.data) return;
1349
- const frameTs = msg.ts ?? msg.timestamp;
1350
- if (msg.dataType === depthDataType) {
1351
- handleDepth(msg.data, frameTs);
1352
- } else if (msg.dataType === tickerDataType) {
1353
- handleTicker(msg.data);
1354
- }
1305
+ if (msg.dataType !== depthDataType) return;
1306
+ handleDepth(msg.data, msg.ts ?? msg.timestamp);
1355
1307
  };
1356
1308
  const onMessage = async (raw) => {
1357
1309
  if (typeof raw === "string") {
@@ -1365,10 +1317,7 @@ function streamBingxOrderbook(opts) {
1365
1317
  url: isSpot ? WS_SPOT3 : WS_SWAP,
1366
1318
  onOpen: (sock) => {
1367
1319
  merger.reset();
1368
- lastMerged = null;
1369
- lastTicker = null;
1370
1320
  sock.send(subscribeDepth);
1371
- sock.send(subscribeTicker);
1372
1321
  },
1373
1322
  onMessage
1374
1323
  // bingx server is the one that sends Ping frames; we don't need client pings.
@@ -1380,32 +1329,6 @@ function streamBingxOrderbook(opts) {
1380
1329
  ws.connect().catch((e) => stream.emit("error", e));
1381
1330
  return stream;
1382
1331
  }
1383
- function overlayAsk(asks, top) {
1384
- if (!(top.size > 0)) return asks;
1385
- let i = 0;
1386
- for (; i < asks.length; i++) {
1387
- const level = asks[i];
1388
- if (!level || level.price >= top.price) break;
1389
- }
1390
- const at = asks[i];
1391
- if (at && at.price === top.price) {
1392
- return [{ price: top.price, size: top.size }, ...asks.slice(i + 1)];
1393
- }
1394
- return [{ price: top.price, size: top.size }, ...asks.slice(i)];
1395
- }
1396
- function overlayBid(bids, top) {
1397
- if (!(top.size > 0)) return bids;
1398
- let i = 0;
1399
- for (; i < bids.length; i++) {
1400
- const level = bids[i];
1401
- if (!level || level.price <= top.price) break;
1402
- }
1403
- const at = bids[i];
1404
- if (at && at.price === top.price) {
1405
- return [{ price: top.price, size: top.size }, ...bids.slice(i + 1)];
1406
- }
1407
- return [{ price: top.price, size: top.size }, ...bids.slice(i)];
1408
- }
1409
1332
 
1410
1333
  // src/exchanges/bingx/index.ts
1411
1334
  var BingxClient = class {
@@ -1515,8 +1438,8 @@ function streamCoinexOrderbook(opts) {
1515
1438
  let timestamp = lastMerged.timestamp;
1516
1439
  let sequence = lastMerged.sequence;
1517
1440
  if (lastTicker) {
1518
- bids = overlayBid2(bids, lastTicker.bid);
1519
- asks = overlayAsk2(asks, lastTicker.ask);
1441
+ bids = overlayBid(bids, lastTicker.bid);
1442
+ asks = overlayAsk(asks, lastTicker.ask);
1520
1443
  if (lastTicker.ts > timestamp) {
1521
1444
  timestamp = lastTicker.ts;
1522
1445
  sequence = Math.max(sequence, lastTicker.ts);
@@ -1614,7 +1537,7 @@ function streamCoinexOrderbook(opts) {
1614
1537
  ws.connect().catch((e) => stream.emit("error", e));
1615
1538
  return stream;
1616
1539
  }
1617
- function overlayAsk2(asks, top) {
1540
+ function overlayAsk(asks, top) {
1618
1541
  if (!(top.size > 0)) return asks;
1619
1542
  let i = 0;
1620
1543
  for (; i < asks.length; i++) {
@@ -1627,7 +1550,7 @@ function overlayAsk2(asks, top) {
1627
1550
  }
1628
1551
  return [{ price: top.price, size: top.size }, ...asks.slice(i)];
1629
1552
  }
1630
- function overlayBid2(bids, top) {
1553
+ function overlayBid(bids, top) {
1631
1554
  if (!(top.size > 0)) return bids;
1632
1555
  let i = 0;
1633
1556
  for (; i < bids.length; i++) {