Verify smart contract inheriting from OpenZeppelin Contracts

The recommended way to verify a smart contract inheriting from OpenZeppelin Contracts is as a multi-file contract. It is easier to read, imports are maintained, licenses are maintained.

Verified using Hardhat

Verified using Truffle

Hardhat

Hardhat has an Etherscan plugin: Hardhat Etherscan plugin

Note: Hardhat was previously Buidler.

  1. Install the plugin
npm install --save-dev @nomiclabs/hardhat-etherscan
  1. Configure the plugin in buidler.config.js
    Add require("@nomiclabs/hardhat-etherscan");
    Add Etherscan API key (:warning: keep secret and don’t commit to version control)
    Set compiler version to match what was deployed
// hardhat.config.js
const { infuraProjectId, mnemonic, etherscanApiKey } = require('./secrets.json');

require('@nomiclabs/hardhat-ethers');
require("@nomiclabs/hardhat-etherscan");
/**
 * @type import('hardhat/config').HardhatUserConfig
 */
module.exports = {

  networks: {
    rinkeby: {
      url: `https://rinkeby.infura.io/v3/${infuraProjectId}`,
      accounts: {mnemonic: mnemonic}
    },
    ropsten: {
      url: `https://ropsten.infura.io/v3/${infuraProjectId}`,
      accounts: {mnemonic: mnemonic}
    },
    kovan: {
      url: `https://kovan.infura.io/v3/${infuraProjectId}`,
      accounts: {mnemonic: mnemonic}
    },
    goerli: {
      url: `https://goerli.infura.io/v3/${infuraProjectId}`,
      accounts: {mnemonic: mnemonic}
    },
    mainnet: {
      url: `https://mainnet.infura.io/v3/${infuraProjectId}`,
      accounts: {mnemonic: mnemonic}
    }
  },

  etherscan: {
    // Your API key for Etherscan
    // Obtain one at https://etherscan.io/
    apiKey: etherscanApiKey
  },
  solidity: "0.6.12"
};
  1. Verify
    :warning: Remove any unnecessary contracts and clear the artifacts otherwise these will also be part of the verified contract.
npx buidler verify --network mainnet DEPLOYED_CONTRACT_ADDRESS "Constructor argument 1"

To verify SimpleToken (below) deployed on Rinkeby (https://rinkeby.etherscan.io/address/0x0bB0e851Da4f0149C7c3f77ac3492C824F1f4dD0#code) I ran the following:

$ npx hardhat verify --network rinkeby 0x0bB0e851Da4f0149C7c3f77ac3492C824F1f4dD0
Nothing to compile
Successfully submitted source code for contract
contracts/SimpleToken.sol:SimpleToken at 0x0bB0e851Da4f0149C7c3f77ac3492C824F1f4dD0
for verification on etherscan. Waiting for verification result...
Successfully verified contract on etherscan

Truffle

Truffle has an Etherscan plugin: truffle-plugin-verify

:warning: You need to deploy with Truffle to verify with the Truffle verify plugin.

  1. Install the plugin
npm install -D truffle-plugin-verify
  1. Configure the plugin in truffle-config.js
// truffle-config.js
...
const { infuraProjectId, mnemonic, etherscanApiKey } = require('./secrets.json');
const HDWalletProvider = require('@truffle/hdwallet-provider');

module.exports = {
  ...

  networks: {
    ...
    rinkeby: {
      provider: () => new HDWalletProvider(
        mnemonic, `https://rinkeby.infura.io/v3/${infuraProjectId}`
      ),
      network_id: 4,
      gasPrice: 10e9,
      skipDryRun: true
    },
    ropsten: {
      provider: () => new HDWalletProvider(
        mnemonic, `https://ropsten.infura.io/v3/${infuraProjectId}`
      ),
      network_id: 3,
      gasPrice: 10e9,
      skipDryRun: true
    },
    kovan: {
      provider: () => new HDWalletProvider(
        mnemonic, `https://kovan.infura.io/v3/${infuraProjectId}`
      ),
      network_id: 42,
      gasPrice: 10e9,
      skipDryRun: true
    },
    goerli: {
      provider: () => new HDWalletProvider(
        mnemonic, `https://goerli.infura.io/v3/${infuraProjectId}`
      ),
      network_id: 5,
      gasPrice: 10e9,
      skipDryRun: true
    }
  },

  // Set default mocha options here, use special reporters etc.
  mocha: {
    // timeout: 100000
  },

  // Configure your compilers
  compilers: {
    solc: {
      version: "0.6.12",    // Fetch exact version from solc-bin (default: truffle's version)
      // docker: true,        // Use "0.5.1" you've installed locally with docker (default: false)
      // settings: {          // See the solidity docs for advice about optimization and evmVersion
      //  optimizer: {
      //    enabled: false,
      //    runs: 200
      //  },
      //  evmVersion: "byzantium"
      // }
    }
  },
  plugins: [
    'truffle-plugin-verify'
  ],
  api_keys: {
    etherscan: etherscanApiKey
  }
};
  1. Verify
npx truffle run verify SomeContractName AnotherContractName --network networkName

To verify SimpleToken (below) deployed on Goerli (https://goerli.etherscan.io/address/0x27Bb3535432DE5351f7f892aDF99085f9cfCbfcb#code) I ran the following (from the project I used to deploy SimpleToken):

$ npx truffle run verify SimpleToken --network goerli
Verifying SimpleToken
Pass - Verified: https://goerli.etherscan.io/address/0x27Bb3535432DE5351f7f892aDF99085f9cfCbfcb#contracts
Successfully verified 1 contract(s).

SimpleToken.sol

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

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

/**
 * @title SimpleToken
 * @dev Very simple ERC20 Token example, where all tokens are pre-assigned to the creator.
 * Note they can later distribute these tokens as they wish using `transfer` and other
 * `ERC20` functions.
 */
contract SimpleToken is ERC20 {

    /**
     * @dev Constructor that gives msg.sender all of existing tokens.
     */
    constructor() public ERC20("SimpleToken", "SIM") {
        _mint(msg.sender, 10000000 * (10 ** uint256(decimals())));
    }
}
Can't verify ERC20 contract importing OpenZeppelin
Error verifying ERC20 without constructor arguments on Etherscan
Use Truffle Flattener on Windows with OpenZeppelin Contracts 2.x
Unable to verify simple ERC20 on Rinkeby
Truffle Contract Verification fails for ERC20 extending OpenZeppelin Contracts
Deployed to mainnet but errored with insufficient funds
Best practice for avoiding cyclic dependencies?
Verify with OpenZeppelin CLI
Call upgradeable contract methods through Etherscan
Cannot verify contract deployed via Remix
Cannot verify contract deployed via Remix
Hardhat Etherscan plugin: "Error occurs while verifying contract .."
Does code comply with OpenZeppelin license and copyright terms?
Cannot estimate gas error calling a function
Verifying a contract inheriting from OpenZeppelin Contracts
Verify Proxy on mainnet
Error verifying contract: ABI Invalid constructor arguments
Truffle flattener produces empty file
Unable to verify ERC721 contract on Etherscan
Verify ERC20 token on Etherscan with GitHub imports
ERC20PresetMinterPauser: how to verify on Etherscan and is it safe to use in a Crowdsale?
Use Truffle Flattener on Windows with OpenZeppelin Contracts 2.x
Error verifying ERC20 without constructor arguments on Etherscan
Error verifying ERC721 in Etherscan: File import callback not supported with GitHub imports
Unable to verify contracts on Binance smart chain testnet
Which version of OpenZeppelin is stable to create a new Currency?
Cannot verify ERC20 contract
Can't verify token contract developed with OpenZeppelin Contracts
Verify a contract extending OpenZeppelin Contracts with a constructor deployed from a factory?
Verify a contract extending OpenZeppelin Contracts with a constructor deployed from a factory?
Verify a contract extending OpenZeppelin Contracts with a constructor deployed from a factory?
Truffle Contract Verification fails for ERC20 extending OpenZeppelin Contracts
Will customizing some OpenZeppelin standard contracts make it harder to verify?
Verify contract deployed with Remix
Verify ERC20 token on Etherscan, Invalid constructor arguments provided
Truffle cannot flatten openzeppelin/contracts
Truffle cannot flatten openzeppelin/contracts

A post was split to a new topic: Verify ERC20PresetMinterPauser

3 posts were split to a new topic: Verify contract deployed with Remix