When does `create2(...)` return the zero address?

Hey OZ community,


I am using the "@openzeppelin/contracts/utils/Create2.sol"; library, with the following deploy function.

function deploy(
        uint256 amount,
        bytes32 salt,
        bytes memory bytecode
    ) internal returns (address) {
        address addr;
        require(address(this).balance >= amount, "Create2: insufficient balance");
        require(bytecode.length != 0, "Create2: bytecode length is zero");
        assembly {
            addr := create2(amount, add(bytecode, 0x20), mload(bytecode), salt)
        require(addr != address(0), "Create2: Failed on deploy");
        return addr;

We've been using the Create2 library in production for a while (here), and only recently noticed a Create2: Failed on deploy error faced by one of our users.


In what cases is the value of addr in addr := create2(amount, add(bytecode, 0x20), mload(bytecode), salt) the zero address?

It returns the zero address when you attempt to call create2 with the same bytecode and the same salt more than once, i.e. deploy the same contract twice.

If you could overwrite it, then it would break the immutability feature of smart contracts.

There was much discussion on EIP-1167 about this - if the contract you proxy to has a selfdestruct method, I believe (though am not 100% certain) that this would mean the logic behind an address could change.

You can confirm this behavior by calling the above method twice in Remix.