iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🍭

Verifying Smart Contracts on Etherscan with Truffle

に公開

Note (Nov 2022 update): I found a plugin called truffle-source-verify that also supports xdai and sokol, so I recommend checking that out if needed.


While you can verify manually from the Etherscan website, it's a lot of work, so here's a note on how to verify from the command line.

The truffle-plugin-verify Truffle plugin is very convenient.

https://github.com/rkalis/truffle-plugin-verify

Installation

  1. Install with npm or yarn
npm install -D truffle-plugin-verify
yarn add -D truffle-plugin-verify
  1. Add the plugin to truffle-config.js

    module.exports = {
      /* ... rest of truffle-config */
    
      plugins: ['truffle-plugin-verify']
    }
    
  2. Create an Etherscan API Key at the Etherscan website

  3. Add the Etherscan API key to truffle-config.js

It is recommended to load the API key via dotenv rather than hardcoding it directly.

module.exports = {
  /* ... rest of truffle-config */

  api_keys: {
    etherscan: 'MY_API_KEY'
  }
}

Usage

Once the smart contract deployment is complete, you can verify one or more contracts with the following command:

truffle run verify SomeContractName AnotherContractName --network networkName [--debug]

For --network, you can specify the network name defined in truffle-config.js.

For example, to verify a SimpleStorage contract deployed on rinkeby, run the following command:

truffle run verify SimpleStorage --network rinkeby

You can also specify the contract address:

truffle run verify SimpleStorage@0x61C9157A9EfCaf6022243fA65Ef4666ECc9FD3D7 --network rinkeby

Supported Chains

Many chains besides Etherscan are supported.

module.exports = {
  /* ... rest of truffle-config */

  api_keys: {
    etherscan: 'MY_API_KEY',
    optimistic_etherscan: 'MY_API_KEY',
    arbiscan: 'MY_API_KEY',
    bscscan: 'MY_API_KEY',
    snowtrace: 'MY_API_KEY',
    polygonscan: 'MY_API_KEY',
    ftmscan: 'MY_API_KEY',
    hecoinfo: 'MY_API_KEY',
    moonscan: 'MY_API_KEY',
    bttcscan: 'MY_API_KEY',
    aurorascan: 'MY_API_KEY',
    cronoscan: 'MY_API_KEY'
  }
}

Please check the official https://github.com/rkalis/truffle-plugin-verify for the latest support status.

Discussion