Skip to content

Commit

Permalink
test: add cast test to forge create test suite (#553)
Browse files Browse the repository at this point in the history
* Added cast test and refactor forge create tests with helper function

* Add regex to test helper for more clarity

---------

Co-authored-by: Jrigada <[email protected]>
  • Loading branch information
Jrigada and Jrigada authored Sep 4, 2024
1 parent fcd9bae commit 71ae07e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 18 deletions.
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");
});

0 comments on commit 71ae07e

Please sign in to comment.