envio 3.8.0 → 3.9.0-resolvers.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.
Files changed (109) hide show
  1. package/README.md +2 -2
  2. package/evm.schema.json +7 -0
  3. package/fuel.schema.json +7 -0
  4. package/index.d.ts +433 -109
  5. package/index.js +10 -0
  6. package/package.json +6 -7
  7. package/src/AddressRows.res +121 -0
  8. package/src/AddressRows.res.mjs +143 -0
  9. package/src/Batch.res +11 -0
  10. package/src/Batch.res.mjs +2 -1
  11. package/src/Bin.res +47 -0
  12. package/src/Bin.res.mjs +34 -1
  13. package/src/ChainState.res +102 -100
  14. package/src/ChainState.res.mjs +81 -67
  15. package/src/ChainState.resi +28 -15
  16. package/src/Config.res +43 -122
  17. package/src/Config.res.mjs +33 -75
  18. package/src/ContractMapping.res +66 -0
  19. package/src/ContractMapping.res.mjs +73 -0
  20. package/src/Core.res +24 -0
  21. package/src/Core.res.mjs +4 -0
  22. package/src/Ecosystem.res +7 -2
  23. package/src/Env.res +128 -1
  24. package/src/Env.res.mjs +123 -1
  25. package/src/EventConfigBuilder.res +169 -28
  26. package/src/EventConfigBuilder.res.mjs +124 -17
  27. package/src/FetchState.res +59 -67
  28. package/src/FetchState.res.mjs +34 -44
  29. package/src/HandlerRegister.res +6 -4
  30. package/src/HandlerRegister.res.mjs +8 -5
  31. package/src/InMemoryStore.res +22 -32
  32. package/src/InMemoryStore.res.mjs +12 -24
  33. package/src/IndexerState.res +40 -3
  34. package/src/IndexerState.res.mjs +30 -4
  35. package/src/IndexerState.resi +4 -1
  36. package/src/Internal.res +16 -17
  37. package/src/Main.res +25 -15
  38. package/src/Main.res.mjs +19 -14
  39. package/src/Metrics.res +15 -1
  40. package/src/Metrics.res.mjs +5 -1
  41. package/src/Persistence.res +42 -19
  42. package/src/Persistence.res.mjs +8 -5
  43. package/src/PgStorage.res +232 -78
  44. package/src/PgStorage.res.mjs +114 -72
  45. package/src/PruneStaleHistory.res +1 -1
  46. package/src/PruneStaleHistory.res.mjs +1 -1
  47. package/src/Rollback.res +62 -41
  48. package/src/Rollback.res.mjs +61 -19
  49. package/src/RollbackCommit.res +7 -9
  50. package/src/RollbackCommit.res.mjs +5 -6
  51. package/src/SimulateItems.res +5 -12
  52. package/src/SimulateItems.res.mjs +6 -10
  53. package/src/Sink.res +35 -26
  54. package/src/Sink.res.mjs +20 -16
  55. package/src/TestIndexer.res +95 -113
  56. package/src/TestIndexer.res.mjs +63 -90
  57. package/src/Utils.res +38 -0
  58. package/src/Utils.res.mjs +39 -2
  59. package/src/Writing.res +4 -2
  60. package/src/Writing.res.mjs +3 -2
  61. package/src/bindings/ClickHouse.res +311 -683
  62. package/src/bindings/ClickHouse.res.mjs +294 -416
  63. package/src/bindings/ClickHouseSink.res +303 -0
  64. package/src/bindings/ClickHouseSink.res.mjs +216 -0
  65. package/src/bindings/NodeJs.res +9 -0
  66. package/src/bindings/NodeJs.res.mjs +8 -1
  67. package/src/bindings/Postgres.res +9 -0
  68. package/src/bindings/Postgres.res.mjs +3 -0
  69. package/src/db/EntityHistory.res +29 -24
  70. package/src/db/EntityHistory.res.mjs +11 -19
  71. package/src/db/InternalTable.res +247 -83
  72. package/src/db/InternalTable.res.mjs +216 -85
  73. package/src/db/RollbackScope.res +29 -0
  74. package/src/db/RollbackScope.res.mjs +31 -0
  75. package/src/db/Table.res +28 -1
  76. package/src/db/Table.res.mjs +26 -2
  77. package/src/resolvers/ResolverProcess.res +809 -0
  78. package/src/resolvers/ResolverProcess.res.mjs +667 -0
  79. package/src/resolvers/ResolverQuery.res +317 -0
  80. package/src/resolvers/ResolverQuery.res.mjs +220 -0
  81. package/src/resolvers/collisions.js +108 -0
  82. package/src/resolvers/db.js +363 -0
  83. package/src/resolvers/dispatch.js +165 -0
  84. package/src/resolvers/errors.js +30 -0
  85. package/src/resolvers/graphqlSelection.js +292 -0
  86. package/src/resolvers/hasuraAction.js +84 -0
  87. package/src/resolvers/hasuraApply.js +323 -0
  88. package/src/resolvers/hasuraMetadata.js +119 -0
  89. package/src/resolvers/index.js +95 -0
  90. package/src/resolvers/manifest.js +370 -0
  91. package/src/resolvers/server.js +270 -0
  92. package/src/sources/AddressSet.res +0 -22
  93. package/src/sources/AddressStore.res +48 -88
  94. package/src/sources/AddressStore.res.mjs +11 -22
  95. package/src/sources/Evm.res +2 -0
  96. package/src/sources/Evm.res.mjs +2 -0
  97. package/src/sources/Fuel.res +2 -0
  98. package/src/sources/Fuel.res.mjs +2 -0
  99. package/src/sources/Svm.res +2 -0
  100. package/src/sources/Svm.res.mjs +2 -0
  101. package/src/sources/SvmHyperSyncClient.res +13 -107
  102. package/src/sources/SvmHyperSyncClient.res.mjs +2 -5
  103. package/src/sources/SvmHyperSyncSource.res +5 -10
  104. package/src/sources/SvmHyperSyncSource.res.mjs +5 -13
  105. package/src/tui/Tui.res +1 -1
  106. package/src/tui/Tui.res.mjs +1 -1
  107. package/svm.schema.json +17 -78
  108. package/src/MemoryStorage.res +0 -756
  109. package/src/MemoryStorage.res.mjs +0 -623
@@ -14,6 +14,7 @@ import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.js";
14
14
  import * as Stdlib_JsError from "@rescript/runtime/lib/es6/Stdlib_JsError.js";
15
15
  import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.js";
16
16
  import * as S$RescriptSchema from "rescript-schema/src/S.res.mjs";
17
+ import * as Primitive_exceptions from "@rescript/runtime/lib/es6/Primitive_exceptions.js";
17
18
 
18
19
  let paramMetaSchema = S$RescriptSchema.recursive(self => S$RescriptSchema.object(s => ({
19
20
  name: s.f("name", S$RescriptSchema.string),
@@ -261,17 +262,13 @@ let svmSelectionKinds = [
261
262
  "log"
262
263
  ];
263
264
 
264
- function quotedJoin(names) {
265
- return names.map(name => `"` + name + `"`).join(", ");
266
- }
267
-
268
265
  function validateFieldsShapeOrThrow(fields, registration, validKeys, shapeNoun) {
269
266
  if (typeof fields !== "object" || fields === null || Array.isArray(fields)) {
270
267
  Stdlib_JsError.throwWithMessage(`The fields option of ` + registration + ` must be an object of ` + shapeNoun + ` field names.`);
271
268
  }
272
269
  Object.keys(fields).forEach(key => {
273
270
  if (!validKeys.includes(key)) {
274
- return Stdlib_JsError.throwWithMessage(`Invalid "` + key + `" key in the fields option of ` + registration + `. Valid keys: ` + quotedJoin(validKeys) + `.`);
271
+ return Stdlib_JsError.throwWithMessage(`Invalid "` + key + `" key in the fields option of ` + registration + `. Valid keys: ` + Utils.$$Array.quotedJoin(validKeys) + `.`);
275
272
  }
276
273
  });
277
274
  }
@@ -295,7 +292,7 @@ function parseFieldsOrThrow(fields, valid, kind, registration, rejectEmptyOpt) {
295
292
  ) : [];
296
293
  fields$1.forEach(name => {
297
294
  if (!valid.has(name)) {
298
- Stdlib_JsError.throwWithMessage(`Invalid "` + name + `" field in the fields.` + kind + ` option of ` + registration + `. Valid ` + kind + ` fields: ` + quotedJoin(Array.from(valid)) + `.`);
295
+ Stdlib_JsError.throwWithMessage(`Invalid "` + name + `" field in the fields.` + kind + ` option of ` + registration + `. Valid ` + kind + ` fields: ` + Utils.$$Array.quotedJoin(Array.from(valid)) + `.`);
299
296
  }
300
297
  if (seen.has(name)) {
301
298
  Stdlib_JsError.throwWithMessage(`Duplicate "` + name + `" field in the fields.` + kind + ` option of ` + registration + `.`);
@@ -396,10 +393,8 @@ let validSvmAccountActivityFields = new Set([
396
393
  "transactionAccountIndex",
397
394
  "isSigner",
398
395
  "isWritable",
399
- "lamports",
400
396
  "lamports.pre",
401
397
  "lamports.post",
402
- "token",
403
398
  "token.mint",
404
399
  "token.owner",
405
400
  "token.decimals",
@@ -422,7 +417,7 @@ let validSvmLogFields = new Set([
422
417
  ]);
423
418
 
424
419
  function resolveSvmInlineFieldSelection(fields, contractName, eventName) {
425
- let registration = `the "` + eventName + `" event registration on contract "` + contractName + `"`;
420
+ let registration = `the "` + eventName + `" instruction on program "` + contractName + `"`;
426
421
  validateFieldsShapeOrThrow(fields, registration, svmSelectionKinds, "instruction, transaction, accountActivity, block and log");
427
422
  let accountActivity = selectionList(fields, "accountActivity");
428
423
  let log = selectionList(fields, "log");
@@ -438,7 +433,7 @@ function resolveSvmInlineFieldSelection(fields, contractName, eventName) {
438
433
  return Internal.makeFieldSelection(blockFields, transactionFields, Primitive_option.some(instructionFields), Primitive_option.some(accountActivityFields), Primitive_option.some(logFields), Svm.eventBlockFieldMask, Svm.eventTransactionFieldMask);
439
434
  }
440
435
 
441
- function buildSvmInstructionEventConfig(contractName, instructionName, programId, discriminator, discriminatorByteLen, accountFilters, isInner, accountsOpt, argsOpt, definedTypesOpt) {
436
+ function buildSvmInstructionEventConfig(contractName, instructionName, programId, discriminator, accountsOpt, argsOpt, definedTypesOpt) {
442
437
  let accounts = accountsOpt !== undefined ? accountsOpt : [];
443
438
  let args = argsOpt !== undefined ? argsOpt : null;
444
439
  let definedTypes = definedTypesOpt !== undefined ? definedTypesOpt : null;
@@ -453,16 +448,121 @@ function buildSvmInstructionEventConfig(contractName, instructionName, programId
453
448
  fieldSelection: fieldSelection,
454
449
  programId: programId,
455
450
  discriminator: discriminator,
456
- discriminatorByteLen: discriminatorByteLen,
457
- accountFilters: accountFilters,
458
- isInner: isInner,
459
451
  accounts: accounts,
460
452
  args: args,
461
453
  definedTypes: definedTypes
462
454
  };
463
455
  }
464
456
 
465
- function buildSvmOnEventRegistration(eventConfig, isWildcard, handler, contractRegister, fieldSelection, startBlock) {
457
+ let validSvmWhereKeys = [
458
+ "accounts",
459
+ "isInner",
460
+ "block"
461
+ ];
462
+
463
+ let svmBlockFilterSchema = S$RescriptSchema.strict(S$RescriptSchema.object(s => ({
464
+ slot: s.f("slot", S$RescriptSchema.option(LogSelection.eventBlockRangeSchema))
465
+ })));
466
+
467
+ function resolveSvmWhereOrThrow(where, contractName, eventName, accountNames) {
468
+ let invalid = message => Stdlib_JsError.throwWithMessage(`Invalid where configuration for the "` + eventName + `" instruction on program "` + contractName + `". ` + message);
469
+ let obj;
470
+ obj = typeof where === "object" && where !== null && !Array.isArray(where) ? where : invalid("Expected an object.");
471
+ Utils.Dict.forEachWithKey(obj, (param, key) => {
472
+ if (!validSvmWhereKeys.includes(key)) {
473
+ return invalid(`Unknown field "` + key + `". Valid fields: ` + Utils.$$Array.quotedJoin(validSvmWhereKeys) + `.`);
474
+ }
475
+ });
476
+ let match = obj["isInner"];
477
+ let isInner = match !== undefined ? (
478
+ typeof match === "boolean" ? match : invalid(`The "isInner" filter must be a boolean.`)
479
+ ) : undefined;
480
+ let parseGroup = group => Object.entries(group).map(param => {
481
+ let name = param[0];
482
+ let position = accountNames.indexOf(name);
483
+ let position$1 = position !== -1 ? (
484
+ position >= 10 ? invalid(`Account "` + name + `" is at position ` + position.toString() + `, and only the first ` + (10).toString() + ` accounts of an instruction can be filtered.`) : position
485
+ ) : (
486
+ Utils.$$Array.isEmpty(accountNames) ? invalid("The instruction has no named accounts to filter on. Add `accounts` and `args` to it in config.yaml, or attach an IDL.") : invalid(`The instruction has no account named "` + name + `" to filter on. Named accounts: ` + Utils.$$Array.quotedJoin(accountNames) + `.`)
487
+ );
488
+ let values = normalizeOrThrow(param[1]);
489
+ if (Utils.$$Array.isEmpty(values)) {
490
+ invalid(`The "` + name + `" filter must list at least one pubkey.`);
491
+ }
492
+ return {
493
+ position: position$1,
494
+ values: values.map(value => {
495
+ if (typeof value === "string") {
496
+ if (Core.getAddon().isSvmPubkey(value)) {
497
+ return value;
498
+ } else {
499
+ return invalid(`The "` + name + `" filter value "` + value + `" is not a base58 SVM pubkey.`);
500
+ }
501
+ } else {
502
+ return invalid(`The "` + name + `" filter must list base58 pubkeys as strings.`);
503
+ }
504
+ })
505
+ };
506
+ });
507
+ let match$1 = obj["accounts"];
508
+ let groups;
509
+ if (match$1 !== undefined) {
510
+ if (Array.isArray(match$1)) {
511
+ groups = match$1.map(entry => {
512
+ if (typeof entry === "object" && entry !== null && !Array.isArray(entry)) {
513
+ return parseGroup(entry);
514
+ } else {
515
+ return invalid(`Each entry in "accounts" must be an object.`);
516
+ }
517
+ });
518
+ } else if (match$1 === null) {
519
+ groups = invalid(`Expected "accounts" to be an object or an array of objects.`);
520
+ } else {
521
+ switch (typeof match$1) {
522
+ case "object" :
523
+ groups = [parseGroup(match$1)];
524
+ break;
525
+ default:
526
+ groups = invalid(`Expected "accounts" to be an object or an array of objects.`);
527
+ }
528
+ }
529
+ } else {
530
+ groups = [];
531
+ }
532
+ let accountFilters = groups.some(Utils.$$Array.isEmpty) ? [] : groups;
533
+ let block = obj["block"];
534
+ let startBlock;
535
+ if (block !== undefined) {
536
+ let filter;
537
+ try {
538
+ filter = S$RescriptSchema.parseOrThrow(block, svmBlockFilterSchema);
539
+ } catch (raw_exn) {
540
+ let exn = Primitive_exceptions.internalToException(raw_exn);
541
+ if (exn.RE_EXN_ID === S$RescriptSchema.Raised) {
542
+ filter = invalid(`The "block" filter is invalid: ` + Utils.prettifyExn(exn._1) + `. Only \`_gte\` is supported on instruction filters — use \`indexer.onSlot\` for \`_lte\` or \`_every\`.`);
543
+ } else {
544
+ throw exn;
545
+ }
546
+ }
547
+ let match$2 = filter.slot;
548
+ startBlock = match$2 !== undefined ? match$2._gte : undefined;
549
+ } else {
550
+ startBlock = undefined;
551
+ }
552
+ return {
553
+ accountFilters: accountFilters,
554
+ isInner: isInner,
555
+ startBlock: startBlock
556
+ };
557
+ }
558
+
559
+ function buildSvmOnEventRegistration(eventConfig, isWildcard, handler, contractRegister, where, fieldSelection, startBlock) {
560
+ let resolvedWhere = where !== undefined ? resolveSvmWhereOrThrow(where, eventConfig.contractName, eventConfig.name, eventConfig.accounts) : ({
561
+ accountFilters: [],
562
+ isInner: undefined,
563
+ startBlock: undefined
564
+ });
565
+ let sb = resolvedWhere.startBlock;
466
566
  return {
467
567
  index: -1,
468
568
  eventConfig: eventConfig,
@@ -471,8 +571,10 @@ function buildSvmOnEventRegistration(eventConfig, isWildcard, handler, contractR
471
571
  isWildcard: isWildcard,
472
572
  filterByAddresses: false,
473
573
  dependsOnAddresses: Internal.dependsOnAddresses(isWildcard, false),
474
- startBlock: startBlock,
475
- fieldSelection: fieldSelection !== undefined ? fieldSelection : eventConfig.fieldSelection
574
+ startBlock: sb !== undefined ? sb : startBlock,
575
+ fieldSelection: fieldSelection !== undefined ? fieldSelection : eventConfig.fieldSelection,
576
+ accountFilters: resolvedWhere.accountFilters,
577
+ isInner: resolvedWhere.isInner
476
578
  };
477
579
  }
478
580
 
@@ -544,6 +646,8 @@ function buildFuelOnEventRegistration(eventConfig, isWildcard, handler, contract
544
646
  };
545
647
  }
546
648
 
649
+ let filterableAccountCount = 10;
650
+
547
651
  export {
548
652
  paramMetaSchema,
549
653
  normalizeOrThrow,
@@ -563,7 +667,6 @@ export {
563
667
  rawEventBlockFields,
564
668
  evmSelectionKinds,
565
669
  svmSelectionKinds,
566
- quotedJoin,
567
670
  validateFieldsShapeOrThrow,
568
671
  selectionList,
569
672
  validBlockFields,
@@ -580,6 +683,10 @@ export {
580
683
  validSvmLogFields,
581
684
  resolveSvmInlineFieldSelection,
582
685
  buildSvmInstructionEventConfig,
686
+ validSvmWhereKeys,
687
+ svmBlockFilterSchema,
688
+ filterableAccountCount,
689
+ resolveSvmWhereOrThrow,
583
690
  buildSvmOnEventRegistration,
584
691
  buildFuelEventConfig,
585
692
  buildFuelOnEventRegistration,
@@ -1469,7 +1469,6 @@ let warnAddressRegistration = (
1469
1469
 
1470
1470
  // A rejected registration is simply absent from every partition, so without a
1471
1471
  // warning the user sees a contract that never indexes and nothing saying why.
1472
- // Shared by config-time registration in `make` and by dynamic registration.
1473
1472
  let warnRejectedRegistration = (
1474
1473
  verdict: AddressStore.verdict,
1475
1474
  ~chainId: ChainId.t,
@@ -1477,20 +1476,7 @@ let warnRejectedRegistration = (
1477
1476
  ~contractName: string,
1478
1477
  ) =>
1479
1478
  switch verdict {
1480
- | Conflict({existingContractName}) =>
1481
- warnAddressRegistration(
1482
- ~chainId,
1483
- ~contractAddress,
1484
- ~params={
1485
- "existingContractType": existingContractName,
1486
- "newContractType": contractName,
1487
- },
1488
- `Skipping contract registration: Contract address is already registered for one contract and cannot be registered for another contract.`,
1489
- )
1490
1479
  | Duplicate({effectiveStartBlock, existingEffectiveStartBlock}) =>
1491
- // FIXME: Instead of filtering out duplicates, we should check the block
1492
- // number first. If a new registration has an earlier block number we
1493
- // should register it for the missing block range.
1494
1480
  if existingEffectiveStartBlock > effectiveStartBlock {
1495
1481
  warnAddressRegistration(
1496
1482
  ~chainId,
@@ -1499,7 +1485,7 @@ let warnRejectedRegistration = (
1499
1485
  "existingBlockNumber": existingEffectiveStartBlock,
1500
1486
  "newBlockNumber": effectiveStartBlock,
1501
1487
  },
1502
- `Skipping contract registration: Contract address is already registered at a later block number. Currently registration of the same contract address is not supported by Envio. Reach out to us if it's a problem for you.`,
1488
+ `Skipping same-contract re-registration: the address is already registered for this contract. The start block does not move earlier.`,
1503
1489
  )
1504
1490
  }
1505
1491
  | Invalid =>
@@ -1723,9 +1709,10 @@ let registerDynamicContracts = (
1723
1709
  // exactly what this batch adds.
1724
1710
  let idCursor = addressStore->AddressStore.nextId
1725
1711
  // The store resolves each address against both what it already holds and the
1726
- // batch's own earlier entries, so two contracts claiming one address inside a
1727
- // single batch conflict the same way as across batches. It also decides which
1728
- // additions this chain fetches for, since it's what holds the contract list.
1712
+ // batch's own earlier entries, so the same address registered twice for one
1713
+ // contract inside a single batch is a duplicate just as it is across batches.
1714
+ // It also decides which additions this chain fetches for, since it's what
1715
+ // holds the contract list.
1729
1716
  let verdicts = addressStore->AddressStore.registerBatch(registrations)
1730
1717
 
1731
1718
  let registeringContractNames = []
@@ -1741,7 +1728,7 @@ let registerDynamicContracts = (
1741
1728
  // no partition to build. The address is still stored and persisted, so a
1742
1729
  // config that later adds address-dependent events picks it up on restart.
1743
1730
  | Added({fetchable: false}) => ()
1744
- | Conflict(_) | Duplicate(_) | Invalid =>
1731
+ | Duplicate(_) | Invalid =>
1745
1732
  verdict->warnRejectedRegistration(
1746
1733
  ~chainId=fetchState.chainId,
1747
1734
  ~contractAddress=registration.address,
@@ -2627,7 +2614,7 @@ let make = (
2627
2614
  ~endBlock,
2628
2615
  ~onEventRegistrations: array<Internal.onEventRegistration>,
2629
2616
  ~addressStore: AddressStore.t,
2630
- ~addresses: array<Internal.indexingAddress>,
2617
+ ~addressRows: AddressRows.seedRows,
2631
2618
  ~maxAddrInPartition,
2632
2619
  ~chainId: ChainId.t,
2633
2620
  ~maxOnBlockBufferSize,
@@ -2681,27 +2668,24 @@ let make = (
2681
2668
  )
2682
2669
 
2683
2670
  // Every address the chain indexes goes into the store — including ones whose
2684
- // contract has no address-dependent events, so a later registration of the
2685
- // same address still conflicts and the address is still persisted.
2671
+ // contract has no address-dependent events, so the address is still persisted
2672
+ // and a config that later adds events picks it up.
2673
+ //
2674
+ // These rows come from the config or from a resume, so they're already stored
2675
+ // and must never be drained back into a write. Only the rows the store
2676
+ // refuses come back: a resume seeds millions of them.
2686
2677
  addressStore
2687
- // These come from the config or from a resume, so they're already stored and
2688
- // must never be drained back into a write.
2689
- ->AddressStore.seedBatch(
2690
- addresses->Array.map((contract): AddressStore.registration => {
2691
- address: contract.address,
2692
- contractName: contract.contractName,
2693
- registrationBlock: contract.registrationBlock,
2694
- }),
2695
- )
2696
- // Verdicts are in the batch's order, so they line up with `addresses`. A
2697
- // config address the store rejects is dropped exactly like a dynamic one, and
2698
- // needs the same warning — restored dynamic addresses come through here too.
2699
- ->Array.forEachWithIndex((verdict, idx) => {
2700
- let contract = addresses->Array.getUnsafe(idx)
2701
- verdict->warnRejectedRegistration(
2678
+ ->AddressStore.seedRows(addressRows)
2679
+ ->Array.forEach(rejected => {
2680
+ warnAddressRegistration(
2702
2681
  ~chainId,
2703
- ~contractAddress=contract.address,
2704
- ~contractName=contract.contractName,
2682
+ ~contractAddress=rejected.address,
2683
+ ~params={
2684
+ "contractName": rejected.contractName,
2685
+ "existingBlockNumber": rejected.existingEffectiveStartBlock,
2686
+ "newBlockNumber": rejected.effectiveStartBlock,
2687
+ },
2688
+ `Skipping a stored address: it is already registered for this contract.`,
2705
2689
  )
2706
2690
  })
2707
2691
 
@@ -2709,23 +2693,24 @@ let make = (
2709
2693
  let clientFilteredContracts = Utils.Set.make()
2710
2694
  let registeringSetsByContract = Dict.make()
2711
2695
 
2712
- addresses->Array.forEach(contract => {
2713
- let contractName = contract.contractName
2714
-
2715
- // Only addresses whose contract has events that depend on addresses get
2716
- // registered for active fetching via partitions.
2696
+ // What each contract needs a partition for is read back off the store rather
2697
+ // than re-derived from the seeded columns: the store is what resolved the
2698
+ // rows, including the ones it refused.
2699
+ contractNamesWithNormalEvents
2700
+ ->Utils.Set.toArray
2701
+ ->Array.forEach(contractName => {
2702
+ if addressStore->AddressStore.contractCount(contractName) > 0 {
2703
+ registeringSetsByContract->Dict.set(
2704
+ contractName,
2705
+ addressStore->AddressStore.makeSet(~contractName),
2706
+ )
2707
+ }
2708
+ })
2709
+ addressStore
2710
+ ->AddressStore.dynamicContractNames
2711
+ ->Array.forEach(contractName => {
2717
2712
  if contractNamesWithNormalEvents->Utils.Set.has(contractName) {
2718
- if !(registeringSetsByContract->Dict.has(contractName)) {
2719
- registeringSetsByContract->Dict.set(
2720
- contractName,
2721
- addressStore->AddressStore.makeSet(~contractName),
2722
- )
2723
- }
2724
-
2725
- // Detect dynamic contracts by registrationBlock
2726
- if contract.registrationBlock !== -1 {
2727
- dynamicContracts->Utils.Set.add(contractName)->ignore
2728
- }
2713
+ dynamicContracts->Utils.Set.add(contractName)->ignore
2729
2714
  }
2730
2715
  })
2731
2716
 
@@ -2768,7 +2753,7 @@ let make = (
2768
2753
  ) {
2769
2754
  JsError.throwWithMessage(
2770
2755
  `Invalid configuration: Nothing to fetch on chain ${chainId->ChainId.toString}. ` ++
2771
- `addresses=${addresses->Array.length->Int.toString}, ` ++
2756
+ `addresses=${addressRows.addresses->Array.length->Int.toString}, ` ++
2772
2757
  `onEventRegistrations=${onEventRegistrations->Array.length->Int.toString}, ` ++
2773
2758
  `normalRegistrations=${normalRegistrations
2774
2759
  ->Array.length
@@ -2845,20 +2830,28 @@ let rollbackPendingQueries = (mutPendingQueries: array<pendingQuery>, ~targetBlo
2845
2830
  adjusted
2846
2831
  }
2847
2832
 
2833
+ type rollbackResult = {
2834
+ fetchState: t,
2835
+ // The registrations the prune dropped, for the storage that deletes their rows.
2836
+ rolledBackAddresses: array<AddressStore.rolledBackAddress>,
2837
+ }
2838
+
2848
2839
  /**
2849
2840
  Rolls back fetch state to the given valid block.
2841
+ Prunes the store first, then rebuilds partitions from it: an address survives iff
2842
+ `filterByRegistrationBlock` keeps it, so the partitions and the rows the caller
2843
+ goes on to delete can't disagree about which registrations died.
2850
2844
  Always recreates optimized partitions to avoid duplicate addresses:
2851
2845
  - Wildcard: only rollback latestFetchedBlock
2852
2846
  - Non-wildcard with lfb <= target: keep, adjust pending queries and mergeBlock
2853
2847
  - Non-wildcard with lfb > target: delete, track addresses for recreation
2854
2848
  */
2855
- let rollback = (fetchState: t, ~addressStore: AddressStore.t, ~targetBlockNumber) => {
2856
- // Step 1: Prune addresses registered after the target block. The pruned store
2857
- // is then the source of truth for partition cleanup below — an address
2858
- // survives iff `filterByRegistrationBlock` keeps it.
2859
- addressStore->AddressStore.rollback(targetBlockNumber)->ignore
2860
-
2861
- // Step 2: Categorize partitions
2849
+ let rollback = (
2850
+ fetchState: t,
2851
+ ~rolledBackAddressStore: AddressStore.t,
2852
+ ~targetBlockNumber,
2853
+ ): rollbackResult => {
2854
+ let rolledBackAddresses = rolledBackAddressStore->AddressStore.rollback(targetBlockNumber)
2862
2855
  let keptPartitions = []
2863
2856
  let nextKeptIdRef = ref(0)
2864
2857
  let registeringSetsByContract: dict<AddressSet.t> = Dict.make()
@@ -2935,10 +2928,9 @@ let rollback = (fetchState: t, ~addressStore: AddressStore.t, ~targetBlockNumber
2935
2928
  }
2936
2929
  }
2937
2930
 
2938
- // Step 3: Recreate partitions from deleted partition addresses
2939
2931
  let optimizedPartitions = createPartitions(
2940
2932
  ~registeringSetsByContract,
2941
- ~addressStore,
2933
+ ~addressStore=rolledBackAddressStore,
2942
2934
  ~dynamicContracts=fetchState.optimizedPartitions.dynamicContracts,
2943
2935
  ~clientFilteredContracts=fetchState.optimizedPartitions.clientFilteredContracts,
2944
2936
  ~normalSelection=fetchState.normalSelection,
@@ -2949,8 +2941,7 @@ let rollback = (fetchState: t, ~addressStore: AddressStore.t, ~targetBlockNumber
2949
2941
  ~knownHeight=fetchState.knownHeight,
2950
2942
  )
2951
2943
 
2952
- // Step 4: Update state
2953
- {
2944
+ let rolledBack = {
2954
2945
  ...fetchState,
2955
2946
  // TODO: Test this. Currently it's not tested.
2956
2947
  latestOnBlockBlockNumber: Pervasives.min(
@@ -2969,6 +2960,7 @@ let rollback = (fetchState: t, ~addressStore: AddressStore.t, ~targetBlockNumber
2969
2960
  targetBlockNumber
2970
2961
  ),
2971
2962
  )
2963
+ {fetchState: rolledBack, rolledBackAddresses}
2972
2964
  }
2973
2965
 
2974
2966
  // Reset pending queries by removing in-flight queries (ones without fetchedBlock).
@@ -1123,25 +1123,16 @@ function warnRejectedRegistration(verdict, chainId, contractAddress, contractNam
1123
1123
  contractName: contractName
1124
1124
  }, `Skipping contract registration: Not a valid address for this chain's ecosystem.`);
1125
1125
  }
1126
- switch (verdict.TAG) {
1127
- case "Added" :
1128
- return;
1129
- case "Duplicate" :
1130
- let existingEffectiveStartBlock = verdict.existingEffectiveStartBlock;
1131
- let effectiveStartBlock = verdict.effectiveStartBlock;
1132
- if (existingEffectiveStartBlock > effectiveStartBlock) {
1133
- return warnAddressRegistration(chainId, contractAddress, {
1134
- existingBlockNumber: existingEffectiveStartBlock,
1135
- newBlockNumber: effectiveStartBlock
1136
- }, `Skipping contract registration: Contract address is already registered at a later block number. Currently registration of the same contract address is not supported by Envio. Reach out to us if it's a problem for you.`);
1137
- } else {
1138
- return;
1139
- }
1140
- case "Conflict" :
1141
- return warnAddressRegistration(chainId, contractAddress, {
1142
- existingContractType: verdict.existingContractName,
1143
- newContractType: contractName
1144
- }, `Skipping contract registration: Contract address is already registered for one contract and cannot be registered for another contract.`);
1126
+ if (verdict.TAG === "Added") {
1127
+ return;
1128
+ }
1129
+ let existingEffectiveStartBlock = verdict.existingEffectiveStartBlock;
1130
+ let effectiveStartBlock = verdict.effectiveStartBlock;
1131
+ if (existingEffectiveStartBlock > effectiveStartBlock) {
1132
+ return warnAddressRegistration(chainId, contractAddress, {
1133
+ existingBlockNumber: existingEffectiveStartBlock,
1134
+ newBlockNumber: effectiveStartBlock
1135
+ }, `Skipping same-contract re-registration: the address is already registered for this contract. The start block does not move earlier.`);
1145
1136
  }
1146
1137
  }
1147
1138
 
@@ -1727,7 +1718,7 @@ function getReadyItemsCount(fetchState, targetSize, fromItem) {
1727
1718
  return acc;
1728
1719
  }
1729
1720
 
1730
- function make$1(startBlock, endBlock, onEventRegistrations, addressStore, addresses, maxAddrInPartition, chainId, maxOnBlockBufferSize, knownHeight, progressBlockNumberOpt, onBlockRegistrationsOpt, blockLagOpt, firstEventBlockOpt, clientFilterAddressThresholdOpt, isResumedOpt) {
1721
+ function make$1(startBlock, endBlock, onEventRegistrations, addressStore, addressRows, maxAddrInPartition, chainId, maxOnBlockBufferSize, knownHeight, progressBlockNumberOpt, onBlockRegistrationsOpt, blockLagOpt, firstEventBlockOpt, clientFilterAddressThresholdOpt, isResumedOpt) {
1731
1722
  let progressBlockNumber = progressBlockNumberOpt !== undefined ? progressBlockNumberOpt : startBlock - 1 | 0;
1732
1723
  let onBlockRegistrations = onBlockRegistrationsOpt !== undefined ? onBlockRegistrationsOpt : [];
1733
1724
  let blockLag = blockLagOpt !== undefined ? blockLagOpt : 0;
@@ -1762,29 +1753,24 @@ function make$1(startBlock, endBlock, onEventRegistrations, addressStore, addres
1762
1753
  });
1763
1754
  }
1764
1755
  let normalSelection = makeSelection(normalRegistrations, true, undefined);
1765
- AddressStore.seedBatch(addressStore, addresses.map(contract => ({
1766
- address: contract.address,
1767
- contractName: contract.contractName,
1768
- registrationBlock: contract.registrationBlock
1769
- }))).forEach((verdict, idx) => {
1770
- let contract = addresses[idx];
1771
- warnRejectedRegistration(verdict, chainId, contract.address, contract.contractName);
1772
- });
1756
+ AddressStore.seedRows(addressStore, addressRows).forEach(rejected => warnAddressRegistration(chainId, rejected.address, {
1757
+ contractName: rejected.contractName,
1758
+ existingBlockNumber: rejected.existingEffectiveStartBlock,
1759
+ newBlockNumber: rejected.effectiveStartBlock
1760
+ }, `Skipping a stored address: it is already registered for this contract.`));
1773
1761
  let dynamicContracts = new Set();
1774
1762
  let clientFilteredContracts = new Set();
1775
1763
  let registeringSetsByContract = {};
1776
- addresses.forEach(contract => {
1777
- let contractName = contract.contractName;
1764
+ Array.from(contractNamesWithNormalEvents).forEach(contractName => {
1765
+ if (addressStore.contractCount(contractName) > 0) {
1766
+ registeringSetsByContract[contractName] = AddressStore.makeSet(addressStore, contractName, undefined);
1767
+ return;
1768
+ }
1769
+ });
1770
+ addressStore.dynamicContractNames().forEach(contractName => {
1778
1771
  if (contractNamesWithNormalEvents.has(contractName)) {
1779
- if (!(contractName in registeringSetsByContract)) {
1780
- registeringSetsByContract[contractName] = AddressStore.makeSet(addressStore, contractName, undefined);
1781
- }
1782
- if (contract.registrationBlock !== -1) {
1783
- dynamicContracts.add(contractName);
1784
- return;
1785
- } else {
1786
- return;
1787
- }
1772
+ dynamicContracts.add(contractName);
1773
+ return;
1788
1774
  }
1789
1775
  });
1790
1776
  if (clientFilterAddressThreshold !== undefined) {
@@ -1797,7 +1783,7 @@ function make$1(startBlock, endBlock, onEventRegistrations, addressStore, addres
1797
1783
  }
1798
1784
  let optimizedPartitions = createPartitions(registeringSetsByContract, addressStore, dynamicContracts, clientFilteredContracts, normalSelection, maxAddrInPartition, partitions.length, partitions, progressBlockNumber, knownHeight);
1799
1785
  if (optimizedPartitions.idsInAscOrder.length === 0 && Utils.$$Array.isEmpty(onBlockRegistrations)) {
1800
- Stdlib_JsError.throwWithMessage(`Invalid configuration: Nothing to fetch on chain ` + ChainId.toString(chainId) + `. ` + (`addresses=` + addresses.length.toString() + `, `) + (`onEventRegistrations=` + onEventRegistrations.length.toString() + `, `) + (`normalRegistrations=` + normalRegistrations.length.toString() + `. `) + `Make sure that you provided at least one contract address to index, or have events with Wildcard mode enabled, or have onBlock handlers.`);
1786
+ Stdlib_JsError.throwWithMessage(`Invalid configuration: Nothing to fetch on chain ` + ChainId.toString(chainId) + `. ` + (`addresses=` + addressRows.addresses.length.toString() + `, `) + (`onEventRegistrations=` + onEventRegistrations.length.toString() + `, `) + (`normalRegistrations=` + normalRegistrations.length.toString() + `. `) + `Make sure that you provided at least one contract address to index, or have events with Wildcard mode enabled, or have onBlock handlers.`);
1801
1787
  }
1802
1788
  let buffer = [];
1803
1789
  let latestOnBlockBlockNumber;
@@ -1861,8 +1847,8 @@ function rollbackPendingQueries(mutPendingQueries, targetBlockNumber) {
1861
1847
  return adjusted;
1862
1848
  }
1863
1849
 
1864
- function rollback(fetchState, addressStore, targetBlockNumber) {
1865
- addressStore.rollback(targetBlockNumber);
1850
+ function rollback(fetchState, rolledBackAddressStore, targetBlockNumber) {
1851
+ let rolledBackAddresses = rolledBackAddressStore.rollback(targetBlockNumber);
1866
1852
  let keptPartitions = [];
1867
1853
  let nextKeptIdRef = 0;
1868
1854
  let registeringSetsByContract = {};
@@ -1921,8 +1907,8 @@ function rollback(fetchState, addressStore, targetBlockNumber) {
1921
1907
  });
1922
1908
  }
1923
1909
  }
1924
- let optimizedPartitions = createPartitions(registeringSetsByContract, addressStore, fetchState.optimizedPartitions.dynamicContracts, fetchState.optimizedPartitions.clientFilteredContracts, fetchState.normalSelection, fetchState.optimizedPartitions.maxAddrInPartition, nextKeptIdRef, keptPartitions, targetBlockNumber, fetchState.knownHeight);
1925
- return updateInternal({
1910
+ let optimizedPartitions = createPartitions(registeringSetsByContract, rolledBackAddressStore, fetchState.optimizedPartitions.dynamicContracts, fetchState.optimizedPartitions.clientFilteredContracts, fetchState.normalSelection, fetchState.optimizedPartitions.maxAddrInPartition, nextKeptIdRef, keptPartitions, targetBlockNumber, fetchState.knownHeight);
1911
+ let rolledBack = updateInternal({
1926
1912
  optimizedPartitions: fetchState.optimizedPartitions,
1927
1913
  startBlock: fetchState.startBlock,
1928
1914
  endBlock: fetchState.endBlock,
@@ -1941,6 +1927,10 @@ function rollback(fetchState, addressStore, targetBlockNumber) {
1941
1927
  tmp = item.kind === 0 ? item.blockNumber : item.blockNumber;
1942
1928
  return tmp <= targetBlockNumber;
1943
1929
  }), true, undefined, undefined);
1930
+ return {
1931
+ fetchState: rolledBack,
1932
+ rolledBackAddresses: rolledBackAddresses
1933
+ };
1944
1934
  }
1945
1935
 
1946
1936
  function resetPendingQueries(fetchState) {
@@ -154,6 +154,7 @@ let buildOnEventRegistrationWith = (
154
154
  ~isWildcard,
155
155
  ~handler,
156
156
  ~contractRegister,
157
+ ~where,
157
158
  ~fieldSelection?,
158
159
  ~startBlock?,
159
160
  ) :> Internal.onEventRegistration)
@@ -320,7 +321,7 @@ let addOnEventRegistration = (
320
321
  )
321
322
  | Fuel =>
322
323
  JsError.throwWithMessage(
323
- `The fields option of the "${eventName}" event registration on contract "${contractName}" is only supported on EVM. Select the fields in your config instead.`,
324
+ `The fields option of the "${eventName}" event registration on contract "${contractName}" is not supported on Fuel. Select the fields in your config instead.`,
324
325
  )
325
326
  }
326
327
  }
@@ -359,17 +360,18 @@ let addOnEventRegistration = (
359
360
  if !matched.contents {
360
361
  let (contractNames, eventNames) = registration->describeConfigured(~contractName)
361
362
  let listOr = (names, empty) =>
362
- names->Utils.Array.isEmpty ? empty : names->Array.joinUnsafe(", ")
363
+ names->Utils.Array.isEmpty ? empty : names->Utils.Array.quotedJoin
364
+ let {contractNoun, eventNoun} = registration.config.ecosystem
363
365
  if eventNames->Utils.Array.isEmpty {
364
366
  JsError.throwWithMessage(
365
- `Contract "${contractName}" is not configured on any chain, so its handler for "${eventName}" would never run. Add it to your config, or remove the registration. Configured contracts: ${listOr(
367
+ `${contractNoun->Utils.String.capitalize} "${contractName}" is not configured on any chain, so its handler for "${eventName}" would never run. Add it to your config, or remove the registration. Configured ${contractNoun}s: ${listOr(
366
368
  contractNames,
367
369
  "none",
368
370
  )}.`,
369
371
  )
370
372
  } else {
371
373
  JsError.throwWithMessage(
372
- `Event "${eventName}" is not configured on contract "${contractName}", so its handler would never run. Add it to your config, or remove the registration. Configured events on "${contractName}": ${listOr(
374
+ `${eventNoun->Utils.String.capitalize} "${eventName}" is not configured on ${contractNoun} "${contractName}", so its handler would never run. Add it to your config, or remove the registration. Configured ${eventNoun}s on "${contractName}": ${listOr(
373
375
  eventNames,
374
376
  "none",
375
377
  )}.`,