Skip to content

Verify on Etherscan

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

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

  3. It requires an email address

  4. Checks for the robot...

  5. ... and create an account

  6. Then sign in, same here, robot check

  7. And Login

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

  9. Create a new file

  10. Add the contract from above

  11. make sure its the contract

  12. Head over to the Deploy & Run Transaction Tab

  13. Select the MetaMask injected web3 provider

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

  15. Click "DEPLOY"

  16. Click "Confirm"

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

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

  19. 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.

  20. That's the unverified bytecode

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

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

  23. 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...

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

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

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

  27. 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.

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

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

  30. Head back to Remix, copy the Source code

  31. And paste the source code

  32. 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

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

  34. Finally, our Bytecode

  35. And the ABI Array

  36. All green!

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

  38. "ContractABI:"

  39. Click on your contract address to get back

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

  41. Click "Read Contract"

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

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

  44. Click "Connect to Web3"

  45. Confirm the dialog box

  46. Select "MetaMask" from the options

  47. Select "Account 1" to connect

  48. Click "Next"

  49. Click "Connect"

  50. Click "1. transfer"

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

  52. Click here:

  53. Click "0.4897"

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

  55. Click "  amount (uint256)"

  56. Click "Write"

  57. Click "Confirm"

  58. Click "Read Contract"

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

  60. And simple click "Query"

It should show you "1" as balance.


Last update: September 4, 2022