@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,8 +1,9 @@
|
|
|
1
|
-
import { createErrorHandlers, toZKsyncError, classifyReadinessFromRevert } from './chunk-
|
|
2
|
-
import { createNTVCodec,
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
1
|
+
import { createErrorHandlers, toZKsyncError, classifyReadinessFromRevert } from './chunk-TUN6NH5D.js';
|
|
2
|
+
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, quoteL2BaseCost, quoteL1Gas, applyL1ToL2Alias, derivePriorityBodyGasEstimateCap, assertProtocolVersion, resolveRegisteredTokenPriorityL2GasLimit, quoteL2Gas2 } from './chunk-WPCZNITC.js';
|
|
3
|
+
import { detectWithdrawalProtocol, buildWithdrawalFinalization, parseBundleHashFromLogs, classifyWithdrawalOutcome, phaseFromReadiness, buildWithdrawalBundle } from './chunk-JJ523MOL.js';
|
|
4
|
+
import { findL1MessageSentLog, messengerLogIndex, l1MessageSentSender, pickWithdrawRoute } from './chunk-OEUDZFXH.js';
|
|
5
|
+
import { IERC7786Attributes_default, isHash66, IL1Nullifier_default, IInteropHandler_default, createError, OP_WITHDRAWALS, normalizeL1Token, isAddressEq, hexEq, OP_DEPOSITS, IERC20_default, isZKsyncError, isReceiptNotFound, IL1NullifierV32_default, OP_INTEROP, IInteropRootStorage_default, IInteropCenter_default, sleep, isETH, normalizeAddrEq, IL2AssetRouter_default, L2NativeTokenVault_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';
|
|
6
7
|
import { Interface, keccak256, AbiCoder, ethers, getBytes, Contract, NonceManager, JsonRpcProvider, getAddress, concat, hexlify, toBeArray, toBeHex, isError } from 'ethers';
|
|
7
8
|
|
|
8
9
|
var I_BRIDGEHUB = new Interface([
|
|
@@ -71,7 +72,7 @@ async function waitForL2ExecutionFromL1Tx(l1, l2, l1TxHash) {
|
|
|
71
72
|
|
|
72
73
|
// src/adapters/ethers/resources/deposits/context.ts
|
|
73
74
|
async function commonCtx(p, client, tokens, contracts) {
|
|
74
|
-
const { bridgehub, l1AssetRouter } = await contracts.addresses();
|
|
75
|
+
const { bridgehub, l1AssetRouter, l1NativeTokenVault } = await contracts.addresses();
|
|
75
76
|
const { chainId } = await client.l2.getNetwork();
|
|
76
77
|
const sender = await client.signer.getAddress();
|
|
77
78
|
const gasPerPubdata = p.gasPerPubdata ?? 800n;
|
|
@@ -99,6 +100,7 @@ async function commonCtx(p, client, tokens, contracts) {
|
|
|
99
100
|
baseTokenL1,
|
|
100
101
|
baseIsEth,
|
|
101
102
|
l1AssetRouter,
|
|
103
|
+
l1NativeTokenVault,
|
|
102
104
|
route,
|
|
103
105
|
bridgehub,
|
|
104
106
|
chainIdL2: BigInt(chainId),
|
|
@@ -188,6 +190,15 @@ function ethersToGasEstimator(provider) {
|
|
|
188
190
|
}
|
|
189
191
|
return await provider.estimateGas(ethTx);
|
|
190
192
|
},
|
|
193
|
+
async estimatePriorityTxGas(tx) {
|
|
194
|
+
if (!("send" in provider)) {
|
|
195
|
+
throw new Error('Provider does not support "send"; cannot estimate priority tx gas.');
|
|
196
|
+
}
|
|
197
|
+
const result = await provider.send("eth_estimateGas", [
|
|
198
|
+
{ from: tx.from, to: tx.to, data: tx.data, type: ZKSYNC_OS_PRIORITY_TX_TYPE }
|
|
199
|
+
]);
|
|
200
|
+
return BigInt(result);
|
|
201
|
+
},
|
|
191
202
|
async estimateFeesPerGas() {
|
|
192
203
|
const fd = await provider.getFeeData();
|
|
193
204
|
return {
|
|
@@ -239,6 +250,14 @@ async function quoteL2BaseCost2(input) {
|
|
|
239
250
|
// src/adapters/ethers/resources/deposits/services/gas.ts
|
|
240
251
|
var DEFAULT_SAFE_NONBASE_L2_GAS_LIMIT = 3000000n;
|
|
241
252
|
var ZERO_L2_TOKEN_ADDRESS = "0x0000000000000000000000000000000000000000";
|
|
253
|
+
async function estimatePriorityTxL2Gas(ctx, leg) {
|
|
254
|
+
if (!leg) return void 0;
|
|
255
|
+
try {
|
|
256
|
+
return await ethersToGasEstimator(ctx.client.l2).estimatePriorityTxGas?.(leg);
|
|
257
|
+
} catch {
|
|
258
|
+
return void 0;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
242
261
|
async function quoteL1Gas2(input) {
|
|
243
262
|
const { ctx, tx, overrides, fallbackGasLimit } = input;
|
|
244
263
|
const estimator = ethersToGasEstimator(ctx.client.l1);
|
|
@@ -292,7 +311,12 @@ async function determineNonBaseL2Gas(input) {
|
|
|
292
311
|
return quoteL2Gas3({
|
|
293
312
|
ctx,
|
|
294
313
|
route,
|
|
295
|
-
overrideGasLimit:
|
|
314
|
+
overrideGasLimit: resolveRegisteredTokenPriorityL2GasLimit({
|
|
315
|
+
chainIdL2: ctx.chainIdL2,
|
|
316
|
+
priorityFloorGasLimit: input.priorityFloorGasLimit,
|
|
317
|
+
gasPerPubdata: ctx.gasPerPubdata,
|
|
318
|
+
nodeEstimate: await estimatePriorityTxL2Gas(ctx, input.priorityTxL2Leg)
|
|
319
|
+
})
|
|
296
320
|
});
|
|
297
321
|
}
|
|
298
322
|
const modelTx = {
|
|
@@ -329,6 +353,7 @@ async function determineEthNonBaseL2Gas(input) {
|
|
|
329
353
|
knownL2Token: input.ctx.resolvedToken?.l2,
|
|
330
354
|
modelTx: input.modelTx,
|
|
331
355
|
priorityFloorGasLimit: input.priorityFloorGasLimit,
|
|
356
|
+
priorityTxL2Leg: input.priorityTxL2Leg,
|
|
332
357
|
undeployedGasLimit: input.undeployedGasLimit
|
|
333
358
|
});
|
|
334
359
|
}
|
|
@@ -388,9 +413,13 @@ function routeEthDirect() {
|
|
|
388
413
|
l2Calldata,
|
|
389
414
|
gasPerPubdata: ctx.gasPerPubdata
|
|
390
415
|
});
|
|
391
|
-
const quotedL2GasLimit = ctx.l2GasLimit ??
|
|
392
|
-
|
|
393
|
-
|
|
416
|
+
const quotedL2GasLimit = ctx.l2GasLimit ?? clampPriorityL2GasLimit({
|
|
417
|
+
gasLimit: applyPriorityL2GasLimitBuffer({
|
|
418
|
+
chainIdL2: ctx.chainIdL2,
|
|
419
|
+
gasLimit: priorityFloorBreakdown.derivedL2GasLimit
|
|
420
|
+
}),
|
|
421
|
+
l2Calldata,
|
|
422
|
+
gasPerPubdata: ctx.gasPerPubdata
|
|
394
423
|
});
|
|
395
424
|
const l2GasParams = await quoteL2Gas3({
|
|
396
425
|
ctx,
|
|
@@ -461,26 +490,18 @@ var ntvCodec = createNTVCodec({
|
|
|
461
490
|
keccak256: (data) => keccak256(data)
|
|
462
491
|
});
|
|
463
492
|
async function encodeSecondBridgeErc20DepositCalldata(input) {
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
const { chainId } = await input.ctx.client.l1.getNetwork();
|
|
468
|
-
const expectedL1AssetId = ntvCodec.encodeAssetId(
|
|
469
|
-
BigInt(chainId),
|
|
493
|
+
const registeredAssetId = input.ctx.resolvedToken?.assetId;
|
|
494
|
+
const assetId = registeredAssetId && registeredAssetId.toLowerCase() !== ZERO_ASSET_ID ? registeredAssetId : ntvCodec.encodeAssetId(
|
|
495
|
+
BigInt((await input.ctx.client.l1.getNetwork()).chainId),
|
|
470
496
|
L2_NATIVE_TOKEN_VAULT_ADDRESS,
|
|
471
497
|
input.token
|
|
472
498
|
);
|
|
473
|
-
const assetId = input.ctx.resolvedToken.assetId.toLowerCase();
|
|
474
|
-
const isL1Origin = assetId === ZERO_ASSET_ID || assetId === expectedL1AssetId.toLowerCase();
|
|
475
|
-
if (isL1Origin) {
|
|
476
|
-
return encodeSecondBridgeErc20Args(input.token, input.amount, input.receiver);
|
|
477
|
-
}
|
|
478
499
|
const transferData = encodeNativeTokenVaultTransferData(
|
|
479
500
|
input.amount,
|
|
480
501
|
input.receiver,
|
|
481
502
|
input.token
|
|
482
503
|
);
|
|
483
|
-
return encodeSecondBridgeDataV1(
|
|
504
|
+
return encodeSecondBridgeDataV1(assetId, transferData);
|
|
484
505
|
}
|
|
485
506
|
async function getPriorityGasModel(input) {
|
|
486
507
|
try {
|
|
@@ -515,9 +536,18 @@ async function getPriorityGasModel(input) {
|
|
|
515
536
|
gasPerPubdata: input.ctx.gasPerPubdata
|
|
516
537
|
});
|
|
517
538
|
const model = {
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
539
|
+
priorityTxL2Leg: {
|
|
540
|
+
from: applyL1ToL2Alias(input.ctx.l1AssetRouter),
|
|
541
|
+
to: L2_ASSET_ROUTER_ADDRESS,
|
|
542
|
+
data: l2Calldata
|
|
543
|
+
},
|
|
544
|
+
priorityFloorGasLimit: clampPriorityL2GasLimit({
|
|
545
|
+
gasLimit: applyPriorityL2GasLimitBuffer({
|
|
546
|
+
chainIdL2: input.ctx.chainIdL2,
|
|
547
|
+
gasLimit: priorityFloorBreakdown.derivedL2GasLimit
|
|
548
|
+
}),
|
|
549
|
+
l2Calldata,
|
|
550
|
+
gasPerPubdata: input.ctx.gasPerPubdata
|
|
521
551
|
})
|
|
522
552
|
};
|
|
523
553
|
if (isFirstBridge || input.ctx.resolvedToken.l2.toLowerCase() === ZERO_L2_TOKEN_ADDRESS2) {
|
|
@@ -575,6 +605,7 @@ function routeErc20NonBase() {
|
|
|
575
605
|
ctx,
|
|
576
606
|
l1Token: p.token,
|
|
577
607
|
priorityFloorGasLimit: priorityGasModel.priorityFloorGasLimit,
|
|
608
|
+
priorityTxL2Leg: priorityGasModel.priorityTxL2Leg,
|
|
578
609
|
undeployedGasLimit: priorityGasModel.undeployedGasLimit,
|
|
579
610
|
modelTx: {
|
|
580
611
|
to: receiver,
|
|
@@ -589,25 +620,29 @@ function routeErc20NonBase() {
|
|
|
589
620
|
const approvals = [];
|
|
590
621
|
const steps = [];
|
|
591
622
|
const assetRouter = ctx.l1AssetRouter;
|
|
623
|
+
const nativeTokenVault = ctx.l1NativeTokenVault;
|
|
592
624
|
const erc20Deposit = new Contract(p.token, IERC20_default, l1Signer);
|
|
593
625
|
const allowanceToken = await wrapAs2(
|
|
594
626
|
"RPC",
|
|
595
627
|
OP_DEPOSITS.nonbase.allowanceToken,
|
|
596
|
-
() => erc20Deposit.allowance(ctx.sender,
|
|
628
|
+
() => erc20Deposit.allowance(ctx.sender, nativeTokenVault),
|
|
597
629
|
{
|
|
598
|
-
ctx: { where: "erc20.allowance", token: p.token, spender:
|
|
630
|
+
ctx: { where: "erc20.allowance", token: p.token, spender: nativeTokenVault },
|
|
599
631
|
message: "Failed to read deposit-token allowance."
|
|
600
632
|
}
|
|
601
633
|
);
|
|
602
634
|
if (allowanceToken < p.amount) {
|
|
603
|
-
approvals.push({ token: p.token, spender:
|
|
635
|
+
approvals.push({ token: p.token, spender: nativeTokenVault, amount: p.amount });
|
|
604
636
|
steps.push({
|
|
605
|
-
key: `approve:${p.token}:${
|
|
637
|
+
key: `approve:${p.token}:${nativeTokenVault}`,
|
|
606
638
|
kind: "approve",
|
|
607
|
-
description: `Approve ${p.amount} for
|
|
639
|
+
description: `Approve ${p.amount} for the native token vault (deposit token)`,
|
|
608
640
|
tx: {
|
|
609
641
|
to: p.token,
|
|
610
|
-
data: erc20Deposit.interface.encodeFunctionData("approve", [
|
|
642
|
+
data: erc20Deposit.interface.encodeFunctionData("approve", [
|
|
643
|
+
nativeTokenVault,
|
|
644
|
+
p.amount
|
|
645
|
+
]),
|
|
611
646
|
from: ctx.sender,
|
|
612
647
|
...ctx.gasOverrides
|
|
613
648
|
}
|
|
@@ -618,21 +653,24 @@ function routeErc20NonBase() {
|
|
|
618
653
|
const allowanceBase = await wrapAs2(
|
|
619
654
|
"RPC",
|
|
620
655
|
OP_DEPOSITS.nonbase.allowanceBase,
|
|
621
|
-
() => erc20Base.allowance(ctx.sender,
|
|
656
|
+
() => erc20Base.allowance(ctx.sender, nativeTokenVault),
|
|
622
657
|
{
|
|
623
|
-
ctx: { where: "erc20.allowance", token: baseToken, spender:
|
|
658
|
+
ctx: { where: "erc20.allowance", token: baseToken, spender: nativeTokenVault },
|
|
624
659
|
message: "Failed to read base-token allowance."
|
|
625
660
|
}
|
|
626
661
|
);
|
|
627
662
|
if (allowanceBase < mintValue) {
|
|
628
|
-
approvals.push({ token: baseToken, spender:
|
|
663
|
+
approvals.push({ token: baseToken, spender: nativeTokenVault, amount: mintValue });
|
|
629
664
|
steps.push({
|
|
630
|
-
key: `approve:${baseToken}:${
|
|
665
|
+
key: `approve:${baseToken}:${nativeTokenVault}`,
|
|
631
666
|
kind: "approve",
|
|
632
667
|
description: `Approve base token for mintValue`,
|
|
633
668
|
tx: {
|
|
634
669
|
to: baseToken,
|
|
635
|
-
data: erc20Base.interface.encodeFunctionData("approve", [
|
|
670
|
+
data: erc20Base.interface.encodeFunctionData("approve", [
|
|
671
|
+
nativeTokenVault,
|
|
672
|
+
mintValue
|
|
673
|
+
]),
|
|
636
674
|
from: ctx.sender,
|
|
637
675
|
...ctx.gasOverrides
|
|
638
676
|
}
|
|
@@ -729,9 +767,18 @@ async function getPriorityGasModel2(input) {
|
|
|
729
767
|
gasPerPubdata: input.ctx.gasPerPubdata
|
|
730
768
|
});
|
|
731
769
|
const model = {
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
770
|
+
priorityTxL2Leg: {
|
|
771
|
+
from: applyL1ToL2Alias(input.ctx.l1AssetRouter),
|
|
772
|
+
to: L2_ASSET_ROUTER_ADDRESS,
|
|
773
|
+
data: l2Calldata
|
|
774
|
+
},
|
|
775
|
+
priorityFloorGasLimit: clampPriorityL2GasLimit({
|
|
776
|
+
gasLimit: applyPriorityL2GasLimitBuffer({
|
|
777
|
+
chainIdL2: input.ctx.chainIdL2,
|
|
778
|
+
gasLimit: priorityFloorBreakdown.derivedL2GasLimit
|
|
779
|
+
}),
|
|
780
|
+
l2Calldata,
|
|
781
|
+
gasPerPubdata: input.ctx.gasPerPubdata
|
|
735
782
|
})
|
|
736
783
|
};
|
|
737
784
|
if (input.ctx.resolvedToken.l2.toLowerCase() === ZERO_L2_TOKEN_ADDRESS3) {
|
|
@@ -808,6 +855,7 @@ function routeEthNonBase() {
|
|
|
808
855
|
ctx,
|
|
809
856
|
modelTx: l2TxModel,
|
|
810
857
|
priorityFloorGasLimit: priorityGasModel.priorityFloorGasLimit,
|
|
858
|
+
priorityTxL2Leg: priorityGasModel.priorityTxL2Leg,
|
|
811
859
|
undeployedGasLimit: priorityGasModel.undeployedGasLimit
|
|
812
860
|
});
|
|
813
861
|
if (!l2GasParams) throw new Error("Failed to estimate L2 gas parameters.");
|
|
@@ -819,21 +867,24 @@ function routeEthNonBase() {
|
|
|
819
867
|
const allowance = await wrapAs3(
|
|
820
868
|
"RPC",
|
|
821
869
|
OP_DEPOSITS.ethNonBase.allowanceBase,
|
|
822
|
-
() => erc20Base.allowance(ctx.sender, ctx.
|
|
870
|
+
() => erc20Base.allowance(ctx.sender, ctx.l1NativeTokenVault),
|
|
823
871
|
{
|
|
824
|
-
ctx: { where: "erc20.allowance", token: baseToken, spender: ctx.
|
|
872
|
+
ctx: { where: "erc20.allowance", token: baseToken, spender: ctx.l1NativeTokenVault },
|
|
825
873
|
message: "Failed to read base-token allowance."
|
|
826
874
|
}
|
|
827
875
|
);
|
|
828
876
|
if (allowance < mintValue) {
|
|
829
|
-
approvals.push({ token: baseToken, spender: ctx.
|
|
877
|
+
approvals.push({ token: baseToken, spender: ctx.l1NativeTokenVault, amount: mintValue });
|
|
830
878
|
steps.push({
|
|
831
|
-
key: `approve:${baseToken}:${ctx.
|
|
879
|
+
key: `approve:${baseToken}:${ctx.l1NativeTokenVault}`,
|
|
832
880
|
kind: "approve",
|
|
833
881
|
description: `Approve base token for fees (mintValue)`,
|
|
834
882
|
tx: {
|
|
835
883
|
to: baseToken,
|
|
836
|
-
data: erc20Base.interface.encodeFunctionData("approve", [
|
|
884
|
+
data: erc20Base.interface.encodeFunctionData("approve", [
|
|
885
|
+
ctx.l1NativeTokenVault,
|
|
886
|
+
mintValue
|
|
887
|
+
]),
|
|
837
888
|
from: ctx.sender,
|
|
838
889
|
...ctx.gasOverrides
|
|
839
890
|
}
|
|
@@ -949,9 +1000,13 @@ function routeErc20Base() {
|
|
|
949
1000
|
l2Calldata,
|
|
950
1001
|
gasPerPubdata: ctx.gasPerPubdata
|
|
951
1002
|
});
|
|
952
|
-
const quotedL2GasLimit = ctx.l2GasLimit ??
|
|
953
|
-
|
|
954
|
-
|
|
1003
|
+
const quotedL2GasLimit = ctx.l2GasLimit ?? clampPriorityL2GasLimit({
|
|
1004
|
+
gasLimit: applyPriorityL2GasLimitBuffer({
|
|
1005
|
+
chainIdL2: ctx.chainIdL2,
|
|
1006
|
+
gasLimit: priorityFloorBreakdown.derivedL2GasLimit
|
|
1007
|
+
}),
|
|
1008
|
+
l2Calldata,
|
|
1009
|
+
gasPerPubdata: ctx.gasPerPubdata
|
|
955
1010
|
});
|
|
956
1011
|
const l2GasParams = await quoteL2Gas3({
|
|
957
1012
|
ctx,
|
|
@@ -968,21 +1023,24 @@ function routeErc20Base() {
|
|
|
968
1023
|
const allowance = await wrapAs4(
|
|
969
1024
|
"RPC",
|
|
970
1025
|
OP_DEPOSITS.base.allowance,
|
|
971
|
-
() => erc20.allowance(ctx.sender, ctx.
|
|
1026
|
+
() => erc20.allowance(ctx.sender, ctx.l1NativeTokenVault),
|
|
972
1027
|
{
|
|
973
|
-
ctx: { where: "erc20.allowance", token: baseToken, spender: ctx.
|
|
1028
|
+
ctx: { where: "erc20.allowance", token: baseToken, spender: ctx.l1NativeTokenVault },
|
|
974
1029
|
message: "Failed to read base-token allowance."
|
|
975
1030
|
}
|
|
976
1031
|
);
|
|
977
1032
|
if (allowance < mintValue) {
|
|
978
|
-
approvals.push({ token: baseToken, spender: ctx.
|
|
1033
|
+
approvals.push({ token: baseToken, spender: ctx.l1NativeTokenVault, amount: mintValue });
|
|
979
1034
|
steps.push({
|
|
980
|
-
key: `approve:${baseToken}:${ctx.
|
|
1035
|
+
key: `approve:${baseToken}:${ctx.l1NativeTokenVault}`,
|
|
981
1036
|
kind: "approve",
|
|
982
1037
|
description: "Approve base token for mintValue",
|
|
983
1038
|
tx: {
|
|
984
1039
|
to: baseToken,
|
|
985
|
-
data: erc20.interface.encodeFunctionData("approve", [
|
|
1040
|
+
data: erc20.interface.encodeFunctionData("approve", [
|
|
1041
|
+
ctx.l1NativeTokenVault,
|
|
1042
|
+
mintValue
|
|
1043
|
+
]),
|
|
986
1044
|
from: ctx.sender,
|
|
987
1045
|
...ctx.gasOverrides
|
|
988
1046
|
}
|
|
@@ -1116,8 +1174,17 @@ function createTokensResource(client) {
|
|
|
1116
1174
|
const { chainId } = await client.l2.getNetwork();
|
|
1117
1175
|
return await client.baseToken(BigInt(chainId));
|
|
1118
1176
|
}
|
|
1119
|
-
const {
|
|
1120
|
-
const
|
|
1177
|
+
const { l2NativeTokenVault, l1NativeTokenVault } = await client.contracts();
|
|
1178
|
+
const assetId = await l2NativeTokenVault.assetId(l2Token);
|
|
1179
|
+
if (BigInt(assetId) === 0n) {
|
|
1180
|
+
throw new Error(`Token ${l2Token} is not registered in the L2 NativeTokenVault.`);
|
|
1181
|
+
}
|
|
1182
|
+
const l1Token = await l1NativeTokenVault.tokenAddress(assetId);
|
|
1183
|
+
if (BigInt(l1Token) === 0n) {
|
|
1184
|
+
throw new Error(
|
|
1185
|
+
`Token ${l2Token} has no L1 counterpart registered in the L1 NativeTokenVault.`
|
|
1186
|
+
);
|
|
1187
|
+
}
|
|
1121
1188
|
return l1Token;
|
|
1122
1189
|
});
|
|
1123
1190
|
}
|
|
@@ -1612,7 +1679,7 @@ function createDepositsResource(client, tokens, contracts) {
|
|
|
1612
1679
|
}
|
|
1613
1680
|
|
|
1614
1681
|
// src/adapters/ethers/resources/withdrawals/context.ts
|
|
1615
|
-
async function commonCtx2(p, client, tokens, contracts) {
|
|
1682
|
+
async function commonCtx2(p, client, tokens, contracts, protocol) {
|
|
1616
1683
|
const sender = await client.signer.getAddress();
|
|
1617
1684
|
const {
|
|
1618
1685
|
bridgehub,
|
|
@@ -1620,10 +1687,12 @@ async function commonCtx2(p, client, tokens, contracts) {
|
|
|
1620
1687
|
l1Nullifier,
|
|
1621
1688
|
l2AssetRouter,
|
|
1622
1689
|
l2NativeTokenVault,
|
|
1623
|
-
l2BaseTokenSystem
|
|
1690
|
+
l2BaseTokenSystem,
|
|
1691
|
+
interopCenter
|
|
1624
1692
|
} = await contracts.addresses();
|
|
1625
1693
|
const { chainId } = await client.l2.getNetwork();
|
|
1626
1694
|
const chainIdL2 = BigInt(chainId);
|
|
1695
|
+
const l1ChainId = BigInt((await client.l1.getNetwork()).chainId);
|
|
1627
1696
|
const resolvedToken = await tokens.resolve(p.token, { chain: "l2" });
|
|
1628
1697
|
const baseTokenAssetId = resolvedToken.baseTokenAssetId;
|
|
1629
1698
|
const baseTokenL1 = await tokens.l1TokenFromAssetId(baseTokenAssetId);
|
|
@@ -1645,6 +1714,9 @@ async function commonCtx2(p, client, tokens, contracts) {
|
|
|
1645
1714
|
l2AssetRouter,
|
|
1646
1715
|
l2NativeTokenVault,
|
|
1647
1716
|
l2BaseTokenSystem,
|
|
1717
|
+
interopCenter,
|
|
1718
|
+
protocol,
|
|
1719
|
+
l1ChainId,
|
|
1648
1720
|
baseIsEth,
|
|
1649
1721
|
gasOverrides: p.l2TxOverrides ? toGasOverrides(p.l2TxOverrides) : void 0
|
|
1650
1722
|
};
|
|
@@ -1826,174 +1898,492 @@ function routeErc20NonBase2() {
|
|
|
1826
1898
|
}
|
|
1827
1899
|
};
|
|
1828
1900
|
}
|
|
1901
|
+
var PREFIX_EVM_CHAIN = getBytes("0x00010000");
|
|
1902
|
+
var PREFIX_EVM_ADDRESS = getBytes("0x000100000014");
|
|
1903
|
+
function formatInteropEvmChain(chainId) {
|
|
1904
|
+
const chainRef = toBeArray(chainId);
|
|
1905
|
+
const chainRefLength = getBytes(toBeHex(chainRef.length, 1));
|
|
1906
|
+
const payload = concat([PREFIX_EVM_CHAIN, chainRefLength, chainRef, new Uint8Array([0])]);
|
|
1907
|
+
return hexlify(payload);
|
|
1908
|
+
}
|
|
1909
|
+
function formatInteropEvmAddress(address) {
|
|
1910
|
+
const normalized = getAddress(address);
|
|
1911
|
+
const addrBytes = getBytes(normalized);
|
|
1912
|
+
const payload = concat([PREFIX_EVM_ADDRESS, addrBytes]);
|
|
1913
|
+
return hexlify(payload);
|
|
1914
|
+
}
|
|
1915
|
+
var interopCodec = {
|
|
1916
|
+
formatChain: formatInteropEvmChain,
|
|
1917
|
+
formatAddress: formatInteropEvmAddress
|
|
1918
|
+
};
|
|
1919
|
+
function getInteropAttributes(params, ctx) {
|
|
1920
|
+
const bundleAttributes = [];
|
|
1921
|
+
if (params.execution?.only) {
|
|
1922
|
+
bundleAttributes.push(ctx.attributes.bundle.executionAddress(params.execution.only));
|
|
1923
|
+
}
|
|
1924
|
+
if (params.unbundling?.by) {
|
|
1925
|
+
bundleAttributes.push(ctx.attributes.bundle.unbundlerAddress(params.unbundling.by));
|
|
1926
|
+
}
|
|
1927
|
+
bundleAttributes.push(ctx.attributes.bundle.useFixedFee(params.fee?.useFixed ?? false));
|
|
1928
|
+
const callAttributes = params.actions.map((action) => {
|
|
1929
|
+
switch (action.type) {
|
|
1930
|
+
case "sendNative": {
|
|
1931
|
+
const baseMatches = ctx.baseTokens.src.toLowerCase() === ctx.baseTokens.dst.toLowerCase();
|
|
1932
|
+
if (baseMatches) {
|
|
1933
|
+
return [ctx.attributes.call.interopCallValue(action.amount)];
|
|
1934
|
+
}
|
|
1935
|
+
return [ctx.attributes.call.indirectCall(action.amount)];
|
|
1936
|
+
}
|
|
1937
|
+
case "call":
|
|
1938
|
+
if (action.value && action.value > 0n) {
|
|
1939
|
+
return [ctx.attributes.call.interopCallValue(action.value)];
|
|
1940
|
+
}
|
|
1941
|
+
return [];
|
|
1942
|
+
case "sendErc20":
|
|
1943
|
+
return [ctx.attributes.call.indirectCall(0n)];
|
|
1944
|
+
default:
|
|
1945
|
+
assertNever(action);
|
|
1946
|
+
}
|
|
1947
|
+
});
|
|
1948
|
+
return { bundleAttributes, callAttributes };
|
|
1949
|
+
}
|
|
1950
|
+
function createEthersAttributesResource(opts = {}) {
|
|
1951
|
+
const iface = opts.iface ?? new Interface(IERC7786Attributes_default);
|
|
1952
|
+
const encode2 = (fn, args) => iface.encodeFunctionData(fn, args);
|
|
1953
|
+
return createAttributesResource({ encode: encode2 });
|
|
1954
|
+
}
|
|
1955
|
+
|
|
1956
|
+
// src/adapters/ethers/resources/withdrawals/routes/interop-bundle.ts
|
|
1829
1957
|
var { wrapAs: wrapAs8 } = createErrorHandlers("withdrawals");
|
|
1830
|
-
var
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1958
|
+
var attributes = createEthersAttributesResource();
|
|
1959
|
+
function saltFor(sender, nonce) {
|
|
1960
|
+
return keccak256(
|
|
1961
|
+
AbiCoder.defaultAbiCoder().encode(["address", "uint256"], [sender, BigInt(nonce)])
|
|
1962
|
+
);
|
|
1963
|
+
}
|
|
1964
|
+
function routeInteropBundle() {
|
|
1835
1965
|
return {
|
|
1836
|
-
async
|
|
1837
|
-
const
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1966
|
+
async build(p, ctx) {
|
|
1967
|
+
const steps = [];
|
|
1968
|
+
const approvals = [];
|
|
1969
|
+
const isBaseToken = ctx.route === "base";
|
|
1970
|
+
const l1Receiver = p.to ?? ctx.sender;
|
|
1971
|
+
let assetId;
|
|
1972
|
+
let allowanceSufficient = true;
|
|
1973
|
+
if (isBaseToken) {
|
|
1974
|
+
assetId = ctx.baseTokenAssetId;
|
|
1975
|
+
} else {
|
|
1976
|
+
assetId = await wrapAs8(
|
|
1977
|
+
"CONTRACT",
|
|
1978
|
+
OP_WITHDRAWALS.erc20.ensureRegistered,
|
|
1979
|
+
async () => {
|
|
1980
|
+
const ntv = await ctx.contracts.l2NativeTokenVault();
|
|
1981
|
+
return await ntv.getFunction("ensureTokenIsRegistered").staticCall(p.token);
|
|
1982
|
+
},
|
|
1983
|
+
{
|
|
1984
|
+
ctx: { where: "L2NativeTokenVault.ensureTokenIsRegistered", token: p.token },
|
|
1985
|
+
message: "Failed to ensure token is registered in L2NativeTokenVault."
|
|
1986
|
+
}
|
|
1987
|
+
);
|
|
1988
|
+
const erc20 = new Contract(p.token, IERC20_default, ctx.client.l2);
|
|
1989
|
+
const current = await wrapAs8(
|
|
1990
|
+
"CONTRACT",
|
|
1991
|
+
OP_WITHDRAWALS.erc20.allowance,
|
|
1992
|
+
() => erc20.allowance(ctx.sender, ctx.l2NativeTokenVault),
|
|
1993
|
+
{
|
|
1994
|
+
ctx: {
|
|
1995
|
+
where: "erc20.allowance",
|
|
1996
|
+
chain: "L2",
|
|
1997
|
+
token: p.token,
|
|
1998
|
+
spender: ctx.l2NativeTokenVault
|
|
1999
|
+
},
|
|
2000
|
+
message: "Failed to read L2 ERC-20 allowance."
|
|
2001
|
+
}
|
|
2002
|
+
);
|
|
2003
|
+
allowanceSufficient = current >= p.amount;
|
|
2004
|
+
if (!allowanceSufficient) {
|
|
2005
|
+
approvals.push({ token: p.token, spender: ctx.l2NativeTokenVault, amount: p.amount });
|
|
2006
|
+
const approveTx = {
|
|
2007
|
+
to: p.token,
|
|
2008
|
+
data: erc20.interface.encodeFunctionData("approve", [ctx.l2NativeTokenVault, p.amount]),
|
|
2009
|
+
from: ctx.sender
|
|
2010
|
+
};
|
|
2011
|
+
const approveGas = await quoteL2Gas4({ ctx, tx: approveTx });
|
|
2012
|
+
if (approveGas) {
|
|
2013
|
+
approveTx.gasLimit = approveGas.gasLimit;
|
|
2014
|
+
approveTx.maxFeePerGas = approveGas.maxFeePerGas;
|
|
2015
|
+
approveTx.maxPriorityFeePerGas = approveGas.maxPriorityFeePerGas;
|
|
2016
|
+
}
|
|
2017
|
+
steps.push({
|
|
2018
|
+
key: `approve:l2:${p.token}:${ctx.l2NativeTokenVault}`,
|
|
2019
|
+
kind: "approve:l2",
|
|
2020
|
+
description: `Approve ${p.amount} to NativeTokenVault`,
|
|
2021
|
+
tx: approveTx
|
|
2022
|
+
});
|
|
1844
2023
|
}
|
|
1845
|
-
);
|
|
1846
|
-
if (!parsed) {
|
|
1847
|
-
throw createError("STATE", {
|
|
1848
|
-
resource: "withdrawals",
|
|
1849
|
-
operation: OP_WITHDRAWALS.finalize.fetchParams.receipt,
|
|
1850
|
-
message: "L2 receipt not found.",
|
|
1851
|
-
context: { l2TxHash }
|
|
1852
|
-
});
|
|
1853
2024
|
}
|
|
1854
|
-
const
|
|
1855
|
-
"
|
|
1856
|
-
OP_WITHDRAWALS.
|
|
1857
|
-
|
|
1858
|
-
() => Promise.resolve(findL1MessageSentLog(parsed, { index: 0 })),
|
|
2025
|
+
const nonce = await wrapAs8(
|
|
2026
|
+
"RPC",
|
|
2027
|
+
OP_WITHDRAWALS.prepare,
|
|
2028
|
+
() => ctx.client.l2.getTransactionCount(ctx.sender, "pending"),
|
|
1859
2029
|
{
|
|
1860
|
-
ctx: {
|
|
1861
|
-
message: "Failed to
|
|
2030
|
+
ctx: { where: "l2.getTransactionCount", sender: ctx.sender },
|
|
2031
|
+
message: "Failed to read L2 nonce for the interop bundle salt."
|
|
1862
2032
|
}
|
|
1863
2033
|
);
|
|
1864
|
-
const
|
|
2034
|
+
const bundle = buildWithdrawalBundle({
|
|
2035
|
+
assetId,
|
|
2036
|
+
amount: p.amount,
|
|
2037
|
+
l1Receiver,
|
|
2038
|
+
l2Token: isBaseToken ? FORMAL_ETH_ADDRESS : p.token,
|
|
2039
|
+
isBaseToken,
|
|
2040
|
+
l1ChainId: ctx.l1ChainId,
|
|
2041
|
+
l2AssetRouter: ctx.l2AssetRouter,
|
|
2042
|
+
salt: saltFor(ctx.sender, nonce),
|
|
2043
|
+
codec: interopCodec,
|
|
2044
|
+
attributes: {
|
|
2045
|
+
indirectCall: attributes.call.indirectCall,
|
|
2046
|
+
interopCallValue: attributes.call.interopCallValue,
|
|
2047
|
+
interopBundleSalt: attributes.bundle.interopBundleSalt
|
|
2048
|
+
},
|
|
2049
|
+
transferData: {
|
|
2050
|
+
encodeBridgeBurnData: (amount, receiver, token) => encodeNativeTokenVaultTransferData(amount, receiver, token),
|
|
2051
|
+
encodeAssetRouterDepositData: (id, data2) => encodeSecondBridgeDataV1(id, data2)
|
|
2052
|
+
}
|
|
2053
|
+
});
|
|
2054
|
+
const interopCenter = await ctx.contracts.interopCenter();
|
|
2055
|
+
const data = await wrapAs8(
|
|
1865
2056
|
"INTERNAL",
|
|
1866
|
-
OP_WITHDRAWALS.
|
|
1867
|
-
() => Promise.resolve(
|
|
2057
|
+
OP_WITHDRAWALS.erc20.encodeWithdraw,
|
|
2058
|
+
() => Promise.resolve(
|
|
2059
|
+
interopCenter.interface.encodeFunctionData("sendBundle", [
|
|
2060
|
+
bundle.destinationChain,
|
|
2061
|
+
bundle.starters,
|
|
2062
|
+
bundle.bundleAttributes
|
|
2063
|
+
])
|
|
2064
|
+
),
|
|
1868
2065
|
{
|
|
1869
|
-
ctx: { where: "
|
|
1870
|
-
message: "Failed to
|
|
2066
|
+
ctx: { where: "InteropCenter.sendBundle", assetId },
|
|
2067
|
+
message: "Failed to encode sendBundle calldata."
|
|
1871
2068
|
}
|
|
1872
2069
|
);
|
|
1873
|
-
const
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
2070
|
+
const sendTx = {
|
|
2071
|
+
to: ctx.interopCenter,
|
|
2072
|
+
data,
|
|
2073
|
+
from: ctx.sender,
|
|
2074
|
+
value: bundle.value
|
|
2075
|
+
};
|
|
2076
|
+
const sendGas = allowanceSufficient ? await quoteL2Gas4({ ctx, tx: sendTx }) : void 0;
|
|
2077
|
+
if (sendGas) {
|
|
2078
|
+
sendTx.gasLimit = sendGas.gasLimit;
|
|
2079
|
+
sendTx.maxFeePerGas = sendGas.maxFeePerGas;
|
|
2080
|
+
sendTx.maxPriorityFeePerGas = sendGas.maxPriorityFeePerGas;
|
|
2081
|
+
}
|
|
2082
|
+
steps.push({
|
|
2083
|
+
key: "interop-center:send-bundle",
|
|
2084
|
+
kind: "interop-center:send-bundle",
|
|
2085
|
+
description: "Burn on L2 & send L2\u2192L1 withdrawal bundle",
|
|
2086
|
+
tx: sendTx
|
|
2087
|
+
});
|
|
2088
|
+
const fees = buildFeeBreakdown2({
|
|
2089
|
+
feeToken: ctx.baseTokenL1 ?? await ctx.client.baseToken(ctx.chainIdL2),
|
|
2090
|
+
l2Gas: sendGas
|
|
2091
|
+
});
|
|
2092
|
+
return { steps, approvals, fees };
|
|
2093
|
+
}
|
|
2094
|
+
};
|
|
2095
|
+
}
|
|
2096
|
+
|
|
2097
|
+
// src/adapters/ethers/resources/withdrawals/services/protocol.ts
|
|
2098
|
+
function createWithdrawalProtocolService(client, override) {
|
|
2099
|
+
let cached;
|
|
2100
|
+
async function hasCodeAt(address) {
|
|
2101
|
+
try {
|
|
2102
|
+
const code = await client.l2.getCode(address);
|
|
2103
|
+
return !!code && code !== "0x";
|
|
2104
|
+
} catch {
|
|
2105
|
+
return false;
|
|
2106
|
+
}
|
|
2107
|
+
}
|
|
2108
|
+
async function protocolVersion() {
|
|
2109
|
+
try {
|
|
2110
|
+
return await client.getChainProtocolVersion();
|
|
2111
|
+
} catch {
|
|
2112
|
+
return void 0;
|
|
2113
|
+
}
|
|
2114
|
+
}
|
|
2115
|
+
function detect() {
|
|
2116
|
+
cached ??= detectWithdrawalProtocol(
|
|
2117
|
+
{
|
|
2118
|
+
protocolVersion,
|
|
2119
|
+
hasCodeAt
|
|
2120
|
+
},
|
|
2121
|
+
override
|
|
2122
|
+
).catch((e) => {
|
|
2123
|
+
cached = void 0;
|
|
2124
|
+
throw e;
|
|
2125
|
+
});
|
|
2126
|
+
return cached;
|
|
2127
|
+
}
|
|
2128
|
+
return {
|
|
2129
|
+
detect,
|
|
2130
|
+
async protocol() {
|
|
2131
|
+
return (await detect()).protocol;
|
|
2132
|
+
},
|
|
2133
|
+
refresh() {
|
|
2134
|
+
cached = void 0;
|
|
2135
|
+
}
|
|
2136
|
+
};
|
|
2137
|
+
}
|
|
2138
|
+
|
|
2139
|
+
// src/adapters/ethers/resources/withdrawals/services/finalization.ts
|
|
2140
|
+
var { wrapAs: wrapAs9 } = createErrorHandlers("withdrawals");
|
|
2141
|
+
var IL1NullifierMini = [
|
|
2142
|
+
"function isWithdrawalFinalized(uint256,uint256,uint256) view returns (bool)"
|
|
2143
|
+
];
|
|
2144
|
+
function createFinalizationServices(client, protocolService = createWithdrawalProtocolService(client)) {
|
|
2145
|
+
const { l1, l2, signer } = client;
|
|
2146
|
+
async function finalizationTarget(protocol) {
|
|
2147
|
+
const { l1Nullifier } = await client.ensureAddresses();
|
|
2148
|
+
if (protocol === "legacy-withdrawal") return l1Nullifier;
|
|
2149
|
+
const nullifier = new Contract(l1Nullifier, IL1NullifierV32_default, l1);
|
|
2150
|
+
const handler = await wrapAs9(
|
|
2151
|
+
"CONTRACT",
|
|
2152
|
+
OP_WITHDRAWALS.finalize.fetchParams.receipt,
|
|
2153
|
+
() => nullifier.l1InteropHandler(),
|
|
2154
|
+
{
|
|
2155
|
+
ctx: { where: "L1Nullifier.l1InteropHandler", l1Nullifier },
|
|
2156
|
+
message: "Failed to resolve the L1 interop handler. The chain reports protocol v32+, but its L1Nullifier does not expose `l1InteropHandler()`."
|
|
2157
|
+
}
|
|
2158
|
+
);
|
|
2159
|
+
return handler;
|
|
2160
|
+
}
|
|
2161
|
+
async function fetchMessageAndProof(l2TxHash) {
|
|
2162
|
+
const raw = await wrapAs9(
|
|
2163
|
+
"RPC",
|
|
2164
|
+
OP_WITHDRAWALS.finalize.fetchParams.receipt,
|
|
2165
|
+
() => client.zks.getReceiptWithL2ToL1(l2TxHash),
|
|
2166
|
+
{
|
|
2167
|
+
ctx: { where: "getReceiptWithL2ToL1", l2TxHash },
|
|
2168
|
+
message: "Failed to fetch L2 receipt (with L2\u2192L1 logs)."
|
|
2169
|
+
}
|
|
2170
|
+
);
|
|
2171
|
+
if (!raw) {
|
|
2172
|
+
throw createError("STATE", {
|
|
2173
|
+
resource: "withdrawals",
|
|
2174
|
+
operation: OP_WITHDRAWALS.finalize.fetchParams.receipt,
|
|
2175
|
+
message: "L2 receipt not found.",
|
|
2176
|
+
context: { l2TxHash }
|
|
2177
|
+
});
|
|
2178
|
+
}
|
|
2179
|
+
const ev = await wrapAs9(
|
|
2180
|
+
"INTERNAL",
|
|
2181
|
+
OP_WITHDRAWALS.finalize.fetchParams.findMessage,
|
|
2182
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
|
|
2183
|
+
() => Promise.resolve(findL1MessageSentLog(raw, { index: 0 })),
|
|
2184
|
+
{
|
|
2185
|
+
ctx: { l2TxHash, index: 0 },
|
|
2186
|
+
message: "Failed to locate L1MessageSent event in L2 receipt."
|
|
2187
|
+
}
|
|
2188
|
+
);
|
|
2189
|
+
const message = await wrapAs9(
|
|
2190
|
+
"INTERNAL",
|
|
2191
|
+
OP_WITHDRAWALS.finalize.fetchParams.decodeMessage,
|
|
2192
|
+
() => Promise.resolve(AbiCoder.defaultAbiCoder().decode(["bytes"], ev.data)[0]),
|
|
2193
|
+
{
|
|
2194
|
+
ctx: { where: "decode L1MessageSent", data: ev.data },
|
|
2195
|
+
message: "Failed to decode withdrawal message."
|
|
2196
|
+
}
|
|
2197
|
+
);
|
|
2198
|
+
const idx = await wrapAs9(
|
|
2199
|
+
"INTERNAL",
|
|
2200
|
+
OP_WITHDRAWALS.finalize.fetchParams.messengerIndex,
|
|
2201
|
+
() => Promise.resolve(messengerLogIndex(raw, { index: 0, messenger: L1_MESSENGER_ADDRESS })),
|
|
2202
|
+
{
|
|
2203
|
+
ctx: { where: "derive messenger log index", l2TxHash, receipt: raw },
|
|
2204
|
+
message: "Failed to derive messenger log index."
|
|
2205
|
+
}
|
|
2206
|
+
);
|
|
2207
|
+
const proof = await wrapAs9(
|
|
2208
|
+
"RPC",
|
|
2209
|
+
OP_WITHDRAWALS.finalize.fetchParams.proof,
|
|
2210
|
+
() => client.zks.getL2ToL1LogProof(l2TxHash, idx),
|
|
2211
|
+
{
|
|
2212
|
+
ctx: { where: "get L2\u2192L1 log proof", l2TxHash, messengerLogIndex: idx },
|
|
2213
|
+
message: "Failed to fetch L2\u2192L1 log proof."
|
|
2214
|
+
}
|
|
2215
|
+
);
|
|
2216
|
+
const { chainId } = await wrapAs9(
|
|
2217
|
+
"RPC",
|
|
2218
|
+
OP_WITHDRAWALS.finalize.fetchParams.network,
|
|
2219
|
+
() => l2.getNetwork(),
|
|
2220
|
+
{ ctx: { where: "l2.getNetwork" }, message: "Failed to read L2 network." }
|
|
2221
|
+
);
|
|
2222
|
+
const txNumberInBatch = Number(raw.transactionIndex ?? 0);
|
|
2223
|
+
const l2Sender = l1MessageSentSender(ev);
|
|
2224
|
+
return { raw, message, proof, chainId: BigInt(chainId), txNumberInBatch, l2Sender };
|
|
2225
|
+
}
|
|
2226
|
+
async function fetchFinalization(l2TxHash) {
|
|
2227
|
+
const protocol = await protocolService.protocol();
|
|
2228
|
+
const { raw, message, proof, chainId, txNumberInBatch, l2Sender } = await fetchMessageAndProof(l2TxHash);
|
|
2229
|
+
const target = await finalizationTarget(protocol);
|
|
2230
|
+
if (protocol === "legacy-withdrawal") {
|
|
2231
|
+
const params2 = {
|
|
2232
|
+
chainId,
|
|
2233
|
+
l2BatchNumber: proof.batchNumber,
|
|
2234
|
+
l2MessageIndex: proof.id,
|
|
2235
|
+
l2Sender,
|
|
2236
|
+
l2TxNumberInBatch: txNumberInBatch,
|
|
2237
|
+
message,
|
|
2238
|
+
merkleProof: proof.proof
|
|
2239
|
+
};
|
|
2240
|
+
return {
|
|
2241
|
+
target,
|
|
2242
|
+
finalization: { protocol, params: params2 },
|
|
2243
|
+
key: {
|
|
2244
|
+
chainIdL2: chainId,
|
|
2245
|
+
l2BatchNumber: proof.batchNumber,
|
|
2246
|
+
l2MessageIndex: proof.id
|
|
1880
2247
|
}
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
2248
|
+
};
|
|
2249
|
+
}
|
|
2250
|
+
const { interopCenter } = await client.ensureAddresses();
|
|
2251
|
+
const params = buildWithdrawalFinalization({
|
|
2252
|
+
messageData: message,
|
|
2253
|
+
sourceChainId: chainId,
|
|
2254
|
+
txNumberInBatch,
|
|
2255
|
+
proof: { batchNumber: proof.batchNumber, id: proof.id, proof: proof.proof },
|
|
2256
|
+
// Prefer the hash the InteropCenter emitted over recomputing it.
|
|
2257
|
+
bundleHash: parseBundleHashFromLogs(raw.logs ?? [], interopCenter),
|
|
2258
|
+
interopCenter
|
|
2259
|
+
});
|
|
2260
|
+
return {
|
|
2261
|
+
target,
|
|
2262
|
+
finalization: { protocol, params },
|
|
2263
|
+
key: {
|
|
2264
|
+
chainIdL2: chainId,
|
|
2265
|
+
l2BatchNumber: proof.batchNumber,
|
|
2266
|
+
l2MessageIndex: proof.id,
|
|
2267
|
+
bundleHash: params.bundleHash
|
|
1889
2268
|
}
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
2269
|
+
};
|
|
2270
|
+
}
|
|
2271
|
+
async function withdrawalOutcome(finalization) {
|
|
2272
|
+
const target = await finalizationTarget(finalization.protocol);
|
|
2273
|
+
if (finalization.protocol === "legacy-withdrawal") {
|
|
2274
|
+
const c = new Contract(target, IL1NullifierMini, l1);
|
|
2275
|
+
const done = await wrapAs9(
|
|
2276
|
+
"RPC",
|
|
2277
|
+
OP_WITHDRAWALS.finalize.isFinalized,
|
|
2278
|
+
() => c.isWithdrawalFinalized(
|
|
2279
|
+
finalization.params.chainId,
|
|
2280
|
+
finalization.params.l2BatchNumber,
|
|
2281
|
+
finalization.params.l2MessageIndex
|
|
2282
|
+
),
|
|
1894
2283
|
{
|
|
1895
|
-
ctx: { where: "
|
|
1896
|
-
message: "Failed to
|
|
2284
|
+
ctx: { where: "isWithdrawalFinalized", params: finalization.params },
|
|
2285
|
+
message: "Failed to read finalization status."
|
|
1897
2286
|
}
|
|
1898
2287
|
);
|
|
1899
|
-
|
|
2288
|
+
return done ? "finalized" : "pending";
|
|
2289
|
+
}
|
|
2290
|
+
const { bundleHash } = finalization.params;
|
|
2291
|
+
const handler = new Contract(target, IInteropHandler_default, l1);
|
|
2292
|
+
const status = Number(
|
|
2293
|
+
await wrapAs9(
|
|
1900
2294
|
"RPC",
|
|
1901
|
-
OP_WITHDRAWALS.finalize.
|
|
1902
|
-
() =>
|
|
2295
|
+
OP_WITHDRAWALS.finalize.isFinalized,
|
|
2296
|
+
() => handler.bundleStatus(bundleHash),
|
|
1903
2297
|
{
|
|
1904
|
-
ctx: { where: "
|
|
1905
|
-
message: "Failed to
|
|
2298
|
+
ctx: { where: "L1InteropHandler.bundleStatus", bundleHash },
|
|
2299
|
+
message: "Failed to read bundle status."
|
|
1906
2300
|
}
|
|
1907
|
-
)
|
|
1908
|
-
|
|
2301
|
+
)
|
|
2302
|
+
);
|
|
2303
|
+
if (status !== 3 /* Unbundled */) return classifyWithdrawalOutcome(status);
|
|
2304
|
+
const callStatus = Number(
|
|
2305
|
+
await wrapAs9(
|
|
1909
2306
|
"RPC",
|
|
1910
|
-
OP_WITHDRAWALS.finalize.
|
|
1911
|
-
() =>
|
|
2307
|
+
OP_WITHDRAWALS.finalize.isFinalized,
|
|
2308
|
+
() => handler.callStatus(bundleHash, 0),
|
|
1912
2309
|
{
|
|
1913
|
-
ctx: { where: "
|
|
1914
|
-
message: "Failed to read
|
|
2310
|
+
ctx: { where: "L1InteropHandler.callStatus", bundleHash },
|
|
2311
|
+
message: "Failed to read bundle call status."
|
|
1915
2312
|
}
|
|
1916
|
-
)
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
2313
|
+
)
|
|
2314
|
+
);
|
|
2315
|
+
return classifyWithdrawalOutcome(status, callStatus);
|
|
2316
|
+
}
|
|
2317
|
+
async function isWithdrawalFinalized(finalization) {
|
|
2318
|
+
return await withdrawalOutcome(finalization) === "finalized";
|
|
2319
|
+
}
|
|
2320
|
+
return {
|
|
2321
|
+
fetchFinalization,
|
|
2322
|
+
isWithdrawalFinalized,
|
|
2323
|
+
withdrawalOutcome,
|
|
2324
|
+
async fetchFinalizeDepositParams(l2TxHash) {
|
|
2325
|
+
const resolved = await fetchFinalization(l2TxHash);
|
|
2326
|
+
if (resolved.finalization.protocol !== "legacy-withdrawal") {
|
|
2327
|
+
throw createError("VALIDATION", {
|
|
2328
|
+
resource: "withdrawals",
|
|
2329
|
+
operation: OP_WITHDRAWALS.finalize.fetchParams.receipt,
|
|
2330
|
+
message: "This chain finalizes withdrawals through the L1 interop handler; `finalizeDeposit` params do not exist. Use `fetchFinalization` instead.",
|
|
2331
|
+
context: { l2TxHash, protocol: resolved.finalization.protocol }
|
|
2332
|
+
});
|
|
2333
|
+
}
|
|
2334
|
+
return { params: resolved.finalization.params, nullifier: resolved.target };
|
|
1929
2335
|
},
|
|
1930
|
-
async simulateFinalizeReadiness(
|
|
1931
|
-
const
|
|
2336
|
+
async simulateFinalizeReadiness(finalization) {
|
|
2337
|
+
const target = await finalizationTarget(finalization.protocol);
|
|
1932
2338
|
const done = await (async () => {
|
|
1933
2339
|
try {
|
|
1934
|
-
|
|
1935
|
-
const isFinalized = await wrapAs8(
|
|
1936
|
-
"RPC",
|
|
1937
|
-
OP_WITHDRAWALS.finalize.readiness.isFinalized,
|
|
1938
|
-
() => cMini.isWithdrawalFinalized(
|
|
1939
|
-
params.chainId,
|
|
1940
|
-
params.l2BatchNumber,
|
|
1941
|
-
params.l2MessageIndex
|
|
1942
|
-
),
|
|
1943
|
-
{
|
|
1944
|
-
ctx: { where: "isWithdrawalFinalized", params },
|
|
1945
|
-
message: "Failed to read finalization status."
|
|
1946
|
-
}
|
|
1947
|
-
);
|
|
1948
|
-
return Boolean(isFinalized);
|
|
2340
|
+
return await isWithdrawalFinalized(finalization);
|
|
1949
2341
|
} catch {
|
|
1950
2342
|
return false;
|
|
1951
2343
|
}
|
|
1952
2344
|
})();
|
|
1953
2345
|
if (done) return { kind: "FINALIZED" };
|
|
1954
|
-
const c = new Contract(l1Nullifier, IL1Nullifier_default, l1);
|
|
1955
2346
|
try {
|
|
1956
|
-
|
|
2347
|
+
if (finalization.protocol === "legacy-withdrawal") {
|
|
2348
|
+
const c = new Contract(target, IL1Nullifier_default, l1);
|
|
2349
|
+
await c.finalizeDeposit.staticCall(finalization.params);
|
|
2350
|
+
} else {
|
|
2351
|
+
const handler = new Contract(target, IInteropHandler_default, l1);
|
|
2352
|
+
await handler.executeBundle.staticCall(
|
|
2353
|
+
finalization.params.bundle,
|
|
2354
|
+
finalization.params.proof
|
|
2355
|
+
);
|
|
2356
|
+
}
|
|
1957
2357
|
return { kind: "READY" };
|
|
1958
2358
|
} catch (e) {
|
|
1959
2359
|
return classifyReadinessFromRevert(e);
|
|
1960
2360
|
}
|
|
1961
2361
|
},
|
|
1962
|
-
async
|
|
1963
|
-
const
|
|
1964
|
-
const
|
|
1965
|
-
|
|
1966
|
-
"RPC",
|
|
1967
|
-
OP_WITHDRAWALS.finalize.isFinalized,
|
|
1968
|
-
() => c.isWithdrawalFinalized(key.chainIdL2, key.l2BatchNumber, key.l2MessageIndex),
|
|
1969
|
-
{
|
|
1970
|
-
ctx: { where: "isWithdrawalFinalized", key },
|
|
1971
|
-
message: "Failed to read finalization status."
|
|
1972
|
-
}
|
|
1973
|
-
);
|
|
1974
|
-
},
|
|
1975
|
-
async estimateFinalization(params) {
|
|
1976
|
-
const { l1Nullifier } = await client.ensureAddresses();
|
|
1977
|
-
const signer2 = client.getL1Signer();
|
|
1978
|
-
const c = new Contract(l1Nullifier, IL1Nullifier_default, signer2);
|
|
1979
|
-
const gasLimit = await wrapAs8(
|
|
2362
|
+
async estimateFinalization(finalization) {
|
|
2363
|
+
const target = await finalizationTarget(finalization.protocol);
|
|
2364
|
+
const l1Signer = client.getL1Signer();
|
|
2365
|
+
const gasLimit = await wrapAs9(
|
|
1980
2366
|
"RPC",
|
|
1981
2367
|
OP_WITHDRAWALS.finalize.estimate,
|
|
1982
|
-
() =>
|
|
2368
|
+
() => {
|
|
2369
|
+
if (finalization.protocol === "legacy-withdrawal") {
|
|
2370
|
+
const c = new Contract(target, IL1Nullifier_default, l1Signer);
|
|
2371
|
+
return c.finalizeDeposit.estimateGas(finalization.params);
|
|
2372
|
+
}
|
|
2373
|
+
const handler = new Contract(target, IInteropHandler_default, l1Signer);
|
|
2374
|
+
return handler.executeBundle.estimateGas(
|
|
2375
|
+
finalization.params.bundle,
|
|
2376
|
+
finalization.params.proof
|
|
2377
|
+
);
|
|
2378
|
+
},
|
|
1983
2379
|
{
|
|
1984
|
-
ctx: {
|
|
1985
|
-
|
|
1986
|
-
chainIdL2: params.chainId,
|
|
1987
|
-
l2BatchNumber: params.l2BatchNumber,
|
|
1988
|
-
l2MessageIndex: params.l2MessageIndex,
|
|
1989
|
-
l1Nullifier
|
|
1990
|
-
},
|
|
1991
|
-
message: "Failed to estimate gas for finalizeDeposit."
|
|
2380
|
+
ctx: { where: "estimateGas(finalize)", protocol: finalization.protocol, target },
|
|
2381
|
+
message: "Failed to estimate gas for withdrawal finalization."
|
|
1992
2382
|
}
|
|
1993
2383
|
);
|
|
1994
|
-
const feeData = await
|
|
2384
|
+
const feeData = await wrapAs9("RPC", OP_WITHDRAWALS.finalize.estimate, () => l1.getFeeData(), {
|
|
1995
2385
|
ctx: { where: "l1.getFeeData" },
|
|
1996
|
-
message: "Failed to estimate fee data for
|
|
2386
|
+
message: "Failed to estimate fee data for withdrawal finalization."
|
|
1997
2387
|
});
|
|
1998
2388
|
const maxFeePerGas = feeData.maxFeePerGas ?? feeData.gasPrice ?? // legacy-style gas price if present
|
|
1999
2389
|
(() => {
|
|
@@ -2004,33 +2394,49 @@ function createFinalizationServices(client) {
|
|
|
2004
2394
|
context: { feeData }
|
|
2005
2395
|
});
|
|
2006
2396
|
})();
|
|
2007
|
-
const maxPriorityFeePerGas = feeData.maxPriorityFeePerGas ?? 0n;
|
|
2008
2397
|
return {
|
|
2009
2398
|
gasLimit,
|
|
2010
2399
|
maxFeePerGas,
|
|
2011
|
-
maxPriorityFeePerGas
|
|
2400
|
+
maxPriorityFeePerGas: feeData.maxPriorityFeePerGas ?? 0n
|
|
2012
2401
|
};
|
|
2013
2402
|
},
|
|
2014
|
-
async
|
|
2015
|
-
const
|
|
2016
|
-
const c = new Contract(l1Nullifier, IL1Nullifier_default, signer);
|
|
2403
|
+
async finalize(finalization) {
|
|
2404
|
+
const target = await finalizationTarget(finalization.protocol);
|
|
2017
2405
|
try {
|
|
2018
|
-
const
|
|
2019
|
-
|
|
2406
|
+
const sent = await (async () => {
|
|
2407
|
+
if (finalization.protocol === "legacy-withdrawal") {
|
|
2408
|
+
const c = new Contract(target, IL1Nullifier_default, signer);
|
|
2409
|
+
return await c.finalizeDeposit(finalization.params);
|
|
2410
|
+
}
|
|
2411
|
+
const handler = new Contract(target, IInteropHandler_default, signer);
|
|
2412
|
+
return await handler.executeBundle(
|
|
2413
|
+
finalization.params.bundle,
|
|
2414
|
+
finalization.params.proof
|
|
2415
|
+
);
|
|
2416
|
+
})();
|
|
2417
|
+
const hash = sent.hash;
|
|
2020
2418
|
return {
|
|
2021
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
2022
2419
|
hash,
|
|
2023
2420
|
wait: async () => {
|
|
2024
2421
|
try {
|
|
2025
|
-
|
|
2422
|
+
const receipt = await sent.wait();
|
|
2423
|
+
if (!receipt) {
|
|
2424
|
+
throw createError("EXECUTION", {
|
|
2425
|
+
resource: "withdrawals",
|
|
2426
|
+
operation: OP_WITHDRAWALS.finalize.wait,
|
|
2427
|
+
message: "Withdrawal finalization transaction produced no receipt.",
|
|
2428
|
+
context: { txHash: hash, protocol: finalization.protocol }
|
|
2429
|
+
});
|
|
2430
|
+
}
|
|
2431
|
+
return receipt;
|
|
2026
2432
|
} catch (e) {
|
|
2027
2433
|
throw toZKsyncError(
|
|
2028
2434
|
"EXECUTION",
|
|
2029
2435
|
{
|
|
2030
2436
|
resource: "withdrawals",
|
|
2031
2437
|
operation: OP_WITHDRAWALS.finalize.wait,
|
|
2032
|
-
message: "Failed while waiting for
|
|
2033
|
-
context: { txHash: hash }
|
|
2438
|
+
message: "Failed while waiting for the withdrawal finalization transaction.",
|
|
2439
|
+
context: { txHash: hash, protocol: finalization.protocol }
|
|
2034
2440
|
},
|
|
2035
2441
|
e
|
|
2036
2442
|
);
|
|
@@ -2043,13 +2449,8 @@ function createFinalizationServices(client) {
|
|
|
2043
2449
|
{
|
|
2044
2450
|
resource: "withdrawals",
|
|
2045
2451
|
operation: OP_WITHDRAWALS.finalize.send,
|
|
2046
|
-
message: "Failed to send
|
|
2047
|
-
context: {
|
|
2048
|
-
chainIdL2: params.chainId,
|
|
2049
|
-
l2BatchNumber: params.l2BatchNumber,
|
|
2050
|
-
l2MessageIndex: params.l2MessageIndex,
|
|
2051
|
-
l1Nullifier
|
|
2052
|
-
}
|
|
2452
|
+
message: "Failed to send the withdrawal finalization transaction.",
|
|
2453
|
+
context: { protocol: finalization.protocol, target }
|
|
2053
2454
|
},
|
|
2054
2455
|
e
|
|
2055
2456
|
);
|
|
@@ -2065,15 +2466,28 @@ var ROUTES2 = {
|
|
|
2065
2466
|
"erc20-nonbase": routeErc20NonBase2()
|
|
2066
2467
|
// AssetRouter.withdraw for non-base ERC-20s
|
|
2067
2468
|
};
|
|
2068
|
-
|
|
2069
|
-
|
|
2469
|
+
var INTEROP_ROUTES = {
|
|
2470
|
+
base: routeInteropBundle(),
|
|
2471
|
+
"erc20-nonbase": routeInteropBundle()
|
|
2472
|
+
};
|
|
2473
|
+
function routeStrategy(protocol, route) {
|
|
2474
|
+
return protocol === "interop-bundle" ? INTEROP_ROUTES[route] : ROUTES2[route];
|
|
2475
|
+
}
|
|
2476
|
+
function createWithdrawalsResource(client, tokens, contracts, options) {
|
|
2477
|
+
const protocolService = createWithdrawalProtocolService(
|
|
2478
|
+
client,
|
|
2479
|
+
options?.protocol
|
|
2480
|
+
);
|
|
2481
|
+
const svc = createFinalizationServices(client, protocolService);
|
|
2070
2482
|
const { wrap: wrap7, toResult: toResult3 } = createErrorHandlers("withdrawals");
|
|
2071
2483
|
const tokensResource = tokens ?? createTokensResource(client);
|
|
2072
2484
|
const contractsResource = contracts ?? createContractsResource(client);
|
|
2073
2485
|
async function buildPlan(p) {
|
|
2074
|
-
const
|
|
2075
|
-
await
|
|
2076
|
-
const
|
|
2486
|
+
const protocol = await protocolService.protocol();
|
|
2487
|
+
const ctx = await commonCtx2(p, client, tokensResource, contractsResource, protocol);
|
|
2488
|
+
const strategy = routeStrategy(protocol, ctx.route);
|
|
2489
|
+
await strategy.preflight?.(p, ctx);
|
|
2490
|
+
const { steps, approvals, fees } = await strategy.build(p, ctx);
|
|
2077
2491
|
return {
|
|
2078
2492
|
route: ctx.route,
|
|
2079
2493
|
summary: {
|
|
@@ -2217,21 +2631,20 @@ function createWithdrawalsResource(client, tokens, contracts) {
|
|
|
2217
2631
|
if (!l2Rcpt) return { phase: "L2_PENDING", l2TxHash };
|
|
2218
2632
|
let pack;
|
|
2219
2633
|
try {
|
|
2220
|
-
pack = await svc.
|
|
2634
|
+
pack = await svc.fetchFinalization(l2TxHash);
|
|
2221
2635
|
} catch {
|
|
2222
2636
|
return { phase: "PENDING", l2TxHash };
|
|
2223
2637
|
}
|
|
2224
|
-
const key =
|
|
2225
|
-
chainIdL2: pack.params.chainId,
|
|
2226
|
-
l2BatchNumber: pack.params.l2BatchNumber,
|
|
2227
|
-
l2MessageIndex: pack.params.l2MessageIndex
|
|
2228
|
-
};
|
|
2638
|
+
const key = pack.key;
|
|
2229
2639
|
try {
|
|
2230
|
-
const
|
|
2231
|
-
if (
|
|
2640
|
+
const outcome = await svc.withdrawalOutcome(pack.finalization);
|
|
2641
|
+
if (outcome === "finalized") return { phase: "FINALIZED", l2TxHash, key };
|
|
2642
|
+
if (outcome === "failed") {
|
|
2643
|
+
return { phase: "UNFINALIZABLE", l2TxHash, key, reason: "bundle-cancelled" };
|
|
2644
|
+
}
|
|
2232
2645
|
} catch {
|
|
2233
2646
|
}
|
|
2234
|
-
const readiness = await svc.simulateFinalizeReadiness(pack.
|
|
2647
|
+
const readiness = await svc.simulateFinalizeReadiness(pack.finalization);
|
|
2235
2648
|
const { phase, reason } = phaseFromReadiness(readiness);
|
|
2236
2649
|
return { phase, reason, l2TxHash, key };
|
|
2237
2650
|
},
|
|
@@ -2321,7 +2734,7 @@ function createWithdrawalsResource(client, tokens, contracts) {
|
|
|
2321
2734
|
async () => {
|
|
2322
2735
|
const pack = await (async () => {
|
|
2323
2736
|
try {
|
|
2324
|
-
return await svc.
|
|
2737
|
+
return await svc.fetchFinalization(l2TxHash);
|
|
2325
2738
|
} catch (e) {
|
|
2326
2739
|
throw createError("STATE", {
|
|
2327
2740
|
resource: "withdrawals",
|
|
@@ -2332,21 +2745,16 @@ function createWithdrawalsResource(client, tokens, contracts) {
|
|
|
2332
2745
|
});
|
|
2333
2746
|
}
|
|
2334
2747
|
})();
|
|
2335
|
-
const {
|
|
2336
|
-
const key = {
|
|
2337
|
-
chainIdL2: params.chainId,
|
|
2338
|
-
l2BatchNumber: params.l2BatchNumber,
|
|
2339
|
-
l2MessageIndex: params.l2MessageIndex
|
|
2340
|
-
};
|
|
2748
|
+
const { finalization } = pack;
|
|
2341
2749
|
try {
|
|
2342
|
-
const
|
|
2343
|
-
if (
|
|
2750
|
+
const outcome = await svc.withdrawalOutcome(finalization);
|
|
2751
|
+
if (outcome === "finalized" || outcome === "failed") {
|
|
2344
2752
|
const statusNow = await status(l2TxHash);
|
|
2345
2753
|
return { status: statusNow };
|
|
2346
2754
|
}
|
|
2347
2755
|
} catch {
|
|
2348
2756
|
}
|
|
2349
|
-
const readiness = await svc.simulateFinalizeReadiness(
|
|
2757
|
+
const readiness = await svc.simulateFinalizeReadiness(finalization);
|
|
2350
2758
|
if (readiness.kind === "FINALIZED") {
|
|
2351
2759
|
const statusNow = await status(l2TxHash);
|
|
2352
2760
|
return { status: statusNow };
|
|
@@ -2360,7 +2768,7 @@ function createWithdrawalsResource(client, tokens, contracts) {
|
|
|
2360
2768
|
});
|
|
2361
2769
|
}
|
|
2362
2770
|
try {
|
|
2363
|
-
const tx = await svc.
|
|
2771
|
+
const tx = await svc.finalize(finalization);
|
|
2364
2772
|
finalizeCache.set(l2TxHash, tx.hash);
|
|
2365
2773
|
const rcpt = await tx.wait();
|
|
2366
2774
|
const statusNow = await status(l2TxHash);
|
|
@@ -2369,7 +2777,7 @@ function createWithdrawalsResource(client, tokens, contracts) {
|
|
|
2369
2777
|
const statusNow = await status(l2TxHash);
|
|
2370
2778
|
if (statusNow.phase === "FINALIZED") return { status: statusNow };
|
|
2371
2779
|
try {
|
|
2372
|
-
const again = await svc.simulateFinalizeReadiness(
|
|
2780
|
+
const again = await svc.simulateFinalizeReadiness(finalization);
|
|
2373
2781
|
if (again.kind === "NOT_READY") {
|
|
2374
2782
|
throw createError("STATE", {
|
|
2375
2783
|
resource: "withdrawals",
|
|
@@ -2427,60 +2835,6 @@ function createWithdrawalsResource(client, tokens, contracts) {
|
|
|
2427
2835
|
tryWait
|
|
2428
2836
|
};
|
|
2429
2837
|
}
|
|
2430
|
-
function getInteropAttributes(params, ctx) {
|
|
2431
|
-
const bundleAttributes = [];
|
|
2432
|
-
if (params.execution?.only) {
|
|
2433
|
-
bundleAttributes.push(ctx.attributes.bundle.executionAddress(params.execution.only));
|
|
2434
|
-
}
|
|
2435
|
-
if (params.unbundling?.by) {
|
|
2436
|
-
bundleAttributes.push(ctx.attributes.bundle.unbundlerAddress(params.unbundling.by));
|
|
2437
|
-
}
|
|
2438
|
-
bundleAttributes.push(ctx.attributes.bundle.useFixedFee(params.fee?.useFixed ?? false));
|
|
2439
|
-
const callAttributes = params.actions.map((action) => {
|
|
2440
|
-
switch (action.type) {
|
|
2441
|
-
case "sendNative": {
|
|
2442
|
-
const baseMatches = ctx.baseTokens.src.toLowerCase() === ctx.baseTokens.dst.toLowerCase();
|
|
2443
|
-
if (baseMatches) {
|
|
2444
|
-
return [ctx.attributes.call.interopCallValue(action.amount)];
|
|
2445
|
-
}
|
|
2446
|
-
return [ctx.attributes.call.indirectCall(action.amount)];
|
|
2447
|
-
}
|
|
2448
|
-
case "call":
|
|
2449
|
-
if (action.value && action.value > 0n) {
|
|
2450
|
-
return [ctx.attributes.call.interopCallValue(action.value)];
|
|
2451
|
-
}
|
|
2452
|
-
return [];
|
|
2453
|
-
case "sendErc20":
|
|
2454
|
-
return [ctx.attributes.call.indirectCall(0n)];
|
|
2455
|
-
default:
|
|
2456
|
-
assertNever(action);
|
|
2457
|
-
}
|
|
2458
|
-
});
|
|
2459
|
-
return { bundleAttributes, callAttributes };
|
|
2460
|
-
}
|
|
2461
|
-
function createEthersAttributesResource(opts = {}) {
|
|
2462
|
-
const iface = opts.iface ?? new Interface(IERC7786Attributes_default);
|
|
2463
|
-
const encode2 = (fn, args) => iface.encodeFunctionData(fn, args);
|
|
2464
|
-
return createAttributesResource({ encode: encode2 });
|
|
2465
|
-
}
|
|
2466
|
-
var PREFIX_EVM_CHAIN = getBytes("0x00010000");
|
|
2467
|
-
var PREFIX_EVM_ADDRESS = getBytes("0x000100000014");
|
|
2468
|
-
function formatInteropEvmChain(chainId) {
|
|
2469
|
-
const chainRef = toBeArray(chainId);
|
|
2470
|
-
const chainRefLength = getBytes(toBeHex(chainRef.length, 1));
|
|
2471
|
-
const payload = concat([PREFIX_EVM_CHAIN, chainRefLength, chainRef, new Uint8Array([0])]);
|
|
2472
|
-
return hexlify(payload);
|
|
2473
|
-
}
|
|
2474
|
-
function formatInteropEvmAddress(address) {
|
|
2475
|
-
const normalized = getAddress(address);
|
|
2476
|
-
const addrBytes = getBytes(normalized);
|
|
2477
|
-
const payload = concat([PREFIX_EVM_ADDRESS, addrBytes]);
|
|
2478
|
-
return hexlify(payload);
|
|
2479
|
-
}
|
|
2480
|
-
var interopCodec = {
|
|
2481
|
-
formatChain: formatInteropEvmChain,
|
|
2482
|
-
formatAddress: formatInteropEvmAddress
|
|
2483
|
-
};
|
|
2484
2838
|
function getErc20Tokens(params) {
|
|
2485
2839
|
const erc20Tokens = /* @__PURE__ */ new Map();
|
|
2486
2840
|
for (const action of params.actions) {
|
|
@@ -2638,7 +2992,7 @@ function routeIndirect() {
|
|
|
2638
2992
|
resolveErc20AssetIds(erc20Tokens, ctx),
|
|
2639
2993
|
buildFeeInfo(params, ctx, params.actions.length)
|
|
2640
2994
|
]);
|
|
2641
|
-
const
|
|
2995
|
+
const attributes2 = getInteropAttributes(params, ctx);
|
|
2642
2996
|
const starterData = await getStarterData(params, ctx, erc20AssetIds);
|
|
2643
2997
|
const bundle = buildIndirectBundle(
|
|
2644
2998
|
params,
|
|
@@ -2649,7 +3003,7 @@ function routeIndirect() {
|
|
|
2649
3003
|
l2NativeTokenVault: ctx.l2NativeTokenVault,
|
|
2650
3004
|
codec: interopCodec
|
|
2651
3005
|
},
|
|
2652
|
-
|
|
3006
|
+
attributes2,
|
|
2653
3007
|
starterData,
|
|
2654
3008
|
feeInfo
|
|
2655
3009
|
);
|
|
@@ -2755,7 +3109,7 @@ async function assertInteropProtocolVersion(client, srcChainId, dstChainId) {
|
|
|
2755
3109
|
assertProtocolVersion(srcChainId, srcProtocolVersion);
|
|
2756
3110
|
assertProtocolVersion(dstChainId, dstProtocolVersion);
|
|
2757
3111
|
}
|
|
2758
|
-
async function commonCtx3(dstProvider, params, client, tokens, contracts,
|
|
3112
|
+
async function commonCtx3(dstProvider, params, client, tokens, contracts, attributes2) {
|
|
2759
3113
|
const sender = await client.signer.getAddress();
|
|
2760
3114
|
const chainId = (await client.l2.getNetwork()).chainId;
|
|
2761
3115
|
const dstChainId = (await dstProvider.getNetwork()).chainId;
|
|
@@ -2792,7 +3146,7 @@ async function commonCtx3(dstProvider, params, client, tokens, contracts, attrib
|
|
|
2792
3146
|
l2NativeTokenVault,
|
|
2793
3147
|
baseTokens: { src: srcBaseToken, dst: dstBaseToken, matches: baseMatches },
|
|
2794
3148
|
ifaces: { interopCenter: interopCenterIface, interopHandler: interopHandlerIface },
|
|
2795
|
-
attributes,
|
|
3149
|
+
attributes: attributes2,
|
|
2796
3150
|
gasOverrides: params.txOverrides ? toGasOverrides(params.txOverrides) : void 0
|
|
2797
3151
|
};
|
|
2798
3152
|
}
|
|
@@ -3316,7 +3670,7 @@ var ROUTES3 = {
|
|
|
3316
3670
|
direct: routeDirect(),
|
|
3317
3671
|
indirect: routeIndirect()
|
|
3318
3672
|
};
|
|
3319
|
-
function createInteropResource(client, config, tokens, contracts,
|
|
3673
|
+
function createInteropResource(client, config, tokens, contracts, attributes2) {
|
|
3320
3674
|
let gwProviderCache;
|
|
3321
3675
|
function requireConfig() {
|
|
3322
3676
|
if (!config)
|
|
@@ -3334,7 +3688,7 @@ function createInteropResource(client, config, tokens, contracts, attributes) {
|
|
|
3334
3688
|
const svc = createInteropFinalizationServices(client);
|
|
3335
3689
|
const tokensResource = tokens ?? createTokensResource(client);
|
|
3336
3690
|
const contractsResource = contracts ?? createContractsResource(client);
|
|
3337
|
-
const attributesResource =
|
|
3691
|
+
const attributesResource = attributes2 ?? createEthersAttributesResource();
|
|
3338
3692
|
async function buildPlanWithCtx(dstProvider, params) {
|
|
3339
3693
|
const ctx = await commonCtx3(
|
|
3340
3694
|
dstProvider,
|
|
@@ -3547,11 +3901,11 @@ function createEthersSdk(client, options) {
|
|
|
3547
3901
|
const interop = createInteropResource(client, options?.interop, tokens, contracts);
|
|
3548
3902
|
return {
|
|
3549
3903
|
deposits: createDepositsResource(client, tokens, contracts),
|
|
3550
|
-
withdrawals: createWithdrawalsResource(client, tokens, contracts),
|
|
3904
|
+
withdrawals: createWithdrawalsResource(client, tokens, contracts, options?.withdrawals),
|
|
3551
3905
|
tokens,
|
|
3552
3906
|
contracts,
|
|
3553
3907
|
interop
|
|
3554
3908
|
};
|
|
3555
3909
|
}
|
|
3556
3910
|
|
|
3557
|
-
export { buildDirectRequestStruct, createDepositsResource, createEthersSdk, createFinalizationServices, createInteropFinalizationServices, createInteropResource, createTokensResource, createWithdrawalsResource, encodeNativeTokenVaultTransferData, encodeSecondBridgeArgs, encodeSecondBridgeDataV1, encodeSecondBridgeErc20Args, encodeSecondBridgeEthArgs, getL2TransactionHashFromLogs };
|
|
3911
|
+
export { buildDirectRequestStruct, createDepositsResource, createEthersSdk, createFinalizationServices, createInteropFinalizationServices, createInteropResource, createTokensResource, createWithdrawalProtocolService, createWithdrawalsResource, encodeNativeTokenVaultTransferData, encodeSecondBridgeArgs, encodeSecondBridgeDataV1, encodeSecondBridgeErc20Args, encodeSecondBridgeEthArgs, getL2TransactionHashFromLogs };
|