How to verify Smart Contract with libraries and Constructor Parameters on Polygonscan using Hardhat.
Tags: verify, hardhat, blockchain, tutorial
Published: 2022-12-07 10:53:50

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wwgmfr2nfkmja926t1a0.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.

``` JavaScript
  // 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"
```

- 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.

```javaScript
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);
});
```

- Now if the constructor of the contract has Parameters, then you need to put the value of that parameter in constructorArguments .

``` javaScript
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);
});
```

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1g59l03fgzpyks7drgfw.png)

## Reference
This tutorial is highly inspired by the [LearnWeb3DAO](https://learnweb3.io/) tutorial on [Hardhat Etherscan Verification](https://learnweb3.io/courses/6394ea7c-0ad6-4a4a-879f-7f9756bc5976/lessons/2012a201-58e1-4d36-9a9b-aafd76caedf4).


You can follow me on [Twitter](https://twitter.com/Aayush_gupta_ji), [GitHub](https://github.com/AAYUSH-GUPTA-coder), and [Linkedin](https://www.linkedin.com/in/aayush-gupta-20023b183/). Keep your suggestions and comments coming!