Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Verifying Your Smart Contract on Etherscan

Hello! In our last lesson, we accomplished the monumental task of deploying your smart contracts to the Ethereum mainnet. Your token now exists as an immutable record on a global blockchain. However, if you look at your contract on Etherscan right now, you'll see its address and a long string of hexadecimal characters—the bytecode. For users and other developers, this is opaque and untrustworthy. How can they be sure it does what you claim?

This lesson addresses that exact problem. Our learning outcome is to verify your contract source code on a mainnet block explorer like Etherscan. Verification is the process of linking your human-readable Solidity code to the compiled bytecode on the blockchain. Doing so provides transparency, builds trust, and unlocks the ability for anyone (including you) to interact with your contract directly through Etherscan's user interface.

We'll use Hardhat's powerful automation tools to make this process seamless, transforming your contract's public page from a cryptic block of code into a transparent, auditable, and interactive application.

Why Verification Matters

Before we dive into the "how," let's solidify the "why." When a contract is verified on Etherscan:

  • Transparency and Trust: Anyone can read your source code and be confident that it matches the code running on the blockchain. This is the cornerstone of trust in a decentralized ecosystem.
  • User Interaction: Etherscan generates a user-friendly interface with Read Contract and Write Contract tabs. This allows non-technical users (and you!) to call functions, check balances, and interact with the contract without needing to build a custom frontend.
  • Developer Composability: Other developers can easily understand how your contract works, making it simpler for them to integrate it into their own dApps.
Etherscan Contract Verification Interface
This image shows the manual verification form on Etherscan. You'd have to fill in the compiler version, optimization settings, and paste in your source code. Our goal is to have Hardhat do all of this for us automatically.

The Automated Approach: The hardhat-verify Plugin

While you can verify contracts manually by filling out the form on Etherscan, this is tedious and error-prone, especially for contracts that import other files (like our OpenZeppelin-based token).

A much better way is to use a Hardhat plugin that automates this entire process. We will use the official @nomicfoundation/hardhat-verify plugin. This tool will read your configuration, package your source code, and send it to the Etherscan API for verification with a single command.

Step 1: Setting Up Your Project for Verification

There are two essential one-time setup steps to enable automated verification.

1. Install the Plugin

If you installed the hardhat-toolbox package when you first created your project, this plugin is already included. If not, or to be sure, you can install it explicitly.

Run the following command in your project's terminal:

npm install --save-dev @nomicfoundation/hardhat-verify

2. Configure hardhat.config.js

The plugin needs to know your Etherscan API key to submit verification requests on your behalf.

First, you need to get an API key:

  1. Go to Etherscan.io and create a free account.
  2. Navigate to your user dashboard, click on "API Keys" in the left-hand menu.
  3. Click "Create a new API key" and give it a name (e.g., "Hardhat Project").
  4. Copy the generated API key.

Just like with your private key, you must not hard-code this key into your configuration file. We will use Hardhat's secure variable manager.

Run this command in your terminal, pasting your new API key:

npx hardhat vars set ETHERSCAN_API_KEY "YOUR_API_KEY_HERE"

Now, add the etherscan configuration to your hardhat.config.js file.

hardhat-verify

The official Hardhat documentation provides the definitive guide on how to configure the verification plugin. We will add the verify block to our configuration.

Read the sections 'Installation' and 'Verifying on Etherscan'. Focus on the part that shows how to add the verify object to your hardhat.config.ts (or .js) file and how to use a configuration variable for the apiKey.

Your hardhat.config.js should now look something like this:

require("@nomicfoundation/hardhat-toolbox");
const { vars } = require("hardhat/config");

// Load variables
const MAINNET_RPC_URL = vars.get("MAINNET_RPC_URL");
const MAINNET_PRIVATE_KEY = vars.get("MAINNET_PRIVATE_KEY");
const ETHERSCAN_API_KEY = vars.get("ETHERSCAN_API_KEY");

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
  solidity: "0.8.20",
  networks: {
    mainnet: {
      url: MAINNET_RPC_URL,
      accounts: [MAINNET_PRIVATE_KEY],
    },
  },
  etherscan: { // <-- Add this block
    apiKey: ETHERSCAN_API_KEY,
  },
};

With this setup complete, you are ready to verify any contract you deploy.

Step 2: Running the Verification Command

Let's watch a video that demonstrates the process. Note that the video uses a slightly older method (secrets.json) for managing keys and references an older plugin name (@nomiclabs/hardhat-etherscan), but the core command and the result on Etherscan are the same. We will stick to our more modern hardhat vars approach.

How to Verify Smart Contracts with Hardhat

The video 'How to Verify Smart Contracts with Hardhat' by Moralis for Developers walks through the exact command we'll be using. It clearly shows the 'before' and 'after' on Etherscan.

Watch the section from 03:17 to 05:04. Pay attention to the npx hardhat verify command being run and the resulting verified contract page on Etherscan, complete with the green checkmark.

The command to verify a contract has the following structure:

npx hardhat verify --network <NETWORK_NAME> <CONTRACT_ADDRESS> [CONSTRUCTOR_ARGUMENTS]

Let's break that down:

  • npx hardhat verify: The command to run the verification task.
  • --network mainnet: Tells Hardhat which network the contract is deployed on. This must match a network name in your hardhat.config.js.
  • CONTRACT_ADDRESS: The address of the deployed contract you saved from the last lesson.
  • CONSTRUCTOR_ARGUMENTS: A space-separated list of the arguments you passed to the contract's constructor during deployment. These must be in the correct order.

For example, if your ERC-20 token was deployed with the name "My Awesome Token" and the symbol "MAT", your command would look like this:

npx hardhat verify --network mainnet 0xYourContractAddressHere "My Awesome Token" "MAT"

If your constructor had no arguments, you would simply omit that part of the command.

Test your understanding!

You deployed your ERC-20 token, which has the constructor constructor(string memory name, string memory symbol, uint256 initialSupply). The deployed address on mainnet is 0x55d398326f99059fF775485246999027B3197955. You deployed it with the name "MyCoin", the symbol "MC", and an initial supply of 1000000000000000000000000 (1 million tokens with 18 decimals). What is the exact command you would run to verify it?

Show answer
npx hardhat verify --network mainnet 0x55d398326f99059fF775485246999027B3197955 "MyCoin" "MC" 1000000000000000000000000

Note that string arguments are enclosed in quotes, while the uint256 number is not.

Your Turn: Verify Your Contracts

Now, it's time to verify the contracts you deployed in the previous lesson.

  1. Retrieve your deployed contract addresses and the exact constructor arguments used in your deployment script.
  2. Open your terminal in your project's root directory.
  3. Run the npx hardhat verify command for each of your contracts, substituting the correct address and arguments.

Important: Sometimes it takes a few minutes for Etherscan to index your new contract. If you run the command immediately after deployment and get an error like Error: The address 0x... does not have bytecode, wait a minute or two and try again.

Upon success, the terminal will print a message and a link to your newly verified contract page on Etherscan. Click it and admire the green checkmark next to the "Contract" tab!

Conclusion

You have now made your project fully transparent and trustworthy to the entire Ethereum community. This is a standard and expected step for any serious project, and mastering the automated workflow with Hardhat is a key skill for any blockchain developer.

Key Takeaways:

  • Verification is essential for trust and usability. It proves your source code matches the on-chain bytecode.
  • The hardhat-verify plugin is the most efficient way to verify contracts in a Hardhat project.
  • Setup requires installing the plugin, getting an Etherscan API key, and configuring it securely in hardhat.config.js.
  • The verification command is npx hardhat verify --network <network> <address> [constructor_args].

Preview of the Next Lesson:

With your contracts deployed and verified, they are live but might still require initial setup. For example, if you have a staking contract, it needs to be funded with reward tokens. If your token supply is managed by the owner, you might need to mint an initial batch. In our next lesson, "Execute the initial token distribution and setup for your project," we will use the Write Contract functionality on Etherscan and Hardhat scripts to perform these crucial day-one administrative tasks.

Can't find a good explanation? Sign up and we'll make it for you

Sign up