@0xarchive/sdk 0.6.4 → 0.6.5

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.mjs CHANGED
@@ -687,6 +687,72 @@ var OrderBookResource = class {
687
687
  createReconstructor() {
688
688
  return new OrderBookReconstructor();
689
689
  }
690
+ /**
691
+ * Iterate over tick-level orderbook history with automatic pagination (Enterprise tier only).
692
+ *
693
+ * This async generator automatically handles pagination, fetching up to 1,000 deltas
694
+ * per API request and yielding reconstructed orderbook snapshots one at a time.
695
+ * Memory-efficient for processing large time ranges.
696
+ *
697
+ * @param coin - The coin symbol (e.g., 'BTC', 'ETH')
698
+ * @param params - Time range parameters
699
+ * @param depth - Maximum price levels to include in output snapshots
700
+ * @yields Reconstructed orderbook snapshots
701
+ *
702
+ * @example
703
+ * ```typescript
704
+ * // Process 24 hours of tick data with automatic pagination
705
+ * for await (const snapshot of client.lighter.orderbook.iterateTickHistory('BTC', {
706
+ * start: Date.now() - 86400000,
707
+ * end: Date.now()
708
+ * })) {
709
+ * console.log(snapshot.timestamp, 'Mid:', snapshot.midPrice);
710
+ * if (someCondition) break; // Early exit supported
711
+ * }
712
+ *
713
+ * // Collect all snapshots (caution: may use significant memory for large ranges)
714
+ * const allSnapshots: ReconstructedOrderBook[] = [];
715
+ * for await (const snapshot of client.lighter.orderbook.iterateTickHistory('BTC', { start, end })) {
716
+ * allSnapshots.push(snapshot);
717
+ * }
718
+ * ```
719
+ */
720
+ async *iterateTickHistory(coin, params, depth) {
721
+ const startTs = typeof params.start === "string" ? new Date(params.start).getTime() : params.start;
722
+ const endTs = typeof params.end === "string" ? new Date(params.end).getTime() : params.end;
723
+ let cursor = startTs;
724
+ const reconstructor = new OrderBookReconstructor();
725
+ const MAX_DELTAS_PER_PAGE = 1e3;
726
+ let isFirstPage = true;
727
+ while (cursor < endTs) {
728
+ const tickData = await this.historyTick(coin, {
729
+ start: cursor,
730
+ end: endTs,
731
+ depth: params.depth
732
+ });
733
+ if (tickData.deltas.length === 0) {
734
+ if (isFirstPage) {
735
+ reconstructor.initialize(tickData.checkpoint);
736
+ yield reconstructor.getSnapshot(depth);
737
+ }
738
+ break;
739
+ }
740
+ let skipFirst = !isFirstPage;
741
+ for (const snapshot of reconstructor.iterate(tickData.checkpoint, tickData.deltas, { depth })) {
742
+ if (skipFirst) {
743
+ skipFirst = false;
744
+ continue;
745
+ }
746
+ yield snapshot;
747
+ }
748
+ isFirstPage = false;
749
+ const lastDelta = tickData.deltas[tickData.deltas.length - 1];
750
+ cursor = lastDelta.timestamp + 1;
751
+ if (tickData.deltas.length < MAX_DELTAS_PER_PAGE) {
752
+ break;
753
+ }
754
+ }
755
+ }
690
756
  };
691
757
 
692
758
  // src/resources/trades.ts