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

feat(cheatcodes): add computeCreateAddress cheatcodes #6296

Merged
merged 3 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
60 changes: 60 additions & 0 deletions crates/cheatcodes/assets/cheatcodes.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions crates/cheatcodes/defs/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1155,5 +1155,17 @@ interface Vm {
/// Gets the label for the specified address.
#[cheatcode(group = Utilities)]
function getLabel(address account) external returns (string memory currentLabel);

/// Compute the address a contract will be deployed at for a given deployer address and nonce.
#[cheatcode(group = Utilities)]
function computeCreateAddress(address deployer, uint256 nonce) external pure returns (address);

/// Compute the address of a contract created with CREATE2 using the given CREATE2 deployer.
#[cheatcode(group = Utilities)]
function computeCreate2Address(bytes32 salt, bytes32 initCodeHash, address deployer) external pure returns (address);

/// Compute the address of a contract created with CREATE2 using the default CREATE2 deployer.
#[cheatcode(group = Utilities)]
function computeCreate2Address(bytes32 salt, bytes32 initCodeHash) external pure returns (address);
}
}
22 changes: 22 additions & 0 deletions crates/cheatcodes/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use ethers_signers::{
},
LocalWallet, MnemonicBuilder, Signer,
};
use foundry_evm_core::constants::DEFAULT_CREATE2_DEPLOYER;
use foundry_utils::types::{ToAlloy, ToEthers};

/// The BIP32 default derivation path prefix.
Expand Down Expand Up @@ -111,6 +112,27 @@ impl Cheatcode for getLabelCall {
}
}

impl Cheatcode for computeCreateAddressCall {
fn apply(&self, _state: &mut Cheatcodes) -> Result {
let Self { nonce, deployer } = self;
Ok(deployer.create(nonce.to()).abi_encode())
}
}

impl Cheatcode for computeCreate2Address_0Call {
fn apply(&self, _state: &mut Cheatcodes) -> Result {
let Self { salt, initCodeHash, deployer } = self;
Ok(deployer.create2(salt, initCodeHash).abi_encode())
}
}

impl Cheatcode for computeCreate2Address_1Call {
fn apply(&self, _state: &mut Cheatcodes) -> Result {
let Self { salt, initCodeHash } = self;
Ok(DEFAULT_CREATE2_DEPLOYER.create2(salt, initCodeHash).abi_encode())
}
}

/// Using a given private key, return its public ETH address, its public key affine x and y
/// coodinates, and its private key (see the 'Wallet' struct)
///
Expand Down
3 changes: 3 additions & 0 deletions testdata/cheats/Vm.sol

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.