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

Mehmet Hüseyin Kafadar
Coinmonks
3 min readJul 28, 2021

--

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

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

Create a truffle project

Start your project with truffle init

After this command you have a boilerplate Truffle project.

Let’s code a simple smart contract.

Add below lines to NumberStorage.sol:

Add below lines to truffle-config.js

When you compile contracts you should see a success message:

Output:

Add truffle verification plugin to the project

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

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.

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

Create an api key on Etherscan.io

We need to generate an etherscan.io API key. Sign up and generate your API key here: https://etherscan.io/myapikey

Then add api-keys to truffle-config.js

Create an api key on Infura

infura.io dashboard

Create an account on infura.io and create an Ethereum project (https://infura.io/dashboard/ethereum). Click the settings of your recently created project.

Select Rinkeby testnet and copy your URL. URL should be like that: https://rinkeby.infura.io/v3/YOUR_PROJECT_ID

Add Rinkeby network for deployment

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

Create .env file and add environment variables

.env file should contain:

Create a migration for the contract deployment

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

Deploy the contract

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

It will take a while to verify.

--

--