envio 2.28.0 → 2.29.0-alpha.1

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,451 @@
1
+ // Generated by ReScript, PLEASE EDIT WITH CARE
2
+ 'use strict';
3
+
4
+ var Rest = require("../vendored/Rest.res.js");
5
+ var Hrtime = require("../bindings/Hrtime.res.js");
6
+ var Js_exn = require("rescript/lib/js/js_exn.js");
7
+ var Source = require("./Source.res.js");
8
+ var Logging = require("../Logging.res.js");
9
+ var $$Promise = require("../bindings/Promise.res.js");
10
+ var HyperFuel = require("./HyperFuel.res.js");
11
+ var HyperSync = require("./HyperSync.res.js");
12
+ var Belt_Array = require("rescript/lib/js/belt_Array.js");
13
+ var Belt_Option = require("rescript/lib/js/belt_Option.js");
14
+ var EventRouter = require("./EventRouter.res.js");
15
+ var ErrorHandling = require("../ErrorHandling.res.js");
16
+ var Caml_exceptions = require("rescript/lib/js/caml_exceptions.js");
17
+ var Caml_js_exceptions = require("rescript/lib/js/caml_js_exceptions.js");
18
+
19
+ var EventRoutingFailed = /* @__PURE__ */Caml_exceptions.create("HyperFuelSource.EventRoutingFailed");
20
+
21
+ var mintEventTag = "mint";
22
+
23
+ var burnEventTag = "burn";
24
+
25
+ var transferEventTag = "transfer";
26
+
27
+ var callEventTag = "call";
28
+
29
+ var logDataReceiptTypeSelection = [6];
30
+
31
+ var txStatusSelection = [1];
32
+
33
+ function makeGetNormalRecieptsSelection(nonWildcardLogDataRbsByContract, nonLogDataReceiptTypesByContract, contractNames) {
34
+ return function (addressesByContractName) {
35
+ var selection = [];
36
+ contractNames.forEach(function (contractName) {
37
+ var addresses = addressesByContractName[contractName];
38
+ if (addresses === undefined) {
39
+ return ;
40
+ }
41
+ if (addresses.length === 0) {
42
+ return ;
43
+ }
44
+ var receiptTypes = nonLogDataReceiptTypesByContract[contractName];
45
+ if (receiptTypes !== undefined) {
46
+ selection.push({
47
+ rootContractId: addresses,
48
+ receiptType: receiptTypes,
49
+ txStatus: txStatusSelection
50
+ });
51
+ }
52
+ var nonWildcardLogDataRbs = nonWildcardLogDataRbsByContract[contractName];
53
+ if (nonWildcardLogDataRbs !== undefined && nonWildcardLogDataRbs.length !== 0) {
54
+ selection.push({
55
+ rootContractId: addresses,
56
+ receiptType: logDataReceiptTypeSelection,
57
+ rb: nonWildcardLogDataRbs,
58
+ txStatus: txStatusSelection
59
+ });
60
+ return ;
61
+ }
62
+
63
+ });
64
+ return selection;
65
+ };
66
+ }
67
+
68
+ function makeWildcardRecieptsSelection(wildcardLogDataRbs, nonLogDataWildcardReceiptTypes) {
69
+ var selection = [];
70
+ if (nonLogDataWildcardReceiptTypes.length !== 0) {
71
+ selection.push({
72
+ receiptType: nonLogDataWildcardReceiptTypes,
73
+ txStatus: txStatusSelection
74
+ });
75
+ }
76
+ if (wildcardLogDataRbs.length !== 0) {
77
+ selection.push({
78
+ receiptType: logDataReceiptTypeSelection,
79
+ rb: wildcardLogDataRbs,
80
+ txStatus: txStatusSelection
81
+ });
82
+ }
83
+ return selection;
84
+ }
85
+
86
+ function getSelectionConfig(selection, chain) {
87
+ var eventRouter = EventRouter.empty();
88
+ var nonWildcardLogDataRbsByContract = {};
89
+ var wildcardLogDataRbs = [];
90
+ var nonLogDataReceiptTypesByContract = {};
91
+ var nonLogDataWildcardReceiptTypes = [];
92
+ var addNonLogDataWildcardReceiptTypes = function (receiptType) {
93
+ nonLogDataWildcardReceiptTypes.push(receiptType);
94
+ };
95
+ var addNonLogDataReceiptType = function (contractName, receiptType) {
96
+ var receiptTypes = nonLogDataReceiptTypesByContract[contractName];
97
+ if (receiptTypes !== undefined) {
98
+ receiptTypes.push(receiptType);
99
+ } else {
100
+ nonLogDataReceiptTypesByContract[contractName] = [receiptType];
101
+ }
102
+ };
103
+ var contractNames = new Set();
104
+ Belt_Array.forEach(selection.eventConfigs, (function (eventConfig) {
105
+ var contractName = eventConfig.contractName;
106
+ if (!eventConfig.isWildcard) {
107
+ contractNames.add(contractName);
108
+ }
109
+ EventRouter.addOrThrow(eventRouter, eventConfig.id, eventConfig, contractName, eventConfig.isWildcard, eventConfig.name, chain);
110
+ var isWildcard = eventConfig.isWildcard;
111
+ if (isWildcard) {
112
+ var tmp = eventConfig.kind;
113
+ if (typeof tmp !== "object") {
114
+ switch (tmp) {
115
+ case "Mint" :
116
+ return addNonLogDataWildcardReceiptTypes(11);
117
+ case "Burn" :
118
+ return addNonLogDataWildcardReceiptTypes(12);
119
+ case "Transfer" :
120
+ addNonLogDataWildcardReceiptTypes(7);
121
+ return addNonLogDataWildcardReceiptTypes(8);
122
+ case "Call" :
123
+ return addNonLogDataWildcardReceiptTypes(0);
124
+
125
+ }
126
+ }
127
+
128
+ }
129
+ var match = eventConfig.kind;
130
+ if (typeof match !== "object") {
131
+ switch (match) {
132
+ case "Mint" :
133
+ return addNonLogDataReceiptType(contractName, 11);
134
+ case "Burn" :
135
+ return addNonLogDataReceiptType(contractName, 12);
136
+ case "Transfer" :
137
+ addNonLogDataReceiptType(contractName, 7);
138
+ return addNonLogDataReceiptType(contractName, 8);
139
+ case "Call" :
140
+ return Js_exn.raiseError("Call receipt indexing currently supported only in wildcard mode");
141
+
142
+ }
143
+ } else {
144
+ var rb = BigInt(match.logId);
145
+ if (isWildcard) {
146
+ wildcardLogDataRbs.push(rb);
147
+ return ;
148
+ }
149
+ var arr = nonWildcardLogDataRbsByContract[contractName];
150
+ if (arr !== undefined) {
151
+ arr.push(rb);
152
+ } else {
153
+ nonWildcardLogDataRbsByContract[contractName] = [rb];
154
+ }
155
+ return ;
156
+ }
157
+ }));
158
+ var tmp;
159
+ if (selection.dependsOnAddresses) {
160
+ tmp = makeGetNormalRecieptsSelection(nonWildcardLogDataRbsByContract, nonLogDataReceiptTypesByContract, contractNames);
161
+ } else {
162
+ var recieptsSelection = makeWildcardRecieptsSelection(wildcardLogDataRbs, nonLogDataWildcardReceiptTypes);
163
+ tmp = (function (param) {
164
+ return recieptsSelection;
165
+ });
166
+ }
167
+ return {
168
+ getRecieptsSelection: tmp,
169
+ eventRouter: eventRouter
170
+ };
171
+ }
172
+
173
+ function memoGetSelectionConfig(chain) {
174
+ var cache = new WeakMap();
175
+ return function (selection) {
176
+ var c = cache.get(selection);
177
+ if (c !== undefined) {
178
+ return c;
179
+ }
180
+ var c$1 = getSelectionConfig(selection, chain);
181
+ cache.set(selection, c$1);
182
+ return c$1;
183
+ };
184
+ }
185
+
186
+ function make(param) {
187
+ var endpointUrl = param.endpointUrl;
188
+ var chain = param.chain;
189
+ var getSelectionConfig = memoGetSelectionConfig(chain);
190
+ var getItemsOrThrow = async function (fromBlock, toBlock, addressesByContractName, indexingContracts, currentBlockHeight, param, selection, retry, logger) {
191
+ var mkLogAndRaise = function (extra, extra$1) {
192
+ return ErrorHandling.mkLogAndRaise(logger, extra, extra$1);
193
+ };
194
+ var totalTimeRef = Hrtime.makeTimer();
195
+ var selectionConfig = getSelectionConfig(selection);
196
+ var recieptsSelection = selectionConfig.getRecieptsSelection(addressesByContractName);
197
+ var startFetchingBatchTimeRef = Hrtime.makeTimer();
198
+ var pageUnsafe;
199
+ try {
200
+ pageUnsafe = await HyperFuel.GetLogs.query(endpointUrl, fromBlock, toBlock, recieptsSelection);
201
+ }
202
+ catch (raw_error){
203
+ var error = Caml_js_exceptions.internalToOCamlException(raw_error);
204
+ if (error.RE_EXN_ID === HyperSync.GetLogs.$$Error) {
205
+ var error$1 = error._1;
206
+ var tmp;
207
+ if (typeof error$1 !== "object") {
208
+ var backoffMillis = retry !== 0 ? Math.imul(500, retry) : 100;
209
+ tmp = {
210
+ TAG: "WithBackoff",
211
+ message: "Block #" + String(fromBlock) + " not found in HyperFuel. HyperFuel has multiple instances and it's possible that they drift independently slightly from the head. Indexing should continue correctly after retrying the query in " + String(backoffMillis) + "ms.",
212
+ backoffMillis: backoffMillis
213
+ };
214
+ } else {
215
+ tmp = {
216
+ TAG: "WithBackoff",
217
+ message: "Received page response with invalid data. Attempt a retry. Missing params: " + error$1.missingParams.join(","),
218
+ backoffMillis: retry !== 0 ? Math.imul(4000, retry) : 1000
219
+ };
220
+ }
221
+ throw {
222
+ RE_EXN_ID: Source.GetItemsError,
223
+ _1: {
224
+ TAG: "FailedGettingItems",
225
+ exn: null,
226
+ attemptedToBlock: Belt_Option.getWithDefault(toBlock, currentBlockHeight),
227
+ retry: tmp
228
+ },
229
+ Error: new Error()
230
+ };
231
+ }
232
+ throw {
233
+ RE_EXN_ID: Source.GetItemsError,
234
+ _1: {
235
+ TAG: "FailedGettingItems",
236
+ exn: error,
237
+ attemptedToBlock: Belt_Option.getWithDefault(toBlock, currentBlockHeight),
238
+ retry: {
239
+ TAG: "WithBackoff",
240
+ message: "Unexpected issue while fetching events from HyperFuel client. Attempt a retry.",
241
+ backoffMillis: retry !== 0 ? Math.imul(1000, retry) : 500
242
+ }
243
+ },
244
+ Error: new Error()
245
+ };
246
+ }
247
+ var pageFetchTime = Hrtime.intFromMillis(Hrtime.toMillis(Hrtime.timeSince(startFetchingBatchTimeRef)));
248
+ var currentBlockHeight$1 = pageUnsafe.archiveHeight;
249
+ var heighestBlockQueried = pageUnsafe.nextBlock - 1 | 0;
250
+ var match = Belt_Array.get(pageUnsafe.items, pageUnsafe.items.length - 1 | 0);
251
+ var lastBlockQueriedPromise;
252
+ var exit = 0;
253
+ if (match !== undefined) {
254
+ var block = match.block;
255
+ if (block.height === heighestBlockQueried) {
256
+ lastBlockQueriedPromise = Promise.resolve({
257
+ blockHash: block.id,
258
+ blockNumber: block.height,
259
+ blockTimestamp: block.time
260
+ });
261
+ } else {
262
+ exit = 1;
263
+ }
264
+ } else {
265
+ exit = 1;
266
+ }
267
+ if (exit === 1) {
268
+ lastBlockQueriedPromise = $$Promise.$$catch(HyperFuel.queryBlockData(endpointUrl, heighestBlockQueried, logger).then(function (res) {
269
+ if (res !== undefined) {
270
+ return res;
271
+ } else {
272
+ return mkLogAndRaise("Failure, blockData for block " + String(heighestBlockQueried) + " unexpectedly returned None", {
273
+ RE_EXN_ID: "Not_found"
274
+ });
275
+ }
276
+ }), (function (exn) {
277
+ return mkLogAndRaise("Failed to query blockData for block " + String(heighestBlockQueried), exn);
278
+ }));
279
+ }
280
+ var parsingTimeRef = Hrtime.makeTimer();
281
+ var parsedQueueItems = Belt_Array.map(pageUnsafe.items, (function (item) {
282
+ var block = item.block;
283
+ var receiptIndex = item.receiptIndex;
284
+ var receipt = item.receipt;
285
+ var contractAddress = item.contractId;
286
+ var eventId;
287
+ switch (receipt.receiptType) {
288
+ case 0 :
289
+ eventId = callEventTag;
290
+ break;
291
+ case 6 :
292
+ eventId = receipt.rb.toString();
293
+ break;
294
+ case 7 :
295
+ case 8 :
296
+ eventId = transferEventTag;
297
+ break;
298
+ case 11 :
299
+ eventId = mintEventTag;
300
+ break;
301
+ case 12 :
302
+ eventId = burnEventTag;
303
+ break;
304
+
305
+ }
306
+ var eventConfig = EventRouter.get(selectionConfig.eventRouter, eventId, contractAddress, block.height, indexingContracts);
307
+ var eventConfig$1;
308
+ if (eventConfig !== undefined) {
309
+ eventConfig$1 = eventConfig;
310
+ } else {
311
+ var logger$1 = Logging.createChildFrom(logger, {
312
+ chainId: chain,
313
+ blockNumber: block.height,
314
+ logIndex: receiptIndex,
315
+ contractAddress: contractAddress,
316
+ eventId: eventId
317
+ });
318
+ eventConfig$1 = ErrorHandling.mkLogAndRaise(logger$1, "Failed to route registered event", {
319
+ RE_EXN_ID: EventRoutingFailed
320
+ });
321
+ }
322
+ var match = eventConfig$1.kind;
323
+ var params;
324
+ var exit = 0;
325
+ if (typeof match !== "object" || receipt.receiptType !== 6) {
326
+ exit = 1;
327
+ } else {
328
+ try {
329
+ params = match.decode(receipt.data);
330
+ }
331
+ catch (raw_exn){
332
+ var exn = Caml_js_exceptions.internalToOCamlException(raw_exn);
333
+ var params$1 = {
334
+ chainId: chain,
335
+ blockNumber: block.height,
336
+ logIndex: receiptIndex
337
+ };
338
+ var logger$2 = Logging.createChildFrom(logger, params$1);
339
+ params = ErrorHandling.mkLogAndRaise(logger$2, "Failed to decode Fuel LogData receipt, please double check your ABI.", exn);
340
+ }
341
+ }
342
+ if (exit === 1) {
343
+ switch (receipt.receiptType) {
344
+ case 0 :
345
+ params = {
346
+ to: receipt.to,
347
+ assetId: receipt.assetId,
348
+ amount: receipt.amount
349
+ };
350
+ break;
351
+ case 6 :
352
+ params = Js_exn.raiseError("Unexpected bug in the event routing logic");
353
+ break;
354
+ case 7 :
355
+ params = {
356
+ to: receipt.to,
357
+ assetId: receipt.assetId,
358
+ amount: receipt.amount
359
+ };
360
+ break;
361
+ case 8 :
362
+ params = {
363
+ to: receipt.toAddress,
364
+ assetId: receipt.assetId,
365
+ amount: receipt.amount
366
+ };
367
+ break;
368
+ case 11 :
369
+ case 12 :
370
+ params = {
371
+ subId: receipt.subId,
372
+ amount: receipt.val
373
+ };
374
+ break;
375
+
376
+ }
377
+ }
378
+ return {
379
+ kind: 0,
380
+ eventConfig: eventConfig$1,
381
+ timestamp: block.time,
382
+ chain: chain,
383
+ blockNumber: block.height,
384
+ logIndex: receiptIndex,
385
+ event: {
386
+ params: params,
387
+ chainId: chain,
388
+ srcAddress: contractAddress,
389
+ logIndex: receiptIndex,
390
+ transaction: {
391
+ id: item.transactionId
392
+ },
393
+ block: block
394
+ }
395
+ };
396
+ }));
397
+ var parsingTimeElapsed = Hrtime.intFromMillis(Hrtime.toMillis(Hrtime.timeSince(parsingTimeRef)));
398
+ var rangeLastBlock = await lastBlockQueriedPromise;
399
+ var reorgGuard = {
400
+ rangeLastBlock: rangeLastBlock,
401
+ prevRangeLastBlock: undefined
402
+ };
403
+ var totalTimeElapsed = Hrtime.intFromMillis(Hrtime.toMillis(Hrtime.timeSince(totalTimeRef)));
404
+ var stats_parsing$unknowntime$unknown$lparms$rpar = parsingTimeElapsed;
405
+ var stats_page$unknownfetch$unknowntime$unknown$lparms$rpar = pageFetchTime;
406
+ var stats = {
407
+ "total time elapsed (ms)": totalTimeElapsed,
408
+ "parsing time (ms)": stats_parsing$unknowntime$unknown$lparms$rpar,
409
+ "page fetch time (ms)": stats_page$unknownfetch$unknowntime$unknown$lparms$rpar
410
+ };
411
+ return {
412
+ currentBlockHeight: currentBlockHeight$1,
413
+ reorgGuard: reorgGuard,
414
+ parsedQueueItems: parsedQueueItems,
415
+ fromBlockQueried: fromBlock,
416
+ latestFetchedBlockNumber: rangeLastBlock.blockNumber,
417
+ latestFetchedBlockTimestamp: rangeLastBlock.blockTimestamp,
418
+ stats: stats
419
+ };
420
+ };
421
+ var getBlockHashes = function (param, param$1) {
422
+ return Js_exn.raiseError("HyperFuel does not support getting block hashes");
423
+ };
424
+ var jsonApiClient = Rest.client(endpointUrl, undefined);
425
+ return {
426
+ name: "HyperFuel",
427
+ sourceFor: "Sync",
428
+ chain: chain,
429
+ poweredByHyperSync: true,
430
+ pollingInterval: 100,
431
+ getBlockHashes: getBlockHashes,
432
+ getHeightOrThrow: (function () {
433
+ return Rest.$$fetch(HyperFuel.heightRoute, undefined, jsonApiClient);
434
+ }),
435
+ getItemsOrThrow: getItemsOrThrow
436
+ };
437
+ }
438
+
439
+ exports.EventRoutingFailed = EventRoutingFailed;
440
+ exports.mintEventTag = mintEventTag;
441
+ exports.burnEventTag = burnEventTag;
442
+ exports.transferEventTag = transferEventTag;
443
+ exports.callEventTag = callEventTag;
444
+ exports.logDataReceiptTypeSelection = logDataReceiptTypeSelection;
445
+ exports.txStatusSelection = txStatusSelection;
446
+ exports.makeGetNormalRecieptsSelection = makeGetNormalRecieptsSelection;
447
+ exports.makeWildcardRecieptsSelection = makeWildcardRecieptsSelection;
448
+ exports.getSelectionConfig = getSelectionConfig;
449
+ exports.memoGetSelectionConfig = memoGetSelectionConfig;
450
+ exports.make = make;
451
+ /* Rest Not a pure module */
@@ -21,7 +21,7 @@ type hyperSyncPage<'item> = {
21
21
 
22
22
  type logsQueryPageItem = {
23
23
  log: Log.t,
24
- block: Internal.eventBlock,
24
+ block: HyperSyncClient.ResponseTypes.block,
25
25
  transaction: Internal.eventTransaction,
26
26
  }
27
27
 
@@ -17,7 +17,7 @@ module Log: {
17
17
 
18
18
  type logsQueryPageItem = {
19
19
  log: Log.t,
20
- block: Internal.eventBlock,
20
+ block: HyperSyncClient.ResponseTypes.block,
21
21
  transaction: Internal.eventTransaction,
22
22
  }
23
23