Skip to content

Commit

Permalink
fix: use isAddressEq when comparing addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
danijelTxFusion committed Apr 16, 2024
1 parent a2e9fb9 commit 75b4e83
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 63 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zksync-ethers",
"version": "5.8.0-beta.1",
"version": "5.8.0-beta.2",
"description": "A Web3 library for interacting with the ZkSync Layer 2 scaling solution.",
"author": {
"name": "Matter Labs",
Expand Down
97 changes: 61 additions & 36 deletions src/adapters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
REQUIRED_L1_TO_L2_GAS_PER_PUBDATA_LIMIT,
scaleGasLimit,
undoL1ToL2Alias,
isAddressEq,
} from './utils';

type Constructor<T = {}> = new (...args: any[]) => T;
Expand Down Expand Up @@ -220,7 +221,7 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
const isEthBasedChain = await this.isETHBasedChain();

if (!bridgeAddress) {
if (!isEthBasedChain && token === baseToken) {
if (!isEthBasedChain && isAddressEq(token, baseToken)) {
bridgeAddress = await (
await this.getBridgehubContract()
).sharedBridge();
Expand Down Expand Up @@ -275,19 +276,19 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
token: Address,
amount: BigNumberish
): Promise<{token: Address; allowance: BigNumberish}[]> {
if (token === LEGACY_ETH_ADDRESS) {
if (isAddressEq(token, LEGACY_ETH_ADDRESS)) {
token = ETH_ADDRESS_IN_CONTRACTS;
}
const baseTokenAddress = await this.getBaseToken();
const isEthBasedChain = await this.isETHBasedChain();

if (isEthBasedChain && token === LEGACY_ETH_ADDRESS) {
if (isEthBasedChain && isAddressEq(token, LEGACY_ETH_ADDRESS)) {
throw new Error(
"ETH token can't be approved! The address of the token does not exist on L1."
);
} else if (baseTokenAddress === ETH_ADDRESS_IN_CONTRACTS) {
} else if (isAddressEq(baseTokenAddress, ETH_ADDRESS_IN_CONTRACTS)) {
return [{token, allowance: amount}];
} else if (token === LEGACY_ETH_ADDRESS) {
} else if (isAddressEq(token, LEGACY_ETH_ADDRESS)) {
return [
{
token: baseTokenAddress,
Expand All @@ -296,7 +297,7 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
).mintValue,
},
];
} else if (token === baseTokenAddress) {
} else if (isAddressEq(token, baseTokenAddress)) {
return [
{
token: baseTokenAddress,
Expand Down Expand Up @@ -378,21 +379,27 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
approveBaseOverrides?: ethers.Overrides;
customBridgeData?: BytesLike;
}): Promise<PriorityOpResponse> {
if (transaction.token === LEGACY_ETH_ADDRESS) {
if (isAddressEq(transaction.token, LEGACY_ETH_ADDRESS)) {
transaction.token = ETH_ADDRESS_IN_CONTRACTS;
}
const bridgehub = await this.getBridgehubContract();
const chainId = (await this._providerL2().getNetwork()).chainId;
const baseTokenAddress = await bridgehub.baseToken(chainId);
const isEthBasedChain = baseTokenAddress === ETH_ADDRESS_IN_CONTRACTS;
const isEthBasedChain = isAddressEq(
baseTokenAddress,
ETH_ADDRESS_IN_CONTRACTS
);

if (isEthBasedChain && transaction.token === ETH_ADDRESS_IN_CONTRACTS) {
if (
isEthBasedChain &&
isAddressEq(transaction.token, ETH_ADDRESS_IN_CONTRACTS)
) {
return await this._depositETHToETHBasedChain(transaction);
} else if (baseTokenAddress === ETH_ADDRESS_IN_CONTRACTS) {
} else if (isAddressEq(baseTokenAddress, ETH_ADDRESS_IN_CONTRACTS)) {
return await this._depositTokenToETHBasedChain(transaction);
} else if (transaction.token === ETH_ADDRESS_IN_CONTRACTS) {
} else if (isAddressEq(transaction.token, ETH_ADDRESS_IN_CONTRACTS)) {
return await this._depositETHToNonETHBasedChain(transaction);
} else if (transaction.token === baseTokenAddress) {
} else if (isAddressEq(transaction.token, baseTokenAddress)) {
return await this._depositBaseTokenToNonETHBasedChain(transaction);
} else {
return await this._depositNonBaseTokenToNonETHBasedChain(transaction);
Expand Down Expand Up @@ -698,13 +705,13 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
refundRecipient?: Address;
overrides?: ethers.PayableOverrides;
}): Promise<BigNumber> {
if (transaction.token === LEGACY_ETH_ADDRESS) {
if (isAddressEq(transaction.token, LEGACY_ETH_ADDRESS)) {
transaction.token = ETH_ADDRESS_IN_CONTRACTS;
}
const tx = await this.getDepositTx(transaction);

let baseGasLimit: BigNumber;
if (tx.token === (await this.getBaseToken())) {
if (tx.token && isAddressEq(tx.token, await this.getBaseToken())) {
baseGasLimit = await this.estimateGasRequestExecute(tx);
} else {
baseGasLimit = await this._providerL1().estimateGas(tx);
Expand Down Expand Up @@ -744,21 +751,27 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
refundRecipient?: Address;
overrides?: ethers.PayableOverrides;
}): Promise<any> {
if (transaction.token === LEGACY_ETH_ADDRESS) {
if (isAddressEq(transaction.token, LEGACY_ETH_ADDRESS)) {
transaction.token = ETH_ADDRESS_IN_CONTRACTS;
}
const bridgehub = await this.getBridgehubContract();
const chainId = (await this._providerL2().getNetwork()).chainId;
const baseTokenAddress = await bridgehub.baseToken(chainId);
const isEthBasedChain = baseTokenAddress === ETH_ADDRESS_IN_CONTRACTS;
const isEthBasedChain = isAddressEq(
baseTokenAddress,
ETH_ADDRESS_IN_CONTRACTS
);

if (isEthBasedChain && transaction.token === ETH_ADDRESS_IN_CONTRACTS) {
if (
isEthBasedChain &&
isAddressEq(transaction.token, ETH_ADDRESS_IN_CONTRACTS)
) {
return await this._getDepositETHOnETHBasedChainTx(transaction);
} else if (isEthBasedChain) {
return await this._getDepositTokenOnETHBasedChainTx(transaction);
} else if (transaction.token === ETH_ADDRESS_IN_CONTRACTS) {
} else if (isAddressEq(transaction.token, ETH_ADDRESS_IN_CONTRACTS)) {
return (await this._getDepositETHOnNonETHBasedChainTx(transaction)).tx;
} else if (transaction.token === baseTokenAddress) {
} else if (isAddressEq(transaction.token, baseTokenAddress)) {
return (
await this._getDepositBaseTokenOnNonETHBasedChainTx(transaction)
).tx;
Expand Down Expand Up @@ -1198,15 +1211,21 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
gasPerPubdataByte?: BigNumberish;
overrides?: ethers.PayableOverrides;
}): Promise<FullDepositFee> {
if (transaction.token === LEGACY_ETH_ADDRESS) {
if (isAddressEq(transaction.token, LEGACY_ETH_ADDRESS)) {
transaction.token = ETH_ADDRESS_IN_CONTRACTS;
}
// It is assumed that the L2 fee for the transaction does not depend on its value.
const token = transaction.token.toLowerCase();
const dummyAmount = BigNumber.from(1);
const bridgehub = await this.getBridgehubContract();
const chainId = (await this._providerL2().getNetwork()).chainId;
const baseTokenAddress = await bridgehub.baseToken(chainId);
const isEthBasedChain = baseTokenAddress === ETH_ADDRESS_IN_CONTRACTS;
const baseTokenAddress = (
await bridgehub.baseToken(chainId)
).toLowerCase();
const isEthBasedChain = isAddressEq(
baseTokenAddress,
ETH_ADDRESS_IN_CONTRACTS
);

const tx = await this._getDepositTxWithDefaults({
...transaction,
Expand All @@ -1227,10 +1246,12 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
// the account needs to have a sufficient ETH balance.
const selfBalanceETH = await this.getBalanceL1();
if (baseCost.gte(selfBalanceETH.add(dummyAmount))) {
const recommendedL1GasLimit =
tx.token === ETH_ADDRESS_IN_CONTRACTS
? L1_RECOMMENDED_MIN_ETH_DEPOSIT_GAS_LIMIT
: L1_RECOMMENDED_MIN_ERC20_DEPOSIT_GAS_LIMIT;
const recommendedL1GasLimit = isAddressEq(
tx.token,
ETH_ADDRESS_IN_CONTRACTS
)
? L1_RECOMMENDED_MIN_ETH_DEPOSIT_GAS_LIMIT
: L1_RECOMMENDED_MIN_ERC20_DEPOSIT_GAS_LIMIT;
const recommendedETHBalance = BigNumber.from(recommendedL1GasLimit)
.mul(gasPriceForEstimation!)
.add(baseCost);
Expand All @@ -1243,7 +1264,7 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
}
// In case of token deposit, a sufficient token allowance is also required.
if (
tx.token !== ETH_ADDRESS_IN_CONTRACTS &&
!isAddressEq(token, ETH_ADDRESS_IN_CONTRACTS) &&
(await this.getAllowanceL1(tx.token)) < dummyAmount
) {
throw new Error('Not enough allowance to cover the deposit!');
Expand All @@ -1256,8 +1277,8 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
);
}
if (
tx.token === ETH_ADDRESS_IN_CONTRACTS ||
tx.token === baseTokenAddress
isAddressEq(token, ETH_ADDRESS_IN_CONTRACTS) ||
isAddressEq(token, baseTokenAddress)
) {
tx.overrides.value ??= tx.amount;
} else {
Expand Down Expand Up @@ -1318,7 +1339,7 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
const receipt = await this._providerL2().getTransactionReceipt(hash);
const log = receipt.logs.filter(
log =>
log.address === L1_MESSENGER_ADDRESS &&
isAddressEq(log.address, L1_MESSENGER_ADDRESS) &&
log.topics[0] ===
ethers.utils.id('L1MessageSent(address,bytes32,bytes)')
)[index];
Expand All @@ -1333,7 +1354,7 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
const hash = ethers.utils.hexlify(withdrawalHash);
const receipt = await this._providerL2().getTransactionReceipt(hash);
const messages = Array.from(receipt.l2ToL1Logs.entries()).filter(
([, log]) => log.sender === L1_MESSENGER_ADDRESS
([, log]) => isAddressEq(log.sender, L1_MESSENGER_ADDRESS)
);
const [l2ToL1LogIndex, l2ToL1Log] = messages[index];

Expand Down Expand Up @@ -1490,7 +1511,7 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
);
const successL2ToL1LogIndex = receipt.l2ToL1Logs.findIndex(
l2ToL1log =>
l2ToL1log.sender === BOOTLOADER_FORMAL_ADDRESS &&
isAddressEq(l2ToL1log.sender, BOOTLOADER_FORMAL_ADDRESS) &&
l2ToL1log.key === depositHash
);
const successL2ToL1Log = receipt.l2ToL1Logs[successL2ToL1LogIndex];
Expand Down Expand Up @@ -1637,8 +1658,10 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
}): Promise<{token: Address; allowance: BigNumberish}> {
const bridgehub = await this.getBridgehubContract();
const chainId = (await this._providerL2().getNetwork()).chainId;
const isETHBaseToken =
(await bridgehub.baseToken(chainId)) === ETH_ADDRESS_IN_CONTRACTS;
const isETHBaseToken = isAddressEq(
await bridgehub.baseToken(chainId),
ETH_ADDRESS_IN_CONTRACTS
);

if (isETHBaseToken) {
throw new Error('Could not estimate mint value on ETH-based chain!');
Expand Down Expand Up @@ -1705,8 +1728,10 @@ export function AdapterL1<TBase extends Constructor<TxSender>>(Base: TBase) {
}): Promise<ethers.PopulatedTransaction> {
const bridgehub = await this.getBridgehubContract();
const chainId = (await this._providerL2().getNetwork()).chainId;
const isETHBaseToken =
(await bridgehub.baseToken(chainId)) === ETH_ADDRESS_IN_CONTRACTS;
const isETHBaseToken = isAddressEq(
await bridgehub.baseToken(chainId),
ETH_ADDRESS_IN_CONTRACTS
);

const {...tx} = transaction;
tx.l2Value ??= BigNumber.from(0);
Expand Down
22 changes: 12 additions & 10 deletions src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
REQUIRED_L1_TO_L2_GAS_PER_PUBDATA_LIMIT,
sleep,
isETH,
isAddressEq,
} from './utils';
import {Signer} from './signer';
import Formatter = providers.Formatter;
Expand Down Expand Up @@ -525,12 +526,12 @@ export class Provider extends ethers.providers.JsonRpcProvider {
* console.log(`L2 token address: ${await provider.l2TokenAddress("0x5C221E77624690fff6dd741493D735a17716c26B")}`);
*/
async l2TokenAddress(token: Address): Promise<string> {
if (token === LEGACY_ETH_ADDRESS) {
if (isAddressEq(token, LEGACY_ETH_ADDRESS)) {
token = ETH_ADDRESS_IN_CONTRACTS;
}

const baseToken = await this.getBaseTokenContractAddress();
if (token.toLowerCase() === baseToken.toLowerCase()) {
if (isAddressEq(token, baseToken)) {
return L2_BASE_TOKEN_ADDRESS;
}

Expand Down Expand Up @@ -558,7 +559,7 @@ export class Provider extends ethers.providers.JsonRpcProvider {
* console.log(`L1 token address: ${await provider.l1TokenAddress("0x3e7676937A7E96CFB7616f255b9AD9FF47363D4b")}`);
*/
async l1TokenAddress(token: Address) {
if (token === LEGACY_ETH_ADDRESS) {
if (isAddressEq(token, LEGACY_ETH_ADDRESS)) {
return LEGACY_ETH_ADDRESS;
}

Expand Down Expand Up @@ -859,8 +860,9 @@ export class Provider extends ethers.providers.JsonRpcProvider {
* Returns whether the chain is ETH-based.
*/
async isEthBasedChain(): Promise<boolean> {
return (
(await this.getBaseTokenContractAddress()) === ETH_ADDRESS_IN_CONTRACTS
return isAddressEq(
await this.getBaseTokenContractAddress(),
ETH_ADDRESS_IN_CONTRACTS
);
}

Expand All @@ -869,8 +871,8 @@ export class Provider extends ethers.providers.JsonRpcProvider {
*/
async isBaseToken(token: Address): Promise<boolean> {
return (
token === (await this.getBaseTokenContractAddress()) ||
token === L2_BASE_TOKEN_ADDRESS
isAddressEq(token, await this.getBaseTokenContractAddress()) ||
isAddressEq(token, L2_BASE_TOKEN_ADDRESS)
);
}

Expand Down Expand Up @@ -1189,7 +1191,7 @@ export class Provider extends ethers.providers.JsonRpcProvider {
overrides?: ethers.CallOverrides;
}): Promise<ethers.providers.TransactionRequest> {
const {...tx} = transaction;
if (tx.token === LEGACY_ETH_ADDRESS) {
if (isAddressEq(tx.token, LEGACY_ETH_ADDRESS)) {
tx.token = ETH_ADDRESS_IN_CONTRACTS;
}

Expand Down Expand Up @@ -1351,7 +1353,7 @@ export class Provider extends ethers.providers.JsonRpcProvider {

if (
!tx.token ||
tx.token === LEGACY_ETH_ADDRESS ||
isAddressEq(tx.token, LEGACY_ETH_ADDRESS) ||
(await this.isBaseToken(tx.token))
) {
if (tx.paymasterParams) {
Expand Down Expand Up @@ -1734,7 +1736,7 @@ export class Provider extends ethers.providers.JsonRpcProvider {
const hash = ethers.utils.hexlify(txHash);
const receipt = await this.getTransactionReceipt(hash);
const messages = Array.from(receipt.l2ToL1Logs.entries()).filter(
([, log]) => log.sender === BOOTLOADER_FORMAL_ADDRESS
([, log]) => isAddressEq(log.sender, BOOTLOADER_FORMAL_ADDRESS)
);
const [l2ToL1LogIndex, l2ToL1Log] = messages[index];

Expand Down
3 changes: 2 additions & 1 deletion src/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
DEFAULT_GAS_PER_PUBDATA_LIMIT,
EIP712_TX_TYPE,
hashBytecode,
isAddressEq,
serialize,
} from './utils';
import {
Expand Down Expand Up @@ -469,7 +470,7 @@ export class Signer extends AdapterL2(ethers.providers.JsonRpcSigner) {
} else {
const address = await this.getAddress();
transaction.from ??= address;
if (transaction.from.toLowerCase() !== address.toLowerCase()) {
if (!isAddressEq(transaction.from, address)) {
throw new Error('Transaction `from` address mismatch!');
}
transaction.type = EIP712_TX_TYPE;
Expand Down
Loading

0 comments on commit 75b4e83

Please sign in to comment.