1

I have the following method in my Solidity contract.

function deploy(bytes memory bytecode, uint _salt) public payable returns (address){
    address addr;

    assembly {
        addr := create2(
            callvalue(),
            add(bytecode, 0x20),
            mload(bytecode), 
            _salt 
        )

        if iszero(extcodesize(addr)) {
            revert(0, 0)
        }
    }

    emit Deployed(addr, _salt);
}

The method works if my bytecode is 2000 characters long and the contract is deployed. But when the bytecode gets 10000 characters long the contract is not being deployed and the revert is being called. Is there a limit for bytecode size of create2?

2
  • There is a hard 24KB limit on contract size. The CREATE2 implementation of go-ethereum only checks against that limit for example : github.com/ethereum/go-ethereum/blob/… Nothing specific to CREATE2 as far as I know of. Are you sure about your sizes ?
    – hroussille
    Commented Nov 18, 2021 at 19:29
  • Yes. But maybe as noted in the answer. I am using the full bytecode not the creation code.
    – Armen
    Commented Nov 19, 2021 at 8:04

1 Answer 1

2

There should not be any problem deploying a contract using the create2 if you are able to compile your code. There is a maximum contract size limit of 24KB. Check out this or this if that's the issue.

you should be using creationCode and not runtimeCode. check docs for more info.

Try running this example on remix to test How to Precompute Contract Address with Create2. Add your contract and Update the TestContract with your contract name (type(YOUR_CONTRACT_NAME). creationCode ) and _foo with your contract constructor arguments in getByteCode function.

function getBytecode(address _owner, uint _foo) public pure returns (bytes memory) {
    bytes memory bytecode = type(TestContract).creationCode;

    return abi.encodePacked(bytecode, abi.encode(_owner, _foo));
}

This code will revert if you try to deploy the same bytecode with same salt.

4
  • Thanks for the answer. I need to get the creation code dynamically. Do you know how I can get it from the compiled file?
    – Armen
    Commented Nov 19, 2021 at 7:59
  • When you compile a contract, the creation bytecode is generated for you. A truffle-generated ABI refers to the creation bytecode as bytecode. This is also the bytecode that is shown when clicking “compilation details” for a contract on Remix. Commented Nov 19, 2021 at 11:22
  • In Hardhat artifact bytecode is creation bytecode and deployedBytecode is runtime bytecode. Commented Nov 19, 2021 at 11:34
  • Thanks for the response. I was able to solve the problem. It turned out that I did not add the constructor parameters. After I added them the contract was deployed.
    – Armen
    Commented Nov 19, 2021 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.