Skip to content

Commit c8954d8

Browse files
authored
feat(cheatcodes): add computeCreateAddress cheatcodes (foundry-rs#6296)
* Add computeCreateAddress cheatcodes * use Address methods * ensure nonce can be u64
1 parent d522499 commit c8954d8

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

crates/cheatcodes/assets/cheatcodes.json

Lines changed: 60 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cheatcodes/defs/src/vm.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,5 +1155,17 @@ interface Vm {
11551155
/// Gets the label for the specified address.
11561156
#[cheatcode(group = Utilities)]
11571157
function getLabel(address account) external returns (string memory currentLabel);
1158+
1159+
/// Compute the address a contract will be deployed at for a given deployer address and nonce.
1160+
#[cheatcode(group = Utilities)]
1161+
function computeCreateAddress(address deployer, uint256 nonce) external pure returns (address);
1162+
1163+
/// Compute the address of a contract created with CREATE2 using the given CREATE2 deployer.
1164+
#[cheatcode(group = Utilities)]
1165+
function computeCreate2Address(bytes32 salt, bytes32 initCodeHash, address deployer) external pure returns (address);
1166+
1167+
/// Compute the address of a contract created with CREATE2 using the default CREATE2 deployer.
1168+
#[cheatcode(group = Utilities)]
1169+
function computeCreate2Address(bytes32 salt, bytes32 initCodeHash) external pure returns (address);
11581170
}
11591171
}

crates/cheatcodes/src/utils.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use ethers_signers::{
1515
},
1616
LocalWallet, MnemonicBuilder, Signer,
1717
};
18+
use foundry_evm_core::constants::DEFAULT_CREATE2_DEPLOYER;
1819
use foundry_utils::types::{ToAlloy, ToEthers};
1920

2021
/// The BIP32 default derivation path prefix.
@@ -111,6 +112,28 @@ impl Cheatcode for getLabelCall {
111112
}
112113
}
113114

115+
impl Cheatcode for computeCreateAddressCall {
116+
fn apply(&self, _state: &mut Cheatcodes) -> Result {
117+
let Self { nonce, deployer } = self;
118+
ensure!(*nonce <= U256::from(u64::MAX), "nonce must be less than 2^64 - 1");
119+
Ok(deployer.create(nonce.to()).abi_encode())
120+
}
121+
}
122+
123+
impl Cheatcode for computeCreate2Address_0Call {
124+
fn apply(&self, _state: &mut Cheatcodes) -> Result {
125+
let Self { salt, initCodeHash, deployer } = self;
126+
Ok(deployer.create2(salt, initCodeHash).abi_encode())
127+
}
128+
}
129+
130+
impl Cheatcode for computeCreate2Address_1Call {
131+
fn apply(&self, _state: &mut Cheatcodes) -> Result {
132+
let Self { salt, initCodeHash } = self;
133+
Ok(DEFAULT_CREATE2_DEPLOYER.create2(salt, initCodeHash).abi_encode())
134+
}
135+
}
136+
114137
/// Using a given private key, return its public ETH address, its public key affine x and y
115138
/// coodinates, and its private key (see the 'Wallet' struct)
116139
///

testdata/cheats/Vm.sol

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)