How to Verify a Smart Contract

How to Verify a Smart Contract Using the hardhat-etherscan Plugin

Prerequisites

Before getting started, you'll need the following:

  • Node.js and npm installed
  • Hardhat installed
  • A ScrollExplorer API

Step-by-Step Guide

1. Install the hardhat-etherscan Plugin

Run the following command in the command line to install the hardhat-etherscan plugin:

npm install --save-dev @nomiclabs/hardhat-etherscan

2. Configure the hardhat-etherscan Plugin

Add the following code to your Hardhat configuration file (usually hardhat.config.js) to configure the hardhat-etherscan plugin:

require('@nomiclabs/hardhat-etherscan');

module.exports = {
  // ...
  etherscan: {
  apiKey: "abc",
  customEndpoints: {
    scrollTestnet: {
      name: "scrollTestnet",
      chainId: 534351,
      url: "https://scroll.l2scan.co/api/contract",
      explorer: "https://scroll.l2scan.co",
    },
  },
},

  // ...
};

3. Compile Your Smart Contract

Compile your smart contract using Hardhat by running the following command:

npx hardhat compile

4. Deploy Your Smart Contract

Deploy your smart contract using Hardhat as you normally would.

5. Verify Your Smart Contract

Run the following command to verify your smart contract:

npx hardhat verify --network NETWORK_NAME CONTRACT_ADDRESS "Constructor argument 1" "Constructor argument 2" ...

Replace NETWORK_NAME with the name of the network you deployed your contract to (e.g. scrollTestnet, linea, etc.). Replace CONTRACT_ADDRESS with the address of your deployed contract. If your contract has constructor arguments, include them after the contract address.

6. Check Your Verification Status

After running the verification command, you will receive a verification request ID. You can use this ID to check the status of your verification on ScrollExplorer.

Congratulations! You have successfully verified your smart contract using the hardhat-etherscan plugin.

Demo

This project demonstrates how to use hardhat or foundry to deploy a contract in Scroll's rollup network. And how to use hardhat to verify a contract in Scroll Explorer.

https://github.com/unifra20/scroll-contract-verify-demo