From 667dc88590714224a555c5eef4c1388836faaca8 Mon Sep 17 00:00:00 2001 From: Santiago Pittella Date: Tue, 19 Dec 2023 15:10:15 -0300 Subject: [PATCH 1/4] add checks and transfer in Mailbox.sol --- ethereum/contracts/zksync/facets/Mailbox.sol | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ethereum/contracts/zksync/facets/Mailbox.sol b/ethereum/contracts/zksync/facets/Mailbox.sol index 7c76c206b..126f8859b 100644 --- a/ethereum/contracts/zksync/facets/Mailbox.sol +++ b/ethereum/contracts/zksync/facets/Mailbox.sol @@ -329,6 +329,14 @@ contract MailboxFacet is Base, IMailbox { refundRecipient = AddressAliasHelper.applyL1ToL2Alias(refundRecipient); } + // TODO: change `_contractL2` for the L1 token contract. + // Check balance and allowance. + require(IERC20(_contractL2).balanceOf(msg.sender) >= _amount, "Not enough balance"); + require(IERC20(_contractL2).allowance(msg.sender, address(this)) >= _amount, "Not enough allowance"); + + // Transfer tokens to the contract. + IERC20(_contractL2).safeTransferFrom(msg.sender, address(this), _amount); + params.sender = _sender; params.txId = s.priorityQueue.getTotalPriorityTxs(); params.l2Value = _l2Value; From 725e900a61748a5a8ee4f5e27cc085c09ea23c39 Mon Sep 17 00:00:00 2001 From: Santiago Pittella Date: Wed, 20 Dec 2023 17:48:52 -0300 Subject: [PATCH 2/4] use define for l1 contract address --- ethereum/contracts/zksync/facets/Mailbox.sol | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/ethereum/contracts/zksync/facets/Mailbox.sol b/ethereum/contracts/zksync/facets/Mailbox.sol index 126f8859b..9915636cd 100644 --- a/ethereum/contracts/zksync/facets/Mailbox.sol +++ b/ethereum/contracts/zksync/facets/Mailbox.sol @@ -24,6 +24,8 @@ import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol /// @author Matter Labs /// @custom:security-contact security@matterlabs.dev contract MailboxFacet is Base, IMailbox { + #define L1_NATIVE_TOKEN_ADDRESS $L1_NATIVE_TOKEN_ADDRESS + using UncheckedMath for uint256; using PriorityQueue for PriorityQueue.Queue; using SafeERC20 for IERC20; @@ -329,13 +331,12 @@ contract MailboxFacet is Base, IMailbox { refundRecipient = AddressAliasHelper.applyL1ToL2Alias(refundRecipient); } - // TODO: change `_contractL2` for the L1 token contract. // Check balance and allowance. - require(IERC20(_contractL2).balanceOf(msg.sender) >= _amount, "Not enough balance"); - require(IERC20(_contractL2).allowance(msg.sender, address(this)) >= _amount, "Not enough allowance"); + require(IERC20(L1_NATIVE_TOKEN_ADDRESS).balanceOf(msg.sender) >= _amount, "Not enough balance"); + require(IERC20(L1_NATIVE_TOKEN_ADDRESS).allowance(msg.sender, address(this)) >= _amount, "Not enough allowance"); // Transfer tokens to the contract. - IERC20(_contractL2).safeTransferFrom(msg.sender, address(this), _amount); + IERC20(L1_NATIVE_TOKEN_ADDRESS).safeTransferFrom(msg.sender, address(this), _amount); params.sender = _sender; params.txId = s.priorityQueue.getTotalPriorityTxs(); From e19c6cfa10b70ed43e1be003f672811bb08d59eb Mon Sep 17 00:00:00 2001 From: Santiago Pittella Date: Thu, 21 Dec 2023 15:34:06 -0300 Subject: [PATCH 3/4] add checks, add token address --- ethereum/contracts/zksync/facets/Mailbox.sol | 9 ++++----- ethereum/hardhat.config.ts | 18 ++++++++++++++++++ ethereum/scripts/deploy-erc20.ts | 10 +++++++--- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/ethereum/contracts/zksync/facets/Mailbox.sol b/ethereum/contracts/zksync/facets/Mailbox.sol index 9915636cd..9c2183dd9 100644 --- a/ethereum/contracts/zksync/facets/Mailbox.sol +++ b/ethereum/contracts/zksync/facets/Mailbox.sol @@ -24,8 +24,6 @@ import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol /// @author Matter Labs /// @custom:security-contact security@matterlabs.dev contract MailboxFacet is Base, IMailbox { - #define L1_NATIVE_TOKEN_ADDRESS $L1_NATIVE_TOKEN_ADDRESS - using UncheckedMath for uint256; using PriorityQueue for PriorityQueue.Queue; using SafeERC20 for IERC20; @@ -331,12 +329,13 @@ contract MailboxFacet is Base, IMailbox { refundRecipient = AddressAliasHelper.applyL1ToL2Alias(refundRecipient); } + address l1TokenAddress = address($(L1_NATIVE_TOKEN_ADDRESS)); // Check balance and allowance. - require(IERC20(L1_NATIVE_TOKEN_ADDRESS).balanceOf(msg.sender) >= _amount, "Not enough balance"); - require(IERC20(L1_NATIVE_TOKEN_ADDRESS).allowance(msg.sender, address(this)) >= _amount, "Not enough allowance"); + require(IERC20(l1TokenAddress).balanceOf(msg.sender) >= _amount, "Not enough balance"); + require(IERC20(l1TokenAddress).allowance(msg.sender, address(this)) >= _amount, "Not enough allowance"); // Transfer tokens to the contract. - IERC20(L1_NATIVE_TOKEN_ADDRESS).safeTransferFrom(msg.sender, address(this), _amount); + IERC20(l1TokenAddress).safeTransferFrom(msg.sender, address(this), _amount); params.sender = _sender; params.txId = s.priorityQueue.getTotalPriorityTxs(); diff --git a/ethereum/hardhat.config.ts b/ethereum/hardhat.config.ts index a89ea4e69..0d3b30a7c 100644 --- a/ethereum/hardhat.config.ts +++ b/ethereum/hardhat.config.ts @@ -9,6 +9,8 @@ import { TASK_COMPILE_SOLIDITY_GET_SOURCE_PATHS } from "hardhat/builtin-tasks/ta import { task } from "hardhat/config"; import "solidity-coverage"; import { getNumberFromEnv } from "./scripts/utils"; +import path = require("path"); +import * as fs from "fs"; // If no network is specified, use the default config if (!process.env.CHAIN_ETH_NETWORK) { @@ -22,6 +24,19 @@ const systemParams = require("../SystemConfig.json"); const PRIORITY_TX_MAX_GAS_LIMIT = getNumberFromEnv("CONTRACTS_PRIORITY_TX_MAX_GAS_LIMIT"); const DEPLOY_L2_BRIDGE_COUNTERPART_GAS_LIMIT = getNumberFromEnv("CONTRACTS_DEPLOY_L2_BRIDGE_COUNTERPART_GAS_LIMIT"); +const testConfigPath = path.join(process.env.ZKSYNC_HOME as string, "etc/tokens"); +console.error("testConfigPath", testConfigPath); +let testConfigFile = fs.readFileSync(`${testConfigPath}/native_erc20.json`, { encoding: "utf-8" }); +console.error("testConfigFile", testConfigFile); + +if (testConfigFile === "") { + testConfigFile = '{ "address": "0x0" }'; +} + +const nativeERC20Token = JSON.parse(testConfigFile); +console.error("nativeERC20Token", nativeERC20Token); +const L1_NATIVE_TOKEN_ADDRESS = nativeERC20Token.address; + const prodConfig = { UPGRADE_NOTICE_PERIOD: 0, // PRIORITY_EXPIRATION: 101, @@ -30,6 +45,7 @@ const prodConfig = { PRIORITY_TX_MAX_GAS_LIMIT, DEPLOY_L2_BRIDGE_COUNTERPART_GAS_LIMIT, DUMMY_VERIFIER: false, + L1_NATIVE_TOKEN_ADDRESS, }; const testnetConfig = { UPGRADE_NOTICE_PERIOD: 0, @@ -39,6 +55,7 @@ const testnetConfig = { PRIORITY_TX_MAX_GAS_LIMIT, DEPLOY_L2_BRIDGE_COUNTERPART_GAS_LIMIT, DUMMY_VERIFIER: true, + L1_NATIVE_TOKEN_ADDRESS, }; const testConfig = { UPGRADE_NOTICE_PERIOD: 0, @@ -47,6 +64,7 @@ const testConfig = { PRIORITY_TX_MAX_GAS_LIMIT, DEPLOY_L2_BRIDGE_COUNTERPART_GAS_LIMIT, DUMMY_VERIFIER: true, + L1_NATIVE_TOKEN_ADDRESS, }; const localConfig = { ...prodConfig, diff --git a/ethereum/scripts/deploy-erc20.ts b/ethereum/scripts/deploy-erc20.ts index 4fc420c8d..a9f8ebe74 100644 --- a/ethereum/scripts/deploy-erc20.ts +++ b/ethereum/scripts/deploy-erc20.ts @@ -34,7 +34,8 @@ async function deployToken(token: TokenDescription, wallet: Wallet): Promise Date: Thu, 21 Dec 2023 15:45:56 -0300 Subject: [PATCH 4/4] update names --- ethereum/contracts/zksync/facets/Mailbox.sol | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/ethereum/contracts/zksync/facets/Mailbox.sol b/ethereum/contracts/zksync/facets/Mailbox.sol index 815f39c70..79553f275 100644 --- a/ethereum/contracts/zksync/facets/Mailbox.sol +++ b/ethereum/contracts/zksync/facets/Mailbox.sol @@ -330,13 +330,14 @@ contract MailboxFacet is Base, IMailbox { refundRecipient = AddressAliasHelper.applyL1ToL2Alias(refundRecipient); } - address l1TokenAddress = address($(L1_NATIVE_TOKEN_ADDRESS)); + // The address of the token that is used in the L2 as native. + address nativeTokenAddress = address($(L1_NATIVE_TOKEN_ADDRESS)); // Check balance and allowance. - require(IERC20(l1TokenAddress).balanceOf(msg.sender) >= _amount, "Not enough balance"); - require(IERC20(l1TokenAddress).allowance(msg.sender, address(this)) >= _amount, "Not enough allowance"); + require(IERC20(nativeTokenAddress).balanceOf(msg.sender) >= _amount, "Not enough balance"); + require(IERC20(nativeTokenAddress).allowance(msg.sender, address(this)) >= _amount, "Not enough allowance"); // Transfer tokens to the contract. - IERC20(l1TokenAddress).safeTransferFrom(msg.sender, address(this), _amount); + IERC20(nativeTokenAddress).safeTransferFrom(msg.sender, address(this), _amount); params.sender = _sender; params.txId = s.priorityQueue.getTotalPriorityTxs();