envio 3.0.0-alpha.1 → 3.0.0-alpha.2
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.
- package/evm.schema.json +19 -15
- package/fuel.schema.json +3 -10
- package/package.json +14 -11
- package/rescript.json +1 -1
- package/src/Config.res +4 -7
- package/src/Config.res.mjs +4 -6
- package/src/{Platform.res → Ecosystem.res} +3 -3
- package/src/{Platform.res.mjs → Ecosystem.res.mjs} +2 -2
- package/src/Env.res +244 -0
- package/src/Env.res.mjs +273 -0
- package/src/EventRegister.res +4 -15
- package/src/EventRegister.res.mjs +3 -9
- package/src/EventRegister.resi +2 -8
- package/src/Internal.gen.ts +3 -14
- package/src/Internal.res +2 -11
- package/src/bindings/EventSource.res +13 -0
- package/src/bindings/EventSource.res.mjs +2 -0
- package/src/sources/HyperSyncHeightStream.res +179 -0
- package/src/sources/HyperSyncHeightStream.res.mjs +127 -0
- package/src/sources/HyperSyncSource.res +2 -60
- package/src/sources/HyperSyncSource.res.mjs +5 -61
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// Generated by ReScript, PLEASE EDIT WITH CARE
|
|
2
|
+
|
|
3
|
+
import * as Utils from "../Utils.res.mjs";
|
|
4
|
+
import * as Js_exn from "rescript/lib/es6/js_exn.js";
|
|
5
|
+
import * as Js_dict from "rescript/lib/es6/js_dict.js";
|
|
6
|
+
import * as Logging from "../Logging.res.mjs";
|
|
7
|
+
import * as Belt_Int from "rescript/lib/es6/belt_Int.js";
|
|
8
|
+
import * as Belt_Option from "rescript/lib/es6/belt_Option.js";
|
|
9
|
+
import * as Caml_option from "rescript/lib/es6/caml_option.js";
|
|
10
|
+
import * as Eventsource from "eventsource";
|
|
11
|
+
|
|
12
|
+
function make(hyperSyncUrl, apiToken) {
|
|
13
|
+
var updateTimeoutId = function (eventsourceRef, timeoutIdRef, hyperSyncUrl, apiToken, heightRef, errorRef) {
|
|
14
|
+
clearTimeout(timeoutIdRef.contents);
|
|
15
|
+
var newTimeoutId = setTimeout((function () {
|
|
16
|
+
Logging.trace({
|
|
17
|
+
msg: "Timeout fired for height stream",
|
|
18
|
+
url: hyperSyncUrl,
|
|
19
|
+
staleTimeMillis: 15000
|
|
20
|
+
});
|
|
21
|
+
refreshEventSource(eventsourceRef, hyperSyncUrl, apiToken, heightRef, errorRef, timeoutIdRef);
|
|
22
|
+
}), 15000);
|
|
23
|
+
timeoutIdRef.contents = newTimeoutId;
|
|
24
|
+
};
|
|
25
|
+
var refreshEventSource = function (eventsourceRef, hyperSyncUrl, apiToken, heightRef, errorRef, timeoutIdRef) {
|
|
26
|
+
var es = eventsourceRef.contents;
|
|
27
|
+
if (es !== undefined) {
|
|
28
|
+
Caml_option.valFromOption(es).close();
|
|
29
|
+
}
|
|
30
|
+
var userAgent = "hyperindex/" + Utils.EnvioPackage.value.version;
|
|
31
|
+
var es$1 = new Eventsource.EventSource(hyperSyncUrl + "/height/sse", {
|
|
32
|
+
headers: Js_dict.fromArray([
|
|
33
|
+
[
|
|
34
|
+
"Authorization",
|
|
35
|
+
"Bearer " + apiToken
|
|
36
|
+
],
|
|
37
|
+
[
|
|
38
|
+
"User-Agent",
|
|
39
|
+
userAgent
|
|
40
|
+
]
|
|
41
|
+
])
|
|
42
|
+
});
|
|
43
|
+
eventsourceRef.contents = Caml_option.some(es$1);
|
|
44
|
+
updateTimeoutId(eventsourceRef, timeoutIdRef, hyperSyncUrl, apiToken, heightRef, errorRef);
|
|
45
|
+
es$1.onopen = (function () {
|
|
46
|
+
Logging.trace({
|
|
47
|
+
msg: "SSE connection opened for height stream",
|
|
48
|
+
url: hyperSyncUrl
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
es$1.onerror = (function (error) {
|
|
52
|
+
Logging.trace({
|
|
53
|
+
msg: "EventSource error",
|
|
54
|
+
error: error.message
|
|
55
|
+
});
|
|
56
|
+
errorRef.contents = Belt_Option.getWithDefault(error.message, "Unexpected no error.message");
|
|
57
|
+
});
|
|
58
|
+
es$1.addEventListener("ping", (function (_event) {
|
|
59
|
+
updateTimeoutId(eventsourceRef, timeoutIdRef, hyperSyncUrl, apiToken, heightRef, errorRef);
|
|
60
|
+
errorRef.contents = undefined;
|
|
61
|
+
}));
|
|
62
|
+
es$1.addEventListener("height", (function ($$event) {
|
|
63
|
+
var height = Belt_Int.fromString($$event.data);
|
|
64
|
+
if (height !== undefined) {
|
|
65
|
+
updateTimeoutId(eventsourceRef, timeoutIdRef, hyperSyncUrl, apiToken, heightRef, errorRef);
|
|
66
|
+
errorRef.contents = undefined;
|
|
67
|
+
heightRef.contents = height;
|
|
68
|
+
} else {
|
|
69
|
+
Logging.trace({
|
|
70
|
+
msg: "Height was not a number in event.data",
|
|
71
|
+
data: $$event.data
|
|
72
|
+
});
|
|
73
|
+
errorRef.contents = "Height was not a number in event.data";
|
|
74
|
+
}
|
|
75
|
+
}));
|
|
76
|
+
};
|
|
77
|
+
var heightRef = {
|
|
78
|
+
contents: 0
|
|
79
|
+
};
|
|
80
|
+
var errorRef = {
|
|
81
|
+
contents: undefined
|
|
82
|
+
};
|
|
83
|
+
var eventsourceRef = {
|
|
84
|
+
contents: undefined
|
|
85
|
+
};
|
|
86
|
+
var timeoutIdRef = {
|
|
87
|
+
contents: setTimeout((function () {
|
|
88
|
+
|
|
89
|
+
}), 0)
|
|
90
|
+
};
|
|
91
|
+
refreshEventSource(eventsourceRef, hyperSyncUrl, apiToken, heightRef, errorRef, timeoutIdRef);
|
|
92
|
+
return {
|
|
93
|
+
heightRef: heightRef,
|
|
94
|
+
errorRef: errorRef,
|
|
95
|
+
timeoutIdRef: timeoutIdRef,
|
|
96
|
+
eventsourceRef: eventsourceRef
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
async function getHeight(t) {
|
|
101
|
+
while(t.heightRef.contents === 0 && t.errorRef.contents === undefined) {
|
|
102
|
+
await Utils.delay(200);
|
|
103
|
+
};
|
|
104
|
+
var error = t.errorRef.contents;
|
|
105
|
+
if (error !== undefined) {
|
|
106
|
+
return Js_exn.raiseError(error);
|
|
107
|
+
} else {
|
|
108
|
+
return t.heightRef.contents;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function close(t) {
|
|
113
|
+
clearTimeout(t.timeoutIdRef.contents);
|
|
114
|
+
var es = t.eventsourceRef.contents;
|
|
115
|
+
if (es !== undefined) {
|
|
116
|
+
Caml_option.valFromOption(es).close();
|
|
117
|
+
return ;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export {
|
|
123
|
+
make ,
|
|
124
|
+
getHeight ,
|
|
125
|
+
close ,
|
|
126
|
+
}
|
|
127
|
+
/* Utils Not a pure module */
|
|
@@ -159,63 +159,6 @@ type options = {
|
|
|
159
159
|
enableQueryCaching: bool,
|
|
160
160
|
}
|
|
161
161
|
|
|
162
|
-
module HeightState: {
|
|
163
|
-
type t
|
|
164
|
-
let make: (HyperSyncClient.t, ~chainId: int) => Promise.t<t>
|
|
165
|
-
let getHeight: t => Promise.t<int>
|
|
166
|
-
} = {
|
|
167
|
-
open HyperSyncClient
|
|
168
|
-
type t = {
|
|
169
|
-
heightStream: HeightStream.t,
|
|
170
|
-
mutable currentHeight: int,
|
|
171
|
-
mutable errMessage: option<string>,
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
let makeInternal = async (client: HyperSyncClient.t) => {
|
|
175
|
-
currentHeight: 0,
|
|
176
|
-
heightStream: await client.streamHeight(),
|
|
177
|
-
errMessage: None,
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
let start = async (state: t, ~chainId) => {
|
|
181
|
-
while true {
|
|
182
|
-
let height = await state.heightStream.recv()
|
|
183
|
-
switch height {
|
|
184
|
-
| Height({height}) => state.currentHeight = height
|
|
185
|
-
| Connected =>
|
|
186
|
-
Logging.trace({"msg": "HyperSync height stream is connected", "chainId": chainId})
|
|
187
|
-
state.errMessage = None
|
|
188
|
-
| Reconnecting({delayMillis, errorMsg}) =>
|
|
189
|
-
Logging.trace({
|
|
190
|
-
"msg": "HyperSync height stream is reconnecting",
|
|
191
|
-
"err": errorMsg,
|
|
192
|
-
"delayMillis": delayMillis,
|
|
193
|
-
"chainId": chainId,
|
|
194
|
-
})
|
|
195
|
-
state.errMessage = Some(errorMsg)
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
let make = async (client: HyperSyncClient.t, ~chainId) => {
|
|
201
|
-
let state = await makeInternal(client)
|
|
202
|
-
let _async = state->start(~chainId)
|
|
203
|
-
state
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
let getHeight = async (state: t) => {
|
|
207
|
-
while state.currentHeight == 0 && state.errMessage->Belt.Option.isNone {
|
|
208
|
-
// Just poll internally until its no longer 0
|
|
209
|
-
await Utils.delay(200)
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
switch state.errMessage {
|
|
213
|
-
| Some(errMessage) => Js.Exn.raiseError(errMessage)
|
|
214
|
-
| None => state.currentHeight
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
162
|
let make = (
|
|
220
163
|
{
|
|
221
164
|
contracts,
|
|
@@ -610,7 +553,7 @@ let make = (
|
|
|
610
553
|
|
|
611
554
|
let malformedTokenMessage = `Your token is malformed. For more info: https://docs.envio.dev/docs/HyperSync/api-tokens.`
|
|
612
555
|
|
|
613
|
-
let
|
|
556
|
+
let heightStream = HyperSyncHeightStream.make(~hyperSyncUrl=endpointUrl, ~apiToken)
|
|
614
557
|
|
|
615
558
|
{
|
|
616
559
|
name,
|
|
@@ -620,8 +563,7 @@ let make = (
|
|
|
620
563
|
poweredByHyperSync: true,
|
|
621
564
|
getBlockHashes,
|
|
622
565
|
getHeightOrThrow: async () => {
|
|
623
|
-
|
|
624
|
-
try await heightState->HeightState.getHeight catch {
|
|
566
|
+
try await heightStream->HyperSyncHeightStream.getHeight catch {
|
|
625
567
|
| Js.Exn.Error(exn)
|
|
626
568
|
if exn
|
|
627
569
|
->Js.Exn.message
|
|
@@ -16,6 +16,7 @@ import * as Caml_exceptions from "rescript/lib/es6/caml_exceptions.js";
|
|
|
16
16
|
import * as HyperSyncClient from "./HyperSyncClient.res.mjs";
|
|
17
17
|
import * as Caml_splice_call from "rescript/lib/es6/caml_splice_call.js";
|
|
18
18
|
import * as Caml_js_exceptions from "rescript/lib/es6/caml_js_exceptions.js";
|
|
19
|
+
import * as HyperSyncHeightStream from "./HyperSyncHeightStream.res.mjs";
|
|
19
20
|
|
|
20
21
|
function getSelectionConfig(selection, chain) {
|
|
21
22
|
var nonOptionalBlockFieldNames = new Set();
|
|
@@ -120,62 +121,7 @@ function memoGetSelectionConfig(chain) {
|
|
|
120
121
|
};
|
|
121
122
|
}
|
|
122
123
|
|
|
123
|
-
|
|
124
|
-
return {
|
|
125
|
-
heightStream: await client.streamHeight(),
|
|
126
|
-
currentHeight: 0,
|
|
127
|
-
errMessage: undefined
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
async function start(state, chainId) {
|
|
132
|
-
while(true) {
|
|
133
|
-
var height = await state.heightStream.recv();
|
|
134
|
-
if (typeof height !== "object") {
|
|
135
|
-
Logging.trace({
|
|
136
|
-
msg: "HyperSync height stream is connected",
|
|
137
|
-
chainId: chainId
|
|
138
|
-
});
|
|
139
|
-
state.errMessage = undefined;
|
|
140
|
-
} else if (height.type === "Height") {
|
|
141
|
-
state.currentHeight = height.height;
|
|
142
|
-
} else {
|
|
143
|
-
var errorMsg = height.errorMsg;
|
|
144
|
-
Logging.trace({
|
|
145
|
-
msg: "HyperSync height stream is reconnecting",
|
|
146
|
-
err: errorMsg,
|
|
147
|
-
delayMillis: height.delayMillis,
|
|
148
|
-
chainId: chainId
|
|
149
|
-
});
|
|
150
|
-
state.errMessage = errorMsg;
|
|
151
|
-
}
|
|
152
|
-
};
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
async function make(client, chainId) {
|
|
156
|
-
var state = await makeInternal(client);
|
|
157
|
-
start(state, chainId);
|
|
158
|
-
return state;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
async function getHeight(state) {
|
|
162
|
-
while(state.currentHeight === 0 && Belt_Option.isNone(state.errMessage)) {
|
|
163
|
-
await Utils.delay(200);
|
|
164
|
-
};
|
|
165
|
-
var errMessage = state.errMessage;
|
|
166
|
-
if (errMessage !== undefined) {
|
|
167
|
-
return Js_exn.raiseError(errMessage);
|
|
168
|
-
} else {
|
|
169
|
-
return state.currentHeight;
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
var HeightState = {
|
|
174
|
-
make: make,
|
|
175
|
-
getHeight: getHeight
|
|
176
|
-
};
|
|
177
|
-
|
|
178
|
-
function make$1(param) {
|
|
124
|
+
function make(param) {
|
|
179
125
|
var lowercaseAddresses = param.lowercaseAddresses;
|
|
180
126
|
var eventRouter = param.eventRouter;
|
|
181
127
|
var shouldUseHypersyncClientDecoder = param.shouldUseHypersyncClientDecoder;
|
|
@@ -437,7 +383,7 @@ function make$1(param) {
|
|
|
437
383
|
var getBlockHashes = function (blockNumbers, logger) {
|
|
438
384
|
return HyperSync.queryBlockDataMulti(endpointUrl, apiToken, blockNumbers, logger).then(HyperSync.mapExn);
|
|
439
385
|
};
|
|
440
|
-
var
|
|
386
|
+
var heightStream = HyperSyncHeightStream.make(endpointUrl, apiToken);
|
|
441
387
|
return {
|
|
442
388
|
name: "HyperSync",
|
|
443
389
|
sourceFor: "Sync",
|
|
@@ -446,9 +392,8 @@ function make$1(param) {
|
|
|
446
392
|
pollingInterval: 32,
|
|
447
393
|
getBlockHashes: getBlockHashes,
|
|
448
394
|
getHeightOrThrow: (async function () {
|
|
449
|
-
var heightState = await heightStatePromise;
|
|
450
395
|
try {
|
|
451
|
-
return await getHeight(
|
|
396
|
+
return await HyperSyncHeightStream.getHeight(heightStream);
|
|
452
397
|
}
|
|
453
398
|
catch (raw_exn){
|
|
454
399
|
var exn = Caml_js_exceptions.internalToOCamlException(raw_exn);
|
|
@@ -472,7 +417,6 @@ function make$1(param) {
|
|
|
472
417
|
export {
|
|
473
418
|
getSelectionConfig ,
|
|
474
419
|
memoGetSelectionConfig ,
|
|
475
|
-
|
|
476
|
-
make$1 as make,
|
|
420
|
+
make ,
|
|
477
421
|
}
|
|
478
422
|
/* Viem Not a pure module */
|