Skip to content

Commit

Permalink
fix(adapter): fix support for custom bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
petarTxFusion authored and danijelTxFusion committed Jun 14, 2024
1 parent dd2e3b9 commit 82a54aa
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 27 deletions.
70 changes: 44 additions & 26 deletions src/adapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import {Il2Bridge as IL2Bridge} from './typechain/Il2Bridge';
import {IBridgehubFactory} from './typechain/IBridgehubFactory';
import {IBridgehub} from './typechain/IBridgehub';
import {Il1SharedBridge as IL1SharedBridge} from './typechain/Il1SharedBridge';
import {Il1SharedBridgeFactory as IL1SharedBridgeFactory} from './typechain/Il1SharedBridgeFactory';
import {
Il1SharedBridgeFactory,
Il1SharedBridgeFactory as IL1SharedBridgeFactory,
} from './typechain/Il1SharedBridgeFactory';
import {INonceHolderFactory} from './typechain/INonceHolderFactory';
import {IZkSyncHyperchainFactory} from './typechain/IZkSyncHyperchainFactory';
import {IZkSyncHyperchain} from './typechain/IZkSyncHyperchain';
Expand Down Expand Up @@ -42,9 +45,13 @@ import {
scaleGasLimit,
undoL1ToL2Alias,
isAddressEq,
L2_BASE_TOKEN_ADDRESS,
} from './utils';
import {Il2SharedBridgeFactory} from './typechain/Il2SharedBridgeFactory';
import {Il2SharedBridge} from './typechain/Il2SharedBridge';
import {Il1SharedBridge} from '../typechain/Il1SharedBridge';
import {Il1Bridge} from '../typechain/Il1Bridge';
import {Il1BridgeFactory} from './typechain/Il1BridgeFactory';

type Constructor<T = {}> = new (...args: any[]) => T;

Expand Down Expand Up @@ -829,7 +836,8 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
l2GasLimit: l2GasLimit,
l2GasPerPubdataByteLimit: gasPerPubdataByte,
refundRecipient: refundRecipient ?? ethers.constants.AddressZero,
secondBridgeAddress: bridgeContracts.shared.address,
secondBridgeAddress:
tx.bridgeAddress ?? bridgeContracts.shared.address,
secondBridgeValue: 0,
secondBridgeCalldata: ethers.utils.defaultAbiCoder.encode(
['address', 'uint256', 'address'],
Expand Down Expand Up @@ -941,7 +949,7 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
l2GasLimit: l2GasLimit,
l2GasPerPubdataByteLimit: gasPerPubdataByte,
refundRecipient: refundRecipient ?? ethers.constants.AddressZero,
secondBridgeAddress: sharedBridge,
secondBridgeAddress: tx.bridgeAddress ?? sharedBridge,
secondBridgeValue: amount,
secondBridgeCalldata: ethers.utils.defaultAbiCoder.encode(
['address', 'uint256', 'address'],
Expand Down Expand Up @@ -995,22 +1003,12 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
overrides.value ??= mintValue;
await checkBaseCost(baseCost, mintValue);

let secondBridgeAddress: string;
let secondBridgeCalldata: BytesLike;
if (tx.bridgeAddress) {
secondBridgeAddress = tx.bridgeAddress;
secondBridgeCalldata = await getERC20DefaultBridgeData(
transaction.token,
this._providerL1()
);
} else {
secondBridgeAddress = (await this.getL1BridgeContracts()).shared
.address;
secondBridgeCalldata = ethers.utils.defaultAbiCoder.encode(
['address', 'uint256', 'address'],
[token, amount, to]
);
}
const secondBridgeAddress =
tx.bridgeAddress ?? (await this.getL1BridgeContracts()).shared.address;
const secondBridgeCalldata = ethers.utils.defaultAbiCoder.encode(
['address', 'uint256', 'address'],
[token, amount, to]
);

return await bridgehub.populateTransaction.requestL2TransactionTwoBridges(
{
Expand Down Expand Up @@ -1420,15 +1418,35 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
index = 0,
overrides?: ethers.Overrides
): Promise<ethers.ContractTransaction> {
const {l1BatchNumber, l2MessageIndex, l2TxNumberInBlock, message, proof} =
await this.finalizeWithdrawalParams(withdrawalHash, index);
const {
l1BatchNumber,
l2MessageIndex,
l2TxNumberInBlock,
message,
sender,
proof,
} = await this.finalizeWithdrawalParams(withdrawalHash, index);

const l1SharedBridge = IL1SharedBridgeFactory.connect(
(await this.getL1BridgeContracts()).shared.address,
this._signerL1()
);
let l1Bridge: Il1SharedBridge | Il1Bridge;
if (isAddressEq(sender, L2_BASE_TOKEN_ADDRESS)) {
l1Bridge = (await this.getL1BridgeContracts()).shared;
} else if (!(await this._providerL2().isL2BridgeLegacy(sender))) {
const l2Bridge = Il2SharedBridgeFactory.connect(
sender,
this._providerL2()
);
const bridgeAddress = await l2Bridge.l1SharedBridge();
l1Bridge = Il1SharedBridgeFactory.connect(
bridgeAddress,
this._signerL1()
);
} else {
const l2Bridge = IL2BridgeFactory.connect(sender, this._providerL2());
const bridgeAddress = await l2Bridge.l1Bridge();
l1Bridge = Il1BridgeFactory.connect(bridgeAddress, this._signerL1());
}

return await l1SharedBridge.finalizeWithdrawal(
return await l1Bridge.finalizeWithdrawal(
(await this._providerL2().getNetwork()).chainId,
l1BatchNumber!,
l2MessageIndex,
Expand Down
2 changes: 1 addition & 1 deletion src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,7 @@ export class Provider extends ethers.providers.JsonRpcProvider {
* import { Provider, types, utils } from "zksync-ethers";
*
* const provider = Provider.getDefaultProvider(types.Network.Sepolia);
* const l2Bridge = await provider.isL2BridgeLegacy("0x36615Cf349d7F6344891B1e7CA7C72883F5dc049");
* const l2Bridge = await provider.connectL2Bridge("0x36615Cf349d7F6344891B1e7CA7C72883F5dc049");
*/
async connectL2Bridge(
address: Address
Expand Down

0 comments on commit 82a54aa

Please sign in to comment.