@secondlayer/subgraphs 1.2.1 → 1.3.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.
@@ -163,6 +163,25 @@ interface SubgraphDefinition {
163
163
  /** Handler functions — keys must match source names (or "*" for catch-all) */
164
164
  handlers: Record<string, SubgraphHandler>;
165
165
  }
166
+ type ReindexBatchConfig = {
167
+ defaultBatchSize: number
168
+ minBatchSize: number
169
+ maxBatchSize: number
170
+ };
171
+ type ReindexBatchEnv = {
172
+ TENANT_PLAN?: string
173
+ SUBGRAPH_REINDEX_BATCH_SIZE?: string
174
+ SUBGRAPH_REINDEX_MIN_BATCH_SIZE?: string
175
+ SUBGRAPH_REINDEX_MAX_BATCH_SIZE?: string
176
+ };
177
+ type ReindexResumeRow = {
178
+ last_processed_block: number | string
179
+ reindex_from_block: number | string | null
180
+ reindex_to_block: number | string | null
181
+ };
182
+ declare function initialReindexProgressBlock(fromBlock: number): number;
183
+ declare function resolveReindexResumeBlock(row: ReindexResumeRow): number | null;
184
+ declare function resolveReindexBatchConfig(env?: ReindexBatchEnv): ReindexBatchConfig;
166
185
  interface ReindexOptions {
167
186
  fromBlock?: number;
168
187
  toBlock?: number;
@@ -201,4 +220,4 @@ declare function backfillSubgraph(def: SubgraphDefinition, opts: {
201
220
  }): Promise<{
202
221
  processed: number
203
222
  }>;
204
- export { resumeReindex, reindexSubgraph, backfillSubgraph, ReindexOptions };
223
+ export { resumeReindex, resolveReindexResumeBlock, resolveReindexBatchConfig, reindexSubgraph, initialReindexProgressBlock, backfillSubgraph, ReindexOptions };
@@ -513,7 +513,8 @@ function buildEventPayload(filter, tx, event) {
513
513
  tx: txMeta
514
514
  };
515
515
  case "print_event": {
516
- const rawValue = decoded.value;
516
+ const decodedRawValue = decoded.raw_value;
517
+ const rawValue = decodedRawValue && typeof decodedRawValue === "object" && !Array.isArray(decodedRawValue) ? decodedRawValue : decoded.value;
517
518
  const clarityObj = rawValue && typeof rawValue === "object" && !Array.isArray(rawValue) ? rawValue : null;
518
519
  const topic = clarityObj?.topic ? String(clarityObj.topic) : decoded.topic ?? "";
519
520
  const { topic: _, ...rest } = clarityObj ?? {};
@@ -824,7 +825,7 @@ function matchFilter(filter, transactions, eventsByTx) {
824
825
  for (const tx of transactions) {
825
826
  const txEvents = eventsByTx.get(tx.tx_id) ?? [];
826
827
  const matched = txEvents.filter((e) => {
827
- if (e.type !== "smart_contract_event")
828
+ if (e.type !== "smart_contract_event" && e.type !== "contract_event")
828
829
  return false;
829
830
  const data = e.data;
830
831
  if (!data)
@@ -1439,9 +1440,44 @@ function avgEventsPerBlock(batch) {
1439
1440
 
1440
1441
  // src/runtime/reindex.ts
1441
1442
  var LOG_INTERVAL = 1000;
1442
- var DEFAULT_BATCH_SIZE = 500;
1443
- var MIN_BATCH_SIZE = 100;
1444
- var MAX_BATCH_SIZE = 1000;
1443
+ var HOBBY_REINDEX_BATCH_CONFIG = {
1444
+ defaultBatchSize: 50,
1445
+ minBatchSize: 25,
1446
+ maxBatchSize: 100
1447
+ };
1448
+ var STANDARD_REINDEX_BATCH_CONFIG = {
1449
+ defaultBatchSize: 500,
1450
+ minBatchSize: 100,
1451
+ maxBatchSize: 1000
1452
+ };
1453
+ function initialReindexProgressBlock(fromBlock) {
1454
+ return Math.max(0, fromBlock - 1);
1455
+ }
1456
+ function resolveReindexResumeBlock(row) {
1457
+ if (row.reindex_from_block == null || row.reindex_to_block == null) {
1458
+ return null;
1459
+ }
1460
+ const lastProcessedBlock = Number(row.last_processed_block);
1461
+ const reindexFromBlock = Number(row.reindex_from_block);
1462
+ return Math.max(lastProcessedBlock + 1, reindexFromBlock);
1463
+ }
1464
+ function parsePositiveInt(value) {
1465
+ if (value == null || value.trim() === "")
1466
+ return;
1467
+ const parsed = Number.parseInt(value, 10);
1468
+ return Number.isFinite(parsed) && parsed > 0 ? parsed : undefined;
1469
+ }
1470
+ function resolveReindexBatchConfig(env = process.env) {
1471
+ const base = env.TENANT_PLAN?.trim().toLowerCase() === "hobby" ? HOBBY_REINDEX_BATCH_CONFIG : STANDARD_REINDEX_BATCH_CONFIG;
1472
+ const minBatchSize = parsePositiveInt(env.SUBGRAPH_REINDEX_MIN_BATCH_SIZE) ?? base.minBatchSize;
1473
+ const maxBatchSize = parsePositiveInt(env.SUBGRAPH_REINDEX_MAX_BATCH_SIZE) ?? base.maxBatchSize;
1474
+ const defaultBatchSize = parsePositiveInt(env.SUBGRAPH_REINDEX_BATCH_SIZE) ?? base.defaultBatchSize;
1475
+ return {
1476
+ minBatchSize,
1477
+ maxBatchSize,
1478
+ defaultBatchSize: Math.min(Math.max(defaultBatchSize, minBatchSize), maxBatchSize)
1479
+ };
1480
+ }
1445
1481
  function coalesceFailedBlocks(blocks) {
1446
1482
  if (blocks.length === 0)
1447
1483
  return [];
@@ -1474,7 +1510,8 @@ async function processBlockRange(def, opts) {
1474
1510
  let blocksProcessed = 0;
1475
1511
  let totalEventsProcessed = 0;
1476
1512
  let totalErrors = 0;
1477
- let batchSize = DEFAULT_BATCH_SIZE;
1513
+ const batchConfig = resolveReindexBatchConfig();
1514
+ let batchSize = batchConfig.defaultBatchSize;
1478
1515
  let currentHeight = fromBlock;
1479
1516
  let aborted = false;
1480
1517
  let nextBatchEnd = Math.min(currentHeight + batchSize - 1, toBlock);
@@ -1557,9 +1594,9 @@ async function processBlockRange(def, opts) {
1557
1594
  }
1558
1595
  const avg = avgEventsPerBlock(batch);
1559
1596
  if (avg > 50)
1560
- batchSize = Math.max(Math.round(batchSize * 0.5), MIN_BATCH_SIZE);
1597
+ batchSize = Math.max(Math.round(batchSize * 0.5), batchConfig.minBatchSize);
1561
1598
  else if (avg < 10)
1562
- batchSize = Math.min(Math.round(batchSize * 1.5), MAX_BATCH_SIZE);
1599
+ batchSize = Math.min(Math.round(batchSize * 1.5), batchConfig.maxBatchSize);
1563
1600
  currentHeight = batchEnd + 1;
1564
1601
  }
1565
1602
  await stats.flush(targetDb);
@@ -1598,13 +1635,18 @@ async function reindexSubgraph(def, opts) {
1598
1635
  await updateSubgraphStatus2(targetDb, subgraphName, "active", 0);
1599
1636
  return { processed: 0 };
1600
1637
  }
1601
- await targetDb.updateTable("subgraphs").set({ reindex_from_block: fromBlock, reindex_to_block: toBlock }).where("name", "=", subgraphName).execute();
1602
1638
  await client.unsafe(`DROP SCHEMA IF EXISTS "${schemaName}" CASCADE`);
1603
1639
  const { statements } = generateSubgraphSQL(def, schemaName);
1604
1640
  for (const stmt of statements) {
1605
1641
  await client.unsafe(stmt);
1606
1642
  }
1607
1643
  logger5.info("Schema recreated for reindex", { subgraph: subgraphName });
1644
+ await targetDb.updateTable("subgraphs").set({
1645
+ last_processed_block: initialReindexProgressBlock(fromBlock),
1646
+ reindex_from_block: fromBlock,
1647
+ reindex_to_block: toBlock,
1648
+ updated_at: new Date
1649
+ }).where("name", "=", subgraphName).execute();
1608
1650
  logger5.info("Reindexing blocks", {
1609
1651
  subgraph: subgraphName,
1610
1652
  fromBlock,
@@ -1667,7 +1709,9 @@ async function resumeReindex(def, opts) {
1667
1709
  ]).where("name", "=", subgraphName).executeTakeFirst();
1668
1710
  if (!row)
1669
1711
  throw new Error(`Subgraph "${subgraphName}" not found`);
1670
- if (row.reindex_from_block == null || row.reindex_to_block == null) {
1712
+ const fromBlock = resolveReindexResumeBlock(row);
1713
+ const toBlock = Number(row.reindex_to_block);
1714
+ if (fromBlock == null) {
1671
1715
  logger5.info("No reindex metadata, starting fresh reindex", {
1672
1716
  subgraph: subgraphName
1673
1717
  });
@@ -1676,8 +1720,6 @@ async function resumeReindex(def, opts) {
1676
1720
  signal: opts.signal
1677
1721
  });
1678
1722
  }
1679
- const fromBlock = Math.max(row.last_processed_block + 1, row.reindex_from_block);
1680
- const toBlock = row.reindex_to_block;
1681
1723
  if (fromBlock > toBlock) {
1682
1724
  logger5.info("Resume: no remaining blocks", { subgraph: subgraphName });
1683
1725
  await updateSubgraphStatus2(targetDb, subgraphName, "active", toBlock);
@@ -1784,9 +1826,12 @@ async function backfillSubgraph(def, opts) {
1784
1826
  }
1785
1827
  export {
1786
1828
  resumeReindex,
1829
+ resolveReindexResumeBlock,
1830
+ resolveReindexBatchConfig,
1787
1831
  reindexSubgraph,
1832
+ initialReindexProgressBlock,
1788
1833
  backfillSubgraph
1789
1834
  };
1790
1835
 
1791
- //# debugId=17D38AB6242AA3E264756E2164756E21
1836
+ //# debugId=A1D4C3C0FA63E7C664756E2164756E21
1792
1837
  //# sourceMappingURL=reindex.js.map