envio 3.0.0-alpha.2 → 3.0.0-alpha.20

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 (175) hide show
  1. package/README.md +164 -30
  2. package/bin.mjs +49 -0
  3. package/evm.schema.json +79 -169
  4. package/fuel.schema.json +50 -21
  5. package/index.d.ts +497 -1
  6. package/index.js +4 -0
  7. package/package.json +42 -31
  8. package/rescript.json +4 -1
  9. package/src/Batch.res +11 -8
  10. package/src/Batch.res.mjs +11 -9
  11. package/src/ChainFetcher.res +531 -0
  12. package/src/ChainFetcher.res.mjs +339 -0
  13. package/src/ChainManager.res +190 -0
  14. package/src/ChainManager.res.mjs +166 -0
  15. package/src/Change.res +3 -3
  16. package/src/Config.gen.ts +19 -0
  17. package/src/Config.res +737 -22
  18. package/src/Config.res.mjs +703 -26
  19. package/src/{Indexer.res → Ctx.res} +1 -1
  20. package/src/Ecosystem.res +9 -124
  21. package/src/Ecosystem.res.mjs +19 -160
  22. package/src/Env.res +30 -74
  23. package/src/Env.res.mjs +25 -87
  24. package/src/Envio.gen.ts +3 -1
  25. package/src/Envio.res +20 -9
  26. package/src/EventProcessing.res +469 -0
  27. package/src/EventProcessing.res.mjs +337 -0
  28. package/src/EvmTypes.gen.ts +6 -0
  29. package/src/EvmTypes.res +1 -0
  30. package/src/FetchState.res +1256 -639
  31. package/src/FetchState.res.mjs +1135 -612
  32. package/src/GlobalState.res +1190 -0
  33. package/src/GlobalState.res.mjs +1183 -0
  34. package/src/GlobalStateManager.res +68 -0
  35. package/src/GlobalStateManager.res.mjs +75 -0
  36. package/src/GlobalStateManager.resi +7 -0
  37. package/src/HandlerLoader.res +89 -0
  38. package/src/HandlerLoader.res.mjs +79 -0
  39. package/src/HandlerRegister.res +357 -0
  40. package/src/HandlerRegister.res.mjs +299 -0
  41. package/src/{EventRegister.resi → HandlerRegister.resi} +13 -13
  42. package/src/Hasura.res +111 -175
  43. package/src/Hasura.res.mjs +88 -150
  44. package/src/InMemoryStore.res +1 -1
  45. package/src/InMemoryStore.res.mjs +3 -3
  46. package/src/InMemoryTable.res +1 -1
  47. package/src/InMemoryTable.res.mjs +1 -1
  48. package/src/Internal.gen.ts +4 -0
  49. package/src/Internal.res +230 -12
  50. package/src/Internal.res.mjs +115 -1
  51. package/src/LoadLayer.res +444 -0
  52. package/src/LoadLayer.res.mjs +296 -0
  53. package/src/LoadLayer.resi +32 -0
  54. package/src/LogSelection.res +33 -27
  55. package/src/LogSelection.res.mjs +6 -0
  56. package/src/Logging.res +21 -7
  57. package/src/Logging.res.mjs +16 -8
  58. package/src/Main.res +377 -0
  59. package/src/Main.res.mjs +339 -0
  60. package/src/Persistence.res +7 -21
  61. package/src/Persistence.res.mjs +3 -3
  62. package/src/PgStorage.gen.ts +10 -0
  63. package/src/PgStorage.res +116 -69
  64. package/src/PgStorage.res.d.mts +5 -0
  65. package/src/PgStorage.res.mjs +93 -50
  66. package/src/Prometheus.res +294 -224
  67. package/src/Prometheus.res.mjs +353 -340
  68. package/src/ReorgDetection.res +6 -10
  69. package/src/ReorgDetection.res.mjs +6 -6
  70. package/src/SafeCheckpointTracking.res +4 -4
  71. package/src/SafeCheckpointTracking.res.mjs +2 -2
  72. package/src/Sink.res +4 -2
  73. package/src/Sink.res.mjs +2 -1
  74. package/src/TableIndices.res +0 -1
  75. package/src/TestIndexer.res +692 -0
  76. package/src/TestIndexer.res.mjs +527 -0
  77. package/src/TestIndexerProxyStorage.res +205 -0
  78. package/src/TestIndexerProxyStorage.res.mjs +151 -0
  79. package/src/TopicFilter.res +1 -1
  80. package/src/Types.ts +1 -1
  81. package/src/UserContext.res +424 -0
  82. package/src/UserContext.res.mjs +279 -0
  83. package/src/Utils.res +97 -26
  84. package/src/Utils.res.mjs +91 -44
  85. package/src/bindings/BigInt.res +10 -0
  86. package/src/bindings/BigInt.res.mjs +15 -0
  87. package/src/bindings/ClickHouse.res +120 -23
  88. package/src/bindings/ClickHouse.res.mjs +118 -28
  89. package/src/bindings/DateFns.res +74 -0
  90. package/src/bindings/DateFns.res.mjs +22 -0
  91. package/src/bindings/EventSource.res +8 -1
  92. package/src/bindings/EventSource.res.mjs +8 -1
  93. package/src/bindings/Express.res +1 -0
  94. package/src/bindings/Hrtime.res +14 -1
  95. package/src/bindings/Hrtime.res.mjs +22 -2
  96. package/src/bindings/Hrtime.resi +4 -0
  97. package/src/bindings/Lodash.res +0 -1
  98. package/src/bindings/NodeJs.res +49 -3
  99. package/src/bindings/NodeJs.res.mjs +11 -3
  100. package/src/bindings/Pino.res +24 -10
  101. package/src/bindings/Pino.res.mjs +14 -8
  102. package/src/bindings/Postgres.gen.ts +8 -0
  103. package/src/bindings/Postgres.res +5 -1
  104. package/src/bindings/Postgres.res.d.mts +5 -0
  105. package/src/bindings/PromClient.res +0 -10
  106. package/src/bindings/PromClient.res.mjs +0 -3
  107. package/src/bindings/Vitest.res +142 -0
  108. package/src/bindings/Vitest.res.mjs +9 -0
  109. package/src/bindings/WebSocket.res +27 -0
  110. package/src/bindings/WebSocket.res.mjs +2 -0
  111. package/src/bindings/Yargs.res +8 -0
  112. package/src/bindings/Yargs.res.mjs +2 -0
  113. package/src/db/EntityHistory.res +7 -7
  114. package/src/db/EntityHistory.res.mjs +9 -9
  115. package/src/db/InternalTable.res +59 -111
  116. package/src/db/InternalTable.res.mjs +73 -104
  117. package/src/db/Table.res +27 -8
  118. package/src/db/Table.res.mjs +25 -14
  119. package/src/sources/Evm.res +84 -0
  120. package/src/sources/Evm.res.mjs +105 -0
  121. package/src/sources/EvmChain.res +94 -0
  122. package/src/sources/EvmChain.res.mjs +60 -0
  123. package/src/sources/Fuel.res +19 -34
  124. package/src/sources/Fuel.res.mjs +34 -16
  125. package/src/sources/FuelSDK.res +38 -0
  126. package/src/sources/FuelSDK.res.mjs +29 -0
  127. package/src/sources/HyperFuel.res +2 -2
  128. package/src/sources/HyperFuel.resi +1 -1
  129. package/src/sources/HyperFuelClient.res +2 -2
  130. package/src/sources/HyperFuelSource.res +33 -13
  131. package/src/sources/HyperFuelSource.res.mjs +24 -16
  132. package/src/sources/HyperSync.res +36 -6
  133. package/src/sources/HyperSync.res.mjs +9 -7
  134. package/src/sources/HyperSync.resi +4 -0
  135. package/src/sources/HyperSyncClient.res +1 -1
  136. package/src/sources/HyperSyncHeightStream.res +47 -116
  137. package/src/sources/HyperSyncHeightStream.res.mjs +46 -73
  138. package/src/sources/HyperSyncSource.res +118 -139
  139. package/src/sources/HyperSyncSource.res.mjs +104 -121
  140. package/src/sources/Rpc.res +86 -14
  141. package/src/sources/Rpc.res.mjs +101 -9
  142. package/src/sources/RpcSource.res +621 -364
  143. package/src/sources/RpcSource.res.mjs +843 -410
  144. package/src/sources/RpcWebSocketHeightStream.res +181 -0
  145. package/src/sources/RpcWebSocketHeightStream.res.mjs +196 -0
  146. package/src/sources/Source.res +7 -5
  147. package/src/sources/SourceManager.res +325 -225
  148. package/src/sources/SourceManager.res.mjs +314 -171
  149. package/src/sources/SourceManager.resi +17 -6
  150. package/src/sources/Svm.res +81 -0
  151. package/src/sources/Svm.res.mjs +90 -0
  152. package/src/tui/Tui.res +247 -0
  153. package/src/tui/Tui.res.mjs +337 -0
  154. package/src/tui/bindings/Ink.res +371 -0
  155. package/src/tui/bindings/Ink.res.mjs +72 -0
  156. package/src/tui/bindings/Style.res +123 -0
  157. package/src/tui/bindings/Style.res.mjs +2 -0
  158. package/src/tui/components/BufferedProgressBar.res +40 -0
  159. package/src/tui/components/BufferedProgressBar.res.mjs +57 -0
  160. package/src/tui/components/CustomHooks.res +122 -0
  161. package/src/tui/components/CustomHooks.res.mjs +179 -0
  162. package/src/tui/components/Messages.res +41 -0
  163. package/src/tui/components/Messages.res.mjs +75 -0
  164. package/src/tui/components/SyncETA.res +174 -0
  165. package/src/tui/components/SyncETA.res.mjs +263 -0
  166. package/src/tui/components/TuiData.res +47 -0
  167. package/src/tui/components/TuiData.res.mjs +34 -0
  168. package/svm.schema.json +112 -0
  169. package/bin.js +0 -48
  170. package/src/EventRegister.res +0 -241
  171. package/src/EventRegister.res.mjs +0 -240
  172. package/src/bindings/Ethers.gen.ts +0 -14
  173. package/src/bindings/Ethers.res +0 -204
  174. package/src/bindings/Ethers.res.mjs +0 -130
  175. /package/src/{Indexer.res.mjs → Ctx.res.mjs} +0 -0
@@ -1,13 +1,15 @@
1
1
  // Generated by ReScript, PLEASE EDIT WITH CARE
2
2
 
3
- import * as Viem from "../bindings/Viem.res.mjs";
3
+ import * as Rest from "../vendored/Rest.res.mjs";
4
4
  import * as Utils from "../Utils.res.mjs";
5
5
  import * as Hrtime from "../bindings/Hrtime.res.mjs";
6
6
  import * as Js_exn from "rescript/lib/es6/js_exn.js";
7
7
  import * as Source from "./Source.res.mjs";
8
8
  import * as Logging from "../Logging.res.mjs";
9
+ import * as Internal from "../Internal.res.mjs";
9
10
  import * as HyperSync from "./HyperSync.res.mjs";
10
11
  import * as Belt_Array from "rescript/lib/es6/belt_Array.js";
12
+ import * as Prometheus from "../Prometheus.res.mjs";
11
13
  import * as Belt_Option from "rescript/lib/es6/belt_Option.js";
12
14
  import * as EventRouter from "./EventRouter.res.mjs";
13
15
  import * as LogSelection from "../LogSelection.res.mjs";
@@ -15,6 +17,7 @@ import * as ErrorHandling from "../ErrorHandling.res.mjs";
15
17
  import * as Caml_exceptions from "rescript/lib/es6/caml_exceptions.js";
16
18
  import * as HyperSyncClient from "./HyperSyncClient.res.mjs";
17
19
  import * as Caml_splice_call from "rescript/lib/es6/caml_splice_call.js";
20
+ import * as HyperSyncJsonApi from "./HyperSyncJsonApi.res.mjs";
18
21
  import * as Caml_js_exceptions from "rescript/lib/es6/caml_js_exceptions.js";
19
22
  import * as HyperSyncHeightStream from "./HyperSyncHeightStream.res.mjs";
20
23
 
@@ -29,13 +32,19 @@ function getSelectionConfig(selection, chain) {
29
32
  var noAddressesTopicSelections = [];
30
33
  var contractNames = new Set();
31
34
  Belt_Array.forEach(selection.eventConfigs, (function (param) {
32
- var transactionSchema = param.transactionSchema;
33
- var blockSchema = param.blockSchema;
34
35
  var contractName = param.contractName;
35
- Utils.$$Set.addMany(nonOptionalBlockFieldNames, Utils.Schema.getNonOptionalFieldNames(blockSchema));
36
- Utils.$$Set.addMany(nonOptionalTransactionFieldNames, Utils.Schema.getNonOptionalFieldNames(transactionSchema));
37
- Utils.$$Set.addMany(capitalizedBlockFields, Utils.Schema.getCapitalizedFieldNames(blockSchema));
38
- Utils.$$Set.addMany(capitalizedTransactionFields, Utils.Schema.getCapitalizedFieldNames(transactionSchema));
36
+ Belt_Array.forEach(Array.from(param.selectedBlockFields), (function (name) {
37
+ if (!Internal.evmNullableBlockFields.has(name)) {
38
+ nonOptionalBlockFieldNames.add(name);
39
+ }
40
+ capitalizedBlockFields.add(Utils.$$String.capitalize(name));
41
+ }));
42
+ Belt_Array.forEach(Array.from(param.selectedTransactionFields), (function (name) {
43
+ if (!Internal.evmNullableTransactionFields.has(name)) {
44
+ nonOptionalTransactionFieldNames.add(name);
45
+ }
46
+ capitalizedTransactionFields.add(Utils.$$String.capitalize(name));
47
+ }));
39
48
  var eventFilters = param.getEventFiltersOrThrow(chain);
40
49
  if (param.dependsOnAddresses) {
41
50
  contractNames.add(contractName);
@@ -109,28 +118,22 @@ function getSelectionConfig(selection, chain) {
109
118
  }
110
119
 
111
120
  function memoGetSelectionConfig(chain) {
112
- var cache = new WeakMap();
113
- return function (selection) {
114
- var c = cache.get(selection);
115
- if (c !== undefined) {
116
- return c;
117
- }
118
- var c$1 = getSelectionConfig(selection, chain);
119
- cache.set(selection, c$1);
120
- return c$1;
121
- };
121
+ return Utils.$$WeakMap.memoize(function (selection) {
122
+ return getSelectionConfig(selection, chain);
123
+ });
122
124
  }
123
125
 
124
126
  function make(param) {
125
127
  var lowercaseAddresses = param.lowercaseAddresses;
128
+ var apiToken = param.apiToken;
126
129
  var eventRouter = param.eventRouter;
127
- var shouldUseHypersyncClientDecoder = param.shouldUseHypersyncClientDecoder;
128
130
  var allEventSignatures = param.allEventSignatures;
129
131
  var endpointUrl = param.endpointUrl;
130
132
  var chain = param.chain;
133
+ var name = "HyperSync";
131
134
  var getSelectionConfig = memoGetSelectionConfig(chain);
132
- var apiToken = Belt_Option.getWithDefault(param.apiToken, "3dc856dd-b0ea-494f-b27e-017b8b6b7e07");
133
- var client = HyperSyncClient.make(endpointUrl, apiToken, param.clientTimeoutMillis, param.clientMaxRetries, !lowercaseAddresses, param.serializationFormat, param.enableQueryCaching, undefined, undefined, undefined);
135
+ var apiToken$1 = apiToken !== undefined ? apiToken : Js_exn.raiseError("An API token is required for using HyperSync as a data-source.\nSet the ENVIO_API_TOKEN environment variable in your .env file.\nLearn more or get a free API token at: https://envio.dev/app/api-tokens");
136
+ var client = HyperSyncClient.make(endpointUrl, apiToken$1, param.clientTimeoutMillis, param.clientMaxRetries, !lowercaseAddresses, param.serializationFormat, param.enableQueryCaching, undefined, undefined, undefined);
134
137
  var hscDecoder = {
135
138
  contents: undefined
136
139
  };
@@ -145,7 +148,7 @@ function make(param) {
145
148
  }
146
149
  catch (raw_exn){
147
150
  var exn = Caml_js_exceptions.internalToOCamlException(raw_exn);
148
- return ErrorHandling.mkLogAndRaise(undefined, "Failed to instantiate a decoder from hypersync client, please double check your ABI or try using 'event_decoder: viem' config option", exn);
151
+ return ErrorHandling.mkLogAndRaise(undefined, "Failed to instantiate a decoder from hypersync client, please double check your ABI", exn);
149
152
  }
150
153
  if (lowercaseAddresses) {
151
154
  decoder$1.disableChecksummedAddresses();
@@ -175,11 +178,7 @@ function make(param) {
175
178
  }
176
179
  };
177
180
  };
178
- var contractNameAbiMapping = {};
179
- Belt_Array.forEach(param.contracts, (function (contract) {
180
- contractNameAbiMapping[contract.name] = contract.abi;
181
- }));
182
- var getItemsOrThrow = async function (fromBlock, toBlock, addressesByContractName, indexingContracts, currentBlockHeight, param, selection, retry, logger) {
181
+ var getItemsOrThrow = async function (fromBlock, toBlock, addressesByContractName, indexingContracts, knownHeight, param, selection, retry, logger) {
183
182
  var mkLogAndRaise = function (extra, extra$1) {
184
183
  return ErrorHandling.mkLogAndRaise(logger, extra, extra$1);
185
184
  };
@@ -194,6 +193,7 @@ function make(param) {
194
193
  logSelections = ErrorHandling.mkLogAndRaise(logger, "Failed getting log selection for the query", exn);
195
194
  }
196
195
  var startFetchingBatchTimeRef = Hrtime.makeTimer();
196
+ Prometheus.SourceRequestCount.increment(name, chain, "getLogs");
197
197
  var pageUnsafe;
198
198
  try {
199
199
  pageUnsafe = await HyperSync.GetLogs.query(client, fromBlock, toBlock, logSelections, selectionConfig.fieldSelection, selectionConfig.nonOptionalBlockFieldNames, selectionConfig.nonOptionalTransactionFieldNames);
@@ -221,7 +221,7 @@ function make(param) {
221
221
  _1: {
222
222
  TAG: "FailedGettingItems",
223
223
  exn: null,
224
- attemptedToBlock: Belt_Option.getWithDefault(toBlock, currentBlockHeight),
224
+ attemptedToBlock: Belt_Option.getWithDefault(toBlock, knownHeight),
225
225
  retry: tmp
226
226
  },
227
227
  Error: new Error()
@@ -232,7 +232,7 @@ function make(param) {
232
232
  _1: {
233
233
  TAG: "FailedGettingItems",
234
234
  exn: error,
235
- attemptedToBlock: Belt_Option.getWithDefault(toBlock, currentBlockHeight),
235
+ attemptedToBlock: Belt_Option.getWithDefault(toBlock, knownHeight),
236
236
  retry: {
237
237
  TAG: "WithBackoff",
238
238
  message: "Unexpected issue while fetching events from HyperSync client. Attempt a retry.",
@@ -242,8 +242,8 @@ function make(param) {
242
242
  Error: new Error()
243
243
  };
244
244
  }
245
- var pageFetchTime = Hrtime.intFromMillis(Hrtime.toMillis(Hrtime.timeSince(startFetchingBatchTimeRef)));
246
- var currentBlockHeight$1 = pageUnsafe.archiveHeight;
245
+ var pageFetchTime = Hrtime.toSecondsFloat(Hrtime.timeSince(startFetchingBatchTimeRef));
246
+ var knownHeight$1 = pageUnsafe.archiveHeight;
247
247
  var heighestBlockQueried = pageUnsafe.nextBlock - 1 | 0;
248
248
  var match = pageUnsafe.rollbackGuard;
249
249
  var lastBlockQueriedPromise;
@@ -271,7 +271,7 @@ function make(param) {
271
271
  exit = 1;
272
272
  }
273
273
  if (exit === 1) {
274
- lastBlockQueriedPromise = HyperSync.queryBlockData(endpointUrl, apiToken, heighestBlockQueried, logger).then(function (res) {
274
+ lastBlockQueriedPromise = HyperSync.queryBlockData(endpointUrl, apiToken$1, heighestBlockQueried, name, chain, logger).then(function (res) {
275
275
  if (res.TAG !== "Ok") {
276
276
  return mkLogAndRaise("Failed to query blockData for block " + String(heighestBlockQueried), HyperSync.queryErrorToMsq(res._0));
277
277
  }
@@ -289,68 +289,47 @@ function make(param) {
289
289
  }
290
290
  var parsingTimeRef = Hrtime.makeTimer();
291
291
  var parsedQueueItems = [];
292
- var handleDecodeFailure = function (eventConfig, decoder, logIndex, blockNumber, chainId, exn) {
293
- if (eventConfig.isWildcard) {
294
- return ;
295
- }
296
- var msg = "Event " + eventConfig.name + " was unexpectedly parsed as undefined";
297
- var logger$1 = Logging.createChildFrom(logger, {
298
- chainId: chainId,
299
- blockNumber: blockNumber,
300
- logIndex: logIndex,
301
- decoder: decoder
302
- });
303
- ErrorHandling.mkLogAndRaise(logger$1, msg, exn);
304
- };
305
- if (shouldUseHypersyncClientDecoder) {
306
- var parsedEvents;
307
- try {
308
- parsedEvents = await getHscDecoder().decodeEvents(pageUnsafe.events);
309
- }
310
- catch (raw_exn$1){
311
- var exn$1 = Caml_js_exceptions.internalToOCamlException(raw_exn$1);
312
- parsedEvents = mkLogAndRaise("Failed to parse events using hypersync client, please double check your ABI.", exn$1);
313
- }
314
- Belt_Array.forEachWithIndex(pageUnsafe.items, (function (index, item) {
315
- var block = item.block;
316
- var log = item.log;
317
- var topic0 = log.topics[0];
318
- var maybeEventConfig = EventRouter.get(eventRouter, EventRouter.getEvmEventId(topic0, log.topics.length), log.address, block.number, indexingContracts);
319
- var maybeDecodedEvent = parsedEvents[index];
320
- if (maybeEventConfig === undefined) {
321
- return ;
322
- }
323
- if (maybeDecodedEvent === null || maybeDecodedEvent === undefined) {
324
- maybeDecodedEvent === null;
325
- } else {
326
- parsedQueueItems.push(makeEventBatchQueueItem(item, maybeEventConfig.convertHyperSyncEventArgs(maybeDecodedEvent), maybeEventConfig));
327
- return ;
328
- }
329
- handleDecodeFailure(maybeEventConfig, "hypersync-client", log.logIndex, block.number, chain, {
330
- RE_EXN_ID: UndefinedValue
331
- });
332
- }));
333
- } else {
334
- Belt_Array.forEach(pageUnsafe.items, (function (item) {
335
- var block = item.block;
336
- var log = item.log;
337
- var topic0 = log.topics[0];
338
- var eventConfig = EventRouter.get(eventRouter, EventRouter.getEvmEventId(topic0, log.topics.length), log.address, block.number, indexingContracts);
339
- if (eventConfig === undefined) {
340
- return ;
341
- }
342
- var decodedEvent;
343
- try {
344
- decodedEvent = Viem.parseLogOrThrow(contractNameAbiMapping, eventConfig.contractName, log.topics, log.data);
345
- }
346
- catch (raw_exn){
347
- var exn = Caml_js_exceptions.internalToOCamlException(raw_exn);
348
- return handleDecodeFailure(eventConfig, "viem", log.logIndex, block.number, chain, exn);
349
- }
350
- parsedQueueItems.push(makeEventBatchQueueItem(item, decodedEvent.args, eventConfig));
351
- }));
292
+ var parsedEvents;
293
+ try {
294
+ parsedEvents = await getHscDecoder().decodeEvents(pageUnsafe.events);
352
295
  }
353
- var parsingTimeElapsed = Hrtime.intFromMillis(Hrtime.toMillis(Hrtime.timeSince(parsingTimeRef)));
296
+ catch (raw_exn$1){
297
+ var exn$1 = Caml_js_exceptions.internalToOCamlException(raw_exn$1);
298
+ parsedEvents = mkLogAndRaise("Failed to parse events using hypersync client, please double check your ABI.", exn$1);
299
+ }
300
+ Belt_Array.forEachWithIndex(pageUnsafe.items, (function (index, item) {
301
+ var block = item.block;
302
+ var log = item.log;
303
+ var topic0 = Utils.$$Array.firstUnsafe(log.topics);
304
+ var maybeEventConfig = EventRouter.get(eventRouter, EventRouter.getEvmEventId(topic0, log.topics.length), log.address, block.number, indexingContracts);
305
+ var maybeDecodedEvent = parsedEvents[index];
306
+ if (maybeEventConfig === undefined) {
307
+ return ;
308
+ }
309
+ if (maybeDecodedEvent === null || maybeDecodedEvent === undefined) {
310
+ maybeDecodedEvent === null;
311
+ } else {
312
+ parsedQueueItems.push(makeEventBatchQueueItem(item, maybeEventConfig.convertHyperSyncEventArgs(maybeDecodedEvent), maybeEventConfig));
313
+ return ;
314
+ }
315
+ var logIndex = log.logIndex;
316
+ var blockNumber = block.number;
317
+ var exn = {
318
+ RE_EXN_ID: UndefinedValue
319
+ };
320
+ if (maybeEventConfig.isWildcard) {
321
+ return ;
322
+ }
323
+ var msg = "Event " + maybeEventConfig.name + " was unexpectedly parsed as undefined";
324
+ var logger$1 = Logging.createChildFrom(logger, {
325
+ chainId: chain,
326
+ blockNumber: blockNumber,
327
+ logIndex: logIndex,
328
+ decoder: "hypersync-client"
329
+ });
330
+ ErrorHandling.mkLogAndRaise(logger$1, msg, exn);
331
+ }));
332
+ var parsingTimeElapsed = Hrtime.toSecondsFloat(Hrtime.timeSince(parsingTimeRef));
354
333
  var rangeLastBlock = await lastBlockQueriedPromise;
355
334
  var reorgGuard_prevRangeLastBlock = Belt_Option.map(pageUnsafe.rollbackGuard, (function (v) {
356
335
  return {
@@ -362,16 +341,16 @@ function make(param) {
362
341
  rangeLastBlock: rangeLastBlock,
363
342
  prevRangeLastBlock: reorgGuard_prevRangeLastBlock
364
343
  };
365
- var totalTimeElapsed = Hrtime.intFromMillis(Hrtime.toMillis(Hrtime.timeSince(totalTimeRef)));
366
- var stats_parsing$unknowntime$unknown$lparms$rpar = parsingTimeElapsed;
367
- var stats_page$unknownfetch$unknowntime$unknown$lparms$rpar = pageFetchTime;
344
+ var totalTimeElapsed = Hrtime.toSecondsFloat(Hrtime.timeSince(totalTimeRef));
345
+ var stats_parsing$unknowntime$unknown$lpars$rpar = parsingTimeElapsed;
346
+ var stats_page$unknownfetch$unknowntime$unknown$lpars$rpar = pageFetchTime;
368
347
  var stats = {
369
- "total time elapsed (ms)": totalTimeElapsed,
370
- "parsing time (ms)": stats_parsing$unknowntime$unknown$lparms$rpar,
371
- "page fetch time (ms)": stats_page$unknownfetch$unknowntime$unknown$lparms$rpar
348
+ "total time elapsed (s)": totalTimeElapsed,
349
+ "parsing time (s)": stats_parsing$unknowntime$unknown$lpars$rpar,
350
+ "page fetch time (s)": stats_page$unknownfetch$unknowntime$unknown$lpars$rpar
372
351
  };
373
352
  return {
374
- currentBlockHeight: currentBlockHeight$1,
353
+ knownHeight: knownHeight$1,
375
354
  reorgGuard: reorgGuard,
376
355
  parsedQueueItems: parsedQueueItems,
377
356
  fromBlockQueried: fromBlock,
@@ -381,36 +360,40 @@ function make(param) {
381
360
  };
382
361
  };
383
362
  var getBlockHashes = function (blockNumbers, logger) {
384
- return HyperSync.queryBlockDataMulti(endpointUrl, apiToken, blockNumbers, logger).then(HyperSync.mapExn);
363
+ return HyperSync.queryBlockDataMulti(endpointUrl, apiToken$1, blockNumbers, name, chain, logger).then(HyperSync.mapExn);
385
364
  };
386
- var heightStream = HyperSyncHeightStream.make(endpointUrl, apiToken);
365
+ var jsonApiClient = Rest.client(endpointUrl, undefined);
387
366
  return {
388
- name: "HyperSync",
367
+ name: name,
389
368
  sourceFor: "Sync",
390
369
  chain: chain,
391
370
  poweredByHyperSync: true,
392
- pollingInterval: 32,
371
+ pollingInterval: 100,
393
372
  getBlockHashes: getBlockHashes,
394
373
  getHeightOrThrow: (async function () {
395
- try {
396
- return await HyperSyncHeightStream.getHeight(heightStream);
397
- }
398
- catch (raw_exn){
399
- var exn = Caml_js_exceptions.internalToOCamlException(raw_exn);
400
- if (exn.RE_EXN_ID === Js_exn.$$Error) {
401
- if (Belt_Option.getWithDefault(exn._1.message, "").includes("Your token is malformed. For more info: https://docs.envio.dev/docs/HyperSync/api-tokens.")) {
402
- Logging.error("Your ENVIO_API_TOKEN is malformed. The indexer will not be able to fetch events. Update the token and restart the indexer using 'pnpm envio start'. For more info: https://docs.envio.dev/docs/HyperSync/api-tokens");
403
- await new Promise((function (param, param$1) {
404
-
405
- }));
406
- return 0;
407
- }
408
- throw exn;
409
- }
410
- throw exn;
374
+ var timerRef = Hrtime.makeTimer();
375
+ var height = await Rest.$$fetch(HyperSyncJsonApi.heightRoute, apiToken$1, jsonApiClient);
376
+ var result;
377
+ if (typeof height === "number") {
378
+ result = height;
379
+ } else if (height === "Your token is malformed. For more info: https://docs.envio.dev/docs/HyperSync/api-tokens.") {
380
+ Logging.error("Your ENVIO_API_TOKEN is malformed. The indexer will not be able to fetch events. Update the token and restart the indexer using 'pnpm envio start'. For more info: https://docs.envio.dev/docs/HyperSync/api-tokens");
381
+ await new Promise((function (param, param$1) {
382
+
383
+ }));
384
+ result = 0;
385
+ } else {
386
+ result = Js_exn.raiseError(height);
411
387
  }
388
+ var seconds = Hrtime.toSecondsFloat(Hrtime.timeSince(timerRef));
389
+ Prometheus.SourceRequestCount.increment(name, chain, "getHeight");
390
+ Prometheus.SourceRequestCount.addSeconds(name, chain, "getHeight", seconds);
391
+ return result;
412
392
  }),
413
- getItemsOrThrow: getItemsOrThrow
393
+ getItemsOrThrow: getItemsOrThrow,
394
+ createHeightSubscription: (function (onHeight) {
395
+ return HyperSyncHeightStream.subscribe(endpointUrl, apiToken$1, chain, onHeight);
396
+ })
414
397
  };
415
398
  }
416
399
 
@@ -419,4 +402,4 @@ export {
419
402
  memoGetSelectionConfig ,
420
403
  make ,
421
404
  }
422
- /* Viem Not a pure module */
405
+ /* Rest Not a pure module */
@@ -1,3 +1,6 @@
1
+ type rpcError = {code: int, message: string}
2
+ exception JsonRpcError(rpcError)
3
+
1
4
  let makeRpcRoute = (method: string, paramsSchema, resultSchema) => {
2
5
  let idSchema = S.literal(1)
3
6
  let versionSchema = S.literal("2.0")
@@ -20,6 +23,23 @@ let makeRpcRoute = (method: string, paramsSchema, resultSchema) => {
20
23
  })
21
24
  }
22
25
 
26
+ let jsonRpcFetcher: Rest.ApiFetcher.t = async args => {
27
+ let response = await Rest.ApiFetcher.default(args)
28
+ let data: {..} = response.data->Obj.magic
29
+ switch data["error"]->Js.Nullable.toOption {
30
+ | Some(error) =>
31
+ raise(
32
+ JsonRpcError({
33
+ code: error["code"],
34
+ message: error["message"],
35
+ }),
36
+ )
37
+ | None => response
38
+ }
39
+ }
40
+
41
+ let makeClient = url => Rest.client(url, ~fetcher=jsonRpcFetcher)
42
+
23
43
  type hex = string
24
44
  let makeHexSchema = fromStr =>
25
45
  S.string->S.transform(s => {
@@ -28,13 +48,25 @@ let makeHexSchema = fromStr =>
28
48
  | Some(v) => v
29
49
  | None => s.fail("The string is not valid hex")
30
50
  },
31
- serializer: value => value->Viem.toHex->Utils.magic,
51
+ serializer: value => value->Viem.toHex->(Utils.magic: EvmTypes.Hex.t => 'a),
32
52
  })
33
53
 
34
54
  let hexBigintSchema: S.schema<bigint> = makeHexSchema(BigInt.fromString)
35
55
  external number: string => int = "Number"
36
56
  let hexIntSchema: S.schema<int> = makeHexSchema(v => v->number->Some)
37
57
 
58
+ external parseFloat: string => float = "Number"
59
+ let decimalFloatSchema: S.schema<float> = S.string->S.transform(s => {
60
+ parser: str => {
61
+ let v = parseFloat(str)
62
+ if Js.Float.isNaN(v) {
63
+ s.fail("The string is not a valid decimal number")
64
+ } else {
65
+ v
66
+ }
67
+ },
68
+ })
69
+
38
70
  module GetLogs = {
39
71
  @unboxed
40
72
  type topicFilter = Single(hex) | Multiple(array<hex>) | @as(null) Null
@@ -77,7 +109,7 @@ module GetLogs = {
77
109
  type param = {
78
110
  fromBlock: int,
79
111
  toBlock: int,
80
- address: array<Address.t>,
112
+ address?: array<Address.t>,
81
113
  topics: topicQuery,
82
114
  // blockHash?: string,
83
115
  }
@@ -85,11 +117,13 @@ module GetLogs = {
85
117
  let paramsSchema = S.object((s): param => {
86
118
  fromBlock: s.field("fromBlock", hexIntSchema),
87
119
  toBlock: s.field("toBlock", hexIntSchema),
88
- address: s.field("address", S.array(Address.schema)),
120
+ address: ?s.field("address", S.option(S.array(Address.schema))),
89
121
  topics: s.field("topics", topicQuerySchema),
90
122
  // blockHash: ?s.field("blockHash", S.option(S.string)),
91
123
  })
92
124
 
125
+ let fullParamsSchema = S.tuple1(paramsSchema)
126
+
93
127
  type log = {
94
128
  address: Address.t,
95
129
  topics: array<hex>,
@@ -114,7 +148,9 @@ module GetLogs = {
114
148
  removed: s.field("removed", S.bool),
115
149
  })
116
150
 
117
- let route = makeRpcRoute("eth_getLogs", S.tuple1(paramsSchema), S.array(logSchema))
151
+ let resultSchema = S.array(logSchema)
152
+
153
+ let route = makeRpcRoute("eth_getLogs", fullParamsSchema, resultSchema)
118
154
  }
119
155
 
120
156
  module GetBlockByNumber = {
@@ -125,7 +161,7 @@ module GetBlockByNumber = {
125
161
  gasUsed: bigint,
126
162
  hash: hex,
127
163
  logsBloom: hex,
128
- miner: Address.t,
164
+ mutable miner: Address.t,
129
165
  mixHash: option<hex>,
130
166
  nonce: option<bigint>,
131
167
  number: int,
@@ -164,18 +200,54 @@ module GetBlockByNumber = {
164
200
  uncles: s.field("uncles", S.null(S.array(S.string))),
165
201
  })
166
202
 
167
- let route = makeRpcRoute(
168
- "eth_getBlockByNumber",
169
- S.tuple(s =>
170
- {
171
- "blockNumber": s.item(0, hexIntSchema),
172
- "includeTransactions": s.item(1, S.bool),
173
- }
174
- ),
175
- S.null(blockSchema),
203
+ let paramsSchema = S.tuple(s =>
204
+ {
205
+ "blockNumber": s.item(0, hexIntSchema),
206
+ "includeTransactions": s.item(1, S.bool),
207
+ }
176
208
  )
209
+
210
+ let resultSchema = S.null(blockSchema)
211
+
212
+ let route = makeRpcRoute("eth_getBlockByNumber", paramsSchema, resultSchema)
213
+
214
+ let rawRoute = makeRpcRoute("eth_getBlockByNumber", paramsSchema, S.null(S.json(~validate=false)))
177
215
  }
178
216
 
179
217
  module GetBlockHeight = {
180
218
  let route = makeRpcRoute("eth_blockNumber", S.tuple(_ => ()), hexIntSchema)
181
219
  }
220
+
221
+ module GetTransactionByHash = {
222
+ let rawRoute = makeRpcRoute(
223
+ "eth_getTransactionByHash",
224
+ S.tuple1(S.string),
225
+ S.null(S.json(~validate=false)),
226
+ )
227
+ }
228
+
229
+ module GetTransactionReceipt = {
230
+ let rawRoute = makeRpcRoute(
231
+ "eth_getTransactionReceipt",
232
+ S.tuple1(S.string),
233
+ S.null(S.json(~validate=false)),
234
+ )
235
+ }
236
+
237
+ let getLogs = async (~client: Rest.client, ~param: GetLogs.param) => {
238
+ await GetLogs.route->Rest.fetch(param, ~client)
239
+ }
240
+
241
+ let getBlock = async (~client: Rest.client, ~blockNumber: int) => {
242
+ await GetBlockByNumber.route->Rest.fetch(
243
+ {"blockNumber": blockNumber, "includeTransactions": false},
244
+ ~client,
245
+ )
246
+ }
247
+
248
+ let getRawBlock = async (~client: Rest.client, ~blockNumber: int) => {
249
+ await GetBlockByNumber.rawRoute->Rest.fetch(
250
+ {"blockNumber": blockNumber, "includeTransactions": false},
251
+ ~client,
252
+ )
253
+ }