Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add cast test to forge create test suite #553

Merged
merged 2 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions crates/forge/tests/it/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -635,3 +635,33 @@ pub fn run_zk_script_test(
);
cmd.forge_fuse();
}

pub fn deploy_zk_contract(
cmd: &mut TestCommand,
url: &str,
private_key: &str,
contract_path: &str,
) -> Result<String, String> {
cmd.forge_fuse().args([
"create",
"--zk-startup",
contract_path,
"--rpc-url",
url,
"--private-key",
private_key,
]);

let (stdout, stderr) = cmd.output_lossy();

if stdout.contains("Deployed to:") {
let regex = regex::Regex::new(r"Deployed to:\s*(\S+)").unwrap();
regex
.captures(&stdout)
.and_then(|cap| cap.get(1))
.map(|m| m.as_str().to_string())
.ok_or_else(|| "Failed to extract deployed address".to_string())
} else {
Err(format!("Deployment failed. Stdout: {stdout}\nStderr: {stderr}"))
}
}
45 changes: 27 additions & 18 deletions crates/forge/tests/it/zk/create.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use foundry_test_utils::{forgetest_async, util, ZkSyncNode};

use crate::test_helpers::deploy_zk_contract;

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();
Expand All @@ -10,46 +12,53 @@ forgetest_async!(forge_zk_can_deploy_erc20, |prj, cmd| {
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 erc20_address =
deploy_zk_contract(&mut cmd, url.as_str(), private_key, "./src/ERC20.sol:MyToken")
.expect("Failed to deploy ERC20 contract");

let (stdout, _) = cmd.output_lossy();
assert!(stdout.contains("Deployer: "));
assert!(stdout.contains("Deployed to: "));
assert!(!erc20_address.is_empty(), "Deployed address should not be empty");
});

forgetest_async!(forge_zk_can_deploy_token_receiver, |prj, cmd| {
forgetest_async!(forge_zk_can_deploy_contracts_and_cast_a_transaction, |prj, cmd| {
util::initialize(prj.root());
prj.add_source(
"TokenReceiver.sol",
include_str!("../../../../../testdata/zk/TokenReceiver.sol"),
)
.unwrap();
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",
let token_receiver_address = deploy_zk_contract(
&mut cmd,
url.as_str(),
private_key,
"./src/TokenReceiver.sol:TokenReceiver",
)
.expect("Failed to deploy TokenReceiver contract");
let erc_20_address =
deploy_zk_contract(&mut cmd, url.as_str(), private_key, "./src/ERC20.sol:MyToken")
.expect("Failed to deploy ERC20 contract");

cmd.cast_fuse().args([
"send",
"--rpc-url",
url.as_str(),
"--private-key",
private_key,
&erc_20_address,
"transfer(address,uint256)",
&token_receiver_address,
"1",
]);

let (stdout, _) = cmd.output_lossy();
assert!(stdout.contains("Deployer: "));
assert!(stdout.contains("Deployed to: "));

assert!(stdout.contains("transactionHash"), "Transaction hash not found in output");
assert!(stdout.contains("success"), "Transaction was not successful");
});