@secondlayer/subgraphs 7.0.4 → 7.1.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,4 @@
1
- import { AbiContract as AbiContract3, ExtractFunctionArgs as ExtractFunctionArgs2, ExtractFunctionNames } from "@secondlayer/stacks/clarity";
1
+ import { AbiContract as AbiContract3, ExtractFunctionArgs as ExtractFunctionArgs2, ExtractFunctionNames, ExtractMapKey, ExtractMapNames, ExtractMapValue, ExtractVariableNames, ExtractVariableType } from "@secondlayer/stacks/clarity";
2
2
  /**
3
3
  * How long a read stays valid.
4
4
  *
@@ -263,11 +263,23 @@ interface VarSetFilter extends TraitScope, FactoryScope {
263
263
  type: "var_set";
264
264
  contractId?: string | readonly string[];
265
265
  varName?: string;
266
+ /**
267
+ * Contract ABI. With a `varName` the abi defines, `event.value` is typed
268
+ * and decoded from the variable's type. Pass it yourself
269
+ * (`normalizeAbi()` from `@secondlayer/stacks/clarity`).
270
+ */
271
+ abi?: AbiContract2;
266
272
  }
267
273
  interface MapWriteFilter extends TraitScope, FactoryScope {
268
274
  type: "map_set" | "map_insert" | "map_delete";
269
275
  contractId?: string | readonly string[];
270
276
  map?: string;
277
+ /**
278
+ * Contract ABI. With a `map` the abi defines, `event.key` and
279
+ * `event.value` are typed and decoded from the map's types. Pass it
280
+ * yourself (`normalizeAbi()` from `@secondlayer/stacks/clarity`).
281
+ */
282
+ abi?: AbiContract2;
271
283
  }
272
284
  /**
273
285
  * One declared field of a print payload.
@@ -578,14 +590,14 @@ interface NestedContractCallPayload {
578
590
  result: unknown;
579
591
  tx: TxMeta;
580
592
  }
581
- interface VarSetPayload {
593
+ interface VarSetEvent {
582
594
  contractId: string;
583
595
  varName: string;
584
596
  rawValue: string;
585
597
  value: unknown;
586
598
  tx: TxMeta;
587
599
  }
588
- interface MapWritePayload {
600
+ interface MapWriteEvent {
589
601
  contractId: string;
590
602
  map: string;
591
603
  rawKey: string;
@@ -594,7 +606,7 @@ interface MapWritePayload {
594
606
  value: unknown;
595
607
  tx: TxMeta;
596
608
  }
597
- interface MapDeletePayload {
609
+ interface MapDeleteEvent {
598
610
  contractId: string;
599
611
  map: string;
600
612
  rawKey: string;
@@ -602,6 +614,31 @@ interface MapDeletePayload {
602
614
  tx: TxMeta;
603
615
  }
604
616
  /**
617
+ * State-write payloads. When the source carries a `const` `abi` and a known
618
+ * `varName` / `map`, `value` (and `key`) are typed from the contract's
619
+ * definition and decoded with it (camelCased tuple keys, `AbiToTS` shapes).
620
+ * Without an `abi`, or with a name the abi lacks, they stay `unknown`.
621
+ */
622
+ type VarSetPayload<F = unknown> = F extends {
623
+ abi: infer A extends AbiContract3;
624
+ varName: infer N extends string;
625
+ } ? N extends ExtractVariableNames<A> ? Omit<VarSetEvent, "value"> & {
626
+ value: ExtractVariableType<A, N>;
627
+ } : VarSetEvent : VarSetEvent;
628
+ type MapWritePayload<F = unknown> = F extends {
629
+ abi: infer A extends AbiContract3;
630
+ map: infer N extends string;
631
+ } ? N extends ExtractMapNames<A> ? Omit<MapWriteEvent, "key" | "value"> & {
632
+ key: ExtractMapKey<A, N>;
633
+ value: ExtractMapValue<A, N>;
634
+ } : MapWriteEvent : MapWriteEvent;
635
+ type MapDeletePayload<F = unknown> = F extends {
636
+ abi: infer A extends AbiContract3;
637
+ map: infer N extends string;
638
+ } ? N extends ExtractMapNames<A> ? Omit<MapDeleteEvent, "key"> & {
639
+ key: ExtractMapKey<A, N>;
640
+ } : MapDeleteEvent : MapDeleteEvent;
641
+ /**
605
642
  * Print event typed per topic. When a `print_event` source declares a `prints`
606
643
  * map, the payload is a discriminated union keyed by `topic` with `data` typed
607
644
  * per topic; otherwise it falls back to the untyped {@link PrintEventPayload}.
@@ -666,9 +703,9 @@ type EventForFilter<F extends SubgraphFilter> = F extends {
666
703
  type: "nested_contract_call";
667
704
  } ? NestedContractCallPayload : F extends {
668
705
  type: "var_set";
669
- } ? VarSetPayload : F extends {
706
+ } ? VarSetPayload<F> : F extends {
670
707
  type: infer T extends "map_set" | "map_insert" | "map_delete";
671
- } ? T extends "map_delete" ? MapDeletePayload : MapWritePayload : never;
708
+ } ? T extends "map_delete" ? MapDeletePayload<F> : MapWritePayload<F> : never;
672
709
  interface BlockMeta {
673
710
  height: number;
674
711
  hash: string;
@@ -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.4",
1452
+ version: "7.1.0",
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: {
@@ -1510,8 +1510,8 @@ var package_default = {
1510
1510
  },
1511
1511
  dependencies: {
1512
1512
  "@secondlayer/shared": "^13.0.0",
1513
- "@secondlayer/stacks": "^6.2.2",
1514
- "@secondlayer/verify": "^0.5.4",
1513
+ "@secondlayer/stacks": "^6.3.0",
1514
+ "@secondlayer/verify": "^0.6.0",
1515
1515
  esbuild: "^0.28.2",
1516
1516
  kysely: "0.28.17",
1517
1517
  zod: "^4.3.6"
@@ -2330,6 +2330,22 @@ function decodeRawHex(hex) {
2330
2330
  }
2331
2331
  return hex;
2332
2332
  }
2333
+ function decodeStateHex(hex, abiType) {
2334
+ if (abiType === undefined || !hex.startsWith("0x") || hex.length <= 2) {
2335
+ return decodeRawHex(hex);
2336
+ }
2337
+ try {
2338
+ const decode = clarityValueToJS;
2339
+ return decode(abiType, deserializeCV4(hex.slice(2)));
2340
+ } catch {
2341
+ return decodeRawHex(hex);
2342
+ }
2343
+ }
2344
+ function mapAbiTypes(filter, name) {
2345
+ const abi = "abi" in filter ? filter.abi : undefined;
2346
+ const def = abi?.maps?.find((m) => m.name === name);
2347
+ return def ? [def.key, def.value] : [];
2348
+ }
2333
2349
  function safeBigInt(val) {
2334
2350
  if (typeof val === "bigint")
2335
2351
  return val;
@@ -2541,11 +2557,14 @@ function buildEventPayload(filter, tx, event) {
2541
2557
  case "var_set": {
2542
2558
  const data = event.data ?? {};
2543
2559
  const rawValue = typeof data.raw_value === "string" ? data.raw_value : "";
2560
+ const varName = typeof data.var_name === "string" ? data.var_name : "";
2561
+ const abi = "abi" in filter ? filter.abi : undefined;
2562
+ const varType = abi?.variables?.find((v) => v.name === varName)?.type;
2544
2563
  return {
2545
2564
  contractId: printContractId(data) ?? "",
2546
- varName: typeof data.var_name === "string" ? data.var_name : "",
2565
+ varName,
2547
2566
  rawValue,
2548
- value: decodeRawHex(rawValue),
2567
+ value: decodeStateHex(rawValue, varType),
2549
2568
  tx: txMeta
2550
2569
  };
2551
2570
  }
@@ -2554,24 +2573,27 @@ function buildEventPayload(filter, tx, event) {
2554
2573
  const data = event.data ?? {};
2555
2574
  const rawKey = typeof data.raw_key === "string" ? data.raw_key : "";
2556
2575
  const rawValue = typeof data.raw_value === "string" ? data.raw_value : "";
2576
+ const map = typeof data.map_name === "string" ? data.map_name : "";
2577
+ const [keyType, valueType] = mapAbiTypes(filter, map);
2557
2578
  return {
2558
2579
  contractId: printContractId(data) ?? "",
2559
- map: typeof data.map_name === "string" ? data.map_name : "",
2580
+ map,
2560
2581
  rawKey,
2561
- key: decodeRawHex(rawKey),
2582
+ key: decodeStateHex(rawKey, keyType),
2562
2583
  rawValue,
2563
- value: decodeRawHex(rawValue),
2584
+ value: decodeStateHex(rawValue, valueType),
2564
2585
  tx: txMeta
2565
2586
  };
2566
2587
  }
2567
2588
  case "map_delete": {
2568
2589
  const data = event.data ?? {};
2569
2590
  const rawKey = typeof data.raw_key === "string" ? data.raw_key : "";
2591
+ const map = typeof data.map_name === "string" ? data.map_name : "";
2570
2592
  return {
2571
2593
  contractId: printContractId(data) ?? "",
2572
- map: typeof data.map_name === "string" ? data.map_name : "",
2594
+ map,
2573
2595
  rawKey,
2574
- key: decodeRawHex(rawKey),
2596
+ key: decodeStateHex(rawKey, mapAbiTypes(filter, map)[0]),
2575
2597
  tx: txMeta
2576
2598
  };
2577
2599
  }
@@ -3077,5 +3099,5 @@ export {
3077
3099
  toHandlerPayload
3078
3100
  };
3079
3101
 
3080
- //# debugId=314CB09040BE8DD164756E2164756E21
3102
+ //# debugId=80779D350271083F64756E2164756E21
3081
3103
  //# sourceMappingURL=index.js.map