How to verify Smart Contract with libraries and Constructor Parameters on Polygonscan using Hardhat.

aayushguptacoder

Aayush Gupta

Posted on December 7, 2022

How to verify Smart Contract with libraries and Constructor Parameters on Polygonscan using Hardhat.

When you deploy code to the public, open, global censorship-resistant Ethereum Blockchain, you expect participants will use the functions and services that your Smart Contract provides all over the world. Therefore, it is important to be a good steward of the blockchain community and help the users of your contract be able to trust that the code will do what you claim it will do.

Why Verify?

  • Verifying contracts is important because it ensures that the code is exactly what was deployed onto the blockchain
  • It also allows the public to read and audit your smart contract code
  • Contracts that are verified are considered to be more trustworthy than the ones which are not
  • It also gives you an UI interface to interact with the contracts

UI interface to interact with the contract.png

Build

In this tutorial, we will Verify Smart contract that is already deployed on Polygon Mumbai Test network.

We will also Verify the contract if it has constructor parameters.

Let's now learn how we can leverage HARDHAT for verifying smart contracts with only some lines of code.

Let's goo 🚀🚀🚀

  • We need to install hardhat-etherscan npm package

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

  • hardhat-etherscan npm package is the package from hardhat which will help us with etherscan verification.

  • We will install dotenv package to import the .env file and use it in our config. Open up a terminal and execute this command.

npm install dotenv

  • Now create a .env file in the folder and add the following lines, use the instructions in the comments to get your ALCHEMY_API_KEY_URL, MUMBAI_PRIVATE_KEY and POLYGONSCAN_KEY .If you don't have Mumbai on MetaMask, you can follow this to add it to your MetaMask, make sure that the account from which you get your mumbai private key is funded with mumbai test matic, you can get some from here.
  // Go to https://www.alchemyapi.io, sign up, create
  // a new App in its dashboard and select the network as Mumbai,   
  and replace "add-the-alchemy-key-url-here" with its key url
  ALCHEMY_API_KEY_URL="add-the-alchemy-key-url-here"

  // Replace this private key with your Mumbai account private key
  // To export your private key from Metamask, open Metamask and
  // go to Account Details > Export Private Key
  // Be aware of NEVER putting real Ether into testing accounts
  MUMBAI_PRIVATE_KEY="add-the-mumbai-private-key-here"

  // Go to https://polygonscan.com/, sign up, on your account 
  // overview page,
  // click on `API Keys`, add a new API key and copy the
  // `API Key Token`
  POLYGONSCAN_KEY="add-the-polygonscan-api-token-here"
Enter fullscreen mode Exit fullscreen mode
  • If you deployed your dapp using Hardhat, you will already have MUMBAI_PRIVATE_KEY and ALCHEMY_API_KEY_URL Then you just need to add POLYGONSCAN_KEY in .env.

  • Create a new file named verify.js under the scripts folder. Notice how we are using code to verify the contract.

const { ethers } = require("hardhat");
require("dotenv").config({ path: ".env" });
require("@nomiclabs/hardhat-etherscan");
async function main() {

// Verify the contract after deploying
await hre.run("verify:verify", {
address: "address-of-your-smart-contract",
constructorArguments: [],
});
}
// Call the main function and catch if there is any error
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
Enter fullscreen mode Exit fullscreen mode
  • Now if the constructor of the contract has Parameters, then you need to put the value of that parameter in constructorArguments .
const { ethers } = require("hardhat");
require("dotenv").config({ path: ".env" });
require("@nomiclabs/hardhat-etherscan");
async function main() {
// Verify the contract after deploying
await hre.run("verify:verify", {
address: "address-of-your-smart-contract",
constructorArguments: ["parameter1","parameter2"],
// for example, if your constructor argument took an address, do something like constructorArguments: ["0xABCDEF..."],
});
}
// Call the main function and catch if there is any error
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
Enter fullscreen mode Exit fullscreen mode
  • To verify, open up a terminal and execute this command.

npx hardhat run scripts/verify.js --network mumbai

  • It should have printed a link to Mumbai polygonscan, your contract is now verified. Click on polygonscan link and interact with your contract there 🚀🚀🚀

Polygonscan Contract page after verification.png

Reference

This tutorial is highly inspired by the LearnWeb3DAO tutorial on Hardhat Etherscan Verification.

You can follow me on Twitter, GitHub, and Linkedin. Keep your suggestions and comments coming!

💖 💪 🙅 🚩
aayushguptacoder
Aayush Gupta

Posted on December 7, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related