envio 2.28.0-alpha.3 → 2.28.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.
@@ -0,0 +1,697 @@
1
+ // Generated by ReScript, PLEASE EDIT WITH CARE
2
+ 'use strict';
3
+
4
+ var Rpc = require("./Rpc.res.js");
5
+ var Caml = require("rescript/lib/js/caml.js");
6
+ var Rest = require("../vendored/Rest.res.js");
7
+ var Time = require("../Time.res.js");
8
+ var Viem = require("../bindings/Viem.res.js");
9
+ var Utils = require("../Utils.res.js");
10
+ var Ethers = require("../bindings/Ethers.res.js");
11
+ var Hrtime = require("../bindings/Hrtime.res.js");
12
+ var Js_exn = require("rescript/lib/js/js_exn.js");
13
+ var Source = require("./Source.res.js");
14
+ var Logging = require("../Logging.res.js");
15
+ var $$Promise = require("../bindings/Promise.res.js");
16
+ var Belt_Int = require("rescript/lib/js/belt_Int.js");
17
+ var Belt_Array = require("rescript/lib/js/belt_Array.js");
18
+ var FetchState = require("../FetchState.res.js");
19
+ var LazyLoader = require("../LazyLoader.res.js");
20
+ var Belt_Option = require("rescript/lib/js/belt_Option.js");
21
+ var Caml_option = require("rescript/lib/js/caml_option.js");
22
+ var EventRouter = require("./EventRouter.res.js");
23
+ var LogSelection = require("../LogSelection.res.js");
24
+ var Caml_exceptions = require("rescript/lib/js/caml_exceptions.js");
25
+ var Caml_splice_call = require("rescript/lib/js/caml_splice_call.js");
26
+ var S$RescriptSchema = require("rescript-schema/src/S.res.js");
27
+ var Caml_js_exceptions = require("rescript/lib/js/caml_js_exceptions.js");
28
+
29
+ var QueryTimout = /* @__PURE__ */Caml_exceptions.create("RpcSource.QueryTimout");
30
+
31
+ function getKnownBlock(provider, blockNumber) {
32
+ return provider.getBlock(blockNumber).then(function (blockNullable) {
33
+ if (blockNullable == null) {
34
+ return Promise.reject(Js_exn.raiseError("RPC returned null for blockNumber " + String(blockNumber)));
35
+ } else {
36
+ return Promise.resolve(blockNullable);
37
+ }
38
+ });
39
+ }
40
+
41
+ async function getKnownBlockWithBackoff(provider, sourceName, chain, blockNumber, backoffMsOnFailure) {
42
+ try {
43
+ return await getKnownBlock(provider, blockNumber);
44
+ }
45
+ catch (raw_err){
46
+ var err = Caml_js_exceptions.internalToOCamlException(raw_err);
47
+ Logging.warn({
48
+ err: err,
49
+ msg: "Issue while running fetching batch of events from the RPC. Will wait " + String(backoffMsOnFailure) + "ms and try again.",
50
+ source: sourceName,
51
+ chainId: chain,
52
+ type: "EXPONENTIAL_BACKOFF"
53
+ });
54
+ await Time.resolvePromiseAfterDelay(backoffMsOnFailure);
55
+ return await getKnownBlockWithBackoff(provider, sourceName, chain, blockNumber, (backoffMsOnFailure << 1));
56
+ }
57
+ }
58
+
59
+ var suggestedRangeRegExp = /retry with the range (\d+)-(\d+)/;
60
+
61
+ var blockRangeLimitRegExp = /limited to a (\d+) blocks range/;
62
+
63
+ var alchemyRangeRegExp = /up to a (\d+) block range/;
64
+
65
+ var cloudflareRangeRegExp = /Max range: (\d+)/;
66
+
67
+ var thirdwebRangeRegExp = /Maximum allowed number of requested blocks is (\d+)/;
68
+
69
+ var blockpiRangeRegExp = /limited to (\d+) block/;
70
+
71
+ var baseRangeRegExp = /block range too large/;
72
+
73
+ var maxAllowedBlocksRegExp = /maximum allowed is (\d+) blocks/;
74
+
75
+ var blastPaidRegExp = /exceeds the range allowed for your plan \(\d+ > (\d+)\)/;
76
+
77
+ var chainstackRegExp = /Block range limit exceeded./;
78
+
79
+ var coinbaseRegExp = /please limit the query to at most (\d+) blocks/;
80
+
81
+ var publicNodeRegExp = /maximum block range: (\d+)/;
82
+
83
+ var hyperliquidRegExp = /query exceeds max block range (\d+)/;
84
+
85
+ function getSuggestedBlockIntervalFromExn(exn) {
86
+ if (exn.RE_EXN_ID !== Js_exn.$$Error) {
87
+ return ;
88
+ }
89
+ try {
90
+ var message = exn._1.error.message;
91
+ S$RescriptSchema.assertOrThrow(message, S$RescriptSchema.string);
92
+ var extractBlockRange = function (execResult, isMaxRange) {
93
+ if (execResult.length !== 2) {
94
+ return ;
95
+ }
96
+ var blockRangeLimit = execResult[1];
97
+ if (blockRangeLimit === null || blockRangeLimit === undefined) {
98
+ return ;
99
+ }
100
+ var blockRangeLimit$1 = Belt_Int.fromString(blockRangeLimit);
101
+ if (blockRangeLimit$1 !== undefined && blockRangeLimit$1 > 0) {
102
+ return [
103
+ blockRangeLimit$1,
104
+ isMaxRange
105
+ ];
106
+ }
107
+
108
+ };
109
+ var execResult = suggestedRangeRegExp.exec(message);
110
+ if (execResult !== null) {
111
+ if (execResult.length !== 3) {
112
+ return ;
113
+ }
114
+ var fromBlock = execResult[1];
115
+ if (fromBlock === null || fromBlock === undefined) {
116
+ return ;
117
+ }
118
+ var toBlock = execResult[2];
119
+ if (toBlock === null || toBlock === undefined) {
120
+ return ;
121
+ }
122
+ var match = Belt_Int.fromString(fromBlock);
123
+ var match$1 = Belt_Int.fromString(toBlock);
124
+ if (match !== undefined && match$1 !== undefined && match$1 >= match) {
125
+ return [
126
+ (match$1 - match | 0) + 1 | 0,
127
+ false
128
+ ];
129
+ } else {
130
+ return ;
131
+ }
132
+ }
133
+ var execResult$1 = blockRangeLimitRegExp.exec(message);
134
+ if (execResult$1 !== null) {
135
+ return extractBlockRange(execResult$1, true);
136
+ }
137
+ var execResult$2 = alchemyRangeRegExp.exec(message);
138
+ if (execResult$2 !== null) {
139
+ return extractBlockRange(execResult$2, true);
140
+ }
141
+ var execResult$3 = cloudflareRangeRegExp.exec(message);
142
+ if (execResult$3 !== null) {
143
+ return extractBlockRange(execResult$3, true);
144
+ }
145
+ var execResult$4 = thirdwebRangeRegExp.exec(message);
146
+ if (execResult$4 !== null) {
147
+ return extractBlockRange(execResult$4, true);
148
+ }
149
+ var execResult$5 = blockpiRangeRegExp.exec(message);
150
+ if (execResult$5 !== null) {
151
+ return extractBlockRange(execResult$5, true);
152
+ }
153
+ var execResult$6 = maxAllowedBlocksRegExp.exec(message);
154
+ if (execResult$6 !== null) {
155
+ return extractBlockRange(execResult$6, true);
156
+ }
157
+ var match$2 = baseRangeRegExp.exec(message);
158
+ if (match$2 !== null) {
159
+ return [
160
+ 2000,
161
+ true
162
+ ];
163
+ }
164
+ var execResult$7 = blastPaidRegExp.exec(message);
165
+ if (execResult$7 !== null) {
166
+ return extractBlockRange(execResult$7, true);
167
+ }
168
+ var match$3 = chainstackRegExp.exec(message);
169
+ if (match$3 !== null) {
170
+ return [
171
+ 10000,
172
+ true
173
+ ];
174
+ }
175
+ var execResult$8 = coinbaseRegExp.exec(message);
176
+ if (execResult$8 !== null) {
177
+ return extractBlockRange(execResult$8, true);
178
+ }
179
+ var execResult$9 = publicNodeRegExp.exec(message);
180
+ if (execResult$9 !== null) {
181
+ return extractBlockRange(execResult$9, true);
182
+ }
183
+ var execResult$10 = hyperliquidRegExp.exec(message);
184
+ if (execResult$10 !== null) {
185
+ return extractBlockRange(execResult$10, true);
186
+ } else {
187
+ return ;
188
+ }
189
+ }
190
+ catch (exn$1){
191
+ return ;
192
+ }
193
+ }
194
+
195
+ var maxSuggestedBlockIntervalKey = "max";
196
+
197
+ function getNextPage(fromBlock, toBlock, addresses, topicQuery, loadBlock, sc, provider, mutSuggestedBlockIntervals, partitionId) {
198
+ var queryTimoutPromise = Time.resolvePromiseAfterDelay(sc.queryTimeoutMillis).then(function () {
199
+ return Promise.reject({
200
+ RE_EXN_ID: QueryTimout,
201
+ _1: "Query took longer than " + String(sc.queryTimeoutMillis / 1000 | 0) + " seconds"
202
+ });
203
+ });
204
+ var latestFetchedBlockPromise = loadBlock(toBlock);
205
+ var logsPromise = provider.getLogs(Ethers.CombinedFilter.toFilter({
206
+ address: addresses,
207
+ topics: topicQuery,
208
+ fromBlock: fromBlock,
209
+ toBlock: toBlock
210
+ })).then(async function (logs) {
211
+ return {
212
+ logs: logs,
213
+ latestFetchedBlock: await latestFetchedBlockPromise
214
+ };
215
+ });
216
+ return $$Promise.$$catch(Promise.race([
217
+ queryTimoutPromise,
218
+ logsPromise
219
+ ]), (function (err) {
220
+ var match = getSuggestedBlockIntervalFromExn(err);
221
+ if (match !== undefined) {
222
+ var nextBlockIntervalTry = match[0];
223
+ mutSuggestedBlockIntervals[match[1] ? maxSuggestedBlockIntervalKey : partitionId] = nextBlockIntervalTry;
224
+ throw {
225
+ RE_EXN_ID: Source.GetItemsError,
226
+ _1: {
227
+ TAG: "FailedGettingItems",
228
+ exn: err,
229
+ attemptedToBlock: toBlock,
230
+ retry: {
231
+ TAG: "WithSuggestedToBlock",
232
+ toBlock: (fromBlock + nextBlockIntervalTry | 0) - 1 | 0
233
+ }
234
+ },
235
+ Error: new Error()
236
+ };
237
+ }
238
+ var executedBlockInterval = (toBlock - fromBlock | 0) + 1 | 0;
239
+ var nextBlockIntervalTry$1 = executedBlockInterval * sc.backoffMultiplicative | 0;
240
+ mutSuggestedBlockIntervals[partitionId] = nextBlockIntervalTry$1;
241
+ throw {
242
+ RE_EXN_ID: Source.GetItemsError,
243
+ _1: {
244
+ TAG: "FailedGettingItems",
245
+ exn: err,
246
+ attemptedToBlock: toBlock,
247
+ retry: {
248
+ TAG: "WithBackoff",
249
+ message: "Failed getting data for the block range. Will try smaller block range for the next attempt.",
250
+ backoffMillis: sc.backoffMillis
251
+ }
252
+ },
253
+ Error: new Error()
254
+ };
255
+ }));
256
+ }
257
+
258
+ function getSelectionConfig(selection, chain) {
259
+ var staticTopicSelections = [];
260
+ var dynamicEventFilters = [];
261
+ Belt_Array.forEach(selection.eventConfigs, (function (param) {
262
+ var s = param.getEventFiltersOrThrow(chain);
263
+ if (s.TAG === "Static") {
264
+ Caml_splice_call.spliceObjApply(staticTopicSelections, "push", [s._0]);
265
+ return ;
266
+ }
267
+ dynamicEventFilters.push(s._0);
268
+ }));
269
+ var match = LogSelection.compressTopicSelections(staticTopicSelections);
270
+ var len = match.length;
271
+ var getLogSelectionOrThrow;
272
+ if (len !== 1) {
273
+ if (len !== 0) {
274
+ throw {
275
+ RE_EXN_ID: Source.GetItemsError,
276
+ _1: {
277
+ TAG: "UnsupportedSelection",
278
+ message: "RPC data-source currently supports event filters only when there's a single wildcard event. Please, create a GitHub issue if it's a blocker for you."
279
+ },
280
+ Error: new Error()
281
+ };
282
+ }
283
+ var len$1 = dynamicEventFilters.length;
284
+ if (len$1 !== 1) {
285
+ if (len$1 !== 0) {
286
+ throw {
287
+ RE_EXN_ID: Source.GetItemsError,
288
+ _1: {
289
+ TAG: "UnsupportedSelection",
290
+ message: "RPC data-source currently supports event filters only when there's a single wildcard event. Please, create a GitHub issue if it's a blocker for you."
291
+ },
292
+ Error: new Error()
293
+ };
294
+ }
295
+ throw {
296
+ RE_EXN_ID: Source.GetItemsError,
297
+ _1: {
298
+ TAG: "UnsupportedSelection",
299
+ message: "Invalid events configuration for the partition. Nothing to fetch. Please, report to the Envio team."
300
+ },
301
+ Error: new Error()
302
+ };
303
+ }
304
+ var dynamicEventFilter = dynamicEventFilters[0];
305
+ if (selection.eventConfigs.length === 1) {
306
+ var eventConfig = selection.eventConfigs[0];
307
+ getLogSelectionOrThrow = (function (addressesByContractName) {
308
+ var addresses = FetchState.addressesByContractNameGetAll(addressesByContractName);
309
+ var match = dynamicEventFilter(addresses);
310
+ if (match.length !== 1) {
311
+ throw {
312
+ RE_EXN_ID: Source.GetItemsError,
313
+ _1: {
314
+ TAG: "UnsupportedSelection",
315
+ message: "RPC data-source currently doesn't support an array of event filters. Please, create a GitHub issue if it's a blocker for you."
316
+ },
317
+ Error: new Error()
318
+ };
319
+ }
320
+ var topicSelection = match[0];
321
+ return {
322
+ addresses: eventConfig.isWildcard ? undefined : addresses,
323
+ topicQuery: Rpc.GetLogs.mapTopicQuery(topicSelection)
324
+ };
325
+ });
326
+ } else {
327
+ throw {
328
+ RE_EXN_ID: Source.GetItemsError,
329
+ _1: {
330
+ TAG: "UnsupportedSelection",
331
+ message: "RPC data-source currently supports event filters only when there's a single wildcard event. Please, create a GitHub issue if it's a blocker for you."
332
+ },
333
+ Error: new Error()
334
+ };
335
+ }
336
+ } else {
337
+ var topicSelection = match[0];
338
+ if (dynamicEventFilters.length !== 0) {
339
+ throw {
340
+ RE_EXN_ID: Source.GetItemsError,
341
+ _1: {
342
+ TAG: "UnsupportedSelection",
343
+ message: "RPC data-source currently supports event filters only when there's a single wildcard event. Please, create a GitHub issue if it's a blocker for you."
344
+ },
345
+ Error: new Error()
346
+ };
347
+ }
348
+ var topicQuery = Rpc.GetLogs.mapTopicQuery(topicSelection);
349
+ getLogSelectionOrThrow = (function (addressesByContractName) {
350
+ var addresses = FetchState.addressesByContractNameGetAll(addressesByContractName);
351
+ return {
352
+ addresses: addresses.length !== 0 ? addresses : undefined,
353
+ topicQuery: topicQuery
354
+ };
355
+ });
356
+ }
357
+ return {
358
+ getLogSelectionOrThrow: getLogSelectionOrThrow
359
+ };
360
+ }
361
+
362
+ function memoGetSelectionConfig(chain) {
363
+ var cache = new WeakMap();
364
+ return function (selection) {
365
+ var c = cache.get(selection);
366
+ if (c !== undefined) {
367
+ return c;
368
+ }
369
+ var c$1 = getSelectionConfig(selection, chain);
370
+ cache.set(selection, c$1);
371
+ return c$1;
372
+ };
373
+ }
374
+
375
+ function makeThrowingGetEventBlock(getBlock) {
376
+ return async function (log) {
377
+ return await getBlock(log.blockNumber);
378
+ };
379
+ }
380
+
381
+ function makeThrowingGetEventTransaction(getTransactionFields) {
382
+ var fnsCache = new WeakMap();
383
+ return function (log, transactionSchema) {
384
+ var fn = fnsCache.get(transactionSchema);
385
+ var tmp;
386
+ if (fn !== undefined) {
387
+ tmp = fn;
388
+ } else {
389
+ var transactionSchema$1 = S$RescriptSchema.removeTypeValidation(transactionSchema);
390
+ var match = transactionSchema$1.t;
391
+ var transactionFieldItems;
392
+ transactionFieldItems = typeof match !== "object" || match.TAG !== "object" ? Js_exn.raiseError("Unexpected internal error: transactionSchema is not an object") : match.items;
393
+ var parseOrThrowReadableError = function (data) {
394
+ try {
395
+ return S$RescriptSchema.parseOrThrow(data, transactionSchema$1);
396
+ }
397
+ catch (raw_error){
398
+ var error = Caml_js_exceptions.internalToOCamlException(raw_error);
399
+ if (error.RE_EXN_ID === S$RescriptSchema.Raised) {
400
+ var error$1 = error._1;
401
+ return Js_exn.raiseError("Invalid transaction field \"" + S$RescriptSchema.Path.toArray(error$1.path).join(".") + "\" found in the RPC response. Error: " + S$RescriptSchema.$$Error.reason(error$1));
402
+ }
403
+ throw error;
404
+ }
405
+ };
406
+ var fn$1;
407
+ var exit = 0;
408
+ var len = transactionFieldItems.length;
409
+ if (len >= 3) {
410
+ exit = 1;
411
+ } else {
412
+ switch (len) {
413
+ case 0 :
414
+ fn$1 = (function (param) {
415
+ return Promise.resolve({});
416
+ });
417
+ break;
418
+ case 1 :
419
+ var match$1 = transactionFieldItems[0];
420
+ switch (match$1.location) {
421
+ case "hash" :
422
+ exit = 2;
423
+ break;
424
+ case "transactionIndex" :
425
+ fn$1 = (function (log) {
426
+ return Promise.resolve(parseOrThrowReadableError(log));
427
+ });
428
+ break;
429
+ default:
430
+ exit = 1;
431
+ }
432
+ break;
433
+ case 2 :
434
+ var match$2 = transactionFieldItems[0];
435
+ switch (match$2.location) {
436
+ case "hash" :
437
+ var match$3 = transactionFieldItems[1];
438
+ exit = match$3.location === "transactionIndex" ? 2 : 1;
439
+ break;
440
+ case "transactionIndex" :
441
+ var match$4 = transactionFieldItems[1];
442
+ exit = match$4.location === "hash" ? 2 : 1;
443
+ break;
444
+ default:
445
+ exit = 1;
446
+ }
447
+ break;
448
+
449
+ }
450
+ }
451
+ switch (exit) {
452
+ case 1 :
453
+ fn$1 = (function (log) {
454
+ return getTransactionFields(log).then(parseOrThrowReadableError);
455
+ });
456
+ break;
457
+ case 2 :
458
+ fn$1 = (function (log) {
459
+ return Promise.resolve(parseOrThrowReadableError({
460
+ hash: log.transactionHash,
461
+ transactionIndex: log.transactionIndex
462
+ }));
463
+ });
464
+ break;
465
+
466
+ }
467
+ fnsCache.set(transactionSchema$1, fn$1);
468
+ tmp = fn$1;
469
+ }
470
+ return tmp(log);
471
+ };
472
+ }
473
+
474
+ function sanitizeUrl(url) {
475
+ var regex = /https?:\/\/([^\/?]+).*/;
476
+ var result = regex.exec(url);
477
+ if (result === null) {
478
+ return ;
479
+ }
480
+ var host = Belt_Array.get(result, 1);
481
+ if (host !== undefined) {
482
+ return Caml_option.nullable_to_opt(Caml_option.valFromOption(host));
483
+ }
484
+
485
+ }
486
+
487
+ function make(param) {
488
+ var eventRouter = param.eventRouter;
489
+ var chain = param.chain;
490
+ var url = param.url;
491
+ var syncConfig = param.syncConfig;
492
+ var host = sanitizeUrl(url);
493
+ var urlHost = host !== undefined ? host : Js_exn.raiseError("EE109: The RPC url \"" + url + "\" is incorrect format. The RPC url needs to start with either http:// or https://");
494
+ var name = "RPC (" + urlHost + ")";
495
+ var provider = Ethers.JsonRpcProvider.make(url, chain);
496
+ var getSelectionConfig = memoGetSelectionConfig(chain);
497
+ var mutSuggestedBlockIntervals = {};
498
+ var transactionLoader = LazyLoader.make((function (transactionHash) {
499
+ return provider.getTransaction(transactionHash);
500
+ }), (function (am, exn) {
501
+ Logging.error({
502
+ err: exn,
503
+ msg: "EE1100: Top level promise timeout reached. Please review other errors or warnings in the code. This function will retry in " + String(am._retryDelayMillis / 1000 | 0) + " seconds. It is highly likely that your indexer isn't syncing on one or more chains currently. Also take a look at the \"suggestedFix\" in the metadata of this command",
504
+ source: name,
505
+ chainId: chain,
506
+ metadata: {
507
+ asyncTaskName: "transactionLoader: fetching transaction data - `getTransaction` rpc call",
508
+ suggestedFix: "This likely means the RPC url you are using is not responding correctly. Please try another RPC endipoint."
509
+ }
510
+ });
511
+ }), undefined, undefined, undefined, undefined);
512
+ var blockLoader = LazyLoader.make((function (blockNumber) {
513
+ return getKnownBlockWithBackoff(provider, name, chain, blockNumber, 1000);
514
+ }), (function (am, exn) {
515
+ Logging.error({
516
+ err: exn,
517
+ msg: "EE1100: Top level promise timeout reached. Please review other errors or warnings in the code. This function will retry in " + String(am._retryDelayMillis / 1000 | 0) + " seconds. It is highly likely that your indexer isn't syncing on one or more chains currently. Also take a look at the \"suggestedFix\" in the metadata of this command",
518
+ source: name,
519
+ chainId: chain,
520
+ metadata: {
521
+ asyncTaskName: "blockLoader: fetching block data - `getBlock` rpc call",
522
+ suggestedFix: "This likely means the RPC url you are using is not responding correctly. Please try another RPC endipoint."
523
+ }
524
+ });
525
+ }), undefined, undefined, undefined, undefined);
526
+ var getEventBlockOrThrow = makeThrowingGetEventBlock(function (blockNumber) {
527
+ return LazyLoader.get(blockLoader, blockNumber);
528
+ });
529
+ var getEventTransactionOrThrow = makeThrowingGetEventTransaction(Ethers.JsonRpcProvider.makeGetTransactionFields(function (__x) {
530
+ return LazyLoader.get(transactionLoader, __x);
531
+ }));
532
+ var contractNameAbiMapping = {};
533
+ Belt_Array.forEach(param.contracts, (function (contract) {
534
+ contractNameAbiMapping[contract.name] = contract.abi;
535
+ }));
536
+ var getItemsOrThrow = async function (fromBlock, toBlock, addressesByContractName, indexingContracts, currentBlockHeight, partitionId, selection, param, param$1) {
537
+ var startFetchingBatchTimeRef = Hrtime.makeTimer();
538
+ var maxSuggestedBlockInterval = mutSuggestedBlockIntervals[maxSuggestedBlockIntervalKey];
539
+ var suggestedBlockInterval = maxSuggestedBlockInterval !== undefined ? maxSuggestedBlockInterval : Belt_Option.getWithDefault(mutSuggestedBlockIntervals[partitionId], syncConfig.initialBlockInterval);
540
+ var toBlock$1 = toBlock !== undefined && toBlock < currentBlockHeight ? toBlock : currentBlockHeight;
541
+ var suggestedToBlock = Caml.int_max(Caml.int_min((fromBlock + suggestedBlockInterval | 0) - 1 | 0, toBlock$1), fromBlock);
542
+ var firstBlockParentPromise = fromBlock > 0 ? LazyLoader.get(blockLoader, fromBlock - 1 | 0).then(function (res) {
543
+ return res;
544
+ }) : Promise.resolve(undefined);
545
+ var match = getSelectionConfig(selection);
546
+ var match$1 = match.getLogSelectionOrThrow(addressesByContractName);
547
+ var match$2 = await getNextPage(fromBlock, suggestedToBlock, match$1.addresses, match$1.topicQuery, (function (blockNumber) {
548
+ return LazyLoader.get(blockLoader, blockNumber);
549
+ }), syncConfig, provider, mutSuggestedBlockIntervals, partitionId);
550
+ var latestFetchedBlock = match$2.latestFetchedBlock;
551
+ var executedBlockInterval = (suggestedToBlock - fromBlock | 0) + 1 | 0;
552
+ if (executedBlockInterval >= suggestedBlockInterval && !Utils.Dict.has(mutSuggestedBlockIntervals, maxSuggestedBlockIntervalKey)) {
553
+ mutSuggestedBlockIntervals[partitionId] = Caml.int_min(executedBlockInterval + syncConfig.accelerationAdditive | 0, syncConfig.intervalCeiling);
554
+ }
555
+ var parsedQueueItems = await Promise.all(Belt_Array.keepMap(match$2.logs, (function (log) {
556
+ var topic0 = log.topics[0];
557
+ var eventConfig = EventRouter.get(eventRouter, EventRouter.getEvmEventId(topic0, log.topics.length), log.address, log.blockNumber, indexingContracts);
558
+ if (eventConfig === undefined) {
559
+ return ;
560
+ }
561
+ var blockNumber = log.blockNumber;
562
+ var logIndex = log.index;
563
+ return Caml_option.some((async function () {
564
+ var match;
565
+ try {
566
+ match = await Promise.all([
567
+ getEventBlockOrThrow(log),
568
+ getEventTransactionOrThrow(log, eventConfig.transactionSchema)
569
+ ]);
570
+ }
571
+ catch (raw_exn){
572
+ var exn = Caml_js_exceptions.internalToOCamlException(raw_exn);
573
+ throw {
574
+ RE_EXN_ID: Source.GetItemsError,
575
+ _1: {
576
+ TAG: "FailedGettingFieldSelection",
577
+ exn: exn,
578
+ blockNumber: blockNumber,
579
+ logIndex: logIndex,
580
+ message: "Failed getting selected fields. Please double-check your RPC provider returns correct data."
581
+ },
582
+ Error: new Error()
583
+ };
584
+ }
585
+ var block = match[0];
586
+ var decodedEvent;
587
+ try {
588
+ decodedEvent = Viem.parseLogOrThrow(contractNameAbiMapping, eventConfig.contractName, log.topics, log.data);
589
+ }
590
+ catch (raw_exn$1){
591
+ var exn$1 = Caml_js_exceptions.internalToOCamlException(raw_exn$1);
592
+ throw {
593
+ RE_EXN_ID: Source.GetItemsError,
594
+ _1: {
595
+ TAG: "FailedParsingItems",
596
+ exn: exn$1,
597
+ blockNumber: blockNumber,
598
+ logIndex: logIndex,
599
+ message: "Failed to parse event with viem, please double-check your ABI."
600
+ },
601
+ Error: new Error()
602
+ };
603
+ }
604
+ return {
605
+ eventConfig: eventConfig,
606
+ timestamp: block.timestamp,
607
+ chain: chain,
608
+ blockNumber: block.number,
609
+ logIndex: log.index,
610
+ event: {
611
+ params: decodedEvent.args,
612
+ chainId: chain,
613
+ srcAddress: log.address,
614
+ logIndex: log.index,
615
+ transaction: match[1],
616
+ block: block
617
+ }
618
+ };
619
+ })());
620
+ })));
621
+ var optFirstBlockParent = await firstBlockParentPromise;
622
+ var totalTimeElapsed = Hrtime.intFromMillis(Hrtime.toMillis(Hrtime.timeSince(startFetchingBatchTimeRef)));
623
+ var reorgGuard_rangeLastBlock = {
624
+ blockHash: latestFetchedBlock.hash,
625
+ blockNumber: latestFetchedBlock.number
626
+ };
627
+ var reorgGuard_prevRangeLastBlock = Belt_Option.map(optFirstBlockParent, (function (b) {
628
+ return {
629
+ blockHash: b.hash,
630
+ blockNumber: b.number
631
+ };
632
+ }));
633
+ var reorgGuard = {
634
+ rangeLastBlock: reorgGuard_rangeLastBlock,
635
+ prevRangeLastBlock: reorgGuard_prevRangeLastBlock
636
+ };
637
+ return {
638
+ currentBlockHeight: currentBlockHeight,
639
+ reorgGuard: reorgGuard,
640
+ parsedQueueItems: parsedQueueItems,
641
+ fromBlockQueried: fromBlock,
642
+ latestFetchedBlockNumber: latestFetchedBlock.number,
643
+ latestFetchedBlockTimestamp: latestFetchedBlock.timestamp,
644
+ stats: {
645
+ "total time elapsed (ms)": totalTimeElapsed
646
+ }
647
+ };
648
+ };
649
+ var getBlockHashes = function (blockNumbers, _currentlyUnusedLogger) {
650
+ return $$Promise.$$catch(Promise.all(Belt_Array.map(blockNumbers, (function (blockNum) {
651
+ return LazyLoader.get(blockLoader, blockNum);
652
+ }))).then(function (blocks) {
653
+ return {
654
+ TAG: "Ok",
655
+ _0: Belt_Array.map(blocks, (function (b) {
656
+ return {
657
+ blockHash: b.hash,
658
+ blockNumber: b.number,
659
+ blockTimestamp: b.timestamp
660
+ };
661
+ }))
662
+ };
663
+ }), (function (exn) {
664
+ return Promise.resolve({
665
+ TAG: "Error",
666
+ _0: exn
667
+ });
668
+ }));
669
+ };
670
+ var client = Rest.client(url, undefined);
671
+ return {
672
+ name: name,
673
+ sourceFor: param.sourceFor,
674
+ chain: chain,
675
+ poweredByHyperSync: false,
676
+ pollingInterval: 1000,
677
+ getBlockHashes: getBlockHashes,
678
+ getHeightOrThrow: (function () {
679
+ return Rest.$$fetch(Rpc.GetBlockHeight.route, undefined, client);
680
+ }),
681
+ getItemsOrThrow: getItemsOrThrow
682
+ };
683
+ }
684
+
685
+ exports.QueryTimout = QueryTimout;
686
+ exports.getKnownBlock = getKnownBlock;
687
+ exports.getKnownBlockWithBackoff = getKnownBlockWithBackoff;
688
+ exports.getSuggestedBlockIntervalFromExn = getSuggestedBlockIntervalFromExn;
689
+ exports.maxSuggestedBlockIntervalKey = maxSuggestedBlockIntervalKey;
690
+ exports.getNextPage = getNextPage;
691
+ exports.getSelectionConfig = getSelectionConfig;
692
+ exports.memoGetSelectionConfig = memoGetSelectionConfig;
693
+ exports.makeThrowingGetEventBlock = makeThrowingGetEventBlock;
694
+ exports.makeThrowingGetEventTransaction = makeThrowingGetEventTransaction;
695
+ exports.sanitizeUrl = sanitizeUrl;
696
+ exports.make = make;
697
+ /* Rpc Not a pure module */