envio 3.6.1 → 3.7.0-subgraph

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 (114) hide show
  1. package/index.d.ts +197 -9
  2. package/package.json +6 -6
  3. package/src/Batch.res +64 -25
  4. package/src/Batch.res.mjs +69 -46
  5. package/src/ChainFetching.res +13 -17
  6. package/src/ChainFetching.res.mjs +9 -14
  7. package/src/ChainState.res +160 -119
  8. package/src/ChainState.res.mjs +75 -46
  9. package/src/ChainState.resi +15 -12
  10. package/src/Config.res +18 -2
  11. package/src/Config.res.mjs +9 -6
  12. package/src/Core.res +20 -0
  13. package/src/Core.res.mjs +11 -0
  14. package/src/Ecosystem.res +0 -1
  15. package/src/Envio.res +16 -3
  16. package/src/EventConfigBuilder.res +155 -40
  17. package/src/EventConfigBuilder.res.mjs +76 -24
  18. package/src/EventProcessing.res +10 -14
  19. package/src/EventProcessing.res.mjs +12 -10
  20. package/src/EventUtils.res +3 -47
  21. package/src/FetchState.res +124 -104
  22. package/src/FetchState.res.mjs +116 -100
  23. package/src/HandlerLoader.res +20 -8
  24. package/src/HandlerLoader.res.mjs +11 -2
  25. package/src/HandlerRegister.res +108 -2
  26. package/src/HandlerRegister.res.mjs +59 -10
  27. package/src/HandlerRegister.resi +4 -0
  28. package/src/InMemoryStore.res +1 -6
  29. package/src/InMemoryStore.res.mjs +1 -3
  30. package/src/Internal.res +84 -24
  31. package/src/Internal.res.mjs +33 -2
  32. package/src/LazyLoader.res +12 -11
  33. package/src/LazyLoader.res.mjs +13 -7
  34. package/src/Main.res +22 -9
  35. package/src/Main.res.mjs +18 -5
  36. package/src/MemoryStorage.res +756 -0
  37. package/src/MemoryStorage.res.mjs +623 -0
  38. package/src/Persistence.res +4 -2
  39. package/src/PgStorage.res +24 -8
  40. package/src/PgStorage.res.mjs +15 -12
  41. package/src/ReorgDetection.res +0 -222
  42. package/src/ReorgDetection.res.mjs +0 -163
  43. package/src/Rollback.res +14 -15
  44. package/src/Rollback.res.mjs +4 -5
  45. package/src/SimulateItems.res +2 -2
  46. package/src/UserContext.res +358 -51
  47. package/src/UserContext.res.mjs +271 -35
  48. package/src/Utils.res +3 -0
  49. package/src/bindings/ClickHouse.res +16 -11
  50. package/src/bindings/ClickHouse.res.mjs +10 -10
  51. package/src/bindings/Vitest.res +1 -1
  52. package/src/sources/BlockStore.res +115 -5
  53. package/src/sources/BlockStore.res.mjs +25 -0
  54. package/src/sources/Evm.res +0 -1
  55. package/src/sources/Evm.res.mjs +0 -1
  56. package/src/sources/EvmHyperSyncSource.res +19 -84
  57. package/src/sources/EvmHyperSyncSource.res.mjs +24 -60
  58. package/src/sources/Fuel.res +24 -4
  59. package/src/sources/Fuel.res.mjs +23 -3
  60. package/src/sources/FuelHyperSync.res +8 -3
  61. package/src/sources/FuelHyperSync.res.mjs +5 -4
  62. package/src/sources/FuelHyperSync.resi +3 -1
  63. package/src/sources/FuelHyperSyncClient.res +7 -10
  64. package/src/sources/FuelHyperSyncSource.res +18 -45
  65. package/src/sources/FuelHyperSyncSource.res.mjs +18 -35
  66. package/src/sources/HyperSync.res +49 -229
  67. package/src/sources/HyperSync.res.mjs +74 -191
  68. package/src/sources/HyperSync.resi +11 -35
  69. package/src/sources/HyperSyncClient.res +9 -79
  70. package/src/sources/HyperSyncClient.res.mjs +2 -6
  71. package/src/sources/RequestStat.res +5 -0
  72. package/src/sources/RequestStat.res.mjs +2 -0
  73. package/src/sources/RpcSource.res +149 -43
  74. package/src/sources/RpcSource.res.mjs +104 -41
  75. package/src/sources/SimulateSource.res +8 -5
  76. package/src/sources/SimulateSource.res.mjs +6 -6
  77. package/src/sources/Source.res +89 -16
  78. package/src/sources/Source.res.mjs +50 -1
  79. package/src/sources/SourceManager.res +255 -50
  80. package/src/sources/SourceManager.res.mjs +149 -55
  81. package/src/sources/SourceManager.resi +2 -6
  82. package/src/sources/Svm.res +0 -7
  83. package/src/sources/Svm.res.mjs +0 -8
  84. package/src/sources/SvmHyperSyncClient.res +21 -16
  85. package/src/sources/SvmHyperSyncClient.res.mjs +3 -4
  86. package/src/sources/SvmHyperSyncSource.res +25 -117
  87. package/src/sources/SvmHyperSyncSource.res.mjs +8 -121
  88. package/src/subgraph/blocks.ts +176 -0
  89. package/src/subgraph/calls.ts +217 -0
  90. package/src/subgraph/conformance.ts +99 -0
  91. package/src/subgraph/division.ts +110 -0
  92. package/src/subgraph/errors.ts +90 -0
  93. package/src/subgraph/graph-ts-types/VERSION +2 -0
  94. package/src/subgraph/graph-ts-types/chain/arweave.d.ts +70 -0
  95. package/src/subgraph/graph-ts-types/chain/cosmos.d.ts +327 -0
  96. package/src/subgraph/graph-ts-types/chain/ethereum.d.ts +233 -0
  97. package/src/subgraph/graph-ts-types/chain/near.d.ts +253 -0
  98. package/src/subgraph/graph-ts-types/chain/starknet.d.ts +32 -0
  99. package/src/subgraph/graph-ts-types/common/collections.d.ts +136 -0
  100. package/src/subgraph/graph-ts-types/common/conversion.d.ts +11 -0
  101. package/src/subgraph/graph-ts-types/common/datasource.d.ts +30 -0
  102. package/src/subgraph/graph-ts-types/common/eager-offset.d.ts +0 -0
  103. package/src/subgraph/graph-ts-types/common/json.d.ts +17 -0
  104. package/src/subgraph/graph-ts-types/common/numbers.d.ts +120 -0
  105. package/src/subgraph/graph-ts-types/common/value.d.ts +120 -0
  106. package/src/subgraph/graph-ts-types/common/yaml.d.ts +90 -0
  107. package/src/subgraph/graph-ts-types/global/global.d.ts +194 -0
  108. package/src/subgraph/graph-ts-types/helper-functions.d.ts +22 -0
  109. package/src/subgraph/graph-ts-types/index.d.ts +102 -0
  110. package/src/subgraph/graph-ts.ts +1871 -0
  111. package/src/subgraph/hosts.ts +148 -0
  112. package/src/subgraph/runtime.ts +845 -0
  113. package/src/subgraph/scope.ts +63 -0
  114. package/svm.schema.json +3 -2
@@ -0,0 +1,148 @@
1
+ /**
2
+ * The graph-node host ops that reach outside the chain data envio already has:
3
+ * IPFS, Arweave, ENS, `eth_getBalance`/`eth_getCode`, and a block handler's
4
+ * timestamp. Each is an envio effect, so preload batches and dedupes them and
5
+ * the sync bridge suspends the mapping until the answer lands.
6
+ */
7
+
8
+ import { createPublicClient, fallback, http } from "viem";
9
+ import * as Sury from "rescript-schema";
10
+ import { createEffect } from "../Envio.res.mjs";
11
+ import { configureBlockTimestamps, requestBlockTimestamp } from "./blocks.ts";
12
+ import { missingRpcMessage } from "./errors.ts";
13
+
14
+ const IPFS_GATEWAY = "https://ipfs.io/ipfs/";
15
+ const ARWEAVE_GATEWAY = "https://arweave.net/";
16
+ /** graph-node heals a hash from its rainbow table; this is the public one. */
17
+ const ENS_RAINBOW = "https://api.ensrainbow.io/v1/heal/";
18
+
19
+ const nullableString = Sury.union([Sury.string, null]);
20
+
21
+ async function fetchBase64(url: string): Promise<string | null> {
22
+ const response = await fetch(url);
23
+ if (!response.ok) {
24
+ return null;
25
+ }
26
+ const buffer = new Uint8Array(await response.arrayBuffer());
27
+ return Buffer.from(buffer).toString("base64");
28
+ }
29
+
30
+ export type HostEffects = ReturnType<typeof makeHostEffects>;
31
+
32
+ export function makeHostEffects(rpcUrls: string[]) {
33
+ configureBlockTimestamps(rpcUrls);
34
+
35
+ let client: ReturnType<typeof createPublicClient> | null = null;
36
+ const clientOrThrow = (callSite: string) => {
37
+ if (rpcUrls.length === 0) {
38
+ throw new Error(missingRpcMessage(callSite));
39
+ }
40
+ client ??= createPublicClient({
41
+ transport: fallback(rpcUrls.map((url) => http(url, { retryCount: 0 }))),
42
+ });
43
+ return client;
44
+ };
45
+
46
+ const ipfsCat = createEffect(
47
+ {
48
+ name: "envio_subgraph_ipfs_cat",
49
+ input: Sury.string,
50
+ output: nullableString,
51
+ rateLimit: false,
52
+ cache: true,
53
+ },
54
+ async ({ input }: { input: string }) => fetchBase64(IPFS_GATEWAY + input),
55
+ );
56
+
57
+ const arweaveData = createEffect(
58
+ {
59
+ name: "envio_subgraph_arweave",
60
+ input: Sury.string,
61
+ output: nullableString,
62
+ rateLimit: false,
63
+ cache: true,
64
+ },
65
+ async ({ input }: { input: string }) => fetchBase64(ARWEAVE_GATEWAY + input),
66
+ );
67
+
68
+ // Best-effort, as graph-node is: it returns null when its rainbow table
69
+ // doesn't hold the hash, so a lookup failure is a miss, not an error.
70
+ const ensName = createEffect(
71
+ {
72
+ name: "envio_subgraph_ens_name",
73
+ input: Sury.string,
74
+ output: nullableString,
75
+ rateLimit: false,
76
+ cache: true,
77
+ },
78
+ async ({ input }: { input: string }) => {
79
+ try {
80
+ const response = await fetch(ENS_RAINBOW + input);
81
+ if (!response.ok) return null;
82
+ const body = (await response.json()) as { label?: string };
83
+ return body.label ?? null;
84
+ } catch {
85
+ return null;
86
+ }
87
+ },
88
+ );
89
+
90
+ const getBalance = createEffect(
91
+ {
92
+ name: "envio_subgraph_eth_balance",
93
+ input: Sury.string,
94
+ output: Sury.string,
95
+ rateLimit: false,
96
+ cache: true,
97
+ crossChain: false,
98
+ },
99
+ async ({ input }: { input: string }) => {
100
+ const { address, blockNumber } = JSON.parse(input);
101
+ const balance = await clientOrThrow("ethereum.getBalance").getBalance({
102
+ address,
103
+ blockNumber: BigInt(blockNumber),
104
+ });
105
+ return balance.toString();
106
+ },
107
+ );
108
+
109
+ const hasCode = createEffect(
110
+ {
111
+ name: "envio_subgraph_has_code",
112
+ input: Sury.string,
113
+ output: Sury.boolean,
114
+ rateLimit: false,
115
+ cache: true,
116
+ crossChain: false,
117
+ },
118
+ async ({ input }: { input: string }) => {
119
+ const { address, blockNumber } = JSON.parse(input);
120
+ const code = await clientOrThrow("ethereum.hasCode").getCode({
121
+ address,
122
+ blockNumber: BigInt(blockNumber),
123
+ });
124
+ return code !== undefined && code !== "0x";
125
+ },
126
+ );
127
+
128
+ // Uncached on purpose: a block's timestamp is read exactly once, by that
129
+ // block's own handler invocation, so a persisted row per indexed block would
130
+ // be bloat with no reuse. The in-memory memo still covers replay rounds and
131
+ // the preload -> execute transition.
132
+ const blockTimestamp = createEffect(
133
+ {
134
+ name: "envio_subgraph_block_timestamp",
135
+ input: Sury.number,
136
+ output: Sury.string,
137
+ rateLimit: false,
138
+ cache: false,
139
+ crossChain: false,
140
+ },
141
+ async ({ input, context }: { input: number; context: { chain: { id: number } } }) => {
142
+ const timestamp = await requestBlockTimestamp(context.chain.id, input);
143
+ return timestamp.toString();
144
+ },
145
+ );
146
+
147
+ return { ipfsCat, arweaveData, ensName, getBalance, hasCode, blockTimestamp };
148
+ }