@xapy/orderbook 0.1.7 → 0.1.9

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.
Files changed (35) hide show
  1. package/dist/exchanges/binance/index.d.cts +1 -1
  2. package/dist/exchanges/binance/index.d.ts +1 -1
  3. package/dist/exchanges/bingx/index.d.cts +1 -1
  4. package/dist/exchanges/bingx/index.d.ts +1 -1
  5. package/dist/exchanges/bitget/index.d.cts +1 -1
  6. package/dist/exchanges/bitget/index.d.ts +1 -1
  7. package/dist/exchanges/bitmart/index.cjs +594 -0
  8. package/dist/exchanges/bitmart/index.cjs.map +1 -0
  9. package/dist/exchanges/bitmart/index.d.cts +56 -0
  10. package/dist/exchanges/bitmart/index.d.ts +56 -0
  11. package/dist/exchanges/bitmart/index.js +590 -0
  12. package/dist/exchanges/bitmart/index.js.map +1 -0
  13. package/dist/exchanges/bybit/index.d.cts +1 -1
  14. package/dist/exchanges/bybit/index.d.ts +1 -1
  15. package/dist/exchanges/coinex/index.d.cts +1 -1
  16. package/dist/exchanges/coinex/index.d.ts +1 -1
  17. package/dist/exchanges/gate/index.d.cts +1 -1
  18. package/dist/exchanges/gate/index.d.ts +1 -1
  19. package/dist/exchanges/huobi/index.d.cts +1 -1
  20. package/dist/exchanges/huobi/index.d.ts +1 -1
  21. package/dist/exchanges/hyperliquid/index.d.cts +1 -1
  22. package/dist/exchanges/hyperliquid/index.d.ts +1 -1
  23. package/dist/exchanges/kucoin/index.d.cts +1 -1
  24. package/dist/exchanges/kucoin/index.d.ts +1 -1
  25. package/dist/exchanges/okx/index.d.cts +1 -1
  26. package/dist/exchanges/okx/index.d.ts +1 -1
  27. package/dist/index.cjs +180 -0
  28. package/dist/index.cjs.map +1 -1
  29. package/dist/index.d.cts +3 -2
  30. package/dist/index.d.ts +3 -2
  31. package/dist/index.js +180 -1
  32. package/dist/index.js.map +1 -1
  33. package/dist/{stream-C73HPYqT.d.cts → stream-L9MJjYbE.d.cts} +1 -1
  34. package/dist/{stream-C73HPYqT.d.ts → stream-L9MJjYbE.d.ts} +1 -1
  35. package/package.json +11 -1
package/dist/index.d.cts CHANGED
@@ -1,5 +1,5 @@
1
- import { B as BookEvent, O as OrderbookLevel, M as Market } from './stream-C73HPYqT.cjs';
2
- export { C as ClientOptions, E as ExchangeName, a as Orderbook, b as OrderbookOptions, c as OrderbookStream, S as Side, d as StreamEvents } from './stream-C73HPYqT.cjs';
1
+ import { B as BookEvent, O as OrderbookLevel, M as Market } from './stream-L9MJjYbE.cjs';
2
+ export { C as ClientOptions, E as ExchangeName, a as Orderbook, b as OrderbookOptions, c as OrderbookStream, S as Side, d as StreamEvents } from './stream-L9MJjYbE.cjs';
3
3
  export { BybitClient } from './exchanges/bybit/index.cjs';
4
4
  export { BitgetClient } from './exchanges/bitget/index.cjs';
5
5
  export { GateClient } from './exchanges/gate/index.cjs';
@@ -10,6 +10,7 @@ export { OkxClient } from './exchanges/okx/index.cjs';
10
10
  export { BinanceClient } from './exchanges/binance/index.cjs';
11
11
  export { HyperliquidClient } from './exchanges/hyperliquid/index.cjs';
12
12
  export { KucoinClient } from './exchanges/kucoin/index.cjs';
13
+ export { BitmartClient } from './exchanges/bitmart/index.cjs';
13
14
 
14
15
  declare class ExchangeError extends Error {
15
16
  readonly exchange: string;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { B as BookEvent, O as OrderbookLevel, M as Market } from './stream-C73HPYqT.js';
2
- export { C as ClientOptions, E as ExchangeName, a as Orderbook, b as OrderbookOptions, c as OrderbookStream, S as Side, d as StreamEvents } from './stream-C73HPYqT.js';
1
+ import { B as BookEvent, O as OrderbookLevel, M as Market } from './stream-L9MJjYbE.js';
2
+ export { C as ClientOptions, E as ExchangeName, a as Orderbook, b as OrderbookOptions, c as OrderbookStream, S as Side, d as StreamEvents } from './stream-L9MJjYbE.js';
3
3
  export { BybitClient } from './exchanges/bybit/index.js';
4
4
  export { BitgetClient } from './exchanges/bitget/index.js';
5
5
  export { GateClient } from './exchanges/gate/index.js';
@@ -10,6 +10,7 @@ export { OkxClient } from './exchanges/okx/index.js';
10
10
  export { BinanceClient } from './exchanges/binance/index.js';
11
11
  export { HyperliquidClient } from './exchanges/hyperliquid/index.js';
12
12
  export { KucoinClient } from './exchanges/kucoin/index.js';
13
+ export { BitmartClient } from './exchanges/bitmart/index.js';
13
14
 
14
15
  declare class ExchangeError extends Error {
15
16
  readonly exchange: string;
package/dist/index.js CHANGED
@@ -2567,6 +2567,185 @@ var KucoinClient = class {
2567
2567
  }
2568
2568
  };
2569
2569
 
2570
- export { Backoff, BinanceClient, BingxClient, BitgetClient, BookMerger, BybitClient, CoinexClient, ExchangeError, GateClient, HuobiClient, HyperliquidClient, KucoinClient, OkxClient, OrderbookStream, RateLimitError, SequenceGapError, parseSymbol };
2570
+ // src/exchanges/bitmart/symbols.ts
2571
+ var QUOTES6 = ["USDT", "USDC", "USD"];
2572
+ function toBitmartSymbol(symbol) {
2573
+ const [base, quote] = symbol.split("/");
2574
+ if (!base || !quote) {
2575
+ throw new Error(`invalid symbol "${symbol}" \u2014 expected "BASE/QUOTE"`);
2576
+ }
2577
+ return `${base}${quote}`.toUpperCase();
2578
+ }
2579
+ function fromBitmartSymbol(s) {
2580
+ const upper = s.toUpperCase();
2581
+ for (const q of QUOTES6) {
2582
+ if (upper.endsWith(q) && upper.length > q.length) {
2583
+ return `${upper.slice(0, -q.length)}/${q}`;
2584
+ }
2585
+ }
2586
+ return upper;
2587
+ }
2588
+
2589
+ // src/exchanges/bitmart/rest.ts
2590
+ var BASE8 = "https://api-cloud-v2.bitmart.com";
2591
+ async function fetchBitmartOrderbook(opts) {
2592
+ if (opts.market !== "perpetual") {
2593
+ throw new ExchangeError(
2594
+ "bitmart",
2595
+ "spot is not supported \u2014 only USDT-M perpetual"
2596
+ );
2597
+ }
2598
+ const symbol = toBitmartSymbol(opts.symbol);
2599
+ const url = `${BASE8}/contract/public/depth?symbol=${symbol}`;
2600
+ const res = await httpJson({
2601
+ url,
2602
+ timeoutMs: opts.timeoutMs,
2603
+ transformUrl: opts.transformUrl
2604
+ });
2605
+ if (res.code !== 1e3) {
2606
+ throw new ExchangeError("bitmart", `${res.code}: ${res.message ?? ""}`);
2607
+ }
2608
+ return {
2609
+ exchange: "bitmart",
2610
+ symbol: fromBitmartSymbol(res.data.symbol),
2611
+ market: "perpetual",
2612
+ bids: parseLevels3(res.data.bids),
2613
+ asks: parseLevels3(res.data.asks),
2614
+ timestamp: res.data.timestamp,
2615
+ sequence: res.data.timestamp
2616
+ };
2617
+ }
2618
+ function parseLevels3(rows) {
2619
+ const out = [];
2620
+ for (const row of rows) {
2621
+ out.push({ price: Number(row[0]), size: Number(row[1]) });
2622
+ }
2623
+ return out;
2624
+ }
2625
+
2626
+ // src/exchanges/bitmart/ws.ts
2627
+ var WS_FUTURES4 = "wss://openapi-ws-v2.bitmart.com/api?protocol=1.1";
2628
+ function streamBitmartOrderbook(opts) {
2629
+ if (opts.market !== "perpetual") {
2630
+ throw new ExchangeError(
2631
+ "bitmart",
2632
+ "spot is not supported \u2014 only USDT-M perpetual"
2633
+ );
2634
+ }
2635
+ const symbol = toBitmartSymbol(opts.symbol);
2636
+ const channelArg = opts.interval ? `futures/depth:${symbol}@${opts.interval}` : `futures/depth:${symbol}`;
2637
+ const merger = new BookMerger();
2638
+ let ws = null;
2639
+ const stream = new OrderbookStream(() => ws?.close());
2640
+ const subscribePayload = JSON.stringify({
2641
+ action: "subscribe",
2642
+ args: [channelArg]
2643
+ });
2644
+ const pingPayload = () => JSON.stringify({ action: "ping" });
2645
+ const handleFrame = (frame) => {
2646
+ if (frame.symbol !== symbol) return;
2647
+ const event = {
2648
+ kind: "snapshot",
2649
+ bids: parseLevels4(frame.bids),
2650
+ asks: parseLevels4(frame.asks),
2651
+ sequence: frame.timestamp,
2652
+ timestamp: frame.timestamp
2653
+ };
2654
+ const r = merger.apply(event);
2655
+ if (!r.ok) return;
2656
+ const book = {
2657
+ exchange: "bitmart",
2658
+ symbol: fromBitmartSymbol(symbol),
2659
+ market: "perpetual",
2660
+ bids: r.bids,
2661
+ asks: r.asks,
2662
+ timestamp: r.timestamp,
2663
+ sequence: r.sequence
2664
+ };
2665
+ stream.emit("update", book);
2666
+ };
2667
+ const handlePayload = (text) => {
2668
+ let msg;
2669
+ try {
2670
+ msg = JSON.parse(text);
2671
+ } catch {
2672
+ return;
2673
+ }
2674
+ if (msg.action === "subscribe" || msg.action === "pong") return;
2675
+ if (msg.errorCode) {
2676
+ stream.emit(
2677
+ "error",
2678
+ new Error(`bitmart: ${msg.errorCode} ${msg.errorMessage ?? ""}`)
2679
+ );
2680
+ return;
2681
+ }
2682
+ if (msg.arg !== channelArg || !msg.data) return;
2683
+ handleFrame(msg.data);
2684
+ };
2685
+ const onMessage = async (raw) => {
2686
+ if (typeof raw === "string") {
2687
+ handlePayload(raw);
2688
+ return;
2689
+ }
2690
+ const inflate = await getGzipInflate();
2691
+ handlePayload(inflate(raw));
2692
+ };
2693
+ ws = new WSClient({
2694
+ url: WS_FUTURES4,
2695
+ onOpen: (sock) => {
2696
+ merger.reset();
2697
+ sock.send(subscribePayload);
2698
+ },
2699
+ onMessage,
2700
+ pingIntervalMs: 15e3,
2701
+ pingPayload
2702
+ });
2703
+ ws.on("open", () => stream.emit("connected"));
2704
+ ws.on("close", (r) => stream.emit("disconnected", r));
2705
+ ws.on("reconnecting", (a, w) => stream.emit("reconnecting", a, w));
2706
+ ws.on("error", (e) => stream.emit("error", e));
2707
+ ws.connect().catch((e) => stream.emit("error", e));
2708
+ return stream;
2709
+ }
2710
+ function parseLevels4(rows) {
2711
+ if (!rows) return [];
2712
+ const out = [];
2713
+ for (const row of rows) {
2714
+ out.push({ price: Number(row[0]), size: Number(row[1]) });
2715
+ }
2716
+ return out;
2717
+ }
2718
+
2719
+ // src/exchanges/bitmart/index.ts
2720
+ var BitmartClient = class {
2721
+ exchange = "bitmart";
2722
+ transformUrl;
2723
+ timeoutMs;
2724
+ constructor(opts = {}) {
2725
+ this.transformUrl = opts.transformUrl;
2726
+ this.timeoutMs = opts.timeoutMs;
2727
+ }
2728
+ fetchOrderbook(symbol, opts = {}) {
2729
+ const { pair, market } = parseSymbol(symbol);
2730
+ return fetchBitmartOrderbook({
2731
+ symbol: pair,
2732
+ market,
2733
+ depth: opts.depth,
2734
+ timeoutMs: this.timeoutMs,
2735
+ transformUrl: this.transformUrl
2736
+ });
2737
+ }
2738
+ streamOrderbook(symbol, _opts = {}) {
2739
+ const { pair, market } = parseSymbol(symbol);
2740
+ return streamBitmartOrderbook({
2741
+ symbol: pair,
2742
+ market,
2743
+ transformUrl: this.transformUrl,
2744
+ timeoutMs: this.timeoutMs
2745
+ });
2746
+ }
2747
+ };
2748
+
2749
+ export { Backoff, BinanceClient, BingxClient, BitgetClient, BitmartClient, BookMerger, BybitClient, CoinexClient, ExchangeError, GateClient, HuobiClient, HyperliquidClient, KucoinClient, OkxClient, OrderbookStream, RateLimitError, SequenceGapError, parseSymbol };
2571
2750
  //# sourceMappingURL=index.js.map
2572
2751
  //# sourceMappingURL=index.js.map