Skip to content

Commit

Permalink
Update verification tests + Test for --guess-constructor-args
Browse files Browse the repository at this point in the history
  • Loading branch information
klkvr committed Jan 7, 2024
1 parent 1df39ab commit 1f5d910
Showing 1 changed file with 92 additions and 31 deletions.
123 changes: 92 additions & 31 deletions crates/forge/tests/cli/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ function doStuff() external {}
.unwrap();
}

fn add_verify_target_with_constructor(prj: &TestProject) {
prj.add_source(
"Verify.sol",
r#"
import {Unique} from "./unique.sol";
contract Verify is Unique {
struct SomeStruct {
uint256 a;
string str;
}
constructor(SomeStruct memory st, address owner) {}
}
"#,
)
.unwrap();
}

fn parse_verification_result(cmd: &mut TestCommand, retries: u32) -> eyre::Result<()> {
// give etherscan some time to verify the contract
let retry = Retry::new(retries, Some(Duration::from_secs(30)));
Expand All @@ -56,6 +74,39 @@ fn parse_verification_result(cmd: &mut TestCommand, retries: u32) -> eyre::Resul
})
}

fn await_verification_response(info: EnvExternalities, mut cmd: TestCommand) {
let guid = {
// give etherscan some time to detect the transaction
let retry = Retry::new(5, Some(Duration::from_secs(60)));
retry
.run(|| -> eyre::Result<String> {
let output = cmd.unchecked_output();
let out = String::from_utf8_lossy(&output.stdout);
utils::parse_verification_guid(&out).ok_or_else(|| {
eyre::eyre!(
"Failed to get guid, stdout: {}, stderr: {}",
out,
String::from_utf8_lossy(&output.stderr)
)
})
})
.expect("Failed to get verify guid")
};

// verify-check
cmd.forge_fuse()
.arg("verify-check")
.arg(guid)
.arg("--chain-id")
.arg(info.chain.to_string())
.arg("--etherscan-api-key")
.arg(info.etherscan)
.arg("--verifier")
.arg(info.verifier);

parse_verification_result(&mut cmd, 6).expect("Failed to verify check")
}

fn verify_on_chain(info: Option<EnvExternalities>, prj: TestProject, mut cmd: TestCommand) {
// only execute if keys present
if let Some(info) = info {
Expand All @@ -75,42 +126,47 @@ fn verify_on_chain(info: Option<EnvExternalities>, prj: TestProject, mut cmd: Te
info.chain.to_string(),
address,
contract_path.to_string(),
"--etherscan-api-key".to_string(),
info.etherscan.to_string(),
"--verifier".to_string(),
info.verifier.to_string(),
]);

// `verify-contract`
let guid = {
// give etherscan some time to detect the transaction
let retry = Retry::new(5, Some(Duration::from_secs(60)));
retry
.run(|| -> eyre::Result<String> {
let output = cmd.unchecked_output();
let out = String::from_utf8_lossy(&output.stdout);
utils::parse_verification_guid(&out).ok_or_else(|| {
eyre::eyre!(
"Failed to get guid, stdout: {}, stderr: {}",
out,
String::from_utf8_lossy(&output.stderr)
)
})
})
.expect("Failed to get verify guid")
};

// verify-check
cmd.forge_fuse()
.arg("verify-check")
.arg(guid)
.arg("--chain-id")
.arg(info.chain.to_string())
.arg("--etherscan-key")
.arg(info.etherscan)
.arg("--verifier")
.arg(info.verifier);

parse_verification_result(&mut cmd, 6).expect("Failed to verify check")
await_verification_response(info, cmd)
}
}

fn guess_constructor_args(info: Option<EnvExternalities>, prj: TestProject, mut cmd: TestCommand) {
// only execute if keys present
if let Some(info) = info {
println!("verifying on {}", info.chain);
add_unique(&prj);
add_verify_target_with_constructor(&prj);

let contract_path = "src/Verify.sol:Verify";
cmd.arg("create").args(info.create_args()).arg(contract_path).args(vec![
"--constructor-args",
"(239,SomeString)",
"0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",
]);

let out = cmd.stdout_lossy();
let address = utils::parse_deployed_address(out.as_str())
.unwrap_or_else(|| panic!("Failed to parse deployer {out}"));

cmd.forge_fuse().arg("verify-contract").root_arg().args([
"--rpc-url".to_string(),
info.rpc.to_string(),
address,
contract_path.to_string(),
"--etherscan-api-key".to_string(),
info.etherscan.to_string(),
"--verifier".to_string(),
info.verifier.to_string(),
"--guess-constructor-args".to_string(),
]);

await_verification_response(info, cmd)
}
}

Expand All @@ -123,3 +179,8 @@ forgetest!(can_verify_random_contract_fantom_testnet, |prj, cmd| {
forgetest!(can_verify_random_contract_optimism_kovan, |prj, cmd| {
verify_on_chain(EnvExternalities::optimism_kovan(), prj, cmd);
});

// tests `create && contract-verify --guess-constructor-args && verify-check` on Goerli testnet if correct env vars are set
forgetest!(can_guess_constructor_args, |prj, cmd| {
guess_constructor_args(EnvExternalities::goerli(), prj, cmd);
});

0 comments on commit 1f5d910

Please sign in to comment.