How to auto verify smart contracts on Etherscan & Bscscan with Truffle


Smart contract verification is important for decentralized projects. You can publish your project codes on with contract verification. Also, read and write function interactions will be available on when you verify the contract.

Although manual verification is available on, making verifications directly with Truffle is the best practice.

Create a truffle project

Start your project with truffle init

mkdir truffle-project
cd truffle-project
truffle init

After this command you have a boilerplate Truffle project.

Let’s code a simple smart contract.

touch contracts/NumberStorage.sol

Add below lines to NumberStorage.sol:

Add below lines to truffle-config.js

compilers: {
solc: {
version: "0.8.6", // Fetch exact version from solc-bin (default: truffle's version)
docker: false, // 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: true,
runs: 200,
evmVersion: "byzantium",

When you compile contracts you should see a success message:

truffle compile


> Compiled successfully using:
- solc: 0.8.6+commit.11564f7e.Emscripten.clang

Add truffle verification plugin to the project

yarn add -D truffle-plugin-verify

Then add truffle-plugin-verify inside of plugins array to truffle-config.js

plugins: [

We will install 2 packages. One for the .env file and the other one for the private key. You can use a private key provider or mnemonic provider. Both of them will work.

yarn add -D dotenv truffle-privatekey-provider

Add those lines at the top of your truffle-config.js

const PrivateKeyProvider = require("truffle-privatekey-provider");

Create an api key on

We need to generate an API key. Sign up and generate your API key here:

Then add api-keys to truffle-config.js

api_keys: {
etherscan: process.env.ETHERSCAN_API_KEY

Create an api key on Infura dashboard

Create an account on and create an Ethereum project ( Click the settings of your recently created project.

Select Rinkeby testnet and copy your URL. URL should be like that:

Add Rinkeby network for deployment

We are about to deploy our smart contract. We need to add Rinkeby network config to truffle-config.js

networks: {
rinkeby: {
provider: () => new PrivateKeyProvider(process.env.PRIVATE_KEY, process.env.RINKEBY_INFURA_URL),
network_id: 4, // Ropsten's id
gas: 5500000, // Ropsten has a lower block limit than mainnet
confirmations: 2, // # of confs to wait between deployments. (default: 0)
timeoutBlocks: 200, // # of blocks before a deployment times out (minimum/default: 50)
skipDryRun: true // Skip dry run before migrations? (default: false for public nets )

Create .env file and add environment variables

touch .env

.env file should contain:


Create a migration for the contract deployment

truffle create migration NumberStorage

You will see a new file in the migrations folder. We will add those lines to number_storage.js

module.exports = async function(deployer) {
const NumberStorage = artifacts.require("NumberStorage");

await deployer.deploy(NumberStorage);
const contract = await NumberStorage.deployed();

console.log("Contract address:", contract.address)

Deploy the contract

truffle migrate --network rinkeby

It will take a few minutes to deploy the contract.

When deployment is completed you will see this message:

Contract is not verified yet, let’s verify it.

Verify the contract

truffle run verify NumberStorage --network rinkeby

It will take a while to verify.

Contract is verified:

Repository of the project:

Also, Read

Set up MetaMask for Binance Smart Chain

How to auto verify smart contracts on Etherscan & Bscscan with Truffle was originally published in Coinmonks on Medium, where people are continuing the conversation by highlighting and responding to this story.