How to verify contract on etherscan deployed using create2?

A contract deploy a contract B with create2 ,how to verify contract B?

1 Like

Hi @pageone,

As far as I know verification should be the same as if the contract was deployed using create.

Did you run into an error?

Yes, contracts deployed with create2 won't verify on bscscan: Error! Unable to generate Contract ByteCode and ABI... we were unable to locate a matching bytecode (err_code_2)

Not sure how to proceed to debug, I'll start with a much smaller test contract and see.

So first flattening the Vault.sol contract and compiling with 0.5.17

// File: @openzeppelin/upgrades/contracts/Initializable.sol

pragma solidity >=0.4.24 <0.7.0;


/**
 * @title Initializable
 *
 * @dev Helper contract to support initializer functions. To use it, replace
 * the constructor with a function that has the `initializer` modifier.
 * WARNING: Unlike constructors, initializer functions must be manually
 * invoked. This applies both to deploying an Initializable contract, as well
 * as extending an Initializable contract via inheritance.
 * WARNING: When used with inheritance, manual care must be taken to not invoke
 * a parent initializer twice, or ensure that all initializers are idempotent,
 * because this is not dealt with automatically as with constructors.
 */
contract Initializable {

  /**
   * @dev Indicates that the contract has been initialized.
   */
  bool private initialized;

  /**
   * @dev Indicates that the contract is in the process of being initialized.
   */
  bool private initializing;

  /**
   * @dev Modifier to use in the initializer function of a contract.
   */
  modifier initializer() {
    require(initializing || isConstructor() || !initialized, "Contract instance has already been initialized");

    bool isTopLevelCall = !initializing;
    if (isTopLevelCall) {
      initializing = true;
      initialized = true;
    }

    _;

    if (isTopLevelCall) {
      initializing = false;
    }
  }

  /// @dev Returns true if and only if the function is running in the constructor
  function isConstructor() private view returns (bool) {
    // extcodesize checks the size of the code stored in an address, and
    // address returns the current address. Since the code is still not
    // deployed when running a constructor, any checks on its code size will
    // yield zero, making it an effective way to detect if a contract is
    // under construction or not.
    address self = address(this);
    uint256 cs;
    assembly { cs := extcodesize(self) }
    return cs == 0;
  }

  // Reserved storage space to allow for layout changes in the future.
  uint256[50] private ______gap;
}

// File: contracts/Vault.sol

// contracts/Vault.sol
pragma solidity 0.5.17;


contract Vault is Initializable {
    address payable owner;

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

    function withdraw() public {
        require(owner == msg.sender);
        owner.transfer(address(this).balance);
    }

 //  owner = msg.sender;

 function close() public {
  selfdestruct(owner);
 }


}

Then the usual steps to:

npx oz create2 Vault --salt 12345 --init --args 0xC114C2C34aBa31407fb315e8634b0e6F13BB0eCD --network bscTestnet

nets a failure to verify:

**Compiler debug log:**
Error! Unable to generate Contract ByteCode and ABI
Found the following ContractName(s) in source code : **Initializable** , **Vault**
But we were unable to locate a matching bytecode (err_code_2)
For troubleshooting, you can try compiling your source code with the [Remix - Solidity IDE](https://remix.ethereum.org/) and check for exceptions

* **Compiler Version:** v0.5.17+commit.d19bba13
* **Optimization Enabled:** True
* **Runs:** 200

**ByteCode (what we are looking for)** :
608060405234801561001057600080fd5b50610d0d806100206000396000f3fe6080604052600436106100705760003560e01c80638f2839701161004e5780638f283970146101bb578063cf7a1d771461020c578063d1f5789414610307578063f851a440146103e257610070565b80633659cfe61461007a5780634f1ef286146100cb5780635c60da1b14610164575b610078610439565b005b34801561008657600080fd5b506100c96004803603602081101561009d57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610453565b005b610162600480360360408110156100e157600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291908035906020019064010000000081111561011e57600080fd5b82018360208201111561013057600080fd5b8035906020019184600183028401116401000000008311171561015257600080fd5b90919293919293905050506104a8565b005b34801561017057600080fd5b50610179610580565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b3480156101c757600080fd5b5061020a600480360360208110156101de57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506105d8565b005b6103056004803603606081101561022257600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff1690602001909291908035906020019064010000000081111561027f57600080fd5b82018360208201111561029157600080fd5b803590602001918460018302840111640100000000831117156102b357600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610753565b005b6103e06004803603604081101561031d57600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291908035906020019064010000000081111561035a57600080fd5b82018360208201111561036c57600080fd5b8035906020019184600183028401116401000000008311171561038e57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610819565b005b3480156103ee57600080fd5b506103f76109a4565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b6104416109fc565b61045161044c610a94565b610ac5565b565b61045b610aeb565b73ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16141561049c5761049781610b1c565b6104a5565b6104a4610439565b5b50565b6104b0610aeb565b73ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610572576104ec83610b1c565b60008373ffffffffffffffffffffffffffffffffffffffff168383604051808383808284378083019250505092505050600060405180830381855af49150503d8060008114610557576040519150601f19603f3d011682016040523d82523d6000602084013e61055c565b606091505b5050905080151561056c57600080fd5b5061057b565b61057a610439565b5b505050565b600061058a610aeb565b73ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614156105cc576105c5610a94565b90506105d5565b6105d4610439565b5b90565b6105e0610aeb565b73ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16141561074757600073ffffffffffffffffffffffffffffffffffffffff168173ffffffffffffffffffffffffffffffffffffffff161415151561069b576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401808060200182810382526036815260200180610c716036913960400191505060405180910390fd5b7f7e644d79422f17c01e4894b5f4f588d331ebfa28653d42ae832dc59e38c9798f6106c4610aeb565b82604051808373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019250505060405180910390a161074281610b6b565b610750565b61074f610439565b5b50565b600073ffffffffffffffffffffffffffffffffffffffff16610773610a94565b73ffffffffffffffffffffffffffffffffffffffff1614151561079557600080fd5b61079f8382610819565b600160405180807f656970313936372e70726f78792e61646d696e000000000000000000000000008152506013019050604051809103902060001c0360001b7fb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d610360001b14151561080b57fe5b61081482610b6b565b505050565b600073ffffffffffffffffffffffffffffffffffffffff16610839610a94565b73ffffffffffffffffffffffffffffffffffffffff1614151561085b57600080fd5b600160405180807f656970313936372e70726f78792e696d706c656d656e746174696f6e00000000815250601c019050604051809103902060001c0360001b7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc60001b1415156108c757fe5b6108d082610b9a565b6000815111156109a05760008273ffffffffffffffffffffffffffffffffffffffff16826040518082805190602001908083835b6020831015156109295780518252602082019150602081019050602083039250610904565b6001836020036101000a038019825116818451168082178552505050505050905001915050600060405180830381855af49150503d8060008114610989576040519150601f19603f3d011682016040523d82523d6000602084013e61098e565b606091505b5050905080151561099e57600080fd5b505b5050565b60006109ae610aeb565b73ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614156109f0576109e9610aeb565b90506109f9565b6109f8610439565b5b90565b610a04610aeb565b73ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614151515610a8a576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401808060200182810382526032815260200180610c3f6032913960400191505060405180910390fd5b610a92610c29565b565b6000807f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc60001b9050805491505090565b3660008037600080366000845af43d6000803e8060008114610ae6573d6000f35b3d6000fd5b6000807fb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d610360001b9050805491505090565b610b2581610b9a565b8073ffffffffffffffffffffffffffffffffffffffff167fbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b60405160405180910390a250565b60007fb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d610360001b90508181555050565b610ba381610c2b565b1515610bfa576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252603b815260200180610ca7603b913960400191505060405180910390fd5b60007f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc60001b90508181555050565b565b600080823b90506000811191505091905056fe43616e6e6f742063616c6c2066616c6c6261636b2066756e6374696f6e2066726f6d207468652070726f78792061646d696e43616e6e6f74206368616e6765207468652061646d696e206f6620612070726f787920746f20746865207a65726f206164647265737343616e6e6f742073657420612070726f787920696d706c656d656e746174696f6e20746f2061206e6f6e2d636f6e74726163742061646472657373a165627a7a7230582040dc1aa42759a5afcff393e7e45a4fd76785fa575d84b890318d3c652a4d5e360029


- vs what we got -

We tried looking for a match from the list of compiled contract bytecode outputs (as listed below), but was unable to find an exact match.

1) **Initializable**
6080604052348015600f57600080fd5b50603e80601d6000396000f3fe6080604052600080fdfe
{bzzr}64736f6c63430005110032

2) **Vault**
608060405234801561001057600080fd5b50610206806100206000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c80633ccfd60b1461004657806343d726d614610050578063c4d66de814610058575b600080fd5b61004e61007e565b005b61004e6100d1565b61004e6004803603602081101561006e57600080fd5b50356001600160a01b03166100df565b6033546001600160a01b0316331461009557600080fd5b6033546040516001600160a01b03909116904780156108fc02916000818181858888f193505050501580156100ce573d6000803e3d6000fd5b50565b6033546001600160a01b0316ff5b600054610100900460ff16806100f857506100f861019d565b80610106575060005460ff16155b6101415760405162461bcd60e51b815260040180806020018281038252602e8152602001806101a4602e913960400191505060405180910390fd5b600054610100900460ff1615801561016c576000805460ff1961ff0019909116610100171660011790555b603380546001600160a01b0319166001600160a01b0384161790558015610199576000805461ff00191690555b5050565b303b159056fe436f6e747261637420696e7374616e63652068617320616c7265616479206265656e20696e697469616c697a6564
{bzzr}64736f6c63430005110032

You can see the compiled bytecode on bscscan.com:

Any advice would be most appreciated!

Continuing this conversation in Verifying CREATE2 code on bscscan/etherscan -- how to do it?.

Will post the solution in the general verificaiton FAQ once we find it: