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