@secondlayer/subgraphs 4.4.1 → 5.0.1

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 (65) hide show
  1. package/README.md +13 -21
  2. package/dist/{src/index.d.ts → index.d.ts} +59 -189
  3. package/dist/index.js +2151 -0
  4. package/dist/index.js.map +24 -0
  5. package/dist/{src/runtime → runtime}/emitter.d.ts +17 -11
  6. package/dist/{src/runtime → runtime}/emitter.js +58 -18
  7. package/dist/runtime/emitter.js.map +19 -0
  8. package/dist/{src/runtime → runtime}/replay.d.ts +15 -1
  9. package/dist/{src/runtime → runtime}/replay.js +418 -723
  10. package/dist/{src/runtime → runtime}/replay.js.map +6 -6
  11. package/dist/{src/schema → schema}/index.d.ts +10 -69
  12. package/dist/{src/schema → schema}/index.js +188 -281
  13. package/dist/schema/index.js.map +13 -0
  14. package/dist/{src/testing → testing}/index.d.ts +65 -115
  15. package/dist/{src/testing → testing}/index.js +554 -968
  16. package/dist/testing/index.js.map +21 -0
  17. package/dist/{src/types.d.ts → types.d.ts} +10 -66
  18. package/dist/{src/validate.d.ts → validate.d.ts} +11 -69
  19. package/dist/{src/validate.js → validate.js} +18 -98
  20. package/dist/validate.js.map +10 -0
  21. package/package.json +18 -26
  22. package/dist/src/index.js +0 -6081
  23. package/dist/src/index.js.map +0 -44
  24. package/dist/src/runtime/block-processor.d.ts +0 -559
  25. package/dist/src/runtime/block-processor.js +0 -3810
  26. package/dist/src/runtime/block-processor.js.map +0 -31
  27. package/dist/src/runtime/catchup.d.ts +0 -473
  28. package/dist/src/runtime/catchup.js +0 -4228
  29. package/dist/src/runtime/catchup.js.map +0 -35
  30. package/dist/src/runtime/clarity.d.ts +0 -15
  31. package/dist/src/runtime/clarity.js +0 -41
  32. package/dist/src/runtime/clarity.js.map +0 -10
  33. package/dist/src/runtime/context.d.ts +0 -325
  34. package/dist/src/runtime/context.js +0 -936
  35. package/dist/src/runtime/context.js.map +0 -13
  36. package/dist/src/runtime/emitter.js.map +0 -19
  37. package/dist/src/runtime/processor.d.ts +0 -11
  38. package/dist/src/runtime/processor.js +0 -5372
  39. package/dist/src/runtime/processor.js.map +0 -40
  40. package/dist/src/runtime/reindex.d.ts +0 -539
  41. package/dist/src/runtime/reindex.js +0 -4802
  42. package/dist/src/runtime/reindex.js.map +0 -37
  43. package/dist/src/runtime/reorg.d.ts +0 -432
  44. package/dist/src/runtime/reorg.js +0 -4355
  45. package/dist/src/runtime/reorg.js.map +0 -36
  46. package/dist/src/runtime/runner.d.ts +0 -719
  47. package/dist/src/runtime/runner.js +0 -1332
  48. package/dist/src/runtime/runner.js.map +0 -16
  49. package/dist/src/runtime/source-matcher.d.ts +0 -304
  50. package/dist/src/runtime/source-matcher.js +0 -335
  51. package/dist/src/runtime/source-matcher.js.map +0 -10
  52. package/dist/src/runtime/stats.d.ts +0 -23
  53. package/dist/src/runtime/stats.js +0 -71
  54. package/dist/src/runtime/stats.js.map +0 -10
  55. package/dist/src/schema/index.js.map +0 -13
  56. package/dist/src/service.d.ts +0 -0
  57. package/dist/src/service.js +0 -5398
  58. package/dist/src/service.js.map +0 -41
  59. package/dist/src/testing/index.js.map +0 -22
  60. package/dist/src/triggers/index.d.ts +0 -381
  61. package/dist/src/triggers/index.js +0 -31
  62. package/dist/src/triggers/index.js.map +0 -10
  63. package/dist/src/validate.js.map +0 -10
  64. /package/dist/{src/types.js → types.js} +0 -0
  65. /package/dist/{src/types.js.map → types.js.map} +0 -0
@@ -39,22 +39,6 @@ interface SubgraphColumn {
39
39
  default?: string | number | boolean;
40
40
  }
41
41
  /**
42
- * A foreign-key relation to another table in the same subgraph. Drives DDL FK
43
- * constraints and ORM codegen (`@relation` in Prisma, `relations()` in Drizzle)
44
- * so generated clients get typed joins. The referenced columns must form a
45
- * `uniqueKeys` entry on the target table.
46
- */
47
- interface SubgraphRelation {
48
- /** Relation field name on this table's generated model (e.g. "pool"). */
49
- name: string;
50
- /** Target table name in this subgraph. */
51
- references: string;
52
- /** Local column(s) holding the foreign key. */
53
- fields: string[];
54
- /** Target column(s) the fields point at (a uniqueKeys entry on the target). */
55
- referencedColumns: string[];
56
- }
57
- /**
58
42
  * Table definition within a subgraph schema.
59
43
  *
60
44
  * The array fields are `readonly` so a schema literal can be hoisted out of
@@ -70,8 +54,6 @@ interface SubgraphTable {
70
54
  indexes?: readonly (readonly string[])[];
71
55
  /** Unique key constraints (each entry is an array of column names). Required for upsert. */
72
56
  uniqueKeys?: readonly (readonly string[])[];
73
- /** Foreign-key relations to other tables (for typed ORM joins). */
74
- relations?: readonly SubgraphRelation[];
75
57
  }
76
58
  /** Subgraph schema — maps table names to table definitions */
77
59
  type SubgraphSchema = Record<string, SubgraphTable>;
@@ -193,9 +175,8 @@ interface ContractCallFilter extends TraitScope, FactoryScope {
193
175
  /**
194
176
  * One contract id, or a SET of them (max 20) — the standard DeFi shape is
195
177
  * a router plus N pools, which used to cost N near-identical sources and
196
- * handlers, or the `"*"` catch-all that erases per-source typing. Payload
197
- * types are identical per `type`, so one handler serves the set and
198
- * `event.contractId` disambiguates.
178
+ * handlers. Payload types are identical per `type`, so one handler serves
179
+ * the set and `event.contractId` disambiguates.
199
180
  *
200
181
  * Supports `*` wildcards (e.g. `"SP1ABC….pool-*"`). Mutually exclusive
201
182
  * with `trait`, which resolves its own set per block.
@@ -209,8 +190,11 @@ interface ContractCallFilter extends TraitScope, FactoryScope {
209
190
  * definition and used at runtime to decode args by name. Omit to keep
210
191
  * `event.args` as a positional `unknown[]`.
211
192
  *
212
- * Deploy validation requires `abi` whenever `functionName` is set
213
- * (`normalizeAbi()` from `@secondlayer/stacks/clarity`).
193
+ * Required whenever `functionName` is set. `secondlayer subgraphs deploy`
194
+ * fills it in from the deployed contract for a single concrete
195
+ * `contractId`; pass it yourself for wildcard, multi-contract or trait
196
+ * sources, or a local deploy (`normalizeAbi()` from
197
+ * `@secondlayer/stacks/clarity`).
214
198
  */
215
199
  abi?: AbiContract2;
216
200
  }
@@ -267,29 +251,9 @@ type PrintField = ColumnType | {
267
251
  * is skipped and logged rather than written as nulls.
268
252
  */
269
253
  type PrintEventPrints = Record<string, Record<string, PrintField>>;
270
- /**
271
- * One column in a static identity map. Literals only — the bundler never
272
- * executes user code, so `{ from: "tokenX" }` is valid and `from("tokenX")`
273
- * is not.
274
- */
275
- type MaterializeColumn = {
276
- from: string
277
- } | {
278
- fromTx: "sender" | "txId"
279
- } | {
280
- fromBlock: "height" | "hash" | "timestamp"
281
- };
282
- /** Static insert from a source event — replaces a 1:1 handler. */
283
- interface MaterializeSpec {
284
- table: string;
285
- /** Keys = schema column names (snake). */
286
- columns: Record<string, MaterializeColumn>;
287
- }
288
254
  type PrintEventFields = FactoryScope & {
289
255
  type: "print_event"
290
256
  topic?: string
291
- /** Identity map: insert without a handler function. XOR with handlers[name]. */
292
- materialize?: MaterializeSpec
293
257
  };
294
258
  /**
295
259
  * Pinned to one or more contract ids with no trait — `prints` is required so
@@ -333,10 +297,6 @@ interface TxMeta {
333
297
  contractId?: string | null;
334
298
  functionName?: string | null;
335
299
  }
336
- /** Scalar types that can appear as row values */
337
- type RowValue = string | number | bigint | boolean | null | undefined | Record<string, unknown> | unknown[];
338
- /** Value or computed function that receives existing row */
339
- type ComputedValue = RowValue | ((existing: Record<string, unknown> | null) => unknown);
340
300
  /** Context passed to subgraph handlers during event processing */
341
301
  interface SubgraphContext {
342
302
  block: {
@@ -367,27 +327,11 @@ interface SubgraphContext {
367
327
  * Atomic counter update — the blessed accumulator primitive. Applies
368
328
  * `col = COALESCE(col, 0) + delta` per column (insert-or-add); deltas may
369
329
  * be negative. Requires a uniqueKeys constraint matching `key`. Prefer
370
- * this over patchOrInsert with functional updaters for running totals.
330
+ * this over read-modify-write handlers for running totals.
371
331
  */
372
332
  increment(table: string, key: Record<string, unknown>, deltas: Record<string, bigint | number>): void;
373
- /** Partial update — sets only specified fields, preserves others */
374
- patch(table: string, where: Record<string, unknown>, set: Record<string, unknown>): void;
375
- /** Find-then-merge-or-insert. Values can be functions: (existing) => newValue */
376
- patchOrInsert(table: string, key: Record<string, unknown>, row: Record<string, ComputedValue>): Promise<void>;
377
333
  findOne(table: string, where: Record<string, unknown>): Promise<Record<string, unknown> | null>;
378
334
  findMany(table: string, where: Record<string, unknown>): Promise<Record<string, unknown>[]>;
379
- /** Format a bigint amount with decimal places */
380
- formatUnits(value: bigint, decimals: number): string;
381
- /** Count rows matching filter */
382
- count(table: string, where?: Record<string, unknown>): Promise<number>;
383
- /** Sum a numeric column */
384
- sum(table: string, column: string, where?: Record<string, unknown>): Promise<bigint>;
385
- /** Min of a numeric column */
386
- min(table: string, column: string, where?: Record<string, unknown>): Promise<bigint | null>;
387
- /** Max of a numeric column */
388
- max(table: string, column: string, where?: Record<string, unknown>): Promise<bigint | null>;
389
- /** Count distinct values in a column */
390
- countDistinct(table: string, column: string, where?: Record<string, unknown>): Promise<number>;
391
335
  }
392
336
  /** Handler function that processes events and writes to the subgraph */
393
337
  type SubgraphHandler = (event: Record<string, unknown>, ctx: SubgraphContext) => Promise<void> | void;
@@ -395,8 +339,6 @@ type SubgraphHandler = (event: Record<string, unknown>, ctx: SubgraphContext) =>
395
339
  interface SubgraphDefinition {
396
340
  /** Unique subgraph name (lowercase, alphanumeric + hyphens) */
397
341
  name: string;
398
- /** Semantic version */
399
- version?: string;
400
342
  /** Human description */
401
343
  description?: string;
402
344
  /** Block height to start indexing from (default: 1) */
@@ -408,7 +350,7 @@ interface SubgraphDefinition {
408
350
  sources: Record<string, SubgraphFilter>;
409
351
  /** Tables in this subgraph */
410
352
  schema: SubgraphSchema;
411
- /** Handler functions — keys must match source names (or "*" for catch-all) */
353
+ /** Handler functions, one per source, keyed by source name */
412
354
  handlers: Record<string, SubgraphHandler>;
413
355
  }
414
356
  declare const TYPE_MAP: Record<ColumnType, string>;
@@ -484,7 +426,6 @@ declare function deploySchema(db: AnyDb, def: SubgraphDefinition, handlerPath: s
484
426
  apiKeyId?: string
485
427
  accountId?: string
486
428
  schemaName?: string
487
- version?: string
488
429
  handlerCode?: string
489
430
  sourceCode?: string
490
431
  }): Promise<{
@@ -495,4 +436,4 @@ declare function deploySchema(db: AnyDb, def: SubgraphDefinition, handlerPath: s
495
436
  /** Non-fatal caveats the caller should surface (e.g. handler grafts at tip). */
496
437
  warnings?: string[]
497
438
  }>;
498
- export { generateSubgraphSQL, diffSchema, deploySchema, TableDiff, TYPE_MAP, GeneratedSQL, ColumnDiff };
439
+ export { ColumnDiff, GeneratedSQL, TYPE_MAP, TableDiff, deploySchema, diffSchema, generateSubgraphSQL };