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

Incorrect CREATE2 addresses when not broadcast #6402

Closed
2 tasks done
0xCLARITY opened this issue Nov 22, 2023 · 2 comments
Closed
2 tasks done

Incorrect CREATE2 addresses when not broadcast #6402

0xCLARITY opened this issue Nov 22, 2023 · 2 comments
Labels
T-bug Type: bug

Comments

@0xCLARITY
Copy link

Component

Forge

Have you ensured that all of these are up to date?

  • Foundry
  • 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

@Thegaram
Copy link

I also ran into this issue, still not working on forge 0.2.0 (14daacfe 2024-04-23T13:27:48.189406000Z).

@mds1
Copy link
Collaborator

mds1 commented May 2, 2024

See #6656, you can set always_use_create_2_factory = true flag which should resolve this :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-bug Type: bug
Projects
Archived in project
Development

No branches or pull requests

3 participants