Skip to content

Commit

Permalink
feat(cast): get create2 address (#8724)
Browse files Browse the repository at this point in the history
* feat(cast): get create2 address

* add additional documentation for `salt`
  • Loading branch information
justinmoore-next committed Aug 23, 2024
1 parent 70ef94a commit 1dad817
Showing 1 changed file with 50 additions and 9 deletions.
59 changes: 50 additions & 9 deletions crates/cast/bin/cmd/create2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct Create2Args {
#[arg(
long,
short,
required_unless_present_any = &["ends_with", "matching"],
required_unless_present_any = &["ends_with", "matching", "salt"],
value_name = "HEX"
)]
starts_with: Option<String>,
Expand All @@ -48,6 +48,23 @@ pub struct Create2Args {
)]
deployer: Address,

/// Salt to be used for the contract deployment. This option separate from the default salt
/// mining with filters.
#[arg(
long,
conflicts_with_all = [
"starts_with",
"ends_with",
"matching",
"case_sensitive",
"caller",
"seed",
"no_random"
],
value_name = "HEX"
)]
salt: Option<String>,

/// Init code of the contract to be deployed.
#[arg(short, long, value_name = "HEX")]
init_code: Option<String>,
Expand Down Expand Up @@ -87,6 +104,7 @@ impl Create2Args {
matching,
case_sensitive,
deployer,
salt,
init_code,
init_code_hash,
jobs,
Expand All @@ -95,6 +113,21 @@ impl Create2Args {
no_random,
} = self;

let init_code_hash = if let Some(init_code_hash) = init_code_hash {
hex::FromHex::from_hex(init_code_hash)
} else if let Some(init_code) = init_code {
hex::decode(init_code).map(keccak256)
} else {
unreachable!();
}?;

if let Some(salt) = salt {
let salt = hex::FromHex::from_hex(salt)?;
let address = deployer.create2(salt, init_code_hash);
println!("{address}");
return Ok(Create2Output { address, salt });
}

let mut regexs = vec![];

if let Some(matches) = matching {
Expand Down Expand Up @@ -134,14 +167,6 @@ impl Create2Args {

let regex = RegexSetBuilder::new(regexs).case_insensitive(!case_sensitive).build()?;

let init_code_hash = if let Some(init_code_hash) = init_code_hash {
hex::FromHex::from_hex(init_code_hash)
} else if let Some(init_code) = init_code {
hex::decode(init_code).map(keccak256)
} else {
unreachable!();
}?;

let mut n_threads = std::thread::available_parallelism().map_or(1, |n| n.get());
if let Some(jobs) = jobs {
n_threads = n_threads.min(jobs.get());
Expand Down Expand Up @@ -302,6 +327,22 @@ mod tests {
assert!(format!("{address:x}").starts_with("bb"));
}

#[test]
fn create2_salt() {
let args = Create2Args::parse_from([
"foundry-cli",
"--deployer=0x8ba1f109551bD432803012645Ac136ddd64DBA72",
"--salt=0x7c5ea36004851c764c44143b1dcb59679b11c9a68e5f41497f6cf3d480715331",
"--init-code=0x6394198df16000526103ff60206004601c335afa6040516060f3",
]);
let create2_out = args.run().unwrap();
let address = create2_out.address;
assert_eq!(
address,
Address::from_str("0x533AE9D683B10C02EBDB05471642F85230071FC3").unwrap()
);
}

#[test]
fn create2_init_code() {
let init_code = "00";
Expand Down

0 comments on commit 1dad817

Please sign in to comment.