Skip to content

Commit

Permalink
test: Migrate forge create tests to cargo (#551)
Browse files Browse the repository at this point in the history
* Migrate forge create tests to cargo

* Forge fmt

---------

Co-authored-by: Jrigada <[email protected]>
  • Loading branch information
Jrigada and Jrigada authored Aug 29, 2024
1 parent f908ce4 commit 11424d0
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
55 changes: 55 additions & 0 deletions crates/forge/tests/it/zk/create.rs
Original file line number Diff line number Diff line change
@@ -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: "));
});
1 change: 1 addition & 0 deletions crates/forge/tests/it/zk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
mod basic;
mod cheats;
mod contracts;
mod create;
mod factory;
mod factory_deps;
mod fork;
Expand Down
29 changes: 29 additions & 0 deletions testdata/zk/ERC20.sol
Original file line number Diff line number Diff line change
@@ -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;
}
}
12 changes: 12 additions & 0 deletions testdata/zk/TokenReceiver.sol
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 11424d0

Please sign in to comment.