@secondlayer/subgraphs 7.0.1 → 7.0.3

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.
@@ -14,6 +14,7 @@ import { AbiContract as AbiContract3, ExtractFunctionArgs as ExtractFunctionArgs
14
14
  * first answer forever, so it is opt-in and never inferred.
15
15
  */
16
16
  type ChainReadCacheMode = "per-block" | "contract-constant";
17
+ /** Options for a contract read, such as cache mode. */
17
18
  type ChainReadOptions = {
18
19
  cache?: ChainReadCacheMode;
19
20
  };
@@ -23,6 +24,12 @@ type ChainReadOptions = {
23
24
  * one and handlers get full inference.
24
25
  */
25
26
  interface ErasedChainReadClient {
27
+ /**
28
+ * Reads a contract's read-only functions at the block being processed.
29
+ *
30
+ * Results are untyped here. Call it through `readContractAt` to get typed
31
+ * results from the contract's ABI.
32
+ */
26
33
  contract(contractId: string, abi: unknown, options?: ChainReadOptions): {
27
34
  read: Record<string, (args?: Record<string, unknown>) => Promise<unknown>>;
28
35
  };
@@ -56,7 +63,7 @@ interface SubgraphTable {
56
63
  /** Unique key constraints (each entry is an array of column names). Required for upsert. */
57
64
  uniqueKeys?: readonly (readonly string[])[];
58
65
  }
59
- /** Subgraph schema — maps table names to table definitions */
66
+ /** Maps table names to table definitions. */
60
67
  type SubgraphSchema = Record<string, SubgraphTable>;
61
68
  /** STX event filters */
62
69
  interface StxTransferFilter {
@@ -328,7 +335,7 @@ type UnpinnedPrintEventFilter = PrintEventFields & {
328
335
  * `prints`; trait and unpinned sources may omit it.
329
336
  */
330
337
  type PrintEventFilter = PinnedPrintEventFilter | TraitPrintEventFilter | UnpinnedPrintEventFilter;
331
- /** All subgraph filter types — discriminated on `type` */
338
+ /** A subgraph filter, discriminated on `type`. */
332
339
  type SubgraphFilter = StxTransferFilter | StxMintFilter | StxBurnFilter | StxLockFilter | FtTransferFilter | FtMintFilter | FtBurnFilter | NftTransferFilter | NftMintFilter | NftBurnFilter | ContractCallFilter | ContractDeployFilter | PrintEventFilter | NestedContractCallFilter | VarSetFilter | MapWriteFilter;
333
340
  /** Transaction metadata available in handlers */
334
341
  interface TxMeta {
@@ -419,21 +426,53 @@ interface TypedSubgraphContext<S extends SubgraphSchema> {
419
426
  * `readContractAt(ctx, id, abi)` for full ABI typing at the call site.
420
427
  */
421
428
  client: ErasedChainReadClient;
429
+ /**
430
+ * Queues a new row for a table.
431
+ *
432
+ * Writes are batched and applied together when the block commits.
433
+ * `_block_height` and `_tx_id` are filled in for you.
434
+ */
422
435
  insert<T extends TableName<S>>(table: T, row: WriteRow<S[T]>): void;
436
+ /**
437
+ * Queues an update to the rows matching `where`.
438
+ *
439
+ * `set` holds only the columns to change.
440
+ */
423
441
  update<T extends TableName<S>>(table: T, where: RowWhere<S, T>, set: Partial<WriteRow<S[T]>>): void;
442
+ /**
443
+ * Inserts a row, or updates the one with the same key.
444
+ *
445
+ * `key` should match a `uniqueKeys` constraint of the table. Without one the
446
+ * write falls back to a lookup first and logs a warning.
447
+ */
424
448
  upsert<
425
449
  T extends TableName<S>,
426
450
  K extends Partial<WriteRow<S[T]>>
427
451
  >(table: T, key: K, row: Omit<WriteRow<S[T]>, keyof K> & Partial<WriteRow<S[T]>>): void;
452
+ /** Queues deletion of the rows matching `where`. */
428
453
  delete<T extends TableName<S>>(table: T, where: RowWhere<S, T>): void;
429
454
  /**
430
- * Atomic accumulator — `INSERT … ON CONFLICT (key) DO UPDATE SET col =
431
- * COALESCE(col,0) + delta`. Deltas commute, so it's the reorg-safe primitive
432
- * for running totals (per-cycle aggregates, balances). Requires a `uniqueKeys`
433
- * constraint matching `key`; deltas may be `number` or `bigint`.
455
+ * Adds signed deltas to a counter row, creating it if missing.
456
+ *
457
+ * It compiles to `INSERT … ON CONFLICT (key) DO UPDATE SET col =
458
+ * COALESCE(col,0) + delta`. Deltas commute, so it is the reorg-safe way to
459
+ * keep running totals such as per-cycle aggregates and balances. It needs a
460
+ * `uniqueKeys` constraint matching `key`, and deltas may be `number` or
461
+ * `bigint`.
434
462
  */
435
463
  increment<T extends TableName<S>>(table: T, key: Partial<WriteRow<S[T]>>, deltas: Partial<Record<ColumnName<S, T>, bigint | number>>): void;
464
+ /**
465
+ * Returns the first row matching `where`, or `null`.
466
+ *
467
+ * Reads see the writes queued earlier in the same block, and rows come back
468
+ * in insertion order.
469
+ */
436
470
  findOne<T extends TableName<S>>(table: T, where: RowWhere<S, T>): Promise<InferTableRow<S[T]> | null>;
471
+ /**
472
+ * Returns every row matching `where`, in insertion order.
473
+ *
474
+ * Reads see the writes queued earlier in the same block.
475
+ */
437
476
  findMany<T extends TableName<S>>(table: T, where: RowWhere<S, T>): Promise<InferTableRow<S[T]>[]>;
438
477
  }
439
478
  interface FtTransferPayload {
@@ -657,6 +696,7 @@ interface MatchedTx {
657
696
  /** Source object key — used for handler dispatch */
658
697
  sourceName: string;
659
698
  }
699
+ /** A transaction as a subgraph source matcher reads it. */
660
700
  type TxRecord = {
661
701
  tx_id: string;
662
702
  type: string;
@@ -669,6 +709,7 @@ type TxRecord = {
669
709
  function_args?: unknown | null;
670
710
  raw_result?: string | null;
671
711
  };
712
+ /** An event as a subgraph source matcher reads it. */
672
713
  type EventRecord = {
673
714
  id: string;
674
715
  tx_id: string;
@@ -707,8 +748,13 @@ interface TestSubgraphContext<S extends SubgraphSchema> extends Omit<TypedSubgra
707
748
  opsCheckpoint(): number;
708
749
  }
709
750
  /**
710
- * Build an in-memory context for a subgraph's schema.
751
+ * Builds an in-memory context for a subgraph's schema.
711
752
  *
753
+ * It is the real handler context with its row store swapped for memory, so
754
+ * reads see earlier writes. Use `ctx.rows(table)` to inspect what a handler
755
+ * wrote.
756
+ *
757
+ * @example
712
758
  * ```ts
713
759
  * const ctx = createTestContext(bns.schema, { block: { height: 167_484 } });
714
760
  * await bns.handlers.bns!(buildEvent(bns.sources.bns, { … }), ctx);
@@ -723,10 +769,12 @@ declare function createTestContext<const S extends SubgraphSchema>(schema: S, op
723
769
  reads?: Record<string, unknown>;
724
770
  }): TestSubgraphContext<S>;
725
771
  /**
726
- * Build a handler-shaped event payload for a source, typed by that source's
727
- * filter. `data` supplies the decoded, camelCased fields the runtime would
728
- * have produced; block/tx metadata is filled in.
772
+ * Builds a handler-shaped event for a source, typed by that source's filter.
773
+ *
774
+ * `data` supplies the decoded, camelCased fields the runtime would have
775
+ * produced. Transaction metadata is filled in.
729
776
  *
777
+ * @example
730
778
  * ```ts
731
779
  * buildEvent(bns.sources.bns, {
732
780
  * topic: "name-register",
@@ -789,6 +837,7 @@ interface EventTrace {
789
837
  keys: string[];
790
838
  }>;
791
839
  }
840
+ /** The outcome of a test run: matches, writes, and table rows. */
792
841
  interface SubgraphTestResult {
793
842
  ok: boolean;
794
843
  code?: "EMPTY_MAPPING" | "NO_EVENTS" | "NO_SOURCES";
@@ -809,6 +858,7 @@ interface SubgraphTestResult {
809
858
  /** Keys present on every matched IN and never written to any OUT column. */
810
859
  unusedInKeys?: string[];
811
860
  }
861
+ /** A subgraph's schema, handlers, sources, and the events to run. */
812
862
  interface RunSubgraphTestInput {
813
863
  schema: SubgraphSchema;
814
864
  handlers: Record<string, unknown>;
@@ -828,6 +878,7 @@ declare function toContractCallPayload(row: IndexContractCallRow): Record<string
828
878
  * with zero written rows is EMPTY_MAPPING (the bns-names field-mapping shape).
829
879
  */
830
880
  declare function runSubgraphTest(input: RunSubgraphTestInput): Promise<SubgraphTestResult>;
881
+ /** A subgraph's schema, sources, and handlers, for a probe run. */
831
882
  interface ProbeHandlersDef {
832
883
  schema: SubgraphSchema;
833
884
  sources: Record<string, {
@@ -836,6 +887,7 @@ interface ProbeHandlersDef {
836
887
  }>;
837
888
  handlers: Record<string, unknown>;
838
889
  }
890
+ /** How many events matched and rows a probe wrote. */
839
891
  interface ProbeHandlersResult {
840
892
  matched: number;
841
893
  written: number;
@@ -1449,7 +1449,7 @@ import { createHash as createHash2 } from "node:crypto";
1449
1449
  // package.json
1450
1450
  var package_default = {
1451
1451
  name: "@secondlayer/subgraphs",
1452
- version: "7.0.1",
1452
+ version: "7.0.3",
1453
1453
  description: "Describe the Stacks data you want as one TypeScript file; it fills itself and survives reorgs.",
1454
1454
  license: "MIT",
1455
1455
  repository: {
@@ -1505,12 +1505,13 @@ var package_default = {
1505
1505
  dev: "bunup --watch",
1506
1506
  test: "bun test",
1507
1507
  typecheck: "tsc --noEmit",
1508
+ openpkg: "bun scripts/sync-openpkg.ts",
1508
1509
  prepublishOnly: "bun run build"
1509
1510
  },
1510
1511
  dependencies: {
1511
- "@secondlayer/shared": "^12.0.0",
1512
- "@secondlayer/stacks": "^6.2.0",
1513
- "@secondlayer/verify": "^0.5.0",
1512
+ "@secondlayer/shared": "^12.1.2",
1513
+ "@secondlayer/stacks": "^6.2.2",
1514
+ "@secondlayer/verify": "^0.5.4",
1514
1515
  esbuild: "^0.28.2",
1515
1516
  kysely: "0.28.17",
1516
1517
  zod: "^4.3.6"
@@ -3076,5 +3077,5 @@ export {
3076
3077
  toHandlerPayload
3077
3078
  };
3078
3079
 
3079
- //# debugId=3C4523A2FFBCFA3864756E2164756E21
3080
+ //# debugId=8313A9F6047DE2E064756E2164756E21
3080
3081
  //# sourceMappingURL=index.js.map