@secondlayer/subgraphs 4.1.5 → 4.2.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.
Files changed (37) hide show
  1. package/dist/src/index.d.ts +87 -29
  2. package/dist/src/index.js +376 -128
  3. package/dist/src/index.js.map +9 -7
  4. package/dist/src/runtime/block-processor.d.ts +72 -23
  5. package/dist/src/runtime/block-processor.js +186 -53
  6. package/dist/src/runtime/block-processor.js.map +7 -5
  7. package/dist/src/runtime/catchup.d.ts +72 -23
  8. package/dist/src/runtime/catchup.js +201 -68
  9. package/dist/src/runtime/catchup.js.map +7 -5
  10. package/dist/src/runtime/processor.js +281 -147
  11. package/dist/src/runtime/processor.js.map +8 -6
  12. package/dist/src/runtime/reindex.d.ts +72 -23
  13. package/dist/src/runtime/reindex.js +238 -105
  14. package/dist/src/runtime/reindex.js.map +7 -5
  15. package/dist/src/runtime/reorg.d.ts +72 -23
  16. package/dist/src/runtime/reorg.js +215 -81
  17. package/dist/src/runtime/reorg.js.map +8 -6
  18. package/dist/src/runtime/runner.d.ts +72 -23
  19. package/dist/src/runtime/runner.js +156 -23
  20. package/dist/src/runtime/runner.js.map +8 -6
  21. package/dist/src/runtime/source-matcher.d.ts +72 -23
  22. package/dist/src/schema/index.d.ts +72 -23
  23. package/dist/src/schema/index.js +115 -1
  24. package/dist/src/schema/index.js.map +3 -3
  25. package/dist/src/service.js +286 -152
  26. package/dist/src/service.js.map +8 -6
  27. package/dist/src/testing/index.d.ts +195 -24
  28. package/dist/src/testing/index.js +448 -25
  29. package/dist/src/testing/index.js.map +9 -6
  30. package/dist/src/triggers/index.d.ts +83 -24
  31. package/dist/src/triggers/index.js +5 -2
  32. package/dist/src/triggers/index.js.map +3 -3
  33. package/dist/src/types.d.ts +73 -24
  34. package/dist/src/validate.d.ts +72 -23
  35. package/dist/src/validate.js +115 -1
  36. package/dist/src/validate.js.map +3 -3
  37. package/package.json +2 -2
@@ -190,7 +190,12 @@ type TraitScope = {
190
190
  *
191
191
  * ```ts
192
192
  * sources: {
193
- * registry: { type: "print_event", contractId: REGISTRY, topic: "pool-created" },
193
+ * registry: {
194
+ * type: "print_event",
195
+ * contractId: REGISTRY,
196
+ * topic: "pool-created",
197
+ * prints: { "pool-created": { pool: "principal" } },
198
+ * },
194
199
  * swaps: {
195
200
  * type: "print_event",
196
201
  * topic: "swap",
@@ -315,6 +320,9 @@ interface ContractCallFilter extends TraitScope, FactoryScope {
315
320
  * decoded function arguments. Dev-provided; serialized into the deployed
316
321
  * definition and used at runtime to decode args by name. Omit to keep
317
322
  * `event.args` as a positional `unknown[]`.
323
+ *
324
+ * Deploy validation requires `abi` whenever `functionName` is set
325
+ * (`normalizeAbi()` from `@secondlayer/stacks/clarity`).
318
326
  */
319
327
  abi?: AbiContract2;
320
328
  }
@@ -345,29 +353,70 @@ type PrintField = ColumnType | {
345
353
  } | {
346
354
  list: PrintField
347
355
  };
348
- interface PrintEventFilter extends TraitScope, FactoryScope {
349
- type: "print_event";
350
- /** One contract id, or a set of them (max 20). Supports `*` wildcards.
351
- * COMPOSES with `trait` — the matcher ANDs them, so the pair means
352
- * "contracts conforming to this trait, narrowed to these ids". (The Index
353
- * read API refuses the combination, but that is its query planner's
354
- * constraint, not a semantic one.) */
355
- contractId?: string | readonly string[];
356
- topic?: string;
357
- /**
358
- * Per-topic field schema. When declared, the handler's `event` is a
359
- * discriminated union keyed by `topic` and `event.data` is typed per topic
360
- * (e.g. `{ "completed-deposit": { amount: "uint", sender: "principal" } }`).
361
- * Nested tuples, lists, and optional fields are expressible — see
362
- * {@link PrintField}.
363
- *
364
- * Declaring `prints` also opts the source into RUNTIME validation: an
365
- * event whose decoded payload does not match is skipped and logged rather
366
- * than written as nulls (and the deploy-time field lint is promoted from
367
- * a warning to an error).
368
- */
369
- prints?: Record<string, Record<string, PrintField>>;
356
+ /**
357
+ * Per-topic field schema for a print_event source. When declared, the handler's
358
+ * `event` is a discriminated union keyed by `topic` and `event.data` is typed
359
+ * per topic. Nested tuples, lists, and optional fields — see {@link PrintField}.
360
+ *
361
+ * Also opts the source into RUNTIME validation: a payload that does not match
362
+ * is skipped and logged rather than written as nulls.
363
+ */
364
+ type PrintEventPrints = Record<string, Record<string, PrintField>>;
365
+ /**
366
+ * One column in a static identity map. Literals only — the bundler never
367
+ * executes user code, so `{ from: "tokenX" }` is valid and `from("tokenX")`
368
+ * is not.
369
+ */
370
+ type MaterializeColumn = {
371
+ from: string
372
+ } | {
373
+ fromTx: "sender" | "txId"
374
+ } | {
375
+ fromBlock: "height" | "hash" | "timestamp"
376
+ };
377
+ /** Static insert from a source event — replaces a 1:1 handler. */
378
+ interface MaterializeSpec {
379
+ table: string;
380
+ /** Keys = schema column names (snake). */
381
+ columns: Record<string, MaterializeColumn>;
370
382
  }
383
+ type PrintEventFields = FactoryScope & {
384
+ type: "print_event"
385
+ topic?: string
386
+ /** Identity map: insert without a handler function. XOR with handlers[name]. */
387
+ materialize?: MaterializeSpec
388
+ };
389
+ /**
390
+ * Pinned to one or more contract ids with no trait — `prints` is required so
391
+ * `event.data` is typed and empty-table handlers cannot typecheck.
392
+ */
393
+ type PinnedPrintEventFilter = PrintEventFields & {
394
+ contractId: string | readonly string[]
395
+ trait?: undefined
396
+ prints: PrintEventPrints
397
+ };
398
+ /**
399
+ * Trait-scoped print source (optionally narrowed by contractId). No single
400
+ * schema — `prints` stays optional.
401
+ */
402
+ type TraitPrintEventFilter = PrintEventFields & {
403
+ contractId?: string | readonly string[]
404
+ trait: string
405
+ prints?: PrintEventPrints
406
+ };
407
+ /**
408
+ * Unpinned / factory-only print source (no contractId). `prints` optional.
409
+ */
410
+ type UnpinnedPrintEventFilter = PrintEventFields & {
411
+ contractId?: undefined
412
+ trait?: string
413
+ prints?: PrintEventPrints
414
+ };
415
+ /**
416
+ * Print-event filter. Pinned sources (contractId set, no trait) require
417
+ * `prints`; trait and unpinned sources may omit it.
418
+ */
419
+ type PrintEventFilter = PinnedPrintEventFilter | TraitPrintEventFilter | UnpinnedPrintEventFilter;
371
420
  /** All subgraph filter types — discriminated on `type` */
372
421
  type SubgraphFilter = StxTransferFilter | StxMintFilter | StxBurnFilter | StxLockFilter | FtTransferFilter | FtMintFilter | FtBurnFilter | NftTransferFilter | NftMintFilter | NftBurnFilter | ContractCallFilter | ContractDeployFilter | PrintEventFilter;
373
422
  /** Transaction metadata available in handlers */
@@ -909,19 +958,25 @@ type EventForFilter<F extends SubgraphFilter> = F extends {
909
958
  } ? ContractDeployPayload : never;
910
959
  /** Union of every event payload — the `"*"` catch-all handler receives this. */
911
960
  type AnyEvent = FtTransferPayload | FtMintPayload | FtBurnPayload | NftTransferPayload | NftMintPayload | NftBurnPayload | StxTransferPayload | StxMintPayload | StxBurnPayload | StxLockPayload | PrintEventPayload | ContractCallEvent | ContractDeployPayload;
961
+ type HandlerFn<
962
+ F extends SubgraphFilter,
963
+ S extends SubgraphSchema
964
+ > = (event: EventForFilter<F>, ctx: TypedSubgraphContext<S>) => void | Promise<void>;
965
+ type SourceHasMaterialize<F> = F extends {
966
+ materialize: MaterializeSpec
967
+ } ? true : false;
912
968
  /**
913
969
  * Handlers keyed by source name. Each handler's `event` is typed from the
914
970
  * matching source's filter `type` (e.g. a `print_event` source → `event.topic`
915
971
  * is a `string`), and `ctx` is typed against the subgraph `schema` (table
916
- * names + row columns checked). The optional `"*"` catch-all receives any event.
917
- *
918
- * Handlers are optional per source (a source with no handler is skipped at
919
- * runtime), matching `handlers[name] ?? handlers["*"]` resolution.
972
+ * names + row columns checked). Sources with `materialize` may omit a
973
+ * handler; every other source key is required. Optional `"*"` catch-all
974
+ * receives any event (validate also accepts `*` alone).
920
975
  */
921
976
  type TypedHandlers<
922
977
  Sources extends Record<string, SubgraphFilter>,
923
978
  S extends SubgraphSchema
924
- > = { [K in keyof Sources]? : (event: EventForFilter<Sources[K]>, ctx: TypedSubgraphContext<S>) => void | Promise<void> } & {
979
+ > = { [K in keyof Sources as SourceHasMaterialize<Sources[K]> extends true ? never : K]-? : HandlerFn<Sources[K], S> } & { [K in keyof Sources as SourceHasMaterialize<Sources[K]> extends true ? K : never]? : HandlerFn<Sources[K], S> } & {
925
980
  "*"?: (event: AnyEvent, ctx: TypedSubgraphContext<S>) => void | Promise<void>
926
981
  };
927
982
  /** Subgraph definition with `sources`/`schema` literals preserved for typed
@@ -1052,6 +1107,9 @@ interface InferredTopicSchema {
1052
1107
  * `transfer-STX` here, becomes `transferSTX` there).
1053
1108
  */
1054
1109
  declare function camelizeDataKey(str: string): string;
1110
+ /** Recursively camelize object keys with the print-payload helper (not ABI
1111
+ * `toCamelCase` — that one strips `-STX` and prefixes leading digits). */
1112
+ declare function camelizeKeys(obj: unknown): unknown;
1055
1113
  /**
1056
1114
  * Infers per-topic field schemas from sampled print events. Counts and
1057
1115
  * height bounds cover ALL rows of a topic (cheap); only a bounded subset is
@@ -1210,4 +1268,4 @@ declare function deploySchema(db: AnyDb, def: SubgraphDefinition, handlerPath: s
1210
1268
  /** Non-fatal caveats the caller should surface (e.g. handler grafts at tip). */
1211
1269
  warnings?: string[]
1212
1270
  }>;
1213
- export { validateSubgraphDefinition, validatePrintPayload, sparseProbeTargets, resumeReindex, renderDeployPlan, reindexSubgraph, readContractAt, pgSchemaName, inferPrintTopics, hasBreakingChanges, generateSubgraphSQL, generatePrismaSchema, generateKyselySchema, generateIndexSchema, generateDrizzleSchema, filterFieldsByType, diffSchema, deploySchema, defineSubgraph, defineSchema, canSparseScan, camelizeDataKey, backfillSubgraph, WriteRow, WhereInput, TypedSubgraphDefinition, TypedSubgraphContext, TypedHandlers, TxMeta, TableDiff, SystemRow, SubscribeOptions, SubgraphTableClient, SubgraphTable, SubgraphSchema, SubgraphHandler, SubgraphFilter, SubgraphDefinition, SubgraphContext, SubgraphColumn, StxTransferPayload, StxTransferFilter, StxMintPayload, StxMintFilter, StxLockPayload, StxLockFilter, StxBurnPayload, StxBurnFilter, SparseProbeTarget, RowValue, ReindexOptions, PrismaGenOptions, PrintValidationResult, PrintSample, PrintFieldToTS, PrintField, PrintEventPayload, PrintEventFor, PrintEventFilter, PrintDataOf, NftTransferPayload, NftTransferFilter, NftMintPayload, NftMintFilter, NftBurnPayload, NftBurnFilter, KyselyGenOptions, InferredTopicSchema, InferredPrintField, InferTableRow, InferSubgraphClient, InferContext, InferColumnType, IndexCodegenTarget, IndexCodegenOptions, INDEX_CODEGEN_TABLES, GeneratedSQL, FtTransferPayload, FtTransferFilter, FtMintPayload, FtMintFilter, FtBurnPayload, FtBurnFilter, FindManyOptions, FactoryScope, EventForFilter, ErasedChainReadClient, DrizzleGenOptions, DeployPlan, DeployDiff, ContractDeployPayload, ContractDeployFilter, ContractCallPayload, ContractCallFilter, ContractCallEvent, ComputedValue, ComparisonFilter, ColumnType, ColumnToTS, ColumnDiff, ChainReadOptions, ChainReadMethods, ChainReadError, ChainReadClient, ChainReadCacheMode, AnyEvent, AggregateSpec, AggregateResult };
1271
+ export { validateSubgraphDefinition, validatePrintPayload, sparseProbeTargets, resumeReindex, renderDeployPlan, reindexSubgraph, readContractAt, pgSchemaName, inferPrintTopics, hasBreakingChanges, generateSubgraphSQL, generatePrismaSchema, generateKyselySchema, generateIndexSchema, generateDrizzleSchema, filterFieldsByType, diffSchema, deploySchema, defineSubgraph, defineSchema, canSparseScan, camelizeKeys, camelizeDataKey, backfillSubgraph, WriteRow, WhereInput, UnpinnedPrintEventFilter, TypedSubgraphDefinition, TypedSubgraphContext, TypedHandlers, TxMeta, TraitPrintEventFilter, TableDiff, SystemRow, SubscribeOptions, SubgraphTableClient, SubgraphTable, SubgraphSchema, SubgraphHandler, SubgraphFilter, SubgraphDefinition, SubgraphContext, SubgraphColumn, StxTransferPayload, StxTransferFilter, StxMintPayload, StxMintFilter, StxLockPayload, StxLockFilter, StxBurnPayload, StxBurnFilter, SparseProbeTarget, RowValue, ReindexOptions, PrismaGenOptions, PrintValidationResult, PrintSample, PrintFieldToTS, PrintField, PrintEventPrints, PrintEventPayload, PrintEventFor, PrintEventFilter, PrintDataOf, PinnedPrintEventFilter, NftTransferPayload, NftTransferFilter, NftMintPayload, NftMintFilter, NftBurnPayload, NftBurnFilter, MaterializeSpec, MaterializeColumn, KyselyGenOptions, InferredTopicSchema, InferredPrintField, InferTableRow, InferSubgraphClient, InferContext, InferColumnType, IndexCodegenTarget, IndexCodegenOptions, INDEX_CODEGEN_TABLES, GeneratedSQL, FtTransferPayload, FtTransferFilter, FtMintPayload, FtMintFilter, FtBurnPayload, FtBurnFilter, FindManyOptions, FactoryScope, EventForFilter, ErasedChainReadClient, DrizzleGenOptions, DeployPlan, DeployDiff, ContractDeployPayload, ContractDeployFilter, ContractCallPayload, ContractCallFilter, ContractCallEvent, ComputedValue, ComparisonFilter, ColumnType, ColumnToTS, ColumnDiff, ChainReadOptions, ChainReadMethods, ChainReadError, ChainReadClient, ChainReadCacheMode, AnyEvent, AggregateSpec, AggregateResult };