matter-labs / foundry-zksync

Fork of Foundry tailored for zkSync environment
Apache License 2.0
299 stars 130 forks source link

getting "invalid CREATE2 factory output" when attempting Create2 deployment #552

Closed stephankmin closed 2 months ago

stephankmin commented 2 months ago

Component

Forge

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

What version of Foundry are you on?

forge 0.0.2 (bde6d976 2024-08-02T18:50:19.315106000Z)

What command(s) is the bug in?

forge script script/ZkScript.s.sol -vvvvv --zksync --rpc-url https://api.testnet.abs.xyz --private-key $DEPLOYER_PRIVATE_KEY --broadcast

Operating System

macOS (Apple Silicon)

Describe the bug

I'm getting the following error when trying to deploy WETH9 to Abstract Testnet via Create2:

. . .
    │   └─ ← [Return] 0x010000f1b911d44e1b2412e12c90ed63b07875017bbb104e218d71e72bd73e40
    ├─ [0] VM::startBroadcast(0xf3d63166F0Ca56C3c1A3508FcE03Ff0Cf3Fb691e)
    │   └─ ← [Return] 
    ├─ [0] Create2Deployer::create2()
    │   └─ ← [Return] 
    └─ ← [Revert] invalid CREATE2 factory output

My full deployment script is below. I'm compiling with solc version 0.8.24.

I've also tried using the low level system call described by @elfedy in this issue and still seeing the same error.

I'm running the following command: forge script script/ZkScript.s.sol -vvvvv --zksync --rpc-url https://api.testnet.abs.xyz --private-key $DEPLOYER_PRIVATE_KEY --broadcast

Here is my foundry.toml:

[profile.default]
solc_version = "0.8.24"
src = "src"
out = "out"
libs = ["lib"]
via_ir = true

fs_permissions = [{ access = "read", path = "./"}]
contract ZkScript is Script {
    uint256 forkAbstractTestnet;
    address deployer = vm.addr(vm.envUint("DEPLOYER_PRIVATE_KEY"));

    function setUp() public {
        forkAbstractTestnet = vm.createFork("https://api.testnet.abs.xyz");
    }

    function run() public {
        deployWeth();
    }

    function deployWeth() public {
        vm.selectFork(forkAbstractTestnet);
        string memory artifact = vm.readFile("zkout/utils/WETH9.sol/WETH9.json");
        bytes32 bytecodeHash = vm.parseJsonBytes32(artifact, ".hash");
        bytes32 salt = "12345";
        bytes32 constructorInputHash = keccak256(abi.encode());
        address expectedDeployedAddress = _computeCreate2Address(
            deployer,
            salt,
            bytes32(bytecodeHash),
            constructorInputHash
        );

        // deploy via create2
        vm.startBroadcast(deployer);
        address actualDeployedAddress = address(
            new WETH9{salt: salt}()
        );
        assert(expectedDeployedAddress == actualDeployedAddress);
    }

    function _computeCreate2Address(
        address sender,
        bytes32 salt,
        bytes32 creationCodeHash,
        bytes32 constructorInputHash
    ) private pure returns (address) {
        bytes32 zksync_create2_prefix = keccak256("zksyncCreate2");
        bytes32 address_hash = keccak256(
            bytes.concat(
                zksync_create2_prefix,
                bytes32(uint256(uint160(sender))),
                salt,
                creationCodeHash,
                constructorInputHash
            )
        );

        return address(uint160(uint256(address_hash)));
    }
}
dutterbutter commented 2 months ago

@stephankmin here is a min example you can look to reproduce. You may also make use of https://github.com/matter-labs/era-contracts/blob/main/system-contracts/contracts/Create2Factory.sol for easier implementation.