How to Verify Smart Contracts on BlockScout with Hardhat

How to Verify Smart Contracts on BlockScout with Hardhat

Verifying a smart contract on Blockscout makes the contract source code publicly available and verifiable, which creates transparency and trust in the community.

This guide will walk you through verifying a smart contract on Blockscout with Hardhat.

Prerequisites

Before you begin the steps in this guide, please ensure you have the following:

  • You should have a Hardhat project initialized on your machine.

  • An Etherscan Account. Don't have one? go to Etherscan and sign up for an account.

Verifying the Smart Contract using Hardhat

Step 1: Install the Hardhat-Verify Plugin

The hardhat-verify plugin helps you verify the source code for your Solidity contracts.

In your project directory, install hardhat-verify

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

And add the following statement to your hardhat.config.js:

require("@nomicfoundation/hardhat-verify");

Or, if you are using TypeScript, add this to your hardhat.config.ts:

import "@nomicfoundation/hardhat-verify";

Step 2: Configure Hardhat

Add the following configuration to the config object in hardhat.config.js.

 etherscan: {
    // Your API key for Etherscan
    // Obtain one at https://etherscan.io/
    apiKey: {
      mode: '<ETHERSCAN_API_KEY>',
    },
    customChains: [
      {
        network: 'mode',
        chainId: 919,
        urls: {
          apiURL: 'https://sepolia.explorer.mode.network/api\?',
          browserURL: 'https://sepolia.explorer.mode.network',
        },
      },
    ],
  },

Replace <ETHERSCAN_API_KEY> with your API key for Etherscan. Under your Etherscan account settings, find the “API Keys” section. Generate one API key using the Free Plan.

This is what the hardhat.config.js file looks like:

require('@nomicfoundation/hardhat-toolbox');

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
  solidity: {
    version: '0.8.19',
    settings: {
      evmVersion: 'london',
    },
  },
  networks: {
    'mode': {
      url: 'https://sepolia.mode.network',
      chainId: 919,
      accounts: ['<MY_PRIVATE_KEY>'],
      gasPrice: 10000,
    },
  },
  etherscan: {
    apiKey: {
      mode: '<ETHERSCAN_API_KEY>',
    },
    customChains: [
      {
        network: 'mode',
        chainId: 919,
        urls: {
          apiURL: 'https://sepolia.explorer.mode.network/api\?',
          browserURL: 'https://sepolia.explorer.mode.network',
        },
      },
    ],
  },
};

Step 3: Deploying and Verifying Contracts

Deploy the Contract

In your terminal, run the following command:

npx hardhat run scripts/deploy.js --network mode

Ensure you have enough funds in your account to pay for Gas. You can grab some test tokens from the Faucet to test your contracts before deploying to the mainnet.

Once your smart contract has been successfully deployed, we can now proceed to verify the contract using the contract address.

Verify the contract

In your terminal, run the verify task, passing the address of the contract, the network where it's deployed, and the constructor arguments that were used to deploy it (if any):

npx hardhat verify --network mode DEPLOYED_CONTRACT_ADDRESS "Constructor argument 1"

Where DEPLOYED_CONTRACT_ADDRESS is the contract of your deployed smart contract. Also, replace "Constructor argument 1" (keep the quotes) with the arguments you passed to your smart contract when deploying.

If you are using complex constructor arguments, reference the following Hardhat Documentation.

Conclusion

Congratulations! In this guide, we’ve covered how to verify smart contracts on Blockscout with Hardhat using the hardhat-verify plugin that simplifies the process. Verifying smart contracts is a crucial step in the deployment process because it allows the community to review the source code before usage, thereby promoting transparency and trust within the community.

What's Next:

Check out these other resources from the Mode C00perators: