@secondlayer/sdk 10.4.2 → 10.5.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.
@@ -1,4 +1,5 @@
1
- import { STREAMS_EVENT_TYPES, StreamsEvent, StreamsEventType } from "@secondlayer/shared/streams-rows";
1
+ import { VmEventType } from "@secondlayer/shared";
2
+ import { STREAMS_EVENT_TYPES, StreamsEvent, StreamsEventType, StreamsWireEvent, VmStreamsEvent } from "@secondlayer/shared/streams-rows";
2
3
  /**
3
4
  * A consumer sink: the destination adapter a consume loop writes through.
4
5
  *
@@ -217,9 +218,39 @@ type IndexPrint = IndexEventBase & {
217
218
  raw_value: string | null;
218
219
  };
219
220
  };
221
+ type IndexNestedContractCall = IndexEventBase & {
222
+ event_type: "nested_contract_call";
223
+ sender: string | null;
224
+ caller: string;
225
+ function_name: string;
226
+ function_args: unknown;
227
+ raw_result: string | null;
228
+ };
229
+ type IndexVarSet = IndexEventBase & {
230
+ event_type: "var_set";
231
+ var_name: string;
232
+ raw_value: string;
233
+ };
234
+ type IndexMapSet = IndexEventBase & {
235
+ event_type: "map_set";
236
+ map: string;
237
+ raw_key: string;
238
+ raw_value: string;
239
+ };
240
+ type IndexMapInsert = IndexEventBase & {
241
+ event_type: "map_insert";
242
+ map: string;
243
+ raw_key: string;
244
+ raw_value: string;
245
+ };
246
+ type IndexMapDelete = IndexEventBase & {
247
+ event_type: "map_delete";
248
+ map: string;
249
+ raw_key: string;
250
+ };
220
251
  /** Decoded chain event, discriminated by `event_type`. */
221
- type IndexEvent = IndexFtTransfer | IndexNftTransfer | IndexStxTransfer | IndexStxMint | IndexStxBurn | IndexStxLock | IndexFtMint | IndexFtBurn | IndexNftMint | IndexNftBurn | IndexPrint;
222
- import { StreamsEventPayload } from "@secondlayer/shared/streams-rows";
252
+ type IndexEvent = IndexFtTransfer | IndexNftTransfer | IndexStxTransfer | IndexStxMint | IndexStxBurn | IndexStxLock | IndexFtMint | IndexFtBurn | IndexNftMint | IndexNftBurn | IndexPrint | IndexNestedContractCall | IndexVarSet | IndexMapSet | IndexMapInsert | IndexMapDelete;
253
+ import { StreamsWireEvent as StreamsWireEvent2, StreamsEventPayload } from "@secondlayer/shared/streams-rows";
223
254
  type StreamsTip = {
224
255
  block_height: number;
225
256
  block_hash: string;
@@ -315,9 +346,11 @@ type StreamsEventsListParams = {
315
346
  cursor?: string | null;
316
347
  fromHeight?: number;
317
348
  toHeight?: number;
318
- types?: readonly StreamsEventType[];
349
+ /** `vm` reads ordinal. Omit for Streams 1.0. */
350
+ clock?: "classic" | "vm";
351
+ types?: readonly (StreamsEventType | VmEventType)[];
319
352
  /** Event types to exclude (applied after `types`). */
320
- notTypes?: readonly StreamsEventType[];
353
+ notTypes?: readonly (StreamsEventType | VmEventType)[];
321
354
  contractId?: StreamsFilterValue;
322
355
  sender?: StreamsFilterValue;
323
356
  recipient?: StreamsFilterValue;
@@ -709,11 +742,26 @@ type StreamsClient = {
709
742
  }): AsyncIterableIterator<StreamsBatch<StreamsEventOfTypes<T>>>;
710
743
  consume(params?: StreamsConsumeParams): AsyncIterableIterator<StreamsBatch>;
711
744
  events: {
712
- /** Narrowing overload, matching `consume`. */
713
- list<const T extends readonly StreamsEventType[]>(params: StreamsEventsListParams & {
745
+ /** `clock: "vm"` pages carry vm rows only (a parallel vocabulary,
746
+ * `event_index` = `ordinal`). Never `StreamsEvent`. */
747
+ list(params: StreamsEventsListParams & {
748
+ clock: "vm";
749
+ }): Promise<StreamsEventsEnvelope<VmStreamsEvent>>;
750
+ /** Narrowing overload, matching `consume`. Classic clock only —
751
+ * `clock: "vm"` is overload 1, and a general `StreamsEventsListParams`
752
+ * variable (clock unresolved) is the wire-union fallback. */
753
+ list<const T extends readonly StreamsEventType[]>(params: Omit<StreamsEventsListParams, "clock" | "types" | "notTypes"> & {
714
754
  types: T;
755
+ notTypes?: readonly StreamsEventType[];
756
+ clock?: "classic";
715
757
  }): Promise<StreamsEventsEnvelope<StreamsEventOfTypes<T>>>;
716
- list(params?: StreamsEventsListParams): Promise<StreamsEventsEnvelope>;
758
+ list(params?: Omit<StreamsEventsListParams, "clock" | "types" | "notTypes"> & {
759
+ types?: readonly StreamsEventType[];
760
+ notTypes?: readonly StreamsEventType[];
761
+ clock?: "classic";
762
+ }): Promise<StreamsEventsEnvelope>;
763
+ /** Unresolved `clock` on a general params variable: the wire union. */
764
+ list(params: StreamsEventsListParams): Promise<StreamsEventsEnvelope<StreamsWireEvent>>;
717
765
  byTxId(txId: string): Promise<StreamsEventsListEnvelope>;
718
766
  /**
719
767
  * Pull pages from Streams and call `onBatch` after each page.
@@ -972,4 +1020,4 @@ declare const Cursor: {
972
1020
  eventIndex: number;
973
1021
  };
974
1022
  };
975
- export { isStxTransfer, isStxMint, isStxLock, isStxBurn, isPrint, isNftTransfer, isNftMint, isNftBurn, isFtTransfer, isFtMint, isFtBurn, decodeStxTransfer, decodeStxMint, decodeStxLock, decodeStxBurn, decodePrint, decodeNftTransfer, decodeNftMint, decodeNftBurn, decodeFtTransfer, decodeFtMint, decodeFtBurn, decode, createStreamsClient, ValidationError, StreamsTip, StreamsSubscription, StreamsSignatureError, StreamsServerError, StreamsReorgsListParams, StreamsReorgsListEnvelope, StreamsReorgContext, StreamsReorg, StreamsEventsSubscribeParams, StreamsEventsStreamParams, StreamsEventsListParams, StreamsEventsListEnvelope, StreamsEventsEnvelope, StreamsEventsConsumeResult, StreamsEventsConsumeParams, StreamsEventType, StreamsEventPayload, StreamsEvent, StreamsDumpsManifest, StreamsDumps, StreamsDumpFile, StreamsConsumeParams, StreamsClient, StreamsCanonicalBlock, StreamsBatchContext, StreamsBatch, STREAMS_EVENT_TYPES, RateLimitError, NftTransferPayload2 as NftTransferPayload, NftTransferEvent, FtTransferPayload2 as FtTransferPayload, FtTransferEvent, FetchLike2 as FetchLike, DecodedStxTransferPayload, DecodedStxTransfer, DecodedStxMintPayload, DecodedStxMint, DecodedStxLockPayload, DecodedStxLock, DecodedStxBurnPayload, DecodedStxBurn, DecodedPrintValue, DecodedPrintPayload, DecodedPrint, DecodedNftTransferPayload, DecodedNftTransfer, DecodedNftMintPayload, DecodedNftMint, DecodedNftBurnPayload, DecodedNftBurn, DecodedFtTransferPayload, DecodedFtTransfer, DecodedFtMintPayload, DecodedFtMint, DecodedFtBurnPayload, DecodedFtBurn, DecodedEventRow, DecodedEventColumns, Cursor, ConsumerBatchContext, AuthError };
1023
+ export { isStxTransfer, isStxMint, isStxLock, isStxBurn, isPrint, isNftTransfer, isNftMint, isNftBurn, isFtTransfer, isFtMint, isFtBurn, decodeStxTransfer, decodeStxMint, decodeStxLock, decodeStxBurn, decodePrint, decodeNftTransfer, decodeNftMint, decodeNftBurn, decodeFtTransfer, decodeFtMint, decodeFtBurn, decode, createStreamsClient, VmStreamsEvent, ValidationError, StreamsWireEvent2 as StreamsWireEvent, StreamsTip, StreamsSubscription, StreamsSignatureError, StreamsServerError, StreamsReorgsListParams, StreamsReorgsListEnvelope, StreamsReorgContext, StreamsReorg, StreamsEventsSubscribeParams, StreamsEventsStreamParams, StreamsEventsListParams, StreamsEventsListEnvelope, StreamsEventsEnvelope, StreamsEventsConsumeResult, StreamsEventsConsumeParams, StreamsEventType, StreamsEventPayload, StreamsEvent, StreamsDumpsManifest, StreamsDumps, StreamsDumpFile, StreamsConsumeParams, StreamsClient, StreamsCanonicalBlock, StreamsBatchContext, StreamsBatch, STREAMS_EVENT_TYPES, RateLimitError, NftTransferPayload2 as NftTransferPayload, NftTransferEvent, FtTransferPayload2 as FtTransferPayload, FtTransferEvent, FetchLike2 as FetchLike, DecodedStxTransferPayload, DecodedStxTransfer, DecodedStxMintPayload, DecodedStxMint, DecodedStxLockPayload, DecodedStxLock, DecodedStxBurnPayload, DecodedStxBurn, DecodedPrintValue, DecodedPrintPayload, DecodedPrint, DecodedNftTransferPayload, DecodedNftTransfer, DecodedNftMintPayload, DecodedNftMint, DecodedNftBurnPayload, DecodedNftBurn, DecodedFtTransferPayload, DecodedFtTransfer, DecodedFtMintPayload, DecodedFtMint, DecodedFtBurnPayload, DecodedFtBurn, DecodedEventRow, DecodedEventColumns, Cursor, ConsumerBatchContext, AuthError };
@@ -159,6 +159,30 @@ function parseRetryAfter(value) {
159
159
  }
160
160
 
161
161
  // src/base.ts
162
+ class MemoryEtagCache {
163
+ limit;
164
+ entries = new Map;
165
+ constructor(limit = 64) {
166
+ this.limit = limit;
167
+ }
168
+ get(key) {
169
+ const entry = this.entries.get(key);
170
+ if (entry) {
171
+ this.entries.delete(key);
172
+ this.entries.set(key, entry);
173
+ }
174
+ return entry;
175
+ }
176
+ set(key, entry) {
177
+ this.entries.delete(key);
178
+ this.entries.set(key, entry);
179
+ if (this.entries.size > this.limit) {
180
+ const oldest = this.entries.keys().next().value;
181
+ if (oldest !== undefined)
182
+ this.entries.delete(oldest);
183
+ }
184
+ }
185
+ }
162
186
  var DEFAULT_REQUEST_TIMEOUT_MS = 30000;
163
187
  function abortError() {
164
188
  return new DOMException("This operation was aborted", "AbortError");
@@ -269,12 +293,14 @@ class BaseClient {
269
293
  origin;
270
294
  fetchImpl;
271
295
  requestTimeoutMs;
296
+ etagCache;
272
297
  constructor(options = {}) {
273
298
  this.baseUrl = resolveBaseUrl(options.baseUrl);
274
299
  this.apiKey = resolveApiKey(options.apiKey);
275
300
  this.origin = options.origin ?? "cli";
276
301
  this.requestTimeoutMs = options.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS;
277
302
  this.fetchImpl = options.fetchImpl ?? ((...args) => fetch(...args));
303
+ this.etagCache = options.etagCache === false ? null : options.etagCache ?? new MemoryEtagCache;
278
304
  }
279
305
  static authHeaders(apiKey) {
280
306
  const headers = {
@@ -287,11 +313,25 @@ class BaseClient {
287
313
  }
288
314
  async request(method, path, body, opts = {}) {
289
315
  return this.withRequestBudget(opts.signal, async (signal) => {
290
- const response = await this.fetchResponse(method, path, body, signal);
316
+ const cacheKey = method === "GET" && this.etagCache ? `${this.baseUrl}${path}` : null;
317
+ const cached = cacheKey ? await this.etagCache?.get(cacheKey) : undefined;
318
+ const response = await this.fetchResponse(method, path, body, signal, cached ? { "If-None-Match": cached.etag } : undefined);
291
319
  if (response.status === 204) {
292
320
  return;
293
321
  }
294
- return await raceAbort(response.json(), signal);
322
+ if (response.status === 304) {
323
+ if (!cached) {
324
+ throw new ApiError(304, "Not Modified without a cached copy");
325
+ }
326
+ return JSON.parse(cached.body);
327
+ }
328
+ const etag = cacheKey ? response.headers.get("ETag") : null;
329
+ if (!cacheKey || !etag) {
330
+ return await raceAbort(response.json(), signal);
331
+ }
332
+ const text = await raceAbort(response.text(), signal);
333
+ await this.etagCache?.set(cacheKey, { etag, body: text });
334
+ return JSON.parse(text);
295
335
  });
296
336
  }
297
337
  async requestOrNull(method, path, body, opts = {}) {
@@ -334,9 +374,12 @@ class BaseClient {
334
374
  callerSignal?.removeEventListener("abort", forward);
335
375
  }
336
376
  }
337
- async fetchResponse(method, path, body, signal) {
377
+ async fetchResponse(method, path, body, signal, extraHeaders) {
338
378
  const url = `${this.baseUrl}${path}`;
339
- const headers = BaseClient.authHeaders(this.apiKey);
379
+ const headers = {
380
+ ...BaseClient.authHeaders(this.apiKey),
381
+ ...extraHeaders
382
+ };
340
383
  headers["x-sl-origin"] = this.origin;
341
384
  let serializedBody;
342
385
  if (body !== undefined && body !== null) {
@@ -361,6 +404,8 @@ class BaseClient {
361
404
  throw err;
362
405
  throw new ApiError(0, `Cannot reach API at ${this.baseUrl}. Check your connection or try again.`, undefined, undefined, { retryable: true, cause: err instanceof Error ? err : undefined });
363
406
  }
407
+ if (response.status === 304)
408
+ return response;
364
409
  if (!response.ok) {
365
410
  const errorBody = signal ? await raceAbort(response.text(), signal) : await response.text();
366
411
  const envelope = parseErrorEnvelope(errorBody);
@@ -1313,6 +1358,7 @@ function createStreamsClient(options) {
1313
1358
  asset_identifier: params.assetIdentifier,
1314
1359
  types: params.types,
1315
1360
  not_types: params.notTypes,
1361
+ clock: params.clock === "vm" ? "vm" : undefined,
1316
1362
  filters: encodeFilters(params.filters)
1317
1363
  })}`);
1318
1364
  }
@@ -1507,5 +1553,5 @@ export {
1507
1553
  AuthError
1508
1554
  };
1509
1555
 
1510
- //# debugId=7D4A6263BDC781FF64756E2164756E21
1556
+ //# debugId=A6328417CAD772A964756E2164756E21
1511
1557
  //# sourceMappingURL=index.js.map