Incorrect CREATE2 addresses when not broadcast
Component
Forge
Have you ensured that all of these are up to date?
- [X] Foundry
- [X] Foundryup
What version of Foundry are you on?
forge 0.2.0 (7b45265 2023-11-21T00:18:30.267248000Z)
What command(s) is the bug in?
No response
Operating System
macOS (Apple Silicon)
Describe the bug
When trying to deploy a contract using a salt calculated using cast create2, it works correctly when vm.startBroadcast() is called (and pointing to a local anvil node) - but does not generate the correct address when not broadcast.
The following script will fail the require() statement at the end - but uncommenting the vm.startBroadcast() line will cause it to pass.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import {Script, console2} from "forge-std/Script.sol";
import {Counter} from "../src/Counter.sol";
contract CounterScript is Script {
function run() external {
// vm.startBroadcast();
bytes32 counterInitCodeHash = keccak256(type(Counter).creationCode);
console2.log("Counter initCodeHash: ");
console2.logBytes32(counterInitCodeHash);
// $ cast create2 --starts-with 0x000000 --init-code-hash 0x479d7e8f31234e208d704ba1a123c76385cea8a6981fd675b784fbd9cffb918d
//
// Starting to generate deterministic contract address...
// Successfully found contract address(es) in 2.492678083s
// Address: 0x0000002e9eEF048A3ccDf115aF53A51Ae312870d
// Salt: 0x0000000000000000000000000000000000000000000000006d3aaf0100000000 (7870795717613191168)
address expectedAddress = address(0x0000002e9eEF048A3ccDf115aF53A51Ae312870d);
bytes32 salt = bytes32(0x0000000000000000000000000000000000000000000000006d3aaf0100000000);
Counter counter = new Counter{salt: salt}();
console2.log("Counter address: %s", address(counter));
require(address(counter) == expectedAddress, "Counter address mismatch");
// vm.stopBroadcast();
}
}
Example repo: https://github.com/0xCLARITY/test-create2
I also ran into this issue, still not working on forge 0.2.0 (14daacfe 2024-04-23T13:27:48.189406000Z).
See https://github.com/foundry-rs/foundry/pull/6656, you can set always_use_create_2_factory = true flag which should resolve this :)