@secondlayer/subgraphs 3.1.0 → 3.2.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Ryan Waits
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the “Software”), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -1,3 +1,4 @@
1
+ import { AbiContract } from "@secondlayer/stacks/clarity";
1
2
  /** Supported column types for subgraph schemas */
2
3
  type ColumnType = "text" | "uint" | "int" | "principal" | "boolean" | "timestamp" | "jsonb";
3
4
  /** Column definition in a subgraph table */
@@ -125,8 +126,13 @@ interface ContractCallFilter {
125
126
  contractId?: string;
126
127
  functionName?: string;
127
128
  caller?: string;
128
- /** ABI for typed event.args. If omitted, auto-fetched at deploy time. */
129
- abi?: Record<string, unknown>;
129
+ /**
130
+ * Contract ABI (pass it `as const`) used to type `event.input` — the named,
131
+ * decoded function arguments. Dev-provided; serialized into the deployed
132
+ * definition and used at runtime to decode args by name. Omit to keep
133
+ * `event.args` as a positional `unknown[]`.
134
+ */
135
+ abi?: AbiContract;
130
136
  }
131
137
  interface ContractDeployFilter {
132
138
  type: "contract_deploy";
@@ -254,6 +260,7 @@ declare function backfillSubgraph(def: SubgraphDefinition, opts: {
254
260
  }): Promise<{
255
261
  processed: number
256
262
  }>;
263
+ import { AbiContract as AbiContract2, ExtractFunctionArgs, ExtractFunctionNames } from "@secondlayer/stacks/clarity";
257
264
  /** Maps a ColumnType string literal to its TypeScript equivalent */
258
265
  type ColumnToTS<T extends string> = T extends "uint" | "int" ? bigint : T extends "text" | "principal" | "timestamp" ? string : T extends "boolean" ? boolean : T extends "jsonb" ? Record<string, unknown> : unknown;
259
266
  /** Infer TS type for a single column definition, respecting nullable */
@@ -440,6 +447,18 @@ interface ContractDeployPayload {
440
447
  tx: TxMeta;
441
448
  }
442
449
  /**
450
+ * Contract-call payload. When the source carries a `const` `abi` and a known
451
+ * `functionName`, `event.input` is the named, typed, decoded arguments
452
+ * (camelCased, via the contract ABI). The positional `event.args` is always
453
+ * present for back-compat. Without an `abi`, the payload is {@link ContractCallEvent}.
454
+ */
455
+ type ContractCallPayload<F> = F extends {
456
+ abi: infer A extends AbiContract2
457
+ functionName: infer N extends string
458
+ } ? N extends ExtractFunctionNames<A> ? ContractCallEvent & {
459
+ input: ExtractFunctionArgs<A, N>
460
+ } : ContractCallEvent : ContractCallEvent;
461
+ /**
443
462
  * Print event typed per topic. When a `print_event` source declares a `prints`
444
463
  * map, the payload is a discriminated union keyed by `topic` with `data` typed
445
464
  * per topic; otherwise it falls back to the untyped {@link PrintEventPayload}.
@@ -477,7 +496,7 @@ type EventForFilter<F extends SubgraphFilter> = F extends {
477
496
  type: "stx_lock"
478
497
  } ? StxLockPayload : F extends {
479
498
  type: "contract_call"
480
- } ? ContractCallEvent : F extends {
499
+ } ? ContractCallPayload<F> : F extends {
481
500
  type: "contract_deploy"
482
501
  } ? ContractDeployPayload : never;
483
502
  /** Union of every event payload — the `"*"` catch-all handler receives this. */
@@ -588,4 +607,4 @@ declare function deploySchema(db: AnyDb, def: SubgraphDefinition, handlerPath: s
588
607
  version: string
589
608
  diff?: DeployDiff
590
609
  }>;
591
- export { validateSubgraphDefinition, resumeReindex, reindexSubgraph, pgSchemaName, generateSubgraphSQL, diffSchema, deploySchema, defineSubgraph, backfillSubgraph, WriteRow, WhereInput, TypedSubgraphDefinition, TypedSubgraphContext, TypedHandlers, TxMeta, TableDiff, SystemRow, SubgraphTableClient, SubgraphTable, SubgraphSchema, SubgraphHandler, SubgraphFilter, SubgraphDefinition, SubgraphContext, SubgraphColumn, StxTransferPayload, StxTransferFilter, StxMintPayload, StxMintFilter, StxLockPayload, StxLockFilter, StxBurnPayload, StxBurnFilter, RowValue, ReindexOptions, PrintEventPayload, PrintEventFor, PrintEventFilter, NftTransferPayload, NftTransferFilter, NftMintPayload, NftMintFilter, NftBurnPayload, NftBurnFilter, InferTableRow, InferSubgraphClient, InferColumnType, GeneratedSQL, FtTransferPayload, FtTransferFilter, FtMintPayload, FtMintFilter, FtBurnPayload, FtBurnFilter, FindManyOptions, EventForFilter, ContractDeployPayload, ContractDeployFilter, ContractCallFilter, ContractCallEvent, ComputedValue, ComparisonFilter, ColumnType, ColumnToTS, ColumnDiff, AnyEvent };
610
+ export { validateSubgraphDefinition, resumeReindex, reindexSubgraph, pgSchemaName, generateSubgraphSQL, diffSchema, deploySchema, defineSubgraph, backfillSubgraph, WriteRow, WhereInput, TypedSubgraphDefinition, TypedSubgraphContext, TypedHandlers, TxMeta, TableDiff, SystemRow, SubgraphTableClient, SubgraphTable, SubgraphSchema, SubgraphHandler, SubgraphFilter, SubgraphDefinition, SubgraphContext, SubgraphColumn, StxTransferPayload, StxTransferFilter, StxMintPayload, StxMintFilter, StxLockPayload, StxLockFilter, StxBurnPayload, StxBurnFilter, RowValue, ReindexOptions, PrintEventPayload, PrintEventFor, PrintEventFilter, NftTransferPayload, NftTransferFilter, NftMintPayload, NftMintFilter, NftBurnPayload, NftBurnFilter, InferTableRow, InferSubgraphClient, InferColumnType, GeneratedSQL, FtTransferPayload, FtTransferFilter, FtMintPayload, FtMintFilter, FtBurnPayload, FtBurnFilter, FindManyOptions, EventForFilter, ContractDeployPayload, ContractDeployFilter, ContractCallPayload, ContractCallFilter, ContractCallEvent, ComputedValue, ComparisonFilter, ColumnType, ColumnToTS, ColumnDiff, AnyEvent };
package/dist/src/index.js CHANGED
@@ -421,6 +421,11 @@ function decodeFunctionArgs(args) {
421
421
  // src/runtime/runner.ts
422
422
  import { getErrorMessage } from "@secondlayer/shared";
423
423
  import { logger as logger2 } from "@secondlayer/shared/logger";
424
+ import {
425
+ clarityValueToJS,
426
+ deserializeCV as deserializeCV2,
427
+ toCamelCase
428
+ } from "@secondlayer/stacks/clarity";
424
429
  var DEFAULT_ERROR_THRESHOLD = 50;
425
430
  function camelCase(str) {
426
431
  return str.replace(/-([a-z0-9])/g, (_, c) => c.toUpperCase());
@@ -475,6 +480,37 @@ function safeBigInt(val) {
475
480
  }
476
481
  return 0n;
477
482
  }
483
+ function buildContractCallInput(filter, tx) {
484
+ const abi = filter.abi;
485
+ const fnName = tx.function_name;
486
+ if (!abi || !fnName)
487
+ return;
488
+ const fn = abi.functions?.find((f) => f.name === fnName);
489
+ if (!fn || !Array.isArray(fn.args))
490
+ return;
491
+ let rawArgs = tx.function_args;
492
+ if (typeof rawArgs === "string") {
493
+ try {
494
+ rawArgs = JSON.parse(rawArgs);
495
+ } catch {
496
+ return;
497
+ }
498
+ }
499
+ if (!Array.isArray(rawArgs))
500
+ return;
501
+ const decodeArg = clarityValueToJS;
502
+ const input = {};
503
+ fn.args.forEach((arg, i) => {
504
+ const hex = rawArgs[i];
505
+ if (typeof hex !== "string")
506
+ return;
507
+ try {
508
+ const clean = hex.startsWith("0x") ? hex.slice(2) : hex;
509
+ input[toCamelCase(arg.name)] = decodeArg(arg.type, deserializeCV2(clean));
510
+ } catch {}
511
+ });
512
+ return input;
513
+ }
478
514
  function buildEventPayload(filter, tx, event) {
479
515
  const txMeta = {
480
516
  txId: tx.tx_id,
@@ -488,17 +524,20 @@ function buildEventPayload(filter, tx, event) {
488
524
  const decodedResult = decodeRawResult(tx.raw_result);
489
525
  if (!event) {
490
526
  switch (filter.type) {
491
- case "contract_call":
527
+ case "contract_call": {
528
+ const input = buildContractCallInput(filter, tx);
492
529
  return {
493
530
  type: "contract_call",
494
531
  contractId: tx.contract_id ?? "",
495
532
  functionName: tx.function_name ?? "",
496
533
  sender: tx.sender,
497
534
  args: decodedArgs,
535
+ ...input !== undefined ? { input } : {},
498
536
  result: decodedResult,
499
537
  resultHex: tx.raw_result ?? null,
500
538
  tx: txMeta
501
539
  };
540
+ }
502
541
  case "contract_deploy":
503
542
  return {
504
543
  contractId: tx.contract_id ?? "",
@@ -596,7 +635,8 @@ function buildEventPayload(filter, tx, event) {
596
635
  tx: txMeta
597
636
  };
598
637
  }
599
- case "contract_call":
638
+ case "contract_call": {
639
+ const input = buildContractCallInput(filter, tx);
600
640
  return {
601
641
  ...decoded,
602
642
  type: "contract_call",
@@ -605,10 +645,12 @@ function buildEventPayload(filter, tx, event) {
605
645
  functionName: tx.function_name ?? "",
606
646
  sender: tx.sender,
607
647
  args: decodedArgs,
648
+ ...input !== undefined ? { input } : {},
608
649
  result: decodedResult,
609
650
  resultHex: tx.raw_result ?? null,
610
651
  tx: txMeta
611
652
  };
653
+ }
612
654
  case "contract_deploy":
613
655
  return {
614
656
  contractId: tx.contract_id ?? "",
@@ -2176,5 +2218,5 @@ export {
2176
2218
  backfillSubgraph
2177
2219
  };
2178
2220
 
2179
- //# debugId=E831E58EC98F118E64756E2164756E21
2221
+ //# debugId=E652DFA8BE73568E64756E2164756E21
2180
2222
  //# sourceMappingURL=index.js.map