@secondlayer/sdk 6.34.0 → 6.36.0

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.
@@ -66,10 +66,12 @@ class BaseClient {
66
66
  baseUrl;
67
67
  apiKey;
68
68
  origin;
69
+ fetchImpl;
69
70
  constructor(options = {}) {
70
71
  this.baseUrl = (options.baseUrl ?? DEFAULT_BASE_URL).replace(/\/+$/, "");
71
72
  this.apiKey = resolveApiKey(options.apiKey);
72
73
  this.origin = options.origin ?? "cli";
74
+ this.fetchImpl = options.fetchImpl ?? ((...args) => fetch(...args));
73
75
  }
74
76
  static authHeaders(apiKey) {
75
77
  const headers = {
@@ -115,7 +117,7 @@ class BaseClient {
115
117
  }
116
118
  let response;
117
119
  try {
118
- response = await fetch(url, {
120
+ response = await this.fetchImpl(url, {
119
121
  method,
120
122
  headers,
121
123
  body: serializedBody
@@ -494,6 +496,19 @@ var Cursor = {
494
496
  function reorgKey(reorg) {
495
497
  return `${reorg.detected_at}|${reorg.fork_point_height}|${reorg.new_canonical_tip}`;
496
498
  }
499
+ function assertFinalizedCheckpoint(returned, checkpoint) {
500
+ if (returned === null || returned === undefined)
501
+ return;
502
+ if (returned === checkpoint)
503
+ return;
504
+ const r = Cursor.parse(returned);
505
+ const c = checkpoint === null ? null : Cursor.parse(checkpoint);
506
+ const above = c === null || r.blockHeight > c.blockHeight || r.blockHeight === c.blockHeight && r.eventIndex > c.eventIndex;
507
+ if (above) {
508
+ const boundary = c ? ` ("${checkpoint}")` : " (none delivered yet)";
509
+ throw new ValidationError(`onBatch returned cursor "${returned}", above the last delivered finalized event${boundary}. With finalizedOnly, events past that point were filtered out and will be re-read once finalized — committing beyond it skips them permanently. Return ctx.cursor (or nothing); never envelope.next_cursor.`, 400);
510
+ }
511
+ }
497
512
  function batchContext(cursor, height, tipHeight) {
498
513
  return {
499
514
  cursor,
@@ -558,6 +573,8 @@ async function consumeStreamsEvents(opts) {
558
573
  const checkpoint = finalizedOnly ? emitted.at(-1)?.cursor ?? cursor : envelope.next_cursor;
559
574
  height = emitted.at(-1)?.block_height ?? height;
560
575
  const returnedCursor = await opts.onBatch(emitted, envelope, batchContext(checkpoint, height, envelope.tip.block_height));
576
+ if (finalizedOnly)
577
+ assertFinalizedCheckpoint(returnedCursor, checkpoint);
561
578
  const nextCursor = returnedCursor ?? checkpoint;
562
579
  if (nextCursor && nextCursor !== cursor) {
563
580
  cursor = nextCursor;
@@ -691,6 +708,8 @@ async function consumeIndexFeed(opts) {
691
708
  const checkpoint = finalizedOnly ? emitted.at(-1)?.cursor ?? cursor : envelope.next_cursor;
692
709
  height = emitted.at(-1)?.block_height ?? height;
693
710
  const returnedCursor = await opts.onBatch(emitted, envelope, batchContext(checkpoint, height, envelope.tip.block_height));
711
+ if (finalizedOnly)
712
+ assertFinalizedCheckpoint(returnedCursor, checkpoint);
694
713
  const nextCursor = returnedCursor ?? checkpoint;
695
714
  if (nextCursor && nextCursor !== cursor) {
696
715
  cursor = nextCursor;
@@ -807,6 +826,19 @@ class Index extends BaseClient {
807
826
  deposits: {
808
827
  list: (params = {}) => this.listSbtcDeposits(params),
809
828
  walk: (params = {}) => this.walkSbtcDeposits(params),
829
+ consume: (params) => consumeIndexFeed({
830
+ ...params,
831
+ fetchPage: ({ cursor, fromHeight, limit }) => this.listSbtcDeposits({
832
+ confirmed: params.confirmed,
833
+ sender: params.sender,
834
+ bitcoinTxid: params.bitcoinTxid,
835
+ toHeight: params.toHeight,
836
+ cursor,
837
+ fromHeight,
838
+ limit
839
+ }),
840
+ itemsOf: (envelope) => envelope.deposits
841
+ }),
810
842
  get: (bitcoinTxid) => this.getSbtcDeposit(bitcoinTxid)
811
843
  },
812
844
  withdrawals: {
@@ -816,7 +848,22 @@ class Index extends BaseClient {
816
848
  },
817
849
  events: {
818
850
  list: (params = {}) => this.listSbtcEvents(params),
819
- walk: (params = {}) => this.walkSbtcEvents(params)
851
+ walk: (params = {}) => this.walkSbtcEvents(params),
852
+ consume: (params) => consumeIndexFeed({
853
+ ...params,
854
+ fetchPage: ({ cursor, fromHeight, limit }) => this.listSbtcEvents({
855
+ confirmed: params.confirmed,
856
+ topic: params.topic,
857
+ sender: params.sender,
858
+ requestId: params.requestId,
859
+ bitcoinTxid: params.bitcoinTxid,
860
+ toHeight: params.toHeight,
861
+ cursor,
862
+ fromHeight,
863
+ limit
864
+ }),
865
+ itemsOf: (envelope) => envelope.events
866
+ })
820
867
  },
821
868
  summary: () => this.getSbtcSummary()
822
869
  };
@@ -1762,5 +1809,5 @@ export {
1762
1809
  Subgraphs
1763
1810
  };
1764
1811
 
1765
- //# debugId=875C963182FEF66164756E2164756E21
1812
+ //# debugId=13B6D7F76CAEAFE964756E2164756E21
1766
1813
  //# sourceMappingURL=index.js.map