Skip to content

Commit

Permalink
Merge pull request #5 from lambdaclass/erc20-transferFrom-and-checks
Browse files Browse the repository at this point in the history
Erc20 transfer from and checks
  • Loading branch information
jrchatruc authored Dec 21, 2023
2 parents 74b9287 + 090c1a8 commit f90292a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
9 changes: 9 additions & 0 deletions ethereum/contracts/zksync/facets/Mailbox.sol
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,15 @@ contract MailboxFacet is Base, IMailbox {
refundRecipient = AddressAliasHelper.applyL1ToL2Alias(refundRecipient);
}

// 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(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(nativeTokenAddress).safeTransferFrom(msg.sender, address(this), _amount);

params.sender = _sender;
params.txId = s.priorityQueue.getTotalPriorityTxs();
params.l2Value = _l2Value;
Expand Down
21 changes: 19 additions & 2 deletions ethereum/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +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 * as fs from 'fs';
import path = require("path");
import * as fs from "fs";

// If no network is specified, use the default config
if (!process.env.CHAIN_ETH_NETWORK) {
Expand All @@ -23,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,
Expand All @@ -31,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,
Expand All @@ -40,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,
Expand All @@ -48,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,
Expand Down Expand Up @@ -92,7 +109,7 @@ export default {
const defs = process.env.CONTRACT_TESTS ? contractDefs.test : contractDefs[process.env.CHAIN_ETH_NETWORK];

let path = `${process.env.ZKSYNC_HOME}/etc/tokens/native_erc20.json`;
let rawData = fs.readFileSync(path, 'utf8');
let rawData = fs.readFileSync(path, "utf8");
let address = "0x52312AD6f01657413b2eaE9287f6B9ADaD93D5FE";
try {
let jsonConfig = JSON.parse(rawData);
Expand Down
12 changes: 10 additions & 2 deletions ethereum/scripts/deploy-erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ async function deployToken(token: TokenDescription, wallet: Wallet): Promise<Tok
const erc20 = await tokenFactory.deploy(...args, { gasLimit: 5000000 });
await erc20.deployTransaction.wait();

console.error("Wallet address: ", wallet.address);
console.error("Wallet PK: ", wallet.privateKey);
console.error("Deploying token: ", token.name);
console.error("erc20: ", erc20);

if (token.implementation !== "WETH9") {
await erc20.mint(wallet.address, parseEther("3000000000"));
}
Expand All @@ -44,8 +49,10 @@ async function deployToken(token: TokenDescription, wallet: Wallet): Promise<Tok
}
}

console.error("erc20 address: ", erc20.address);
console.error("token address: ", token.address);
token.address = erc20.address;

console.error("token address after: ", token.address);
// Remove the unneeded field
if (token.implementation) {
delete token.implementation;
Expand Down Expand Up @@ -97,8 +104,9 @@ async function main() {
for (const token of tokens) {
result.push(await deployToken(token, wallet));
}

console.error("result: ", result);
console.log(JSON.stringify(result, null, 2));
console.error("Json stringify passed");
});

await program.parseAsync(process.argv);
Expand Down

0 comments on commit f90292a

Please sign in to comment.