Create2: Failed on deploy when using Create2 library

Hi @alexander,

Welcome to the community forum :wave:

If you use a constructor, this is part of the creation code and will impact the CREATE2 deployment address. See Getting the most out of CREATE2 for more details.

A simple example is as follows, with the Vault contract using an Initializer:

Vault.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

import "@openzeppelin/upgrades/contracts/Initializable.sol";

contract Vault is Initializable {
    address payable public owner;

    function initialize(address payable _owner) public initializer {
        owner = _owner;
    }

    function withdraw() public {
        require(owner == msg.sender, "Vault: Not owner");
        owner.transfer(address(this).balance);
    }
}

VaultFactory.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;

import "./Vault.sol";
import "@openzeppelin/contracts/utils/Create2.sol";

contract VaultFactory {
    event VaultCreated(address vault);

    function deployVault(bytes32 salt, address payable owner) public {
        address vaultAddress;

        vaultAddress = Create2.deploy(0, salt, type(Vault).creationCode);
        Vault(vaultAddress).initialize(owner);

        emit VaultCreated(vaultAddress);
    }

    function computeAddress(bytes32 salt) public view returns (address) {
        return
            Create2.computeAddress(salt, keccak256(type(Vault).creationCode));
    }
}

Deploy VaultFactory

$ npx oz deploy
✓ Compiled contracts with solc 0.6.10 (commit.00c0fcaf)
Compilation warnings:
@openzeppelin/upgrades/contracts/Initializable.sol: Warning: SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing "SPDX-License-Identifier: <SPDX-License>" to each source file. Use "SPDX-License-Identifier: UNLICENSED" for non-open-source code. Please see https://spdx.org for more information.

? Choose the kind of deployment regular
? Pick a network development
? Pick a contract to deploy VaultFactory
✓ Deployed instance of VaultFactory
0xe78A0F7E598Cc8b0Bb87894B0F60dD2a88d6a8Ab

Compute address (for salt 0x42)

$ npx oz call
? Pick a network development
? Pick an instance VaultFactory at 0xe78A0F7E598Cc8b0Bb87894B0F60dD2a88d6a8Ab
? Select which function computeAddress(salt: bytes32)
? salt: bytes32: 0x42
✓ Method 'computeAddress(bytes32)' returned: 0xEdbC0b28Ac53390ccd1b987d6348bE97C93c4f53
0xEdbC0b28Ac53390ccd1b987d6348bE97C93c4f53

Deploy (for salt (0x42)

$ npx oz send-tx
? Pick a network development
? Pick an instance VaultFactory at 0xe78A0F7E598Cc8b0Bb87894B0F60dD2a88d6a8Ab
? Select which function deployVault(salt: bytes32, owner: address)
? salt: bytes32: 0x42
? owner: address: 0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1
✓ Transaction successful. Transaction hash: 0x6956a48e6748cd90a51cd578924c63fac36274443e64f4681e5f791b3f3a0769
Events emitted:
 - VaultCreated(0xEdbC0b28Ac53390ccd1b987d6348bE97C93c4f53)