From 11424d0424225d689a88477a5ffced65a3228d2c Mon Sep 17 00:00:00 2001 From: Juan Rigada <62958725+Jrigada@users.noreply.github.com> Date: Thu, 29 Aug 2024 09:19:52 -0300 Subject: [PATCH] test: Migrate forge create tests to cargo (#551) * Migrate forge create tests to cargo * Forge fmt --------- Co-authored-by: Jrigada --- crates/forge/tests/it/zk/create.rs | 55 ++++++++++++++++++++++++++++++ crates/forge/tests/it/zk/mod.rs | 1 + testdata/zk/ERC20.sol | 29 ++++++++++++++++ testdata/zk/TokenReceiver.sol | 12 +++++++ 4 files changed, 97 insertions(+) create mode 100644 crates/forge/tests/it/zk/create.rs create mode 100644 testdata/zk/ERC20.sol create mode 100644 testdata/zk/TokenReceiver.sol diff --git a/crates/forge/tests/it/zk/create.rs b/crates/forge/tests/it/zk/create.rs new file mode 100644 index 000000000..83569a6bb --- /dev/null +++ b/crates/forge/tests/it/zk/create.rs @@ -0,0 +1,55 @@ +use foundry_test_utils::{forgetest_async, util, ZkSyncNode}; + +forgetest_async!(forge_zk_can_deploy_erc20, |prj, cmd| { + util::initialize(prj.root()); + prj.add_source("ERC20.sol", include_str!("../../../../../testdata/zk/ERC20.sol")).unwrap(); + + let node = ZkSyncNode::start(); + let url = node.url(); + + let private_key = + ZkSyncNode::rich_wallets().next().map(|(_, pk, _)| pk).expect("No rich wallets available"); + + cmd.forge_fuse().args([ + "create", + "--zk-startup", + "./src/ERC20.sol:MyToken", + "--rpc-url", + url.as_str(), + "--private-key", + private_key, + ]); + + let (stdout, _) = cmd.output_lossy(); + assert!(stdout.contains("Deployer: ")); + assert!(stdout.contains("Deployed to: ")); +}); + +forgetest_async!(forge_zk_can_deploy_token_receiver, |prj, cmd| { + util::initialize(prj.root()); + prj.add_source( + "TokenReceiver.sol", + include_str!("../../../../../testdata/zk/TokenReceiver.sol"), + ) + .unwrap(); + + let node = ZkSyncNode::start(); + let url = node.url(); + + let private_key = + ZkSyncNode::rich_wallets().next().map(|(_, pk, _)| pk).expect("No rich wallets available"); + + cmd.forge_fuse().args([ + "create", + "--zk-startup", + "./src/TokenReceiver.sol:TokenReceiver", + "--rpc-url", + url.as_str(), + "--private-key", + private_key, + ]); + + let (stdout, _) = cmd.output_lossy(); + assert!(stdout.contains("Deployer: ")); + assert!(stdout.contains("Deployed to: ")); +}); diff --git a/crates/forge/tests/it/zk/mod.rs b/crates/forge/tests/it/zk/mod.rs index ff3aa883d..4149c7dfd 100644 --- a/crates/forge/tests/it/zk/mod.rs +++ b/crates/forge/tests/it/zk/mod.rs @@ -2,6 +2,7 @@ mod basic; mod cheats; mod contracts; +mod create; mod factory; mod factory_deps; mod fork; diff --git a/testdata/zk/ERC20.sol b/testdata/zk/ERC20.sol new file mode 100644 index 000000000..1fe10b382 --- /dev/null +++ b/testdata/zk/ERC20.sol @@ -0,0 +1,29 @@ +// SPDX-License-Identifier: MIT +pragma solidity >=0.8.0; + +contract MyToken { + string public owner; + string public constant name = "MyToken"; + string public constant symbol = "MTK"; + uint8 public constant decimals = 18; + uint256 public totalSupply = 1000000 * (10 ** uint256(decimals)); + mapping(address => uint256) public balanceOf; + + event Transfer(address indexed from, address indexed to, uint256 value); + + constructor() { + balanceOf[msg.sender] = totalSupply; + } + + function setTotalSupply(uint256 amount) public { + totalSupply = amount; + } + + function transfer(address to, uint256 amount) public returns (bool) { + require(balanceOf[msg.sender] >= amount, "Not enough tokens"); + balanceOf[msg.sender] -= amount; + balanceOf[to] += amount; + emit Transfer(msg.sender, to, amount); + return true; + } +} diff --git a/testdata/zk/TokenReceiver.sol b/testdata/zk/TokenReceiver.sol new file mode 100644 index 000000000..efd75034d --- /dev/null +++ b/testdata/zk/TokenReceiver.sol @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: MIT +pragma solidity >=0.8.0; + +interface IMyToken { + function transfer(address to, uint256 amount) external returns (bool); +} + +contract TokenReceiver { + function receiveAndHoldToken(address token, uint256 amount) external { + IMyToken(token).transfer(msg.sender, amount); + } +}