@matterlabs/zksync-js 0.0.21 → 0.0.23
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/dist/adapters/ethers/client.cjs +31 -8
- package/dist/adapters/ethers/client.cjs.map +1 -1
- package/dist/adapters/ethers/client.d.ts +11 -0
- package/dist/adapters/ethers/client.js +6 -5
- package/dist/adapters/ethers/index.cjs +1194 -364
- package/dist/adapters/ethers/index.cjs.map +1 -1
- package/dist/adapters/ethers/index.d.ts +2 -1
- package/dist/adapters/ethers/index.js +10 -8
- package/dist/adapters/ethers/resources/deposits/context.d.ts +2 -0
- package/dist/adapters/ethers/resources/deposits/services/gas.d.ts +3 -0
- package/dist/adapters/ethers/resources/withdrawals/context.d.ts +12 -1
- package/dist/adapters/ethers/resources/withdrawals/index.d.ts +17 -1
- package/dist/adapters/ethers/resources/withdrawals/routes/interop-bundle.d.ts +2 -0
- package/dist/adapters/ethers/resources/withdrawals/services/finalization.d.ts +35 -17
- package/dist/adapters/ethers/resources/withdrawals/services/protocol.d.ts +11 -0
- package/dist/adapters/ethers/sdk.cjs +1166 -370
- package/dist/adapters/ethers/sdk.cjs.map +1 -1
- package/dist/adapters/ethers/sdk.d.ts +3 -1
- package/dist/adapters/ethers/sdk.js +8 -7
- package/dist/adapters/viem/client.cjs +44 -7
- package/dist/adapters/viem/client.cjs.map +1 -1
- package/dist/adapters/viem/client.d.ts +11 -0
- package/dist/adapters/viem/client.js +6 -5
- package/dist/adapters/viem/index.cjs +1237 -383
- package/dist/adapters/viem/index.cjs.map +1 -1
- package/dist/adapters/viem/index.d.ts +2 -1
- package/dist/adapters/viem/index.js +10 -8
- package/dist/adapters/viem/resources/deposits/context.d.ts +2 -0
- package/dist/adapters/viem/resources/deposits/services/gas.d.ts +3 -0
- package/dist/adapters/viem/resources/withdrawals/context.d.ts +12 -1
- package/dist/adapters/viem/resources/withdrawals/index.d.ts +17 -1
- package/dist/adapters/viem/resources/withdrawals/routes/interop-bundle.d.ts +2 -0
- package/dist/adapters/viem/resources/withdrawals/services/finalization.d.ts +36 -18
- package/dist/adapters/viem/resources/withdrawals/services/protocol.d.ts +11 -0
- package/dist/adapters/viem/sdk.cjs +1191 -385
- package/dist/adapters/viem/sdk.cjs.map +1 -1
- package/dist/adapters/viem/sdk.d.ts +3 -1
- package/dist/adapters/viem/sdk.js +8 -7
- package/dist/{chunk-UEKFQAOS.js → chunk-2DDJFHHD.js} +304 -9
- package/dist/{chunk-MT4X5FEO.js → chunk-3IPSDGHO.js} +19 -1
- package/dist/{chunk-JSBMIT4S.js → chunk-4GPIHYVF.js} +1 -1
- package/dist/{chunk-SBGBYZJM.js → chunk-4M7VNYGC.js} +2 -2
- package/dist/{chunk-CK5UFAZK.js → chunk-6WIAMFIW.js} +37 -3
- package/dist/chunk-JJ523MOL.js +157 -0
- package/dist/{chunk-7GZDTLHX.js → chunk-NVOFHV3A.js} +666 -304
- package/dist/{chunk-LGTYM5XP.js → chunk-OEUDZFXH.js} +1 -1
- package/dist/{chunk-NJK325XV.js → chunk-TUN6NH5D.js} +2 -2
- package/dist/chunk-UMPAKZCQ.js +19 -0
- package/dist/{chunk-24TE2NNJ.js → chunk-URHDNDFN.js} +24 -4
- package/dist/{chunk-2CGBLD5H.js → chunk-V2TDNGI5.js} +642 -288
- package/dist/{chunk-IDXSGCGF.js → chunk-WPCZNITC.js} +63 -50
- package/dist/{chunk-TYYUG5GA.js → chunk-X2JJDQ4O.js} +1 -1
- package/dist/core/abi.d.ts +1 -0
- package/dist/core/adapters/interfaces.d.ts +2 -0
- package/dist/core/constants.cjs +18 -0
- package/dist/core/constants.cjs.map +1 -1
- package/dist/core/constants.d.ts +17 -0
- package/dist/core/constants.js +1 -1
- package/dist/core/index.cjs +504 -7
- package/dist/core/index.cjs.map +1 -1
- package/dist/core/index.d.ts +6 -1
- package/dist/core/index.js +6 -4
- package/dist/core/internal/abis/IERC7786Attributes.d.ts +10 -0
- package/dist/core/internal/abis/IL1NullifierV32.d.ts +221 -0
- package/dist/core/internal/abis/IL2AssetRouter.d.ts +0 -14
- package/dist/core/resources/deposits/priority.d.ts +30 -0
- package/dist/core/resources/interop/attributes/bundle.d.ts +1 -0
- package/dist/core/resources/interop/attributes/resource.d.ts +1 -0
- package/dist/core/resources/protocol/semver.d.ts +18 -0
- package/dist/core/resources/withdrawals/bundle.d.ts +80 -0
- package/dist/core/resources/withdrawals/finalization.d.ts +142 -0
- package/dist/core/resources/withdrawals/protocol.d.ts +55 -0
- package/dist/core/types/flows/withdrawals.d.ts +30 -0
- package/dist/index.cjs +320 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +4 -4
- package/package.json +1 -1
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import { createNTVCodec,
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
1
|
+
import { createNTVCodec, createAttributesResource, resolveCreateDepositL1GasLimit, isInteropFinalizationInfo, DEFAULT_POLL_MS, DEFAULT_TIMEOUT_MS, resolveIdsFromWaitable, parseBundleReceiptInfo, buildFinalizationInfo, parseBundleSentFromReceipt, pickInteropRoute, ZERO_HASH, toGasOverrides, clampPriorityL2GasLimit, applyPriorityL2GasLimitBuffer, buildFeeBreakdown, buildIndirectBundle, preflightIndirect, buildDirectBundle, preflightDirect, derivePriorityTxGasBreakdown, quoteL2Gas, quoteL1Gas, applyL1ToL2Alias, derivePriorityBodyGasEstimateCap, assertProtocolVersion, resolveRegisteredTokenPriorityL2GasLimit, quoteL2Gas2 } from './chunk-WPCZNITC.js';
|
|
2
|
+
import { detectWithdrawalProtocol, buildWithdrawalFinalization, parseBundleHashFromLogs, classifyWithdrawalOutcome, phaseFromReadiness, buildWithdrawalBundle } from './chunk-JJ523MOL.js';
|
|
3
|
+
import { findL1MessageSentLog, messengerLogIndex, l1MessageSentSender, pickWithdrawRoute } from './chunk-OEUDZFXH.js';
|
|
4
|
+
import { createErrorHandlers, toZKsyncError, classifyReadinessFromRevert } from './chunk-4M7VNYGC.js';
|
|
5
|
+
import { IERC7786Attributes_default, isHash66, IL1Nullifier_default, IInteropHandler_default, OP_WITHDRAWALS, createError, normalizeL1Token, isAddressEq, hexEq, OP_DEPOSITS, IERC20_default, isZKsyncError, isReceiptNotFound, IL1NullifierV32_default, OP_INTEROP, IInteropRootStorage_default, IInteropCenter_default, sleep, IBridgehub_default, isETH, normalizeAddrEq, IL2AssetRouter_default, L2NativeTokenVault_default, IBaseToken_default, assertNever } from './chunk-2DDJFHHD.js';
|
|
6
|
+
import { ETH_ADDRESS, TOPIC_CANONICAL_ASSIGNED, TOPIC_CANONICAL_SUCCESS, L2_BASE_TOKEN_ADDRESS, L2_NATIVE_TOKEN_VAULT_ADDRESS, L1_MESSENGER_ADDRESS, L2_INTEROP_ROOT_STORAGE_ADDRESS, BUFFER, SAFE_L1_BRIDGE_GAS, L2_ASSET_ROUTER_ADDRESS, FORMAL_ETH_ADDRESS, ZKSYNC_OS_PRIORITY_TX_TYPE } from './chunk-3IPSDGHO.js';
|
|
7
|
+
import { keccak256, encodeAbiParameters, toBytes, encodeFunctionData, concat, decodeEventLog, createWalletClient, custom, decodeAbiParameters, createPublicClient, http, encodeEventTopics, numberToHex, zeroAddress, getAddress, toHex } from 'viem';
|
|
7
8
|
|
|
8
9
|
// src/adapters/viem/resources/deposits/context.ts
|
|
9
10
|
async function commonCtx(p, client, tokens, contracts) {
|
|
10
|
-
const { bridgehub, l1AssetRouter } = await contracts.addresses();
|
|
11
|
+
const { bridgehub, l1AssetRouter, l1NativeTokenVault } = await contracts.addresses();
|
|
11
12
|
const chainId = await client.l2.getChainId();
|
|
12
13
|
const sender = client.account.address;
|
|
13
14
|
const gasPerPubdata = p.gasPerPubdata ?? 800n;
|
|
@@ -35,6 +36,7 @@ async function commonCtx(p, client, tokens, contracts) {
|
|
|
35
36
|
baseTokenL1,
|
|
36
37
|
baseIsEth,
|
|
37
38
|
l1AssetRouter,
|
|
39
|
+
l1NativeTokenVault,
|
|
38
40
|
route,
|
|
39
41
|
bridgehub,
|
|
40
42
|
chainIdL2: BigInt(chainId),
|
|
@@ -147,6 +149,20 @@ function viemToGasEstimator(client) {
|
|
|
147
149
|
maxPriorityFeePerGas: tx.maxPriorityFeePerGas
|
|
148
150
|
});
|
|
149
151
|
},
|
|
152
|
+
async estimatePriorityTxGas(tx) {
|
|
153
|
+
const result = await client.request({
|
|
154
|
+
method: "eth_estimateGas",
|
|
155
|
+
params: [
|
|
156
|
+
{
|
|
157
|
+
from: tx.from,
|
|
158
|
+
to: tx.to,
|
|
159
|
+
data: tx.data,
|
|
160
|
+
type: ZKSYNC_OS_PRIORITY_TX_TYPE
|
|
161
|
+
}
|
|
162
|
+
]
|
|
163
|
+
});
|
|
164
|
+
return BigInt(result);
|
|
165
|
+
},
|
|
150
166
|
async estimateFeesPerGas() {
|
|
151
167
|
try {
|
|
152
168
|
const fees = await client.estimateFeesPerGas();
|
|
@@ -180,6 +196,14 @@ function viemToGasEstimator(client) {
|
|
|
180
196
|
|
|
181
197
|
// src/adapters/viem/resources/deposits/services/gas.ts
|
|
182
198
|
var DEFAULT_SAFE_NONBASE_L2_GAS_LIMIT = 3000000n;
|
|
199
|
+
async function estimatePriorityTxL2Gas(ctx, leg) {
|
|
200
|
+
if (!leg) return void 0;
|
|
201
|
+
try {
|
|
202
|
+
return await viemToGasEstimator(ctx.client.l2).estimatePriorityTxGas?.(leg);
|
|
203
|
+
} catch {
|
|
204
|
+
return void 0;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
183
207
|
async function quoteL1Gas2(input) {
|
|
184
208
|
const { ctx, tx, overrides, fallbackGasLimit } = input;
|
|
185
209
|
const estimator = viemToGasEstimator(ctx.client.l1);
|
|
@@ -234,7 +258,12 @@ async function determineNonBaseL2Gas(input) {
|
|
|
234
258
|
return quoteL2Gas3({
|
|
235
259
|
ctx,
|
|
236
260
|
route,
|
|
237
|
-
overrideGasLimit:
|
|
261
|
+
overrideGasLimit: resolveRegisteredTokenPriorityL2GasLimit({
|
|
262
|
+
chainIdL2: ctx.chainIdL2,
|
|
263
|
+
priorityFloorGasLimit: input.priorityFloorGasLimit,
|
|
264
|
+
gasPerPubdata: ctx.gasPerPubdata,
|
|
265
|
+
nodeEstimate: await estimatePriorityTxL2Gas(ctx, input.priorityTxL2Leg)
|
|
266
|
+
})
|
|
238
267
|
});
|
|
239
268
|
}
|
|
240
269
|
const modelTx = {
|
|
@@ -271,6 +300,7 @@ async function determineEthNonBaseL2Gas(input) {
|
|
|
271
300
|
knownL2Token: input.ctx.resolvedToken?.l2,
|
|
272
301
|
modelTx: input.modelTx,
|
|
273
302
|
priorityFloorGasLimit: input.priorityFloorGasLimit,
|
|
303
|
+
priorityTxL2Leg: input.priorityTxL2Leg,
|
|
274
304
|
undeployedGasLimit: input.undeployedGasLimit
|
|
275
305
|
});
|
|
276
306
|
}
|
|
@@ -372,9 +402,13 @@ function routeEthDirect() {
|
|
|
372
402
|
l2Calldata,
|
|
373
403
|
gasPerPubdata: ctx.gasPerPubdata
|
|
374
404
|
});
|
|
375
|
-
const quotedL2GasLimit = ctx.l2GasLimit ??
|
|
376
|
-
|
|
377
|
-
|
|
405
|
+
const quotedL2GasLimit = ctx.l2GasLimit ?? clampPriorityL2GasLimit({
|
|
406
|
+
gasLimit: applyPriorityL2GasLimitBuffer({
|
|
407
|
+
chainIdL2: ctx.chainIdL2,
|
|
408
|
+
gasLimit: priorityFloorBreakdown.derivedL2GasLimit
|
|
409
|
+
}),
|
|
410
|
+
l2Calldata,
|
|
411
|
+
gasPerPubdata: ctx.gasPerPubdata
|
|
378
412
|
});
|
|
379
413
|
const l2GasParams = await quoteL2Gas3({
|
|
380
414
|
ctx,
|
|
@@ -520,26 +554,18 @@ var ntvCodec = createNTVCodec({
|
|
|
520
554
|
keccak256
|
|
521
555
|
});
|
|
522
556
|
async function encodeSecondBridgeErc20DepositCalldata(input) {
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
const l1ChainId = BigInt(await input.ctx.client.l1.getChainId());
|
|
527
|
-
const expectedL1AssetId = ntvCodec.encodeAssetId(
|
|
528
|
-
l1ChainId,
|
|
557
|
+
const registeredAssetId = input.ctx.resolvedToken?.assetId;
|
|
558
|
+
const assetId = registeredAssetId && registeredAssetId.toLowerCase() !== ZERO_ASSET_ID ? registeredAssetId : ntvCodec.encodeAssetId(
|
|
559
|
+
BigInt(await input.ctx.client.l1.getChainId()),
|
|
529
560
|
L2_NATIVE_TOKEN_VAULT_ADDRESS,
|
|
530
561
|
input.token
|
|
531
562
|
);
|
|
532
|
-
const assetId = input.ctx.resolvedToken.assetId.toLowerCase();
|
|
533
|
-
const isL1Origin = assetId === ZERO_ASSET_ID || assetId === expectedL1AssetId.toLowerCase();
|
|
534
|
-
if (isL1Origin) {
|
|
535
|
-
return encodeSecondBridgeErc20Args(input.token, input.amount, input.receiver);
|
|
536
|
-
}
|
|
537
563
|
const transferData = encodeNativeTokenVaultTransferData(
|
|
538
564
|
input.amount,
|
|
539
565
|
input.receiver,
|
|
540
566
|
input.token
|
|
541
567
|
);
|
|
542
|
-
return encodeSecondBridgeDataV1(
|
|
568
|
+
return encodeSecondBridgeDataV1(assetId, transferData);
|
|
543
569
|
}
|
|
544
570
|
async function getPriorityGasModel(input) {
|
|
545
571
|
try {
|
|
@@ -581,9 +607,18 @@ async function getPriorityGasModel(input) {
|
|
|
581
607
|
gasPerPubdata: input.ctx.gasPerPubdata
|
|
582
608
|
});
|
|
583
609
|
const model = {
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
610
|
+
priorityTxL2Leg: {
|
|
611
|
+
from: applyL1ToL2Alias(input.ctx.l1AssetRouter),
|
|
612
|
+
to: L2_ASSET_ROUTER_ADDRESS,
|
|
613
|
+
data: l2Calldata
|
|
614
|
+
},
|
|
615
|
+
priorityFloorGasLimit: clampPriorityL2GasLimit({
|
|
616
|
+
gasLimit: applyPriorityL2GasLimitBuffer({
|
|
617
|
+
chainIdL2: input.ctx.chainIdL2,
|
|
618
|
+
gasLimit: priorityFloorBreakdown.derivedL2GasLimit
|
|
619
|
+
}),
|
|
620
|
+
l2Calldata,
|
|
621
|
+
gasPerPubdata: input.ctx.gasPerPubdata
|
|
587
622
|
})
|
|
588
623
|
};
|
|
589
624
|
if (isFirstBridge || input.ctx.resolvedToken.l2.toLowerCase() === zeroAddress) {
|
|
@@ -626,6 +661,7 @@ function routeErc20NonBase() {
|
|
|
626
661
|
const baseToken = ctx.baseTokenL1 ?? await ctx.client.baseToken(ctx.chainIdL2);
|
|
627
662
|
const baseIsEth = ctx.baseIsEth ?? isETH(baseToken);
|
|
628
663
|
const assetRouter = ctx.l1AssetRouter;
|
|
664
|
+
const nativeTokenVault = ctx.l1NativeTokenVault;
|
|
629
665
|
const receiver = p.to ?? ctx.sender;
|
|
630
666
|
const secondBridgeCalldata = await wrapAs3(
|
|
631
667
|
"INTERNAL",
|
|
@@ -655,6 +691,7 @@ function routeErc20NonBase() {
|
|
|
655
691
|
ctx,
|
|
656
692
|
l1Token: p.token,
|
|
657
693
|
priorityFloorGasLimit: priorityGasModel.priorityFloorGasLimit,
|
|
694
|
+
priorityTxL2Leg: priorityGasModel.priorityTxL2Leg,
|
|
658
695
|
undeployedGasLimit: priorityGasModel.undeployedGasLimit,
|
|
659
696
|
modelTx: {
|
|
660
697
|
to: receiver,
|
|
@@ -675,10 +712,10 @@ function routeErc20NonBase() {
|
|
|
675
712
|
address: p.token,
|
|
676
713
|
abi: IERC20_default,
|
|
677
714
|
functionName: "allowance",
|
|
678
|
-
args: [ctx.sender,
|
|
715
|
+
args: [ctx.sender, nativeTokenVault]
|
|
679
716
|
}),
|
|
680
717
|
{
|
|
681
|
-
ctx: { where: "erc20.allowance", token: p.token, spender:
|
|
718
|
+
ctx: { where: "erc20.allowance", token: p.token, spender: nativeTokenVault },
|
|
682
719
|
message: "Failed to read deposit-token allowance."
|
|
683
720
|
}
|
|
684
721
|
);
|
|
@@ -689,7 +726,7 @@ function routeErc20NonBase() {
|
|
|
689
726
|
() => buildApprovalRequest({
|
|
690
727
|
ctx,
|
|
691
728
|
token: p.token,
|
|
692
|
-
spender:
|
|
729
|
+
spender: nativeTokenVault,
|
|
693
730
|
amount: p.amount
|
|
694
731
|
}),
|
|
695
732
|
{
|
|
@@ -697,9 +734,9 @@ function routeErc20NonBase() {
|
|
|
697
734
|
message: "Failed to simulate deposit token approve."
|
|
698
735
|
}
|
|
699
736
|
);
|
|
700
|
-
approvals.push({ token: p.token, spender:
|
|
737
|
+
approvals.push({ token: p.token, spender: nativeTokenVault, amount: p.amount });
|
|
701
738
|
steps.push({
|
|
702
|
-
key: `approve:${p.token}:${
|
|
739
|
+
key: `approve:${p.token}:${nativeTokenVault}`,
|
|
703
740
|
kind: "approve",
|
|
704
741
|
description: `Approve deposit token for amount`,
|
|
705
742
|
tx: approveTx
|
|
@@ -713,10 +750,10 @@ function routeErc20NonBase() {
|
|
|
713
750
|
address: baseToken,
|
|
714
751
|
abi: IERC20_default,
|
|
715
752
|
functionName: "allowance",
|
|
716
|
-
args: [ctx.sender,
|
|
753
|
+
args: [ctx.sender, nativeTokenVault]
|
|
717
754
|
}),
|
|
718
755
|
{
|
|
719
|
-
ctx: { where: "erc20.allowance", token: baseToken, spender:
|
|
756
|
+
ctx: { where: "erc20.allowance", token: baseToken, spender: nativeTokenVault },
|
|
720
757
|
message: "Failed to read base-token allowance."
|
|
721
758
|
}
|
|
722
759
|
);
|
|
@@ -727,7 +764,7 @@ function routeErc20NonBase() {
|
|
|
727
764
|
() => buildApprovalRequest({
|
|
728
765
|
ctx,
|
|
729
766
|
token: baseToken,
|
|
730
|
-
spender:
|
|
767
|
+
spender: nativeTokenVault,
|
|
731
768
|
amount: mintValue
|
|
732
769
|
}),
|
|
733
770
|
{
|
|
@@ -735,9 +772,9 @@ function routeErc20NonBase() {
|
|
|
735
772
|
message: "Failed to simulate base token approve."
|
|
736
773
|
}
|
|
737
774
|
);
|
|
738
|
-
approvals.push({ token: baseToken, spender:
|
|
775
|
+
approvals.push({ token: baseToken, spender: nativeTokenVault, amount: mintValue });
|
|
739
776
|
steps.push({
|
|
740
|
-
key: `approve:${baseToken}:${
|
|
777
|
+
key: `approve:${baseToken}:${nativeTokenVault}`,
|
|
741
778
|
kind: "approve",
|
|
742
779
|
description: `Approve base token for mintValue`,
|
|
743
780
|
tx: approveBaseTx
|
|
@@ -876,9 +913,18 @@ async function getPriorityGasModel2(input) {
|
|
|
876
913
|
gasPerPubdata: input.ctx.gasPerPubdata
|
|
877
914
|
});
|
|
878
915
|
const model = {
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
916
|
+
priorityTxL2Leg: {
|
|
917
|
+
from: applyL1ToL2Alias(input.ctx.l1AssetRouter),
|
|
918
|
+
to: L2_ASSET_ROUTER_ADDRESS,
|
|
919
|
+
data: l2Calldata
|
|
920
|
+
},
|
|
921
|
+
priorityFloorGasLimit: clampPriorityL2GasLimit({
|
|
922
|
+
gasLimit: applyPriorityL2GasLimitBuffer({
|
|
923
|
+
chainIdL2: input.ctx.chainIdL2,
|
|
924
|
+
gasLimit: priorityFloorBreakdown.derivedL2GasLimit
|
|
925
|
+
}),
|
|
926
|
+
l2Calldata,
|
|
927
|
+
gasPerPubdata: input.ctx.gasPerPubdata
|
|
882
928
|
})
|
|
883
929
|
};
|
|
884
930
|
if (input.ctx.resolvedToken.l2.toLowerCase() === zeroAddress) {
|
|
@@ -953,6 +999,7 @@ function routeEthNonBase() {
|
|
|
953
999
|
ctx,
|
|
954
1000
|
modelTx: l2TxModel,
|
|
955
1001
|
priorityFloorGasLimit: priorityGasModel.priorityFloorGasLimit,
|
|
1002
|
+
priorityTxL2Leg: priorityGasModel.priorityTxL2Leg,
|
|
956
1003
|
undeployedGasLimit: priorityGasModel.undeployedGasLimit
|
|
957
1004
|
});
|
|
958
1005
|
if (!l2Gas) throw new Error("Failed to estimate L2 gas parameters.");
|
|
@@ -967,10 +1014,10 @@ function routeEthNonBase() {
|
|
|
967
1014
|
address: baseToken,
|
|
968
1015
|
abi: IERC20_default,
|
|
969
1016
|
functionName: "allowance",
|
|
970
|
-
args: [ctx.sender, ctx.
|
|
1017
|
+
args: [ctx.sender, ctx.l1NativeTokenVault]
|
|
971
1018
|
}),
|
|
972
1019
|
{
|
|
973
|
-
ctx: { where: "erc20.allowance", token: baseToken, spender: ctx.
|
|
1020
|
+
ctx: { where: "erc20.allowance", token: baseToken, spender: ctx.l1NativeTokenVault },
|
|
974
1021
|
message: "Failed to read base-token allowance."
|
|
975
1022
|
}
|
|
976
1023
|
);
|
|
@@ -982,7 +1029,7 @@ function routeEthNonBase() {
|
|
|
982
1029
|
() => buildApprovalRequest({
|
|
983
1030
|
ctx,
|
|
984
1031
|
token: baseToken,
|
|
985
|
-
spender: ctx.
|
|
1032
|
+
spender: ctx.l1NativeTokenVault,
|
|
986
1033
|
amount: mintValue
|
|
987
1034
|
}),
|
|
988
1035
|
{
|
|
@@ -990,9 +1037,9 @@ function routeEthNonBase() {
|
|
|
990
1037
|
message: "Failed to simulate base-token approve."
|
|
991
1038
|
}
|
|
992
1039
|
);
|
|
993
|
-
approvals.push({ token: baseToken, spender: ctx.
|
|
1040
|
+
approvals.push({ token: baseToken, spender: ctx.l1NativeTokenVault, amount: mintValue });
|
|
994
1041
|
steps.push({
|
|
995
|
-
key: `approve:${baseToken}:${ctx.
|
|
1042
|
+
key: `approve:${baseToken}:${ctx.l1NativeTokenVault}`,
|
|
996
1043
|
kind: "approve",
|
|
997
1044
|
description: `Approve base token for fees (mintValue)`,
|
|
998
1045
|
tx: approveTx
|
|
@@ -1145,9 +1192,13 @@ function routeErc20Base() {
|
|
|
1145
1192
|
l2Calldata,
|
|
1146
1193
|
gasPerPubdata: ctx.gasPerPubdata
|
|
1147
1194
|
});
|
|
1148
|
-
const quotedL2GasLimit = ctx.l2GasLimit ??
|
|
1149
|
-
|
|
1150
|
-
|
|
1195
|
+
const quotedL2GasLimit = ctx.l2GasLimit ?? clampPriorityL2GasLimit({
|
|
1196
|
+
gasLimit: applyPriorityL2GasLimitBuffer({
|
|
1197
|
+
chainIdL2: ctx.chainIdL2,
|
|
1198
|
+
gasLimit: priorityFloorBreakdown.derivedL2GasLimit
|
|
1199
|
+
}),
|
|
1200
|
+
l2Calldata,
|
|
1201
|
+
gasPerPubdata: ctx.gasPerPubdata
|
|
1151
1202
|
});
|
|
1152
1203
|
const l2Gas = await quoteL2Gas3({
|
|
1153
1204
|
ctx,
|
|
@@ -1166,10 +1217,10 @@ function routeErc20Base() {
|
|
|
1166
1217
|
address: baseToken,
|
|
1167
1218
|
abi: IERC20_default,
|
|
1168
1219
|
functionName: "allowance",
|
|
1169
|
-
args: [ctx.sender, ctx.
|
|
1220
|
+
args: [ctx.sender, ctx.l1NativeTokenVault]
|
|
1170
1221
|
}),
|
|
1171
1222
|
{
|
|
1172
|
-
ctx: { where: "erc20.allowance", token: baseToken, spender: ctx.
|
|
1223
|
+
ctx: { where: "erc20.allowance", token: baseToken, spender: ctx.l1NativeTokenVault },
|
|
1173
1224
|
message: "Failed to read base-token allowance."
|
|
1174
1225
|
}
|
|
1175
1226
|
);
|
|
@@ -1181,7 +1232,7 @@ function routeErc20Base() {
|
|
|
1181
1232
|
() => buildApprovalRequest({
|
|
1182
1233
|
ctx,
|
|
1183
1234
|
token: baseToken,
|
|
1184
|
-
spender: ctx.
|
|
1235
|
+
spender: ctx.l1NativeTokenVault,
|
|
1185
1236
|
amount: mintValue
|
|
1186
1237
|
}),
|
|
1187
1238
|
{
|
|
@@ -1189,9 +1240,9 @@ function routeErc20Base() {
|
|
|
1189
1240
|
message: "Failed to simulate ERC-20 approve."
|
|
1190
1241
|
}
|
|
1191
1242
|
);
|
|
1192
|
-
approvals.push({ token: baseToken, spender: ctx.
|
|
1243
|
+
approvals.push({ token: baseToken, spender: ctx.l1NativeTokenVault, amount: mintValue });
|
|
1193
1244
|
steps.push({
|
|
1194
|
-
key: `approve:${baseToken}:${ctx.
|
|
1245
|
+
key: `approve:${baseToken}:${ctx.l1NativeTokenVault}`,
|
|
1195
1246
|
kind: "approve",
|
|
1196
1247
|
description: "Approve base token for mintValue",
|
|
1197
1248
|
tx: approveTx
|
|
@@ -1437,8 +1488,17 @@ function createTokensResource(client) {
|
|
|
1437
1488
|
const chainId = BigInt(await client.l2.getChainId());
|
|
1438
1489
|
return await client.baseToken(chainId);
|
|
1439
1490
|
}
|
|
1440
|
-
const {
|
|
1441
|
-
const
|
|
1491
|
+
const { l2NativeTokenVault, l1NativeTokenVault } = await client.contracts();
|
|
1492
|
+
const assetId = await l2NativeTokenVault.read.assetId([l2Token]);
|
|
1493
|
+
if (BigInt(assetId) === 0n) {
|
|
1494
|
+
throw new Error(`Token ${l2Token} is not registered in the L2 NativeTokenVault.`);
|
|
1495
|
+
}
|
|
1496
|
+
const l1Token = await l1NativeTokenVault.read.tokenAddress([assetId]);
|
|
1497
|
+
if (BigInt(l1Token) === 0n) {
|
|
1498
|
+
throw new Error(
|
|
1499
|
+
`Token ${l2Token} has no L1 counterpart registered in the L1 NativeTokenVault.`
|
|
1500
|
+
);
|
|
1501
|
+
}
|
|
1442
1502
|
return l1Token;
|
|
1443
1503
|
});
|
|
1444
1504
|
}
|
|
@@ -1950,7 +2010,7 @@ function createDepositsResource(client, tokens, contracts) {
|
|
|
1950
2010
|
}
|
|
1951
2011
|
|
|
1952
2012
|
// src/adapters/viem/resources/withdrawals/context.ts
|
|
1953
|
-
async function commonCtx2(p, client, tokens, contracts) {
|
|
2013
|
+
async function commonCtx2(p, client, tokens, contracts, protocol) {
|
|
1954
2014
|
const sender = client.account.address;
|
|
1955
2015
|
const {
|
|
1956
2016
|
bridgehub,
|
|
@@ -1958,9 +2018,11 @@ async function commonCtx2(p, client, tokens, contracts) {
|
|
|
1958
2018
|
l1Nullifier,
|
|
1959
2019
|
l2AssetRouter,
|
|
1960
2020
|
l2NativeTokenVault,
|
|
1961
|
-
l2BaseTokenSystem
|
|
2021
|
+
l2BaseTokenSystem,
|
|
2022
|
+
interopCenter
|
|
1962
2023
|
} = await contracts.addresses();
|
|
1963
2024
|
const chainIdL2 = BigInt(await client.l2.getChainId());
|
|
2025
|
+
const l1ChainId = BigInt(await client.l1.getChainId());
|
|
1964
2026
|
const resolvedToken = await tokens.resolve(p.token, { chain: "l2" });
|
|
1965
2027
|
const baseTokenAssetId = resolvedToken.baseTokenAssetId;
|
|
1966
2028
|
const baseTokenL1 = await tokens.l1TokenFromAssetId(baseTokenAssetId);
|
|
@@ -1982,6 +2044,9 @@ async function commonCtx2(p, client, tokens, contracts) {
|
|
|
1982
2044
|
l2AssetRouter,
|
|
1983
2045
|
l2NativeTokenVault,
|
|
1984
2046
|
l2BaseTokenSystem,
|
|
2047
|
+
interopCenter,
|
|
2048
|
+
protocol,
|
|
2049
|
+
l1ChainId,
|
|
1985
2050
|
baseIsEth,
|
|
1986
2051
|
gasOverrides: p.l2TxOverrides ? toGasOverrides(p.l2TxOverrides) : void 0
|
|
1987
2052
|
};
|
|
@@ -2230,7 +2295,261 @@ function routeErc20NonBase2() {
|
|
|
2230
2295
|
}
|
|
2231
2296
|
};
|
|
2232
2297
|
}
|
|
2298
|
+
var PREFIX_EVM_CHAIN = toBytes("0x00010000");
|
|
2299
|
+
var PREFIX_EVM_ADDRESS = toBytes("0x000100000014");
|
|
2300
|
+
function formatInteropEvmChain(chainId) {
|
|
2301
|
+
const chainRef = toBytes(toHex(chainId));
|
|
2302
|
+
const chainRefLength = toBytes(toHex(chainRef.length, { size: 1 }));
|
|
2303
|
+
const payload = concat([PREFIX_EVM_CHAIN, chainRefLength, chainRef, new Uint8Array([0])]);
|
|
2304
|
+
return toHex(payload);
|
|
2305
|
+
}
|
|
2306
|
+
function formatInteropEvmAddress(address) {
|
|
2307
|
+
const normalized = getAddress(address);
|
|
2308
|
+
const addrBytes = toBytes(normalized);
|
|
2309
|
+
const payload = concat([PREFIX_EVM_ADDRESS, addrBytes]);
|
|
2310
|
+
return toHex(payload);
|
|
2311
|
+
}
|
|
2312
|
+
var interopCodec = {
|
|
2313
|
+
formatChain: formatInteropEvmChain,
|
|
2314
|
+
formatAddress: formatInteropEvmAddress
|
|
2315
|
+
};
|
|
2316
|
+
function getInteropAttributes(params, ctx) {
|
|
2317
|
+
const bundleAttributes = [];
|
|
2318
|
+
if (params.execution?.only) {
|
|
2319
|
+
bundleAttributes.push(ctx.attributes.bundle.executionAddress(params.execution.only));
|
|
2320
|
+
}
|
|
2321
|
+
if (params.unbundling?.by) {
|
|
2322
|
+
bundleAttributes.push(ctx.attributes.bundle.unbundlerAddress(params.unbundling.by));
|
|
2323
|
+
}
|
|
2324
|
+
bundleAttributes.push(ctx.attributes.bundle.useFixedFee(params.fee?.useFixed ?? false));
|
|
2325
|
+
const callAttributes = params.actions.map((action) => {
|
|
2326
|
+
switch (action.type) {
|
|
2327
|
+
case "sendNative": {
|
|
2328
|
+
const baseMatches = ctx.baseTokens.src.toLowerCase() === ctx.baseTokens.dst.toLowerCase();
|
|
2329
|
+
if (baseMatches) {
|
|
2330
|
+
return [ctx.attributes.call.interopCallValue(action.amount)];
|
|
2331
|
+
}
|
|
2332
|
+
return [ctx.attributes.call.indirectCall(action.amount)];
|
|
2333
|
+
}
|
|
2334
|
+
case "call":
|
|
2335
|
+
if (action.value && action.value > 0n) {
|
|
2336
|
+
return [ctx.attributes.call.interopCallValue(action.value)];
|
|
2337
|
+
}
|
|
2338
|
+
return [];
|
|
2339
|
+
case "sendErc20":
|
|
2340
|
+
return [ctx.attributes.call.indirectCall(0n)];
|
|
2341
|
+
default:
|
|
2342
|
+
assertNever(action);
|
|
2343
|
+
}
|
|
2344
|
+
});
|
|
2345
|
+
return { bundleAttributes, callAttributes };
|
|
2346
|
+
}
|
|
2347
|
+
function createViemAttributesResource() {
|
|
2348
|
+
const encode = (fn, args) => encodeFunctionData({
|
|
2349
|
+
abi: IERC7786Attributes_default,
|
|
2350
|
+
functionName: fn,
|
|
2351
|
+
args
|
|
2352
|
+
});
|
|
2353
|
+
return createAttributesResource({ encode });
|
|
2354
|
+
}
|
|
2355
|
+
|
|
2356
|
+
// src/adapters/viem/resources/withdrawals/routes/interop-bundle.ts
|
|
2233
2357
|
var { wrapAs: wrapAs9 } = createErrorHandlers("withdrawals");
|
|
2358
|
+
var attributes = createViemAttributesResource();
|
|
2359
|
+
function saltFor(sender, nonce) {
|
|
2360
|
+
return keccak256(
|
|
2361
|
+
encodeAbiParameters(
|
|
2362
|
+
[
|
|
2363
|
+
{ type: "address", name: "sender" },
|
|
2364
|
+
{ type: "uint256", name: "nonce" }
|
|
2365
|
+
],
|
|
2366
|
+
[sender, BigInt(nonce)]
|
|
2367
|
+
)
|
|
2368
|
+
);
|
|
2369
|
+
}
|
|
2370
|
+
function routeInteropBundle() {
|
|
2371
|
+
return {
|
|
2372
|
+
async build(p, ctx) {
|
|
2373
|
+
const steps = [];
|
|
2374
|
+
const approvals = [];
|
|
2375
|
+
const isBaseToken = ctx.route === "base";
|
|
2376
|
+
const l1Receiver = p.to ?? ctx.sender;
|
|
2377
|
+
let assetId;
|
|
2378
|
+
let allowanceSufficient = true;
|
|
2379
|
+
if (isBaseToken) {
|
|
2380
|
+
assetId = ctx.baseTokenAssetId;
|
|
2381
|
+
} else {
|
|
2382
|
+
assetId = (await wrapAs9(
|
|
2383
|
+
"CONTRACT",
|
|
2384
|
+
OP_WITHDRAWALS.erc20.ensureRegistered,
|
|
2385
|
+
() => ctx.client.l2.simulateContract({
|
|
2386
|
+
address: ctx.l2NativeTokenVault,
|
|
2387
|
+
abi: L2NativeTokenVault_default,
|
|
2388
|
+
functionName: "ensureTokenIsRegistered",
|
|
2389
|
+
args: [p.token],
|
|
2390
|
+
account: ctx.client.account
|
|
2391
|
+
}),
|
|
2392
|
+
{
|
|
2393
|
+
ctx: { where: "L2NativeTokenVault.ensureTokenIsRegistered", token: p.token },
|
|
2394
|
+
message: "Failed to ensure token is registered in L2NativeTokenVault."
|
|
2395
|
+
}
|
|
2396
|
+
)).result;
|
|
2397
|
+
const current = await wrapAs9(
|
|
2398
|
+
"CONTRACT",
|
|
2399
|
+
OP_WITHDRAWALS.erc20.allowance,
|
|
2400
|
+
() => ctx.client.l2.readContract({
|
|
2401
|
+
address: p.token,
|
|
2402
|
+
abi: IERC20_default,
|
|
2403
|
+
functionName: "allowance",
|
|
2404
|
+
args: [ctx.sender, ctx.l2NativeTokenVault],
|
|
2405
|
+
account: ctx.client.account
|
|
2406
|
+
}),
|
|
2407
|
+
{
|
|
2408
|
+
ctx: {
|
|
2409
|
+
where: "erc20.allowance",
|
|
2410
|
+
chain: "L2",
|
|
2411
|
+
token: p.token,
|
|
2412
|
+
spender: ctx.l2NativeTokenVault
|
|
2413
|
+
},
|
|
2414
|
+
message: "Failed to read L2 ERC-20 allowance."
|
|
2415
|
+
}
|
|
2416
|
+
);
|
|
2417
|
+
allowanceSufficient = current >= p.amount;
|
|
2418
|
+
if (!allowanceSufficient) {
|
|
2419
|
+
approvals.push({ token: p.token, spender: ctx.l2NativeTokenVault, amount: p.amount });
|
|
2420
|
+
const approveTxCandidate = {
|
|
2421
|
+
to: p.token,
|
|
2422
|
+
data: encodeFunctionData({
|
|
2423
|
+
abi: IERC20_default,
|
|
2424
|
+
functionName: "approve",
|
|
2425
|
+
args: [ctx.l2NativeTokenVault, p.amount]
|
|
2426
|
+
}),
|
|
2427
|
+
value: 0n,
|
|
2428
|
+
from: ctx.sender
|
|
2429
|
+
};
|
|
2430
|
+
const approveGas = await quoteL2Gas4({ ctx, tx: approveTxCandidate });
|
|
2431
|
+
steps.push({
|
|
2432
|
+
key: `approve:l2:${p.token}:${ctx.l2NativeTokenVault}`,
|
|
2433
|
+
kind: "approve:l2",
|
|
2434
|
+
description: `Approve ${p.amount} to NativeTokenVault`,
|
|
2435
|
+
tx: {
|
|
2436
|
+
address: p.token,
|
|
2437
|
+
abi: IERC20_default,
|
|
2438
|
+
functionName: "approve",
|
|
2439
|
+
args: [ctx.l2NativeTokenVault, p.amount],
|
|
2440
|
+
account: ctx.client.account,
|
|
2441
|
+
...approveGas
|
|
2442
|
+
}
|
|
2443
|
+
});
|
|
2444
|
+
}
|
|
2445
|
+
}
|
|
2446
|
+
const nonce = await wrapAs9(
|
|
2447
|
+
"RPC",
|
|
2448
|
+
OP_WITHDRAWALS.prepare,
|
|
2449
|
+
() => ctx.client.l2.getTransactionCount({ address: ctx.sender, blockTag: "pending" }),
|
|
2450
|
+
{
|
|
2451
|
+
ctx: { where: "l2.getTransactionCount", sender: ctx.sender },
|
|
2452
|
+
message: "Failed to read L2 nonce for the interop bundle salt."
|
|
2453
|
+
}
|
|
2454
|
+
);
|
|
2455
|
+
const bundle = buildWithdrawalBundle({
|
|
2456
|
+
assetId,
|
|
2457
|
+
amount: p.amount,
|
|
2458
|
+
l1Receiver,
|
|
2459
|
+
l2Token: isBaseToken ? FORMAL_ETH_ADDRESS : p.token,
|
|
2460
|
+
isBaseToken,
|
|
2461
|
+
l1ChainId: ctx.l1ChainId,
|
|
2462
|
+
l2AssetRouter: ctx.l2AssetRouter,
|
|
2463
|
+
salt: saltFor(ctx.sender, nonce),
|
|
2464
|
+
codec: interopCodec,
|
|
2465
|
+
attributes: {
|
|
2466
|
+
indirectCall: attributes.call.indirectCall,
|
|
2467
|
+
interopCallValue: attributes.call.interopCallValue,
|
|
2468
|
+
interopBundleSalt: attributes.bundle.interopBundleSalt
|
|
2469
|
+
},
|
|
2470
|
+
transferData: {
|
|
2471
|
+
encodeBridgeBurnData: encodeNativeTokenVaultTransferData,
|
|
2472
|
+
encodeAssetRouterDepositData: encodeSecondBridgeDataV1
|
|
2473
|
+
}
|
|
2474
|
+
});
|
|
2475
|
+
const sendArgs = [bundle.destinationChain, bundle.starters, bundle.bundleAttributes];
|
|
2476
|
+
const sendTxCandidate = {
|
|
2477
|
+
to: ctx.interopCenter,
|
|
2478
|
+
data: encodeFunctionData({
|
|
2479
|
+
abi: IInteropCenter_default,
|
|
2480
|
+
functionName: "sendBundle",
|
|
2481
|
+
args: sendArgs
|
|
2482
|
+
}),
|
|
2483
|
+
value: bundle.value,
|
|
2484
|
+
from: ctx.sender
|
|
2485
|
+
};
|
|
2486
|
+
const sendGas = allowanceSufficient ? await quoteL2Gas4({ ctx, tx: sendTxCandidate }) : void 0;
|
|
2487
|
+
const sendTx = {
|
|
2488
|
+
address: ctx.interopCenter,
|
|
2489
|
+
abi: IInteropCenter_default,
|
|
2490
|
+
functionName: "sendBundle",
|
|
2491
|
+
args: sendArgs,
|
|
2492
|
+
account: ctx.client.account,
|
|
2493
|
+
value: bundle.value,
|
|
2494
|
+
...sendGas
|
|
2495
|
+
};
|
|
2496
|
+
steps.push({
|
|
2497
|
+
key: "interop-center:send-bundle",
|
|
2498
|
+
kind: "interop-center:send-bundle",
|
|
2499
|
+
description: "Burn on L2 & send L2\u2192L1 withdrawal bundle",
|
|
2500
|
+
tx: sendTx
|
|
2501
|
+
});
|
|
2502
|
+
const fees = buildFeeBreakdown2({
|
|
2503
|
+
feeToken: ctx.baseTokenL1 ?? await ctx.client.baseToken(ctx.chainIdL2),
|
|
2504
|
+
l2Gas: sendGas
|
|
2505
|
+
});
|
|
2506
|
+
return { steps, approvals, fees };
|
|
2507
|
+
}
|
|
2508
|
+
};
|
|
2509
|
+
}
|
|
2510
|
+
|
|
2511
|
+
// src/adapters/viem/resources/withdrawals/services/protocol.ts
|
|
2512
|
+
function createWithdrawalProtocolService(client, override) {
|
|
2513
|
+
let cached;
|
|
2514
|
+
async function hasCodeAt(address) {
|
|
2515
|
+
try {
|
|
2516
|
+
const code = await client.l2.getCode({ address });
|
|
2517
|
+
return !!code && code !== "0x";
|
|
2518
|
+
} catch {
|
|
2519
|
+
return false;
|
|
2520
|
+
}
|
|
2521
|
+
}
|
|
2522
|
+
async function protocolVersion() {
|
|
2523
|
+
try {
|
|
2524
|
+
return await client.getChainProtocolVersion();
|
|
2525
|
+
} catch {
|
|
2526
|
+
return void 0;
|
|
2527
|
+
}
|
|
2528
|
+
}
|
|
2529
|
+
function detect() {
|
|
2530
|
+
cached ??= detectWithdrawalProtocol(
|
|
2531
|
+
{
|
|
2532
|
+
protocolVersion,
|
|
2533
|
+
hasCodeAt
|
|
2534
|
+
},
|
|
2535
|
+
override
|
|
2536
|
+
).catch((e) => {
|
|
2537
|
+
cached = void 0;
|
|
2538
|
+
throw e;
|
|
2539
|
+
});
|
|
2540
|
+
return cached;
|
|
2541
|
+
}
|
|
2542
|
+
return {
|
|
2543
|
+
detect,
|
|
2544
|
+
async protocol() {
|
|
2545
|
+
return (await detect()).protocol;
|
|
2546
|
+
},
|
|
2547
|
+
refresh() {
|
|
2548
|
+
cached = void 0;
|
|
2549
|
+
}
|
|
2550
|
+
};
|
|
2551
|
+
}
|
|
2552
|
+
var { wrapAs: wrapAs10 } = createErrorHandlers("withdrawals");
|
|
2234
2553
|
var IL1NullifierMini = [
|
|
2235
2554
|
{
|
|
2236
2555
|
type: "function",
|
|
@@ -2244,183 +2563,279 @@ var IL1NullifierMini = [
|
|
|
2244
2563
|
outputs: [{ type: "bool" }]
|
|
2245
2564
|
}
|
|
2246
2565
|
];
|
|
2247
|
-
function createFinalizationServices(client) {
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
|
|
2255
|
-
|
|
2256
|
-
|
|
2566
|
+
function createFinalizationServices(client, protocolService = createWithdrawalProtocolService(client)) {
|
|
2567
|
+
async function finalizationTarget(protocol) {
|
|
2568
|
+
const { l1Nullifier } = await client.ensureAddresses();
|
|
2569
|
+
if (protocol === "legacy-withdrawal") return l1Nullifier;
|
|
2570
|
+
return await wrapAs10(
|
|
2571
|
+
"CONTRACT",
|
|
2572
|
+
OP_WITHDRAWALS.finalize.fetchParams.receipt,
|
|
2573
|
+
() => client.l1.readContract({
|
|
2574
|
+
address: l1Nullifier,
|
|
2575
|
+
abi: IL1NullifierV32_default,
|
|
2576
|
+
functionName: "l1InteropHandler"
|
|
2577
|
+
}),
|
|
2578
|
+
{
|
|
2579
|
+
ctx: { where: "L1Nullifier.l1InteropHandler", l1Nullifier },
|
|
2580
|
+
message: "Failed to resolve the L1 interop handler. The chain reports protocol v32+, but its L1Nullifier does not expose `l1InteropHandler()`."
|
|
2581
|
+
}
|
|
2582
|
+
);
|
|
2583
|
+
}
|
|
2584
|
+
async function fetchMessageAndProof(l2TxHash) {
|
|
2585
|
+
const raw = await wrapAs10(
|
|
2586
|
+
"RPC",
|
|
2587
|
+
OP_WITHDRAWALS.finalize.fetchParams.receipt,
|
|
2588
|
+
() => client.zks.getReceiptWithL2ToL1(l2TxHash),
|
|
2589
|
+
{
|
|
2590
|
+
ctx: { where: "getReceiptWithL2ToL1", l2TxHash },
|
|
2591
|
+
message: "Failed to fetch L2 receipt (with L2\u2192L1 logs)."
|
|
2592
|
+
}
|
|
2593
|
+
);
|
|
2594
|
+
if (!raw) {
|
|
2595
|
+
throw createError("STATE", {
|
|
2596
|
+
resource: "withdrawals",
|
|
2597
|
+
operation: OP_WITHDRAWALS.finalize.fetchParams.receipt,
|
|
2598
|
+
message: "L2 receipt not found.",
|
|
2599
|
+
context: { l2TxHash }
|
|
2600
|
+
});
|
|
2601
|
+
}
|
|
2602
|
+
const ev = await wrapAs10(
|
|
2603
|
+
"INTERNAL",
|
|
2604
|
+
OP_WITHDRAWALS.finalize.fetchParams.findMessage,
|
|
2605
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
|
|
2606
|
+
() => Promise.resolve(findL1MessageSentLog(raw, { index: 0 })),
|
|
2607
|
+
{
|
|
2608
|
+
ctx: { l2TxHash, index: 0 },
|
|
2609
|
+
message: "Failed to locate L1MessageSent event in L2 receipt."
|
|
2610
|
+
}
|
|
2611
|
+
);
|
|
2612
|
+
const message = await wrapAs10(
|
|
2613
|
+
"INTERNAL",
|
|
2614
|
+
OP_WITHDRAWALS.finalize.fetchParams.decodeMessage,
|
|
2615
|
+
() => {
|
|
2616
|
+
const [decoded] = decodeAbiParameters([{ type: "bytes" }], ev.data);
|
|
2617
|
+
return Promise.resolve(decoded);
|
|
2618
|
+
},
|
|
2619
|
+
{
|
|
2620
|
+
ctx: { where: "decode L1MessageSent", data: ev.data },
|
|
2621
|
+
message: "Failed to decode withdrawal message."
|
|
2622
|
+
}
|
|
2623
|
+
);
|
|
2624
|
+
const idx = await wrapAs10(
|
|
2625
|
+
"INTERNAL",
|
|
2626
|
+
OP_WITHDRAWALS.finalize.fetchParams.messengerIndex,
|
|
2627
|
+
() => Promise.resolve(messengerLogIndex(raw, { index: 0, messenger: L1_MESSENGER_ADDRESS })),
|
|
2628
|
+
{
|
|
2629
|
+
ctx: { where: "derive messenger log index", l2TxHash, receipt: raw },
|
|
2630
|
+
message: "Failed to derive messenger log index."
|
|
2631
|
+
}
|
|
2632
|
+
);
|
|
2633
|
+
const proof = await wrapAs10(
|
|
2634
|
+
"RPC",
|
|
2635
|
+
OP_WITHDRAWALS.finalize.fetchParams.proof,
|
|
2636
|
+
() => client.zks.getL2ToL1LogProof(l2TxHash, idx),
|
|
2637
|
+
{
|
|
2638
|
+
ctx: { where: "get L2\u2192L1 log proof", l2TxHash, messengerLogIndex: idx },
|
|
2639
|
+
message: "Failed to fetch L2\u2192L1 log proof."
|
|
2640
|
+
}
|
|
2641
|
+
);
|
|
2642
|
+
const chainId = await wrapAs10(
|
|
2643
|
+
"RPC",
|
|
2644
|
+
OP_WITHDRAWALS.finalize.fetchParams.network,
|
|
2645
|
+
() => client.l2.getChainId(),
|
|
2646
|
+
{ ctx: { where: "l2.getChainId" }, message: "Failed to read L2 chain id." }
|
|
2647
|
+
);
|
|
2648
|
+
const txNumberInBatch = Number(raw.transactionIndex ?? 0);
|
|
2649
|
+
const l2Sender = l1MessageSentSender(ev);
|
|
2650
|
+
return { raw, message, proof, chainId: BigInt(chainId), txNumberInBatch, l2Sender };
|
|
2651
|
+
}
|
|
2652
|
+
async function fetchFinalization(l2TxHash) {
|
|
2653
|
+
const protocol = await protocolService.protocol();
|
|
2654
|
+
const { raw, message, proof, chainId, txNumberInBatch, l2Sender } = await fetchMessageAndProof(l2TxHash);
|
|
2655
|
+
const target = await finalizationTarget(protocol);
|
|
2656
|
+
if (protocol === "legacy-withdrawal") {
|
|
2657
|
+
const params2 = {
|
|
2658
|
+
chainId,
|
|
2659
|
+
l2BatchNumber: proof.batchNumber,
|
|
2660
|
+
l2MessageIndex: proof.id,
|
|
2661
|
+
l2Sender,
|
|
2662
|
+
l2TxNumberInBatch: txNumberInBatch,
|
|
2663
|
+
message,
|
|
2664
|
+
merkleProof: proof.proof
|
|
2665
|
+
};
|
|
2666
|
+
return {
|
|
2667
|
+
target,
|
|
2668
|
+
finalization: { protocol, params: params2 },
|
|
2669
|
+
key: {
|
|
2670
|
+
chainIdL2: chainId,
|
|
2671
|
+
l2BatchNumber: proof.batchNumber,
|
|
2672
|
+
l2MessageIndex: proof.id
|
|
2257
2673
|
}
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2674
|
+
};
|
|
2675
|
+
}
|
|
2676
|
+
const { interopCenter } = await client.ensureAddresses();
|
|
2677
|
+
const params = buildWithdrawalFinalization({
|
|
2678
|
+
messageData: message,
|
|
2679
|
+
sourceChainId: chainId,
|
|
2680
|
+
txNumberInBatch,
|
|
2681
|
+
proof: { batchNumber: proof.batchNumber, id: proof.id, proof: proof.proof },
|
|
2682
|
+
// Prefer the hash the InteropCenter emitted over recomputing it.
|
|
2683
|
+
bundleHash: parseBundleHashFromLogs(raw.logs ?? [], interopCenter),
|
|
2684
|
+
interopCenter
|
|
2685
|
+
});
|
|
2686
|
+
return {
|
|
2687
|
+
target,
|
|
2688
|
+
finalization: { protocol, params },
|
|
2689
|
+
key: {
|
|
2690
|
+
chainIdL2: chainId,
|
|
2691
|
+
l2BatchNumber: proof.batchNumber,
|
|
2692
|
+
l2MessageIndex: proof.id,
|
|
2693
|
+
bundleHash: params.bundleHash
|
|
2266
2694
|
}
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2695
|
+
};
|
|
2696
|
+
}
|
|
2697
|
+
async function withdrawalOutcome(finalization) {
|
|
2698
|
+
const target = await finalizationTarget(finalization.protocol);
|
|
2699
|
+
if (finalization.protocol === "legacy-withdrawal") {
|
|
2700
|
+
const done = await wrapAs10(
|
|
2701
|
+
"RPC",
|
|
2702
|
+
OP_WITHDRAWALS.finalize.isFinalized,
|
|
2703
|
+
() => client.l1.readContract({
|
|
2704
|
+
address: target,
|
|
2705
|
+
abi: IL1NullifierMini,
|
|
2706
|
+
functionName: "isWithdrawalFinalized",
|
|
2707
|
+
args: [
|
|
2708
|
+
finalization.params.chainId,
|
|
2709
|
+
finalization.params.l2BatchNumber,
|
|
2710
|
+
finalization.params.l2MessageIndex
|
|
2711
|
+
]
|
|
2712
|
+
}),
|
|
2272
2713
|
{
|
|
2273
|
-
ctx: {
|
|
2274
|
-
message: "Failed to
|
|
2714
|
+
ctx: { where: "isWithdrawalFinalized", params: finalization.params },
|
|
2715
|
+
message: "Failed to read finalization status."
|
|
2275
2716
|
}
|
|
2276
2717
|
);
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2718
|
+
return done ? "finalized" : "pending";
|
|
2719
|
+
}
|
|
2720
|
+
const { bundleHash } = finalization.params;
|
|
2721
|
+
const status = Number(
|
|
2722
|
+
await wrapAs10(
|
|
2723
|
+
"RPC",
|
|
2724
|
+
OP_WITHDRAWALS.finalize.isFinalized,
|
|
2725
|
+
() => client.l1.readContract({
|
|
2726
|
+
address: target,
|
|
2727
|
+
abi: IInteropHandler_default,
|
|
2728
|
+
functionName: "bundleStatus",
|
|
2729
|
+
args: [bundleHash]
|
|
2730
|
+
}),
|
|
2284
2731
|
{
|
|
2285
|
-
ctx: { where: "
|
|
2286
|
-
message: "Failed to
|
|
2732
|
+
ctx: { where: "L1InteropHandler.bundleStatus", bundleHash },
|
|
2733
|
+
message: "Failed to read bundle status."
|
|
2287
2734
|
}
|
|
2288
|
-
)
|
|
2289
|
-
|
|
2735
|
+
)
|
|
2736
|
+
);
|
|
2737
|
+
if (status !== 3 /* Unbundled */) return classifyWithdrawalOutcome(status);
|
|
2738
|
+
const callStatus = Number(
|
|
2739
|
+
await wrapAs10(
|
|
2290
2740
|
"RPC",
|
|
2291
|
-
OP_WITHDRAWALS.finalize.
|
|
2292
|
-
() => client.
|
|
2741
|
+
OP_WITHDRAWALS.finalize.isFinalized,
|
|
2742
|
+
() => client.l1.readContract({
|
|
2743
|
+
address: target,
|
|
2744
|
+
abi: IInteropHandler_default,
|
|
2745
|
+
functionName: "callStatus",
|
|
2746
|
+
args: [bundleHash, 0n]
|
|
2747
|
+
}),
|
|
2293
2748
|
{
|
|
2294
|
-
ctx: { where: "
|
|
2295
|
-
message: "Failed to
|
|
2749
|
+
ctx: { where: "L1InteropHandler.callStatus", bundleHash },
|
|
2750
|
+
message: "Failed to read bundle call status."
|
|
2296
2751
|
}
|
|
2297
|
-
)
|
|
2298
|
-
|
|
2299
|
-
|
|
2752
|
+
)
|
|
2753
|
+
);
|
|
2754
|
+
return classifyWithdrawalOutcome(status, callStatus);
|
|
2755
|
+
}
|
|
2756
|
+
async function isWithdrawalFinalized(finalization) {
|
|
2757
|
+
return await withdrawalOutcome(finalization) === "finalized";
|
|
2758
|
+
}
|
|
2759
|
+
return {
|
|
2760
|
+
fetchFinalization,
|
|
2761
|
+
isWithdrawalFinalized,
|
|
2762
|
+
withdrawalOutcome,
|
|
2763
|
+
async fetchFinalizeDepositParams(l2TxHash) {
|
|
2764
|
+
const resolved = await fetchFinalization(l2TxHash);
|
|
2765
|
+
if (resolved.finalization.protocol !== "legacy-withdrawal") {
|
|
2766
|
+
throw createError("VALIDATION", {
|
|
2300
2767
|
resource: "withdrawals",
|
|
2301
|
-
operation: OP_WITHDRAWALS.finalize.fetchParams.
|
|
2302
|
-
message: "
|
|
2303
|
-
context: { l2TxHash }
|
|
2768
|
+
operation: OP_WITHDRAWALS.finalize.fetchParams.receipt,
|
|
2769
|
+
message: "This chain finalizes withdrawals through the L1 interop handler; `finalizeDeposit` params do not exist. Use `fetchFinalization` instead.",
|
|
2770
|
+
context: { l2TxHash, protocol: resolved.finalization.protocol }
|
|
2304
2771
|
});
|
|
2305
2772
|
}
|
|
2306
|
-
|
|
2307
|
-
"INTERNAL",
|
|
2308
|
-
OP_WITHDRAWALS.finalize.fetchParams.messengerIndex,
|
|
2309
|
-
() => Promise.resolve(messengerLogIndex(raw, { index: 0, messenger: L1_MESSENGER_ADDRESS })),
|
|
2310
|
-
{
|
|
2311
|
-
ctx: { where: "derive messenger log index", l2TxHash, receipt: raw },
|
|
2312
|
-
message: "Failed to derive messenger log index."
|
|
2313
|
-
}
|
|
2314
|
-
);
|
|
2315
|
-
const proof = await wrapAs9(
|
|
2316
|
-
"RPC",
|
|
2317
|
-
OP_WITHDRAWALS.finalize.fetchParams.proof,
|
|
2318
|
-
() => client.zks.getL2ToL1LogProof(l2TxHash, idx),
|
|
2319
|
-
{
|
|
2320
|
-
ctx: { where: "get L2\u2192L1 log proof", l2TxHash, messengerLogIndex: idx },
|
|
2321
|
-
message: "Failed to fetch L2\u2192L1 log proof."
|
|
2322
|
-
}
|
|
2323
|
-
);
|
|
2324
|
-
const chainId = await wrapAs9(
|
|
2325
|
-
"RPC",
|
|
2326
|
-
OP_WITHDRAWALS.finalize.fetchParams.network,
|
|
2327
|
-
() => client.l2.getChainId(),
|
|
2328
|
-
{ ctx: { where: "l2.getChainId" }, message: "Failed to read L2 chain id." }
|
|
2329
|
-
);
|
|
2330
|
-
const txIndex = Number(parsed.transactionIndex ?? 0);
|
|
2331
|
-
const params = {
|
|
2332
|
-
chainId: BigInt(chainId),
|
|
2333
|
-
l2BatchNumber: proof.batchNumber,
|
|
2334
|
-
l2MessageIndex: proof.id,
|
|
2335
|
-
l2Sender: l1MessageSentSender(ev),
|
|
2336
|
-
l2TxNumberInBatch: txIndex,
|
|
2337
|
-
message,
|
|
2338
|
-
merkleProof: proof.proof
|
|
2339
|
-
};
|
|
2340
|
-
const { l1Nullifier } = await client.ensureAddresses();
|
|
2341
|
-
return { params, nullifier: l1Nullifier };
|
|
2773
|
+
return { params: resolved.finalization.params, nullifier: resolved.target };
|
|
2342
2774
|
},
|
|
2343
|
-
async simulateFinalizeReadiness(
|
|
2344
|
-
const
|
|
2775
|
+
async simulateFinalizeReadiness(finalization) {
|
|
2776
|
+
const target = await finalizationTarget(finalization.protocol);
|
|
2345
2777
|
const done = await (async () => {
|
|
2346
2778
|
try {
|
|
2347
|
-
|
|
2348
|
-
"RPC",
|
|
2349
|
-
OP_WITHDRAWALS.finalize.readiness.isFinalized,
|
|
2350
|
-
() => client.l1.readContract({
|
|
2351
|
-
address: l1Nullifier,
|
|
2352
|
-
abi: IL1NullifierMini,
|
|
2353
|
-
functionName: "isWithdrawalFinalized",
|
|
2354
|
-
args: [params.chainId, params.l2BatchNumber, params.l2MessageIndex]
|
|
2355
|
-
}),
|
|
2356
|
-
{
|
|
2357
|
-
ctx: { where: "isWithdrawalFinalized", params },
|
|
2358
|
-
message: "Failed to read finalization status."
|
|
2359
|
-
}
|
|
2360
|
-
);
|
|
2361
|
-
return result;
|
|
2779
|
+
return await isWithdrawalFinalized(finalization);
|
|
2362
2780
|
} catch {
|
|
2363
2781
|
return false;
|
|
2364
2782
|
}
|
|
2365
2783
|
})();
|
|
2366
2784
|
if (done) return { kind: "FINALIZED" };
|
|
2367
2785
|
try {
|
|
2368
|
-
|
|
2369
|
-
|
|
2370
|
-
|
|
2371
|
-
|
|
2372
|
-
|
|
2373
|
-
|
|
2374
|
-
|
|
2786
|
+
if (finalization.protocol === "legacy-withdrawal") {
|
|
2787
|
+
await client.l1.simulateContract({
|
|
2788
|
+
address: target,
|
|
2789
|
+
abi: IL1Nullifier_default,
|
|
2790
|
+
functionName: "finalizeDeposit",
|
|
2791
|
+
args: [finalization.params],
|
|
2792
|
+
account: client.account
|
|
2793
|
+
});
|
|
2794
|
+
} else {
|
|
2795
|
+
await client.l1.simulateContract({
|
|
2796
|
+
address: target,
|
|
2797
|
+
abi: IInteropHandler_default,
|
|
2798
|
+
functionName: "executeBundle",
|
|
2799
|
+
args: [finalization.params.bundle, finalization.params.proof],
|
|
2800
|
+
account: client.account
|
|
2801
|
+
});
|
|
2802
|
+
}
|
|
2375
2803
|
return { kind: "READY" };
|
|
2376
2804
|
} catch (e) {
|
|
2377
2805
|
return classifyReadinessFromRevert(e);
|
|
2378
2806
|
}
|
|
2379
2807
|
},
|
|
2380
|
-
async
|
|
2381
|
-
const
|
|
2382
|
-
|
|
2383
|
-
"RPC",
|
|
2384
|
-
OP_WITHDRAWALS.finalize.isFinalized,
|
|
2385
|
-
() => client.l1.readContract({
|
|
2386
|
-
address: l1Nullifier,
|
|
2387
|
-
abi: IL1NullifierMini,
|
|
2388
|
-
functionName: "isWithdrawalFinalized",
|
|
2389
|
-
args: [key.chainIdL2, key.l2BatchNumber, key.l2MessageIndex]
|
|
2390
|
-
}),
|
|
2391
|
-
{
|
|
2392
|
-
ctx: { where: "isWithdrawalFinalized", key },
|
|
2393
|
-
message: "Failed to read finalization status."
|
|
2394
|
-
}
|
|
2395
|
-
);
|
|
2396
|
-
},
|
|
2397
|
-
async estimateFinalization(params) {
|
|
2398
|
-
const { l1Nullifier } = await client.ensureAddresses();
|
|
2399
|
-
const gasLimit = await wrapAs9(
|
|
2808
|
+
async estimateFinalization(finalization) {
|
|
2809
|
+
const target = await finalizationTarget(finalization.protocol);
|
|
2810
|
+
const gasLimit = await wrapAs10(
|
|
2400
2811
|
"RPC",
|
|
2401
2812
|
OP_WITHDRAWALS.finalize.estimate,
|
|
2402
|
-
() => client.l1.estimateContractGas({
|
|
2403
|
-
address:
|
|
2813
|
+
() => finalization.protocol === "legacy-withdrawal" ? client.l1.estimateContractGas({
|
|
2814
|
+
address: target,
|
|
2404
2815
|
abi: IL1Nullifier_default,
|
|
2405
2816
|
functionName: "finalizeDeposit",
|
|
2406
|
-
args: [params],
|
|
2817
|
+
args: [finalization.params],
|
|
2818
|
+
account: client.account
|
|
2819
|
+
}) : client.l1.estimateContractGas({
|
|
2820
|
+
address: target,
|
|
2821
|
+
abi: IInteropHandler_default,
|
|
2822
|
+
functionName: "executeBundle",
|
|
2823
|
+
args: [finalization.params.bundle, finalization.params.proof],
|
|
2407
2824
|
account: client.account
|
|
2408
2825
|
}),
|
|
2409
2826
|
{
|
|
2410
2827
|
ctx: {
|
|
2411
|
-
where: "estimateContractGas(
|
|
2412
|
-
|
|
2413
|
-
|
|
2414
|
-
l2MessageIndex: params.l2MessageIndex,
|
|
2415
|
-
l1Nullifier
|
|
2828
|
+
where: "estimateContractGas(finalize)",
|
|
2829
|
+
protocol: finalization.protocol,
|
|
2830
|
+
target
|
|
2416
2831
|
},
|
|
2417
|
-
message: "Failed to estimate gas for
|
|
2832
|
+
message: "Failed to estimate gas for withdrawal finalization."
|
|
2418
2833
|
}
|
|
2419
2834
|
);
|
|
2420
2835
|
let maxFeePerGas;
|
|
2421
2836
|
let maxPriorityFeePerGas;
|
|
2422
2837
|
try {
|
|
2423
|
-
const fee = await
|
|
2838
|
+
const fee = await wrapAs10(
|
|
2424
2839
|
"RPC",
|
|
2425
2840
|
OP_WITHDRAWALS.finalize.estimate,
|
|
2426
2841
|
() => client.l1.estimateFeesPerGas(),
|
|
@@ -2439,32 +2854,34 @@ function createFinalizationServices(client) {
|
|
|
2439
2854
|
})();
|
|
2440
2855
|
maxPriorityFeePerGas = fee.maxPriorityFeePerGas ?? 0n;
|
|
2441
2856
|
} catch {
|
|
2442
|
-
const gasPrice = await
|
|
2857
|
+
const gasPrice = await wrapAs10(
|
|
2443
2858
|
"RPC",
|
|
2444
2859
|
OP_WITHDRAWALS.finalize.estimate,
|
|
2445
2860
|
() => client.l1.getGasPrice(),
|
|
2446
2861
|
{
|
|
2447
2862
|
ctx: { where: "getGasPrice" },
|
|
2448
|
-
message: "Failed to read gas price for
|
|
2863
|
+
message: "Failed to read gas price for withdrawal finalization."
|
|
2449
2864
|
}
|
|
2450
2865
|
);
|
|
2451
2866
|
maxFeePerGas = gasPrice;
|
|
2452
2867
|
maxPriorityFeePerGas = 0n;
|
|
2453
2868
|
}
|
|
2454
|
-
return {
|
|
2455
|
-
gasLimit,
|
|
2456
|
-
maxFeePerGas,
|
|
2457
|
-
maxPriorityFeePerGas
|
|
2458
|
-
};
|
|
2869
|
+
return { gasLimit, maxFeePerGas, maxPriorityFeePerGas };
|
|
2459
2870
|
},
|
|
2460
|
-
async
|
|
2461
|
-
const
|
|
2871
|
+
async finalize(finalization) {
|
|
2872
|
+
const target = await finalizationTarget(finalization.protocol);
|
|
2462
2873
|
try {
|
|
2463
|
-
const hash = await client.l1Wallet.writeContract({
|
|
2464
|
-
address:
|
|
2874
|
+
const hash = finalization.protocol === "legacy-withdrawal" ? await client.l1Wallet.writeContract({
|
|
2875
|
+
address: target,
|
|
2465
2876
|
abi: IL1Nullifier_default,
|
|
2466
2877
|
functionName: "finalizeDeposit",
|
|
2467
|
-
args: [params],
|
|
2878
|
+
args: [finalization.params],
|
|
2879
|
+
account: client.account
|
|
2880
|
+
}) : await client.l1Wallet.writeContract({
|
|
2881
|
+
address: target,
|
|
2882
|
+
abi: IInteropHandler_default,
|
|
2883
|
+
functionName: "executeBundle",
|
|
2884
|
+
args: [finalization.params.bundle, finalization.params.proof],
|
|
2468
2885
|
account: client.account
|
|
2469
2886
|
});
|
|
2470
2887
|
return {
|
|
@@ -2478,8 +2895,8 @@ function createFinalizationServices(client) {
|
|
|
2478
2895
|
{
|
|
2479
2896
|
resource: "withdrawals",
|
|
2480
2897
|
operation: OP_WITHDRAWALS.finalize.wait,
|
|
2481
|
-
message: "Failed while waiting for
|
|
2482
|
-
context: { txHash: hash }
|
|
2898
|
+
message: "Failed while waiting for the withdrawal finalization transaction.",
|
|
2899
|
+
context: { txHash: hash, protocol: finalization.protocol }
|
|
2483
2900
|
},
|
|
2484
2901
|
e
|
|
2485
2902
|
);
|
|
@@ -2492,13 +2909,8 @@ function createFinalizationServices(client) {
|
|
|
2492
2909
|
{
|
|
2493
2910
|
resource: "withdrawals",
|
|
2494
2911
|
operation: OP_WITHDRAWALS.finalize.send,
|
|
2495
|
-
message: "Failed to send
|
|
2496
|
-
context: {
|
|
2497
|
-
chainIdL2: params.chainId,
|
|
2498
|
-
l2BatchNumber: params.l2BatchNumber,
|
|
2499
|
-
l2MessageIndex: params.l2MessageIndex,
|
|
2500
|
-
l1Nullifier
|
|
2501
|
-
}
|
|
2912
|
+
message: "Failed to send the withdrawal finalization transaction.",
|
|
2913
|
+
context: { protocol: finalization.protocol, target }
|
|
2502
2914
|
},
|
|
2503
2915
|
e
|
|
2504
2916
|
);
|
|
@@ -2514,15 +2926,28 @@ var ROUTES2 = {
|
|
|
2514
2926
|
"erc20-nonbase": routeErc20NonBase2()
|
|
2515
2927
|
// AssetRouter.withdraw for non-base ERC-20s
|
|
2516
2928
|
};
|
|
2517
|
-
|
|
2518
|
-
|
|
2929
|
+
var INTEROP_ROUTES = {
|
|
2930
|
+
base: routeInteropBundle(),
|
|
2931
|
+
"erc20-nonbase": routeInteropBundle()
|
|
2932
|
+
};
|
|
2933
|
+
function routeStrategy(protocol, route) {
|
|
2934
|
+
return protocol === "interop-bundle" ? INTEROP_ROUTES[route] : ROUTES2[route];
|
|
2935
|
+
}
|
|
2936
|
+
function createWithdrawalsResource(client, tokens, contracts, options) {
|
|
2937
|
+
const protocolService = createWithdrawalProtocolService(
|
|
2938
|
+
client,
|
|
2939
|
+
options?.protocol
|
|
2940
|
+
);
|
|
2941
|
+
const svc = createFinalizationServices(client, protocolService);
|
|
2519
2942
|
const { wrap: wrap7, toResult: toResult3 } = createErrorHandlers("withdrawals");
|
|
2520
2943
|
const tokensResource = tokens ?? createTokensResource(client);
|
|
2521
2944
|
const contractsResource = contracts ?? createContractsResource(client);
|
|
2522
2945
|
async function buildPlan(p) {
|
|
2523
|
-
const
|
|
2524
|
-
await
|
|
2525
|
-
const
|
|
2946
|
+
const protocol = await protocolService.protocol();
|
|
2947
|
+
const ctx = await commonCtx2(p, client, tokensResource, contractsResource, protocol);
|
|
2948
|
+
const strategy = routeStrategy(protocol, ctx.route);
|
|
2949
|
+
await strategy.preflight?.(p, ctx);
|
|
2950
|
+
const { steps, approvals, fees } = await strategy.build(p, ctx);
|
|
2526
2951
|
return {
|
|
2527
2952
|
route: ctx.route,
|
|
2528
2953
|
summary: {
|
|
@@ -2689,21 +3114,20 @@ function createWithdrawalsResource(client, tokens, contracts) {
|
|
|
2689
3114
|
if (!l2Rcpt) return { phase: "L2_PENDING", l2TxHash };
|
|
2690
3115
|
let pack;
|
|
2691
3116
|
try {
|
|
2692
|
-
pack = await svc.
|
|
3117
|
+
pack = await svc.fetchFinalization(l2TxHash);
|
|
2693
3118
|
} catch {
|
|
2694
3119
|
return { phase: "PENDING", l2TxHash };
|
|
2695
3120
|
}
|
|
2696
|
-
const key =
|
|
2697
|
-
chainIdL2: pack.params.chainId,
|
|
2698
|
-
l2BatchNumber: pack.params.l2BatchNumber,
|
|
2699
|
-
l2MessageIndex: pack.params.l2MessageIndex
|
|
2700
|
-
};
|
|
3121
|
+
const key = pack.key;
|
|
2701
3122
|
try {
|
|
2702
|
-
const
|
|
2703
|
-
if (
|
|
3123
|
+
const outcome = await svc.withdrawalOutcome(pack.finalization);
|
|
3124
|
+
if (outcome === "finalized") return { phase: "FINALIZED", l2TxHash, key };
|
|
3125
|
+
if (outcome === "failed") {
|
|
3126
|
+
return { phase: "UNFINALIZABLE", l2TxHash, key, reason: "bundle-cancelled" };
|
|
3127
|
+
}
|
|
2704
3128
|
} catch {
|
|
2705
3129
|
}
|
|
2706
|
-
const readiness = await svc.simulateFinalizeReadiness(pack.
|
|
3130
|
+
const readiness = await svc.simulateFinalizeReadiness(pack.finalization);
|
|
2707
3131
|
const { phase, reason } = phaseFromReadiness(readiness);
|
|
2708
3132
|
return { phase, reason, l2TxHash, key };
|
|
2709
3133
|
},
|
|
@@ -2797,7 +3221,7 @@ function createWithdrawalsResource(client, tokens, contracts) {
|
|
|
2797
3221
|
async () => {
|
|
2798
3222
|
const pack = await (async () => {
|
|
2799
3223
|
try {
|
|
2800
|
-
return await svc.
|
|
3224
|
+
return await svc.fetchFinalization(l2TxHash);
|
|
2801
3225
|
} catch (e) {
|
|
2802
3226
|
throw createError("STATE", {
|
|
2803
3227
|
resource: "withdrawals",
|
|
@@ -2808,21 +3232,16 @@ function createWithdrawalsResource(client, tokens, contracts) {
|
|
|
2808
3232
|
});
|
|
2809
3233
|
}
|
|
2810
3234
|
})();
|
|
2811
|
-
const {
|
|
2812
|
-
const key = {
|
|
2813
|
-
chainIdL2: params.chainId,
|
|
2814
|
-
l2BatchNumber: params.l2BatchNumber,
|
|
2815
|
-
l2MessageIndex: params.l2MessageIndex
|
|
2816
|
-
};
|
|
3235
|
+
const { finalization } = pack;
|
|
2817
3236
|
try {
|
|
2818
|
-
const
|
|
2819
|
-
if (
|
|
3237
|
+
const outcome = await svc.withdrawalOutcome(finalization);
|
|
3238
|
+
if (outcome === "finalized" || outcome === "failed") {
|
|
2820
3239
|
const statusNow = await status(l2TxHash);
|
|
2821
3240
|
return { status: statusNow };
|
|
2822
3241
|
}
|
|
2823
3242
|
} catch {
|
|
2824
3243
|
}
|
|
2825
|
-
const readiness = await svc.simulateFinalizeReadiness(
|
|
3244
|
+
const readiness = await svc.simulateFinalizeReadiness(finalization);
|
|
2826
3245
|
if (readiness.kind === "FINALIZED") {
|
|
2827
3246
|
const statusNow = await status(l2TxHash);
|
|
2828
3247
|
return { status: statusNow };
|
|
@@ -2836,7 +3255,7 @@ function createWithdrawalsResource(client, tokens, contracts) {
|
|
|
2836
3255
|
});
|
|
2837
3256
|
}
|
|
2838
3257
|
try {
|
|
2839
|
-
const tx = await svc.
|
|
3258
|
+
const tx = await svc.finalize(finalization);
|
|
2840
3259
|
finalizeCache.set(l2TxHash, tx.hash);
|
|
2841
3260
|
const rcpt = await tx.wait();
|
|
2842
3261
|
const statusNow = await status(l2TxHash);
|
|
@@ -2845,7 +3264,7 @@ function createWithdrawalsResource(client, tokens, contracts) {
|
|
|
2845
3264
|
const statusNow = await status(l2TxHash);
|
|
2846
3265
|
if (statusNow.phase === "FINALIZED") return { status: statusNow };
|
|
2847
3266
|
try {
|
|
2848
|
-
const again = await svc.simulateFinalizeReadiness(
|
|
3267
|
+
const again = await svc.simulateFinalizeReadiness(finalization);
|
|
2849
3268
|
if (again.kind === "NOT_READY") {
|
|
2850
3269
|
throw createError("STATE", {
|
|
2851
3270
|
resource: "withdrawals",
|
|
@@ -2903,63 +3322,6 @@ function createWithdrawalsResource(client, tokens, contracts) {
|
|
|
2903
3322
|
tryWait
|
|
2904
3323
|
};
|
|
2905
3324
|
}
|
|
2906
|
-
function getInteropAttributes(params, ctx) {
|
|
2907
|
-
const bundleAttributes = [];
|
|
2908
|
-
if (params.execution?.only) {
|
|
2909
|
-
bundleAttributes.push(ctx.attributes.bundle.executionAddress(params.execution.only));
|
|
2910
|
-
}
|
|
2911
|
-
if (params.unbundling?.by) {
|
|
2912
|
-
bundleAttributes.push(ctx.attributes.bundle.unbundlerAddress(params.unbundling.by));
|
|
2913
|
-
}
|
|
2914
|
-
bundleAttributes.push(ctx.attributes.bundle.useFixedFee(params.fee?.useFixed ?? false));
|
|
2915
|
-
const callAttributes = params.actions.map((action) => {
|
|
2916
|
-
switch (action.type) {
|
|
2917
|
-
case "sendNative": {
|
|
2918
|
-
const baseMatches = ctx.baseTokens.src.toLowerCase() === ctx.baseTokens.dst.toLowerCase();
|
|
2919
|
-
if (baseMatches) {
|
|
2920
|
-
return [ctx.attributes.call.interopCallValue(action.amount)];
|
|
2921
|
-
}
|
|
2922
|
-
return [ctx.attributes.call.indirectCall(action.amount)];
|
|
2923
|
-
}
|
|
2924
|
-
case "call":
|
|
2925
|
-
if (action.value && action.value > 0n) {
|
|
2926
|
-
return [ctx.attributes.call.interopCallValue(action.value)];
|
|
2927
|
-
}
|
|
2928
|
-
return [];
|
|
2929
|
-
case "sendErc20":
|
|
2930
|
-
return [ctx.attributes.call.indirectCall(0n)];
|
|
2931
|
-
default:
|
|
2932
|
-
assertNever(action);
|
|
2933
|
-
}
|
|
2934
|
-
});
|
|
2935
|
-
return { bundleAttributes, callAttributes };
|
|
2936
|
-
}
|
|
2937
|
-
function createViemAttributesResource() {
|
|
2938
|
-
const encode = (fn, args) => encodeFunctionData({
|
|
2939
|
-
abi: IERC7786Attributes_default,
|
|
2940
|
-
functionName: fn,
|
|
2941
|
-
args
|
|
2942
|
-
});
|
|
2943
|
-
return createAttributesResource({ encode });
|
|
2944
|
-
}
|
|
2945
|
-
var PREFIX_EVM_CHAIN = toBytes("0x00010000");
|
|
2946
|
-
var PREFIX_EVM_ADDRESS = toBytes("0x000100000014");
|
|
2947
|
-
function formatInteropEvmChain(chainId) {
|
|
2948
|
-
const chainRef = toBytes(toHex(chainId));
|
|
2949
|
-
const chainRefLength = toBytes(toHex(chainRef.length, { size: 1 }));
|
|
2950
|
-
const payload = concat([PREFIX_EVM_CHAIN, chainRefLength, chainRef, new Uint8Array([0])]);
|
|
2951
|
-
return toHex(payload);
|
|
2952
|
-
}
|
|
2953
|
-
function formatInteropEvmAddress(address) {
|
|
2954
|
-
const normalized = getAddress(address);
|
|
2955
|
-
const addrBytes = toBytes(normalized);
|
|
2956
|
-
const payload = concat([PREFIX_EVM_ADDRESS, addrBytes]);
|
|
2957
|
-
return toHex(payload);
|
|
2958
|
-
}
|
|
2959
|
-
var interopCodec = {
|
|
2960
|
-
formatChain: formatInteropEvmChain,
|
|
2961
|
-
formatAddress: formatInteropEvmAddress
|
|
2962
|
-
};
|
|
2963
3325
|
function getErc20Tokens(params) {
|
|
2964
3326
|
const erc20Tokens = /* @__PURE__ */ new Map();
|
|
2965
3327
|
for (const action of params.actions) {
|
|
@@ -3143,7 +3505,7 @@ function routeIndirect() {
|
|
|
3143
3505
|
resolveErc20AssetIds(erc20Tokens, ctx),
|
|
3144
3506
|
buildFeeInfo(params, ctx, params.actions.length)
|
|
3145
3507
|
]);
|
|
3146
|
-
const
|
|
3508
|
+
const attributes2 = getInteropAttributes(params, ctx);
|
|
3147
3509
|
const starterData = await getStarterData(params, ctx, erc20AssetIds);
|
|
3148
3510
|
const bundle = buildIndirectBundle(
|
|
3149
3511
|
params,
|
|
@@ -3154,7 +3516,7 @@ function routeIndirect() {
|
|
|
3154
3516
|
l2NativeTokenVault: ctx.l2NativeTokenVault,
|
|
3155
3517
|
codec: interopCodec
|
|
3156
3518
|
},
|
|
3157
|
-
|
|
3519
|
+
attributes2,
|
|
3158
3520
|
starterData,
|
|
3159
3521
|
feeInfo
|
|
3160
3522
|
);
|
|
@@ -3259,7 +3621,7 @@ async function assertInteropProtocolVersion(client, srcChainId, dstChainId) {
|
|
|
3259
3621
|
assertProtocolVersion(srcChainId, srcProtocolVersion);
|
|
3260
3622
|
assertProtocolVersion(dstChainId, dstProtocolVersion);
|
|
3261
3623
|
}
|
|
3262
|
-
async function commonCtx3(dstPublicClient, params, client, tokens, contracts,
|
|
3624
|
+
async function commonCtx3(dstPublicClient, params, client, tokens, contracts, attributes2) {
|
|
3263
3625
|
const sender = client.account.address;
|
|
3264
3626
|
const chainId = BigInt(await client.l2.getChainId());
|
|
3265
3627
|
const dstChainId = BigInt(await dstPublicClient.getChainId());
|
|
@@ -3293,7 +3655,7 @@ async function commonCtx3(dstPublicClient, params, client, tokens, contracts, at
|
|
|
3293
3655
|
l2AssetRouter,
|
|
3294
3656
|
l2NativeTokenVault,
|
|
3295
3657
|
baseTokens: { src: srcBaseToken, dst: dstBaseToken, matches: baseMatches },
|
|
3296
|
-
attributes,
|
|
3658
|
+
attributes: attributes2,
|
|
3297
3659
|
gasOverrides: params.txOverrides ? toGasOverrides(params.txOverrides) : void 0
|
|
3298
3660
|
};
|
|
3299
3661
|
}
|
|
@@ -3861,7 +4223,7 @@ var ROUTES3 = {
|
|
|
3861
4223
|
direct: routeDirect(),
|
|
3862
4224
|
indirect: routeIndirect()
|
|
3863
4225
|
};
|
|
3864
|
-
function createInteropResource(client, config, tokens, contracts,
|
|
4226
|
+
function createInteropResource(client, config, tokens, contracts, attributes2) {
|
|
3865
4227
|
let gwProviderCache;
|
|
3866
4228
|
function requireConfig() {
|
|
3867
4229
|
if (!config)
|
|
@@ -3879,7 +4241,7 @@ function createInteropResource(client, config, tokens, contracts, attributes) {
|
|
|
3879
4241
|
const svc = createInteropFinalizationServices(client);
|
|
3880
4242
|
const tokensResource = tokens ?? createTokensResource(client);
|
|
3881
4243
|
const contractsResource = contracts ?? createContractsResource(client);
|
|
3882
|
-
const attributesResource =
|
|
4244
|
+
const attributesResource = attributes2 ?? createViemAttributesResource();
|
|
3883
4245
|
async function buildPlanWithCtx(dstPublicClient, params) {
|
|
3884
4246
|
const ctx = await commonCtx3(
|
|
3885
4247
|
dstPublicClient,
|
|
@@ -4099,11 +4461,11 @@ function createViemSdk(client, options) {
|
|
|
4099
4461
|
const interop = createInteropResource(client, options?.interop, tokens, contracts);
|
|
4100
4462
|
return {
|
|
4101
4463
|
deposits: createDepositsResource(client, tokens, contracts),
|
|
4102
|
-
withdrawals: createWithdrawalsResource(client, tokens, contracts),
|
|
4464
|
+
withdrawals: createWithdrawalsResource(client, tokens, contracts, options?.withdrawals),
|
|
4103
4465
|
tokens,
|
|
4104
4466
|
contracts,
|
|
4105
4467
|
interop
|
|
4106
4468
|
};
|
|
4107
4469
|
}
|
|
4108
4470
|
|
|
4109
|
-
export { buildDirectRequestStruct, createContractsResource, createDepositsResource, createFinalizationServices, createInteropFinalizationServices, createInteropResource, createTokensResource, createViemSdk, createWithdrawalsResource, encodeNativeTokenVaultTransferData, encodeSecondBridgeArgs, encodeSecondBridgeDataV1, encodeSecondBridgeErc20Args, encodeSecondBridgeEthArgs, getL2TransactionHashFromLogs };
|
|
4471
|
+
export { buildDirectRequestStruct, createContractsResource, createDepositsResource, createFinalizationServices, createInteropFinalizationServices, createInteropResource, createTokensResource, createViemSdk, createWithdrawalProtocolService, createWithdrawalsResource, encodeNativeTokenVaultTransferData, encodeSecondBridgeArgs, encodeSecondBridgeDataV1, encodeSecondBridgeErc20Args, encodeSecondBridgeEthArgs, getL2TransactionHashFromLogs };
|