@secondlayer/subgraphs 7.0.4 → 7.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.
@@ -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.2.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: {
@@ -1509,9 +1509,9 @@ var package_default = {
1509
1509
  prepublishOnly: "bun run build"
1510
1510
  },
1511
1511
  dependencies: {
1512
- "@secondlayer/shared": "^13.0.0",
1513
- "@secondlayer/stacks": "^6.2.2",
1514
- "@secondlayer/verify": "^0.5.4",
1512
+ "@secondlayer/shared": "^13.1.0",
1513
+ "@secondlayer/stacks": "^6.4.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"
@@ -1970,6 +1970,41 @@ function printContractId(data) {
1970
1970
  const id = (typeof data.contract_identifier === "string" ? data.contract_identifier : undefined) ?? (typeof data.contract_id === "string" ? data.contract_id : undefined);
1971
1971
  return id || undefined;
1972
1972
  }
1973
+ var decodedPrints = new WeakMap;
1974
+ function decodedPrintTuple(event, data) {
1975
+ if (decodedPrints.has(event))
1976
+ return decodedPrints.get(event) ?? null;
1977
+ const raw = data.raw_value;
1978
+ const value = typeof raw === "string" && raw.startsWith("0x") ? decodeClarityValue(raw) : null;
1979
+ const tuple = value && typeof value === "object" && !Array.isArray(value) ? value : null;
1980
+ decodedPrints.set(event, tuple);
1981
+ return tuple;
1982
+ }
1983
+ function printFieldEquals(decoded, key, expected) {
1984
+ const value = decoded[key];
1985
+ let actual;
1986
+ if (typeof value === "string")
1987
+ actual = value;
1988
+ else if (typeof value === "bigint" || typeof value === "boolean")
1989
+ actual = String(value);
1990
+ else
1991
+ return false;
1992
+ return typeof expected === "string" ? actual === expected : expected.includes(actual);
1993
+ }
1994
+ function printFieldsAllow(filter, event, data) {
1995
+ if (!filter.topic && !filter.where)
1996
+ return true;
1997
+ const decoded = decodedPrintTuple(event, data);
1998
+ if (!decoded)
1999
+ return false;
2000
+ if (filter.topic && !printFieldEquals(decoded, "topic", filter.topic))
2001
+ return false;
2002
+ for (const [key, expected] of Object.entries(filter.where ?? {})) {
2003
+ if (!printFieldEquals(decoded, key, expected))
2004
+ return false;
2005
+ }
2006
+ return true;
2007
+ }
1973
2008
  function matchFilter(filter, transactions, eventsByTx, traitContracts, factoryContracts = new Map, vmEventsByTx = new Map) {
1974
2009
  const results = [];
1975
2010
  switch (filter.type) {
@@ -2206,7 +2241,7 @@ function matchFilter(filter, transactions, eventsByTx, traitContracts, factoryCo
2206
2241
  return false;
2207
2242
  if (!factoryAllows(filter, contractId, factoryContracts))
2208
2243
  return false;
2209
- return true;
2244
+ return printFieldsAllow(filter, e, data);
2210
2245
  });
2211
2246
  if (matched.length > 0) {
2212
2247
  results.push({ tx, events: matched });
@@ -2330,6 +2365,22 @@ function decodeRawHex(hex) {
2330
2365
  }
2331
2366
  return hex;
2332
2367
  }
2368
+ function decodeStateHex(hex, abiType) {
2369
+ if (abiType === undefined || !hex.startsWith("0x") || hex.length <= 2) {
2370
+ return decodeRawHex(hex);
2371
+ }
2372
+ try {
2373
+ const decode = clarityValueToJS;
2374
+ return decode(abiType, deserializeCV4(hex.slice(2)));
2375
+ } catch {
2376
+ return decodeRawHex(hex);
2377
+ }
2378
+ }
2379
+ function mapAbiTypes(filter, name) {
2380
+ const abi = "abi" in filter ? filter.abi : undefined;
2381
+ const def = abi?.maps?.find((m) => m.name === name);
2382
+ return def ? [def.key, def.value] : [];
2383
+ }
2333
2384
  function safeBigInt(val) {
2334
2385
  if (typeof val === "bigint")
2335
2386
  return val;
@@ -2541,11 +2592,14 @@ function buildEventPayload(filter, tx, event) {
2541
2592
  case "var_set": {
2542
2593
  const data = event.data ?? {};
2543
2594
  const rawValue = typeof data.raw_value === "string" ? data.raw_value : "";
2595
+ const varName = typeof data.var_name === "string" ? data.var_name : "";
2596
+ const abi = "abi" in filter ? filter.abi : undefined;
2597
+ const varType = abi?.variables?.find((v) => v.name === varName)?.type;
2544
2598
  return {
2545
2599
  contractId: printContractId(data) ?? "",
2546
- varName: typeof data.var_name === "string" ? data.var_name : "",
2600
+ varName,
2547
2601
  rawValue,
2548
- value: decodeRawHex(rawValue),
2602
+ value: decodeStateHex(rawValue, varType),
2549
2603
  tx: txMeta
2550
2604
  };
2551
2605
  }
@@ -2554,24 +2608,27 @@ function buildEventPayload(filter, tx, event) {
2554
2608
  const data = event.data ?? {};
2555
2609
  const rawKey = typeof data.raw_key === "string" ? data.raw_key : "";
2556
2610
  const rawValue = typeof data.raw_value === "string" ? data.raw_value : "";
2611
+ const map = typeof data.map_name === "string" ? data.map_name : "";
2612
+ const [keyType, valueType] = mapAbiTypes(filter, map);
2557
2613
  return {
2558
2614
  contractId: printContractId(data) ?? "",
2559
- map: typeof data.map_name === "string" ? data.map_name : "",
2615
+ map,
2560
2616
  rawKey,
2561
- key: decodeRawHex(rawKey),
2617
+ key: decodeStateHex(rawKey, keyType),
2562
2618
  rawValue,
2563
- value: decodeRawHex(rawValue),
2619
+ value: decodeStateHex(rawValue, valueType),
2564
2620
  tx: txMeta
2565
2621
  };
2566
2622
  }
2567
2623
  case "map_delete": {
2568
2624
  const data = event.data ?? {};
2569
2625
  const rawKey = typeof data.raw_key === "string" ? data.raw_key : "";
2626
+ const map = typeof data.map_name === "string" ? data.map_name : "";
2570
2627
  return {
2571
2628
  contractId: printContractId(data) ?? "",
2572
- map: typeof data.map_name === "string" ? data.map_name : "",
2629
+ map,
2573
2630
  rawKey,
2574
- key: decodeRawHex(rawKey),
2631
+ key: decodeStateHex(rawKey, mapAbiTypes(filter, map)[0]),
2575
2632
  tx: txMeta
2576
2633
  };
2577
2634
  }
@@ -2781,6 +2838,14 @@ function txFromRow(row) {
2781
2838
  ...typeof r.function_name === "string" ? { functionName: r.function_name } : {}
2782
2839
  };
2783
2840
  }
2841
+ function indexPrintWhereAllows(where, row) {
2842
+ if (!where)
2843
+ return true;
2844
+ const value = row.payload?.value;
2845
+ if (!value || typeof value !== "object" || Array.isArray(value))
2846
+ return false;
2847
+ return Object.entries(where).every(([key, expected]) => printFieldEquals(value, key, expected));
2848
+ }
2784
2849
  function toHandlerPayload(_filter, row) {
2785
2850
  const tx = defaultTx(txFromRow(row));
2786
2851
  if (row.event_type === "print") {
@@ -2938,7 +3003,7 @@ async function runSubgraphTest(input) {
2938
3003
  continue;
2939
3004
  for (const row of rows) {
2940
3005
  const payload = filter?.type === "contract_call" ? toContractCallPayload(row) : toHandlerPayload(filter, row);
2941
- if (filter?.type === "print_event" && filter.topic && payload.topic !== filter.topic) {
3006
+ if (filter?.type === "print_event" && (filter.topic && payload.topic !== filter.topic || !indexPrintWhereAllows(filter.where, row))) {
2942
3007
  continue;
2943
3008
  }
2944
3009
  if (!firstEvent) {
@@ -3077,5 +3142,5 @@ export {
3077
3142
  toHandlerPayload
3078
3143
  };
3079
3144
 
3080
- //# debugId=314CB09040BE8DD164756E2164756E21
3145
+ //# debugId=6B86A4A41B6DB6DF64756E2164756E21
3081
3146
  //# sourceMappingURL=index.js.map