How to verify a contract on Etherscan.
Johnson Chidera
Posted on August 16, 2023
INTRODUCTION
In this tutorial, I will be showing you how to verify a contract with no external imports and also how to verify a contract with external imports.
Etherscan is a Block Explorer and Analytics Platform for Ethereum. With etherscan, users can view and verify smart contracts, check the current balance and transaction history of a particular Ethereum address and witness real-time transactions taking place on the Ethereum network.
Contract verification is important because it gives room for transparency and trust. Users can view your source code since everything will be public once you verify your smart contract. Users can just use your smart contract address to check if your smart contract is verified on etherscan.
Verifying a contract with no external imports.
Let's write a simple smart contract and deploy it using the remix IDE.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
// contract address = 0x2f75D20eFE3115c2334bcc2e4CeB06b3Db30809c
contract Counter {
uint public count = 0;
//get count
function getCount() public view returns(uint){
return count;
}
//increment count
function incrementCount() public {
count += 1;
}
//decrement count
function decrementCount() public {
count -= 1;
}
}
A contract to get count, increment count and decrement count. I have already deployed to the Goerli testnet and gotten a contract address. Now, let us verify it.
Head over to etherscan.io. Hover on the Ethereum logo at the top right corner of your screen to toggle between testnets. Since I used the Goerli testnet, I will choose Goerli testnet. If you deployed your contract to another testnet, choose that testnet.
On the navigation bar, hover on 'Misc' and click on 'verify contract'.
Your screen should be looking like this:
Let's fill the form. Enter in your contract address, compiler type (since I am using a single file, I chose 'Solidity (single file)'), compiler version and license.
This is how my form looks like:
Now, click on 'continue', you will be redirected to another page that looks like this:
There is a form field labeled 'Enter the Solidity Contract Code below'. There, enter in your smart contract code. Don't forget to include your license and compiler version!
There is another field for constructor arguments. Since I don't have a constructor in my contract code, I will leave that place blank. Next, I will verify reCAPTCHA and once I am verified, I click on 'Verify and Publish'. It takes a couple of seconds to verify but when it does, it redirects you to another page showing that your smart contract is officially verified.
Your page should look like this:
Verifying a contract with external imports.
Let's create another contract that inherits from the Counter
contract.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
import './Counter.sol';
contract Name is Counter {
string public name;
constructor(string memory _myName) {
name = _myName;
}
function getName() public view returns(string memory) {
return name;
}
function setName(string memory _newName) public {
name = _newName;
incrementCount();
}
}
Let's break this down. We have a Name
contract that is inheriting from the Counter
contract. A constructor
to set a name. A getName
function that returns the name and a setName
function that sets a new name and also increments our count by 1.
Next thing we are going to do is 'flatten' our Name.sol
file. Which will generate a new file that combines all the source code of our imports plus our current contract code. To flatten our file, click on the plugin icon at the bottom left of your remix IDE and search for the 'FLATTENER' plugin and activate it.
After activation, an icon named 'flattener' should appear at the left hand side of your screen.
Now, let's compile and deploy our Name.sol
contract.
Click on the 'solidity compiler' icon. It has a 'S' shape at the left hand side of your screen. Change your 'compiler version' to '0.8.13' or whatever compiler version you used in your smart contract and then click on the 'Compile Name.sol' button to compile your smart contract.
A green check mark should appear on the 'solidity compiler' icon. Meaning your contract had been compiled successfully.
Now, let us flatten the file before we deploy. Click on the 'flattener' icon and then click on 'Flatten contracts/Name.sol'.
After flattening, a green check mark appears under the 'flattener' icon. Another button, 'Save Name_flat.sol' appears under 'Flatten contracts/Name.sol'. Click on it and a pop-up appears. Asking for permission to create a new file with our flattened contents in it.
Accept it.
Next, a new file called Name_flat.sol
appears. That is the contract we are going to deploy but we still have to compile it first.
_Note: _ If the flattened file doesn't have 'SPDX-license-Identifier', please include it at the top.
After compilation, Let us deploy to the Goerli testnet. Click on the 'deploy and run transactions' icon that is directly under the 'solidity compiler icon'. I will be using 'Injected Web3' as my environment, Metamask pops up and I login. My current testnet appears below my environment. Head over here to get test Goerli eth. Your page should look like the following:
Then I click on deploy. Which prompts Metamask and I need to confirm the transaction.
After deployment, You can view your transaction on etherscan. Check your terminal window for a 'view on etherscan' link.
Clicking on 'view on etherscan' should redirect to another page that includes transaction hash, status, block, timestamp, among others.
Posted on August 16, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.