How to verify your smart contract on etherscan and polygonscan using hardhat

·

4 min read

The first thing you need to know is How to get a etherscan api key or a polygonscan api key.

Etherscan API Key

Go to etherscan.io and create an account if you don't have one, if you have one just sign in. After signing it, Click on your profile and select API Keys from the drop down. Now, click on Add and write the name of the app then create the API Key. You have successfully created your etherscan API Key. Copy the API Key Token and use in your project.

Polygonscan API Key

Go to polygonscan.com and create an account if you don't have one, if you have one just sign in. After signing in, Click on your profile and select API Keys from the drop down. Now, click on Add and write the name of the app then create the API Key. You have successfully created your polygonscan API Key. Copy the Api-Key Token and use in your project.

Go to your .env file and it there, give it any name you like. I am going to call mine Etherscan_API_Key.

I am not going to show you in this tutorial how to set up your config or .env file, i will just be showing you how to verify your smart contract with hardhat. Go to this to learn how to do it. If you have any doubt, check out this tutorial

Your .env file should look like this

ETHERSCAN_API_KEY=2JZ87JIJDSBRCD3USMN4JMDUJNCP5UKIQ9
ALCHEMY_RINKEBY_URL=<YOUR ALCHEMY KEY or YOUR INFURA KEY>
PRIVATE_KEY=your private key

###Packages to install for etherscan verification to work

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

Now, open the hardhat-config file. Make sure you have imported / required the two above packages.

If you are working with javascript

require("dotenv").config({ path: ".env" });
require("@nomiclabs/hardhat-etherscan");

If you are working with typescript

import * as dotenv from "dotenv";
import "@nomiclabs/hardhat-etherscan";

Inside module.exports if you are working with polygon mumbai testnet

etherscan: {
    apiKey: {
      polygonMumbai: ETHERSCAN_API_KEY,
    },
  },

If you are using ethereum rinkeby testnet add

etherscan: {
    apiKey: ETHERSCAN_API_KEY,
  },

Your hardhat config file should look like this for rinkeby testnet

const ALCHEMY_API_KEY_URL = process.env.ALCHEMY_API_KEY_URL;

const PRIVATE_KEY = process.env.PRIVATE_KEY;

const ETHERSCAN_API_KEY = process.env.ETHERSCAN_API_KEY;

module.exports = {
  solidity: "0.8.4",
  networks: {
    rinkeby: {
      url: ALCHEMY_API_KEY_URL,
      accounts: [PRIVATE_KEY],
    },
  },
  etherscan: {
    apiKey: ETHERSCAN_API_KEY,
  },
  },
};

and for polygon mumbai testnet

const ALCHEMY_API_KEY_URL = process.env.ALCHEMY_API_KEY_URL;

const PRIVATE_KEY = process.env.PRIVATE_KEY;

const ETHERSCAN_API_KEY = process.env.ETHERSCAN_API_KEY;

module.exports = {
  solidity: "0.8.4",
  networks: {
    mumbai: {
      url: ALCHEMY_API_KEY_URL,
      accounts: [PRIVATE_KEY],
    },
  },
  etherscan: {
    apiKey: {
      polygonMumbai: ETHERSCAN_API_KEY,
    },
  },
};

###Deploying the smart contract This is where we will be doing the verification, Our smart contract will be verified after it has been deployed, this is because we need the address of the deployed contract to as to verify it.

In this tutorial, we will assume that you already know how to deploy a smart contract with hardhat, if not I recommend you check this out. You will see the section of deploying your contract.

You can find the explanation of the following block of code below it.

import { ethers } from "hardhat";
import hre from "hardhat";

const main = async () => {
  const AYLToken = await ethers.getContractFactory("AYLToken");
  const aylToken = await AYLToken.deploy(ethers.utils.parseEther("40000000.0"));
  await aylToken.deployed();

  console.log("AYLTOken is deployed on ", aylToken.address);
  console.log("Sleeping.......");
  await sleep(400000);

  await hre.run("verify:verify", {
    address: aylToken.address,
    constructorArguments: [ethers.utils.parseEther("40000000.0")],
  });
};

function sleep(ms: number) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

Before the console.log("Sleeping.......") line, we are deploying our contract.

Note: Our contract has a parameter in its constructor, this is very important to take note of since we will be needing it to verify our smart contract.

After deploying our smart contract, we now have our deployed contract address.

This, await sleep(400000); is Very important as many people usually forget about it.

Verification of smart contracts need at least 5 minutes after the smart contract has be deployed before you are able to verify it. If not it will fail.

So to be on the save side, I am waiting for 400,000 miliseconds before verifying my smart contract.

function sleep(ms: number) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

Is used to delay the execution for the number of miliseconds provided.

await hre.run("verify:verify", {
    address: aylToken.address,
    constructorArguments: [ethers.utils.parseEther("40000000.0")],
  });

These lines of code, verifies the smart contract, address is the address of the deployed smart contract constructorArguments is / are the parameters that you provided when deploying the smart contract.

If you did not provide any parameters when deploying your smart contract then use constructorArguments: [],

After doing all these, run npx hardhat compile to compile your smart contract code and run npx hardhat run scripts/deploy.js --network rinkeby (update this as per your selected network). This will deploy and verify your contract. Go to ethercan ot polygonscan and see your verified contract there.

###Conclusion I hope you have learned something out of this article. Feel free to get back to me below or on Twitter and ask your questions.

If you want me to write on how to test your smart contract using hardhat, leave a comment below.