Skip to content

Verify on Etherscan

View the Full Course Now

One thing that comes up every now and then is

  1. how to verify smart contracts on block explorers
  2. why verify them in the first place
  3. how to directly interact with smart contracts using the Etherscan interface.

Why Verify Smart Contracts

First and foremost, in an open society that fosters transparency, it is considered good practice to open source the source code. Your bytecode is there anyways. But having the source code there is an additional step towards transparency.

In a world where smart contracts can handle multiple millions of dollars, its hard to justify for the wider public to interact with smart contracts if they can't be verified.

Verifying smart contracts also lets other easier interact with them, as you'll see in a second.

But how to go about that?

How to Verify A Smart Contract manually on Etherscan

Let's run with a sample smart contract

//SPDX-License-Identifier: MIT

pragma solidity 0.8.16;

contract MyContract {
    mapping(address => uint) public balance;

    constructor() {
        balance[msg.sender] = 100;
    }

    function transfer(address to, uint amount) public {
        balance[msg.sender] -= amount;
        balance[to] += amount;
    }

    function someCrypticFunctionName(address _addr) public view returns(uint) {
        return balance[_addr];
    }
}

Step by step:

  1. Sign in to Etherscan

  1. If you don't have an account, you need to signup first

  1. It requires an email address

  1. Checks for the robot...

  1. ... and create an account

  1. Then sign in, same here, robot check

  1. And Login

  1. Let's head over to Remix, open a new Tab

  1. Create a new file

  1. Add the contract from above

  1. make sure its the contract

  1. Head over to the Deploy & Run Transaction Tab

  1. Select the MetaMask injected web3 provider

  1. If your MetaMask is locked, then unlock it first

  1. Click "DEPLOY"

  1. Click "Confirm"

  1. Click "View on Etherscan" to open the transaction on Etherscan

  1. Click on the new Contract (the address) to open the contract

  1. Then click on the Contract - you see its just bytecode there. Etherscan can't guess your interface, your function names, nothing.

Function names

Sometimes Etherscan infers a function name. That is, for interfaces from known contracts, like the ERC20 contract. If you interact with this smart contract and use the transfer function, Etherscan would probably show you correctly that the function "transfer" was used. But if you use the function someCrypticFunctionName it would just show you the 4 byte hashed function signature.

  1. That's the unverified bytecode

  1. Let's verify that Smart Contract now! Click "Verify and Publish"

  1. There are two things to select: The compiler and the license

  1. Select Solidity single-file from the compiler

You will later see that we can automate the source code verification with scripts from Truffle, Hardhat and Foundry. But, as you're an Ethereum developer, its good practice to have that done at least once manually...

  1. Once you select Solidity, another dropdown will appear for the compiler version

  1. Select 0.8.16, or whatever compiler you used for your contract...

  1. I'm licensing this contract as MIT - select the correct license here.

  1. You see there are a ton of licenses. That might be parsed in later versions directly from the source code file.

SPDX License Identifier

That identifier wasn't always in there. So, naturally, not all tools are using this identifier to determine the license.

  1. Let's continue now and see what happens!

  1. We need to copy the source code from Remix into the source code field

  1. Head back to Remix, copy the Source code

  1. And paste the source code

  1. Sometimes you need to specify the compiler optimizations. To check if you had any, head back to Remix, and click "Advanced Configurations" in the compiler plugin

  1. If this checkbox is checked, then set the appropriate optimizations in Etherscan as well

  1. Finally, our Bytecode

  1. And the ABI Array

  1. All green!

  1. It should say something like "Successfully generated ByteCode and ABI for Contract Address "

  1. "ContractABI:"

  1. Click on your contract address to get back

  1. Click "Contract" - which now has a little check next to it

  1. Click "Read Contract"

  1. You see, now you have even the most crypto function names as cleartext here to interact with.

  1. Let's interact with our smart contract. Click "Write"

  1. Click "Connect to Web3"

  1. Confirm the dialog box

  1. Select "MetaMask" from the options

  1. Select "Account 1" to connect

  1. Click "Next"

  1. Click "Connect"

  1. Click "1. transfer"

  1. Let's copy the second address from MetaMask...

  1. Click here:

  1. Click "0.4897"

  1. Click "Account 2 0x99b...d4F6"

  1. Click "  amount (uint256)"

  1. Click "Write"

  1. Click "Confirm"

  1. Click "Read Contract"

  1. Now lets read out the balance. Click "1. balance" and paste in the same account that we used for transferring the funds to

  1. And simple click "Query"

It should show you "1" as balance.