@unionlabs/payments 0.2.1 → 0.3.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.
Files changed (144) hide show
  1. package/ChainRegistry/package.json +6 -0
  2. package/README.md +283 -1
  3. package/dist/cjs/Attestor.js +9 -2
  4. package/dist/cjs/Attestor.js.map +1 -1
  5. package/dist/cjs/ChainRegistry.js +24 -0
  6. package/dist/cjs/ChainRegistry.js.map +1 -0
  7. package/dist/cjs/Domain.js +8 -2
  8. package/dist/cjs/Domain.js.map +1 -1
  9. package/dist/cjs/Error.js +1 -1
  10. package/dist/cjs/Error.js.map +1 -1
  11. package/dist/cjs/EvmPublicClient.js +5 -277
  12. package/dist/cjs/EvmPublicClient.js.map +1 -1
  13. package/dist/cjs/EvmWalletClient.js +7 -642
  14. package/dist/cjs/EvmWalletClient.js.map +1 -1
  15. package/dist/cjs/Payment.js +86 -79
  16. package/dist/cjs/Payment.js.map +1 -1
  17. package/dist/cjs/Prover.js +2 -2
  18. package/dist/cjs/Prover.js.map +1 -1
  19. package/dist/cjs/PublicClient.js +2 -2
  20. package/dist/cjs/PublicClient.js.map +1 -1
  21. package/dist/cjs/WalletClient.js +2 -2
  22. package/dist/cjs/WalletClient.js.map +1 -1
  23. package/dist/cjs/constants/ibc-core-registry.js +7 -9
  24. package/dist/cjs/constants/ibc-core-registry.js.map +1 -1
  25. package/dist/cjs/constants/z-asset-registry.js +9 -7
  26. package/dist/cjs/constants/z-asset-registry.js.map +1 -1
  27. package/dist/cjs/internal/evmPublicClient.js +261 -0
  28. package/dist/cjs/internal/evmPublicClient.js.map +1 -0
  29. package/dist/cjs/internal/evmWalletClient.js +650 -0
  30. package/dist/cjs/internal/evmWalletClient.js.map +1 -1
  31. package/dist/cjs/internal/publicClient.js +0 -2
  32. package/dist/cjs/internal/publicClient.js.map +1 -1
  33. package/dist/cjs/promises/Payment.js +4 -3
  34. package/dist/cjs/promises/Payment.js.map +1 -1
  35. package/dist/cjs/promises/PublicClient.js +1 -2
  36. package/dist/cjs/promises/PublicClient.js.map +1 -1
  37. package/dist/cjs/promises/WalletClient.js +0 -1
  38. package/dist/cjs/promises/WalletClient.js.map +1 -1
  39. package/dist/cjs/rpc.js +3 -5
  40. package/dist/cjs/rpc.js.map +1 -1
  41. package/dist/dts/Attestor.d.ts +21 -6
  42. package/dist/dts/Attestor.d.ts.map +1 -1
  43. package/dist/dts/ChainRegistry.d.ts +6 -0
  44. package/dist/dts/ChainRegistry.d.ts.map +1 -0
  45. package/dist/dts/Domain.d.ts +92 -59
  46. package/dist/dts/Domain.d.ts.map +1 -1
  47. package/dist/dts/Error.d.ts +2 -2
  48. package/dist/dts/Error.d.ts.map +1 -1
  49. package/dist/dts/EvmPublicClient.d.ts +7 -19
  50. package/dist/dts/EvmPublicClient.d.ts.map +1 -1
  51. package/dist/dts/EvmWalletClient.d.ts +7 -23
  52. package/dist/dts/EvmWalletClient.d.ts.map +1 -1
  53. package/dist/dts/Payment.d.ts +72 -71
  54. package/dist/dts/Payment.d.ts.map +1 -1
  55. package/dist/dts/Prover.d.ts +6 -0
  56. package/dist/dts/Prover.d.ts.map +1 -1
  57. package/dist/dts/PublicClient.d.ts +11 -15
  58. package/dist/dts/PublicClient.d.ts.map +1 -1
  59. package/dist/dts/WalletClient.d.ts +10 -6
  60. package/dist/dts/WalletClient.d.ts.map +1 -1
  61. package/dist/dts/constants/ibc-core-registry.d.ts +5 -6
  62. package/dist/dts/constants/ibc-core-registry.d.ts.map +1 -1
  63. package/dist/dts/constants/z-asset-registry.d.ts +4 -4
  64. package/dist/dts/constants/z-asset-registry.d.ts.map +1 -1
  65. package/dist/dts/internal/evm.d.ts +3 -3
  66. package/dist/dts/internal/evm.d.ts.map +1 -1
  67. package/dist/dts/internal/evmPublicClient.d.ts +6 -0
  68. package/dist/dts/internal/evmPublicClient.d.ts.map +1 -0
  69. package/dist/dts/promises/Attestor.d.ts +5 -5
  70. package/dist/dts/promises/EvmPublicClient.d.ts +73 -74
  71. package/dist/dts/promises/EvmPublicClient.d.ts.map +1 -1
  72. package/dist/dts/promises/EvmWalletClient.d.ts +4 -5
  73. package/dist/dts/promises/EvmWalletClient.d.ts.map +1 -1
  74. package/dist/dts/promises/Payment.d.ts +39 -17
  75. package/dist/dts/promises/Payment.d.ts.map +1 -1
  76. package/dist/dts/promises/PublicClient.d.ts +3 -4
  77. package/dist/dts/promises/PublicClient.d.ts.map +1 -1
  78. package/dist/dts/promises/WalletClient.d.ts +77 -34
  79. package/dist/dts/promises/WalletClient.d.ts.map +1 -1
  80. package/dist/dts/rpc.d.ts +31 -0
  81. package/dist/dts/rpc.d.ts.map +1 -1
  82. package/dist/esm/Attestor.js +8 -1
  83. package/dist/esm/Attestor.js.map +1 -1
  84. package/dist/esm/ChainRegistry.js +16 -0
  85. package/dist/esm/ChainRegistry.js.map +1 -0
  86. package/dist/esm/Domain.js +7 -1
  87. package/dist/esm/Domain.js.map +1 -1
  88. package/dist/esm/Error.js +1 -1
  89. package/dist/esm/Error.js.map +1 -1
  90. package/dist/esm/EvmPublicClient.js +4 -275
  91. package/dist/esm/EvmPublicClient.js.map +1 -1
  92. package/dist/esm/EvmWalletClient.js +5 -638
  93. package/dist/esm/EvmWalletClient.js.map +1 -1
  94. package/dist/esm/Payment.js +84 -78
  95. package/dist/esm/Payment.js.map +1 -1
  96. package/dist/esm/Prover.js +1 -1
  97. package/dist/esm/Prover.js.map +1 -1
  98. package/dist/esm/PublicClient.js +2 -2
  99. package/dist/esm/PublicClient.js.map +1 -1
  100. package/dist/esm/WalletClient.js +2 -2
  101. package/dist/esm/WalletClient.js.map +1 -1
  102. package/dist/esm/constants/ibc-core-registry.js +5 -6
  103. package/dist/esm/constants/ibc-core-registry.js.map +1 -1
  104. package/dist/esm/constants/z-asset-registry.js +7 -4
  105. package/dist/esm/constants/z-asset-registry.js.map +1 -1
  106. package/dist/esm/internal/evmPublicClient.js +253 -0
  107. package/dist/esm/internal/evmPublicClient.js.map +1 -0
  108. package/dist/esm/internal/evmWalletClient.js +647 -1
  109. package/dist/esm/internal/evmWalletClient.js.map +1 -1
  110. package/dist/esm/internal/publicClient.js +0 -2
  111. package/dist/esm/internal/publicClient.js.map +1 -1
  112. package/dist/esm/promises/Payment.js +2 -2
  113. package/dist/esm/promises/Payment.js.map +1 -1
  114. package/dist/esm/promises/PublicClient.js +2 -3
  115. package/dist/esm/promises/PublicClient.js.map +1 -1
  116. package/dist/esm/promises/WalletClient.js +0 -1
  117. package/dist/esm/promises/WalletClient.js.map +1 -1
  118. package/dist/esm/rpc.js +2 -4
  119. package/dist/esm/rpc.js.map +1 -1
  120. package/package.json +15 -3
  121. package/src/Attestor.ts +12 -3
  122. package/src/ChainRegistry.ts +27 -0
  123. package/src/Domain.ts +30 -9
  124. package/src/Error.ts +1 -0
  125. package/src/EvmPublicClient.ts +9 -488
  126. package/src/EvmWalletClient.ts +21 -973
  127. package/src/Payment.ts +234 -159
  128. package/src/Prover.ts +2 -2
  129. package/src/PublicClient.ts +11 -18
  130. package/src/WalletClient.ts +11 -7
  131. package/src/constants/ibc-core-registry.ts +14 -14
  132. package/src/constants/z-asset-registry.ts +10 -7
  133. package/src/internal/evmPublicClient.ts +459 -0
  134. package/src/internal/evmWalletClient.ts +1008 -0
  135. package/src/internal/publicClient.ts +0 -3
  136. package/src/promises/Payment.ts +26 -25
  137. package/src/promises/PublicClient.ts +2 -3
  138. package/src/promises/WalletClient.ts +0 -1
  139. package/src/rpc.ts +2 -4
  140. package/unionlabs-payments-0.3.1.tgz +0 -0
  141. package/src/tsdoc-metadata.json +0 -11
  142. /package/{constants → Constants}/ibc-core-registry/package.json +0 -0
  143. /package/{constants → Constants}/services/package.json +0 -0
  144. /package/{constants → Constants}/z-asset-registry/package.json +0 -0
@@ -3,4 +3,654 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.makeViem = exports.makeBrowser = exports.TypeId = void 0;
7
+ var A = _interopRequireWildcard(require("effect/Array"));
8
+ var Effect = _interopRequireWildcard(require("effect/Effect"));
9
+ var _Function = require("effect/Function");
10
+ var Number = _interopRequireWildcard(require("effect/Number"));
11
+ var Ref = _interopRequireWildcard(require("effect/Ref"));
12
+ var Abi = _interopRequireWildcard(require("../Abi.js"));
13
+ var ZAssetRegistry = _interopRequireWildcard(require("../constants/z-asset-registry.js"));
14
+ var Domain = _interopRequireWildcard(require("../Domain.js"));
15
+ var Error = _interopRequireWildcard(require("../Error.js"));
16
+ var Client = _interopRequireWildcard(require("../WalletClient.js"));
17
+ var internalEvmPublicClient = _interopRequireWildcard(require("./evmPublicClient.js"));
18
+ function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
19
+ /** @internal */
20
+ const TypeId = exports.TypeId = /*#__PURE__*/Symbol.for("@unionlabs/payments/EvmWalletClient");
21
+ /** @internal */
22
+ const makeViem = wallet => Effect.gen(function* () {
23
+ const Viem = yield* Effect.tryPromise({
24
+ try: () => import("viem").then(x => x),
25
+ catch: cause => new Error.SystemError({
26
+ reason: "DynamicImport",
27
+ method: "makeViem",
28
+ module: "EvmWalletClient",
29
+ cause,
30
+ description: "could not import viem"
31
+ })
32
+ });
33
+ const ViemActions = yield* Effect.tryPromise({
34
+ try: () => import("viem/actions").then(x => x),
35
+ catch: cause => new Error.SystemError({
36
+ reason: "DynamicImport",
37
+ method: "makeViemActions",
38
+ module: "EvmWalletClient",
39
+ cause,
40
+ description: "could not import viem/actions"
41
+ })
42
+ });
43
+ const publicClient = yield* (0, _Function.pipe)(Effect.try({
44
+ try: () => Viem.createPublicClient({
45
+ chain: wallet.chain,
46
+ transport: Viem.custom(wallet)
47
+ }),
48
+ catch: cause => new Error.SystemError({
49
+ reason: "DynamicImport",
50
+ method: "make",
51
+ module: "EvmWalletClient",
52
+ cause,
53
+ description: "could derive public client from wallet client"
54
+ })
55
+ }), Effect.flatMap(internalEvmPublicClient.makeViem));
56
+ // Use "latest" to avoid stale pending txs in mempool
57
+ const transactionCount = yield* (0, _Function.pipe)(Effect.tryPromise({
58
+ try: () => ViemActions.getTransactionCount(wallet, {
59
+ address: wallet.account?.address,
60
+ blockTag: "latest"
61
+ }),
62
+ catch: cause => new Error.SystemError({
63
+ reason: "InvalidData",
64
+ method: "encodeFunctionData",
65
+ module: "EvmWalletClient",
66
+ cause
67
+ })
68
+ }), Effect.flatMap(Ref.make));
69
+ const encodeFunctionData = Effect.fn("encodeFunctionData")(parameters => Effect.try({
70
+ try: () => Viem.encodeFunctionData(parameters),
71
+ catch: cause => new Error.SystemError({
72
+ reason: "InvalidData",
73
+ method: "encodeFunctionData",
74
+ module: "EvmWalletClient",
75
+ cause
76
+ })
77
+ }));
78
+ const validateFunctionData = Effect.fn("validateFunctionData")(parameters => (0, _Function.pipe)(Effect.try({
79
+ try: () => Viem.encodeFunctionData(parameters),
80
+ catch: cause => new Error.SystemError({
81
+ reason: "InvalidData",
82
+ method: "validateFunctionData",
83
+ module: "EvmWalletClient",
84
+ cause
85
+ })
86
+ }), Effect.andThen(() => ({
87
+ ...parameters,
88
+ args: parameters.args ?? [],
89
+ functionName: parameters.functionName
90
+ }))));
91
+ const writeContract = Effect.fn("writeContract")(params => (0, _Function.pipe)(Effect.tryPromise({
92
+ try: () => wallet.writeContract(params),
93
+ catch: cause => new Error.SystemError({
94
+ method: "writeContract",
95
+ module: "EvmWalletClient",
96
+ reason: "InvalidData",
97
+ cause
98
+ })
99
+ }), Effect.map(hash => Domain.SubmissionResult.SubmissionEvm({
100
+ hash: Domain.TxHash(hash)
101
+ }))));
102
+ const prepareTransactionRequest = Effect.fn("prepareTransactionRequest")(params => (0, _Function.pipe)(Ref.get(transactionCount), Effect.flatMap(nonce => Effect.tryPromise({
103
+ try: () => wallet.prepareTransactionRequest({
104
+ ...params,
105
+ nonce
106
+ }),
107
+ catch: cause => new Error.SystemError({
108
+ method: "prepareTransactionRequest",
109
+ module: "EvmWalletClient",
110
+ reason: "InvalidData",
111
+ cause
112
+ })
113
+ })), Effect.tap(() => Ref.update(transactionCount, Number.increment))));
114
+ const prepareBatchTransactionRequest = Effect.fn("prepareBatchTransactionRequest")(requests => Effect.gen(function* () {
115
+ // Refresh nonce from chain before batch to ensure we're in sync
116
+ const chainNonce = yield* Effect.tryPromise({
117
+ try: () => ViemActions.getTransactionCount(wallet, {
118
+ address: wallet.account?.address,
119
+ blockTag: "latest"
120
+ }),
121
+ catch: cause => new Error.SystemError({
122
+ reason: "InvalidData",
123
+ method: "prepareBatchTransactionRequest",
124
+ module: "EvmWalletClient",
125
+ cause
126
+ })
127
+ });
128
+ yield* Ref.set(transactionCount, chainNonce);
129
+ yield* Effect.log("[prepareBatchTransactionRequest] Synced nonce", {
130
+ chainNonce
131
+ });
132
+ const calls = yield* (0, _Function.pipe)(requests, Effect.forEach(request => (0, _Function.pipe)(encodeFunctionData({
133
+ abi: request.abi,
134
+ args: request.args,
135
+ functionName: request.functionName
136
+ }), Effect.map(data => ({
137
+ to: request.contractAddress,
138
+ data,
139
+ value: undefined
140
+ })))));
141
+ yield* Effect.log({
142
+ calls
143
+ });
144
+ const simulationResult = yield* publicClient.simulateCalls({
145
+ calls: calls.map(call => ({
146
+ to: call.to,
147
+ data: call.data,
148
+ value: call.value
149
+ })),
150
+ account: wallet.account?.address
151
+ });
152
+ yield* Effect.log("[prepareBatchTransactionRequest] Simulation results", {
153
+ results: simulationResult.results
154
+ });
155
+ // Extract gas estimates with 20% buffer for safety
156
+ const gasEstimates = simulationResult.results?.map(r => r?.gasUsed ? BigInt(r.gasUsed) * 120n / 100n : undefined) ?? [];
157
+ yield* Effect.log("[prepareBatchTransactionRequest] Gas estimates", {
158
+ gasEstimates
159
+ });
160
+ // Prepare each transaction with gas
161
+ const prepared = [];
162
+ for (let i = 0; i < calls.length; i++) {
163
+ const call = calls[i];
164
+ const gas = gasEstimates[i];
165
+ const nonce = yield* Ref.get(transactionCount);
166
+ const request = yield* Effect.tryPromise({
167
+ try: () => wallet.prepareTransactionRequest({
168
+ to: call.to,
169
+ data: call.data,
170
+ ...(call.value !== undefined && {
171
+ value: call.value
172
+ }),
173
+ ...(gas !== undefined && {
174
+ gas
175
+ }),
176
+ nonce,
177
+ chain: wallet.chain
178
+ }),
179
+ catch: cause => new Error.SystemError({
180
+ method: "prepareBatchTransactionRequest",
181
+ module: "EvmWalletClient",
182
+ reason: "InvalidData",
183
+ cause
184
+ })
185
+ });
186
+ yield* Ref.update(transactionCount, Number.increment);
187
+ prepared.push(request);
188
+ }
189
+ return prepared;
190
+ }));
191
+ const signTransaction = Effect.fn("signTransaction")(params => Effect.tryPromise({
192
+ try: () => wallet.signTransaction(params),
193
+ catch: cause => new Error.SystemError({
194
+ method: "signTransaction",
195
+ module: "EvmWalletClient",
196
+ reason: "InvalidData",
197
+ cause
198
+ })
199
+ }));
200
+ const sendRawTransaction = Effect.fn("sendRawTransaction")(params => Effect.tryPromise({
201
+ try: () => wallet.sendRawTransaction(params),
202
+ catch: cause => new Error.SystemError({
203
+ method: "sendRawTransaction",
204
+ module: "EvmWalletClient",
205
+ reason: "InvalidData",
206
+ cause
207
+ })
208
+ }));
209
+ const sign = Effect.fn("sign")(function* (request) {
210
+ const requests = yield* (0, _Function.pipe)(request, A.ensure, Effect.forEach(n => (0, _Function.pipe)(n, Effect.liftPredicate(Domain.PreparedRequest.$is("PreparedEvm"), cause => new Error.SystemError({
211
+ method: "sign",
212
+ module: "EvmWalletClient",
213
+ reason: "InvalidData",
214
+ cause
215
+ })))));
216
+ return yield* (0, _Function.pipe)(requests, prepareBatchTransactionRequest, Effect.flatMap(Effect.forEach(value => signTransaction({
217
+ ...value,
218
+ account: wallet.account,
219
+ chain: wallet.chain
220
+ }))),
221
+ // Effect.tap((success) => Effect.log({ success })),
222
+ Effect.map(A.map(value => Domain.SignedRequest.SignedEvm({
223
+ value
224
+ }))));
225
+ });
226
+ const submit = Effect.fn("submit")(request => (0, _Function.pipe)(request, Effect.liftPredicate(Domain.SignedRequest.$is("SignedEvm"), cause => new Error.SystemError({
227
+ method: "submit",
228
+ module: "EvmWalletClient",
229
+ reason: "InvalidData",
230
+ cause
231
+ })), Effect.map(x => x.value), Effect.flatMap(serializedTransaction => sendRawTransaction({
232
+ serializedTransaction
233
+ })), Effect.map(hash => Domain.SubmissionResult.SubmissionEvm({
234
+ hash: Domain.TxHash(hash)
235
+ }))));
236
+ const signAndSubmit = Effect.fn("signAndSubmit")(function* (request) {
237
+ const _request = yield* Effect.liftPredicate(request, Domain.PreparedRequest.$is("PreparedEvm"), cause => new Error.SystemError({
238
+ method: "signAndSubmit",
239
+ module: "EvmWalletClient",
240
+ reason: "InvalidData",
241
+ cause
242
+ }));
243
+ const data = yield* encodeFunctionData({
244
+ abi: _request.abi,
245
+ args: _request.args,
246
+ functionName: _request.functionName
247
+ });
248
+ const prepared = yield* prepareTransactionRequest({
249
+ to: _request.contractAddress,
250
+ data,
251
+ chain: wallet.chain
252
+ });
253
+ return yield* (0, _Function.pipe)(Effect.tryPromise({
254
+ try: () => ViemActions.sendTransaction(wallet, {
255
+ to: prepared.to,
256
+ data: prepared.data,
257
+ value: prepared.value,
258
+ gas: prepared.gas,
259
+ nonce: prepared.nonce,
260
+ account: wallet.account,
261
+ chain: wallet.chain
262
+ }),
263
+ catch: cause => new Error.SystemError({
264
+ method: "signAndSubmit",
265
+ module: "EvmWalletClient",
266
+ reason: "InvalidData",
267
+ cause
268
+ })
269
+ }), Effect.map(hash => Domain.SubmissionResult.SubmissionEvm({
270
+ hash: Domain.TxHash(hash)
271
+ })));
272
+ });
273
+ // TODO: check if required
274
+ const approveZAssetToSpendErc20 = Effect.fn("approveZAssetToSpendErc20")(function* (args) {
275
+ return yield* (0, _Function.pipe)(ZAssetRegistry.getZAsset(args.universalChainId, args.srcErc20Address), Effect.mapError(cause => new Error.SystemError({
276
+ method: "approveZAssetToSpendErc20",
277
+ module: "EvmWalletClient",
278
+ reason: "InvalidData",
279
+ cause
280
+ })), Effect.flatMap(zAsset => validateFunctionData({
281
+ abi: Abi.ERC20_ABI,
282
+ functionName: "approve",
283
+ args: [zAsset, args.amount]
284
+ })), Effect.map(request => Domain.PreparedRequest.PreparedEvm({
285
+ abi: request.abi,
286
+ args: request.args ?? [],
287
+ contractAddress: args.srcErc20Address,
288
+ functionName: request.functionName || "",
289
+ universalChainId: args.universalChainId,
290
+ kind: "Erc20.Approve"
291
+ })));
292
+ });
293
+ // TODO: check if required
294
+ const updateLoopbackClient = Effect.fn("updateLoopbackClient")(function* ({
295
+ clientId,
296
+ height,
297
+ ibcHandlerAddress,
298
+ universalChainId
299
+ }) {
300
+ // const publicClient = Viem.createPublicClient({
301
+ // transport: Viem.http(wallet.transport.rpcUrl),
302
+ // });
303
+ const publicClient = wallet.extend(Viem.publicActions);
304
+ const blockNumber = height;
305
+ const block = yield* Effect.tryPromise({
306
+ try: () => publicClient.getBlock({
307
+ blockNumber
308
+ }),
309
+ catch: cause => new Error.SystemError({
310
+ module: "EvmWalletClient",
311
+ method: "updateLoopbackClient",
312
+ reason: "InvalidData",
313
+ description: "Cannot getBlockNumber",
314
+ cause
315
+ })
316
+ });
317
+ // if (!block.number) {
318
+ // throw new Error("Block number is null");
319
+ // }
320
+ // Helper to convert bigint/number to minimal RLP hex encoding
321
+ const toRlpHex = value => {
322
+ if (value === undefined || value === null || value === 0n || value === 0) {
323
+ return "0x";
324
+ }
325
+ let hex = typeof value === "bigint" ? value.toString(16) : value.toString(16);
326
+ if (hex.length % 2 !== 0) {
327
+ hex = "0" + hex;
328
+ }
329
+ return `0x${hex}`;
330
+ };
331
+ // Build header fields in order (pre-merge + post-merge fields)
332
+ const headerFields = [block.parentHash, block.sha3Uncles, block.miner, block.stateRoot, block.transactionsRoot, block.receiptsRoot, block.logsBloom ?? "0x" + "00".repeat(256), toRlpHex(block.difficulty), toRlpHex(block.number), toRlpHex(block.gasLimit), toRlpHex(block.gasUsed), toRlpHex(block.timestamp), block.extraData, block.mixHash ?? "0x0000000000000000000000000000000000000000000000000000000000000000", block.nonce ?? "0x0000000000000000"];
333
+ // Post-merge fields
334
+ if (block.baseFeePerGas !== undefined && block.baseFeePerGas !== null) {
335
+ headerFields.push(toRlpHex(block.baseFeePerGas));
336
+ }
337
+ if (block.withdrawalsRoot) {
338
+ headerFields.push(block.withdrawalsRoot);
339
+ }
340
+ if (block.blobGasUsed !== undefined && block.blobGasUsed !== null) {
341
+ headerFields.push(toRlpHex(block.blobGasUsed));
342
+ }
343
+ if (block.excessBlobGas !== undefined && block.excessBlobGas !== null) {
344
+ headerFields.push(toRlpHex(block.excessBlobGas));
345
+ }
346
+ if (block.parentBeaconBlockRoot) {
347
+ headerFields.push(block.parentBeaconBlockRoot);
348
+ }
349
+ const blockAny = block;
350
+ if (blockAny.requestsHash) {
351
+ headerFields.push(blockAny.requestsHash);
352
+ }
353
+ const rlpEncoded = Viem.toRlp(headerFields);
354
+ // Verify the encoding produces the correct block hash
355
+ const computedHash = Viem.keccak256(rlpEncoded);
356
+ if (computedHash !== block.hash) {
357
+ return yield* new Error.SystemError({
358
+ module: "EvmWalletClient",
359
+ method: "updateLooopbackClient",
360
+ reason: "InvalidData",
361
+ description: `RLP encoding mismatch: computed hash ${computedHash} does not match block hash ${block.hash}`
362
+ });
363
+ }
364
+ // Encode the Header struct: (uint64 height, bytes encodedHeader)
365
+ const clientMessage = Viem.encodeAbiParameters([{
366
+ type: "uint64",
367
+ name: "height"
368
+ }, {
369
+ type: "bytes",
370
+ name: "encodedHeader"
371
+ }], [block.number, rlpEncoded]);
372
+ const LIGHTCLIENT_ABI = [{
373
+ inputs: [{
374
+ name: "caller",
375
+ type: "address"
376
+ }, {
377
+ name: "clientId",
378
+ type: "uint32"
379
+ }, {
380
+ name: "clientMessage",
381
+ type: "bytes"
382
+ }, {
383
+ name: "relayer",
384
+ type: "address"
385
+ }],
386
+ name: "updateClient",
387
+ outputs: [],
388
+ stateMutability: "nonpayable",
389
+ type: "function"
390
+ }];
391
+ // Wait for the next block so the fetched block's hash is verifiable on-chain
392
+ let currentBlock = yield* Effect.tryPromise({
393
+ try: () => publicClient.getBlockNumber(),
394
+ catch: cause => new Error.SystemError({
395
+ module: "EvmWalletClient",
396
+ method: "updateLoopbackClient",
397
+ reason: "InvalidData",
398
+ description: "Cannot getBlockNumber",
399
+ cause
400
+ })
401
+ });
402
+ while (currentBlock <= blockNumber) {
403
+ yield* Effect.sleep("1 second");
404
+ currentBlock = yield* Effect.tryPromise({
405
+ try: () => publicClient.getBlockNumber(),
406
+ catch: cause => new Error.SystemError({
407
+ module: "EvmWalletClient",
408
+ method: "updateLoopbackClient",
409
+ reason: "InvalidData",
410
+ description: "Cannot getBlockNumber",
411
+ cause
412
+ })
413
+ });
414
+ }
415
+ const loopbackClient = yield* (0, _Function.pipe)(Effect.tryPromise({
416
+ try: () => publicClient.readContract({
417
+ address: ibcHandlerAddress,
418
+ abi: Abi.IBC_STORE_ABI,
419
+ functionName: "getClient",
420
+ args: [clientId]
421
+ }),
422
+ catch: cause => new Error.SystemError({
423
+ module: "EvmWalletClient",
424
+ method: "updateLoopbackClient",
425
+ reason: "InvalidData",
426
+ description: "cannot get client given ibc handler address",
427
+ cause
428
+ })
429
+ }), Effect.map(Domain.Erc20Address));
430
+ return yield* (0, _Function.pipe)(validateFunctionData({
431
+ abi: LIGHTCLIENT_ABI,
432
+ functionName: "updateClient",
433
+ args: [wallet.account.address, clientId, clientMessage, wallet.account.address]
434
+ }), Effect.map(data => Domain.PreparedRequest.PreparedEvm({
435
+ ...data,
436
+ contractAddress: loopbackClient,
437
+ universalChainId,
438
+ kind: "LoopbackClient.Update"
439
+ })));
440
+ });
441
+ const depositUnderlyingZAsset = Effect.fn("depositUnderlyingZAsset")(function* (args) {
442
+ const zAssetAddress = yield* (0, _Function.pipe)(ZAssetRegistry.getZAsset(args.universalChainId, args.srcErc20Address), Effect.mapError(cause => new Error.SystemError({
443
+ method: "depositUnderlyingZAsset",
444
+ module: "EvmWalletClient",
445
+ reason: "InvalidData",
446
+ cause
447
+ })));
448
+ return yield* (0, _Function.pipe)(validateFunctionData({
449
+ abi: Abi.ZASSET_ABI,
450
+ functionName: "deposit",
451
+ args: [args.amount]
452
+ }), Effect.map(data => Domain.PreparedRequest.PreparedEvm({
453
+ ...data,
454
+ contractAddress: Domain.Erc20Address(zAssetAddress),
455
+ universalChainId: args.universalChainId,
456
+ kind: "Erc20.Wrap"
457
+ })));
458
+ });
459
+ const transferZAsset = Effect.fn("transferZAsset")(function* (args) {
460
+ const zAssetAddress = yield* (0, _Function.pipe)(ZAssetRegistry.getZAsset(args.universalChainId, args.srcErc20Address), Effect.mapError(cause => new Error.SystemError({
461
+ method: "transferZAsset",
462
+ module: "EvmWalletClient",
463
+ reason: "InvalidData",
464
+ cause
465
+ })));
466
+ return yield* (0, _Function.pipe)(validateFunctionData({
467
+ abi: Abi.ZASSET_ABI,
468
+ functionName: "transfer",
469
+ args: [args.depositAddress, args.amount]
470
+ }), Effect.map(data => Domain.PreparedRequest.PreparedEvm({
471
+ ...data,
472
+ contractAddress: Domain.Erc20Address(zAssetAddress),
473
+ universalChainId: args.universalChainId,
474
+ kind: "ZAsset.Transfer"
475
+ })));
476
+ });
477
+ const prepareDeposit = Effect.fn("prepareDeposit")(function* (args) {
478
+ const zAssetAddress = yield* (0, _Function.pipe)(ZAssetRegistry.getZAsset(args.destinationChainId, args.srcErc20Address), Effect.mapError(cause => new Error.SystemError({
479
+ method: "prepareDeposit",
480
+ module: "EvmWalletClient",
481
+ reason: "InvalidData",
482
+ cause
483
+ })));
484
+ const [approval, deposit, transfer] = yield* Effect.all([(0, _Function.pipe)(validateFunctionData({
485
+ abi: Abi.ERC20_ABI,
486
+ functionName: "approve",
487
+ args: [zAssetAddress, args.amount]
488
+ }), Effect.map(data => Domain.PreparedRequest.PreparedEvm({
489
+ ...data,
490
+ contractAddress: args.srcErc20Address,
491
+ universalChainId: args.destinationChainId,
492
+ kind: "Erc20.Approve"
493
+ }))), (0, _Function.pipe)(validateFunctionData({
494
+ abi: Abi.ZASSET_ABI,
495
+ functionName: "deposit",
496
+ args: [args.amount]
497
+ }), Effect.map(data => Domain.PreparedRequest.PreparedEvm({
498
+ ...data,
499
+ contractAddress: Domain.Erc20Address(zAssetAddress),
500
+ universalChainId: args.destinationChainId,
501
+ kind: "Erc20.Wrap"
502
+ }))), (0, _Function.pipe)(validateFunctionData({
503
+ abi: Abi.ZASSET_ABI,
504
+ functionName: "transfer",
505
+ args: [args.depositAddress, args.amount]
506
+ }), Effect.map(data => Domain.PreparedRequest.PreparedEvm({
507
+ ...data,
508
+ contractAddress: Domain.Erc20Address(zAssetAddress),
509
+ universalChainId: args.destinationChainId,
510
+ kind: "ZAsset.Transfer"
511
+ })))], {
512
+ concurrency: "unbounded"
513
+ });
514
+ yield* Effect.log("[deposit]", {
515
+ approval,
516
+ deposit,
517
+ transfer
518
+ });
519
+ return [approval, deposit, transfer];
520
+ });
521
+ const prepareRedemption = Effect.fn("prepareRedemption")(function* (args) {
522
+ const zAssetAddress = yield* (0, _Function.pipe)(ZAssetRegistry.getZAsset(args.universalChainId, args.dstErc20Address), Effect.mapError(cause => new Error.SystemError({
523
+ method: "prepareRedemption",
524
+ module: "EvmWalletClient",
525
+ reason: "InvalidData",
526
+ cause
527
+ })));
528
+ return yield* (0, _Function.pipe)(validateFunctionData({
529
+ abi: Abi.ZASSET_ABI,
530
+ functionName: "redeem",
531
+ args: [args.proof, args.commitments, args.commitmentPok, args.lightClients, args.nullifier, args.value, args.beneficiary, args.attestedMessage, args.signature, args.unwrap ?? true]
532
+ }), Effect.map(data => Domain.PreparedRequest.PreparedEvm({
533
+ ...data,
534
+ contractAddress: Domain.Erc20Address(zAssetAddress),
535
+ universalChainId: args.universalChainId,
536
+ kind: "ZAsset.Transfer"
537
+ })));
538
+ });
539
+ return Object.assign(yield* Client.make({
540
+ sign: sign,
541
+ submit,
542
+ approveZAssetToSpendErc20,
543
+ updateLoopbackClient,
544
+ depositUnderlyingZAsset,
545
+ transferZAsset,
546
+ prepareDeposit,
547
+ prepareRedemption,
548
+ signAndSubmit
549
+ }), {
550
+ [TypeId]: TypeId,
551
+ signTransaction,
552
+ sendRawTransaction,
553
+ writeContract,
554
+ prepareBatchTransactionRequest
555
+ });
556
+ });
557
+ /** @internal */
558
+ exports.makeViem = makeViem;
559
+ const makeBrowser = exports.makeBrowser = /*#__PURE__*/Effect.fn(function* (options) {
560
+ const Mipd = yield* Effect.tryPromise({
561
+ try: () => import("mipd").then(x => x),
562
+ catch: cause => new Error.SystemError({
563
+ reason: "DynamicImport",
564
+ method: "makeBrowser",
565
+ module: "EvmWalletClient",
566
+ cause,
567
+ description: "could not import mipd"
568
+ })
569
+ });
570
+ const store = Mipd.createStore();
571
+ yield* Effect.log({
572
+ store
573
+ });
574
+ const providers = store.getProviders();
575
+ yield* Effect.log({
576
+ providers
577
+ });
578
+ const selected = yield* (0, _Function.pipe)(A.findFirst(providers, options.providerPredicate), Effect.mapError(cause => new Error.SystemError({
579
+ reason: "DynamicImport",
580
+ method: "makeBrowser",
581
+ module: "EvmWalletClient",
582
+ cause,
583
+ description: "could not find provider"
584
+ })));
585
+ yield* Effect.log({
586
+ selected
587
+ });
588
+ const account = yield* (0, _Function.pipe)(Effect.tryPromise({
589
+ try: () => selected.provider.request({
590
+ method: "eth_requestAccounts"
591
+ }),
592
+ catch: cause => new Error.SystemError({
593
+ reason: "DynamicImport",
594
+ method: "makeBrowser",
595
+ module: "EvmWalletClient",
596
+ cause,
597
+ description: "could not request accounts"
598
+ })
599
+ }), Effect.flatMap(A.findFirst(options.accountPredicate)), Effect.catchTag("NoSuchElementException", cause => new Error.SystemError({
600
+ reason: "DynamicImport",
601
+ method: "makeBrowser",
602
+ module: "EvmWalletClient",
603
+ cause,
604
+ description: "could not match account by predicate"
605
+ })));
606
+ const Viem = yield* Effect.tryPromise({
607
+ try: () => import("viem").then(x => x),
608
+ catch: cause => new Error.SystemError({
609
+ reason: "DynamicImport",
610
+ method: "makeViem",
611
+ module: "EvmWalletClient",
612
+ cause,
613
+ description: "could not import viem"
614
+ })
615
+ });
616
+ const ViemChains = yield* Effect.tryPromise({
617
+ try: () => import("viem/chains").then(x => x),
618
+ catch: cause => new Error.SystemError({
619
+ reason: "DynamicImport",
620
+ method: "makeViem",
621
+ module: "EvmWalletClient",
622
+ cause,
623
+ description: "could not import viem/chains"
624
+ })
625
+ });
626
+ const wallet = yield* Effect.try({
627
+ try: () => Viem.createWalletClient({
628
+ account: account,
629
+ chain: ViemChains.base,
630
+ transport: Viem.custom(selected.provider)
631
+ }),
632
+ catch: cause => new Error.SystemError({
633
+ reason: "DynamicImport",
634
+ method: "makeViem",
635
+ module: "EvmWalletClient",
636
+ cause,
637
+ description: "could not import viem/chains"
638
+ })
639
+ });
640
+ return yield* makeViem(wallet);
641
+ });
642
+ // import type * as ThirdwebAdaptersViem from "thirdweb/adapters/viem"
643
+ // export const makeThirdweb = Effect.fn(function* (...options: Parameters<typeof ThirdwebAdaptersViem['viemAdapter']['wallet']['toViem']>) {
644
+ // const { viemAdapter } = yield* Effect.tryPromise({
645
+ // try: () => import("thirdweb/adapters/viem"),
646
+ // catch: (cause) => new Error.SystemError({
647
+ // method: "makeThirdweb",
648
+ // module: "EvmWalletClient",
649
+ // reason: "DynamicImport",
650
+ // cause,
651
+ // })
652
+ // })
653
+ // const wallet = viemAdapter.wallet.toViem(...options)
654
+ // return layerFromViem(wallet as Viem.WalletClient)
655
+ // })
6
656
  //# sourceMappingURL=evmWalletClient.js.map