If you open this etherscan link, you can see that you can interact with this smart contract's functions directly through etherscan, similar to how you used to do it on Remix.
Are you wondering why that doesn't happen for your contract?
- The reason is that the contract mentioned above is verified on etherscan while yours is not.
So lets learn why and how to verify contracts on etherscan 🚀
- 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 trust worthy than the ones which are not
- It also gives you an UI interface to interact with the contracts
- Verifying the code manually on etherscan is very hard because you need to make sure that you not only verify your main contract but also the contracts that you are inheriting or using along with your main contract.
- If you deployed your contract for testing and verified it already with the slightest of changes to your contract you will have to again go through the manual process which gets tedious over time.
Lets now learn how we can leverage hardhat for verifying smart contracts with only some lines of code.
Lets goo 🚀🚀🚀
-
Open up a terminal and execute these commands
mkdir hardhat-verification cd hardhat-verification
-
You will now need to set up your Hardhat project
npm init --yes npm install --save-dev hardhat
-
In the same directory where you installed Hardhat run:
npx hardhat
- Select
Create a Javascript project
- Press enter for the already specified
Hardhat Project root
- Press enter for the question on if you want to add a
.gitignore
- Press enter for
Do you want to install this sample project's dependencies with npm (@nomicfoundation/hardhat-toolbox)?
- Select
Now you have a hardhat project ready to go!
If you are on Windows, please do this extra step and install these libraries as well :)
npm install --save-dev @nomicfoundation/hardhat-toolbox
-
Now create a new file inside the
contracts
directory calledVerify.sol
.//SPDX-License-Identifier: Unlicense pragma solidity ^0.8.4; contract Verify { string private greeting; constructor() { } function hello(bool sayHello) public pure returns (string memory) { if(sayHello) { return "hello"; } return ""; } }
-
We will install
dotenv
package to be able to import the env file and use it in our config. Open up a terminal pointing athardhat-verification
directory and execute this commandnpm install dotenv
-
Mumbai network is one of the testnet's on Polygon. We will learn today how to verify our contracts on mumbai.
-
Now create a
.env
file in thehardhat-verification
folder and add the following lines, use the instructions in the comments to get yourALCHEMY_API_KEY_URL
,MUMBAI_PRIVATE_KEY
andPOLYGONSCAN_KEY
.If you dont 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 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"
-
Lets deploy the contract to
mumbai
network. Create a new file, or replace the existing default one, nameddeploy.js
under thescripts
folder. Notice how we are using code to verify the contract.const { ethers } = require("hardhat"); require("dotenv").config({ path: ".env" }); async function main() { /* A ContractFactory in ethers.js is an abstraction used to deploy new smart contracts, so verifyContract here is a factory for instances of our Verify contract. */ const verifyContract = await ethers.getContractFactory("Verify"); // deploy the contract const deployedVerifyContract = await verifyContract.deploy(); await deployedVerifyContract.deployed(); // print the address of the deployed contract console.log("Verify Contract Address:", deployedVerifyContract.address); console.log("Sleeping....."); // Wait for etherscan to notice that the contract has been deployed await sleep(10000); // Verify the contract after deploying await hre.run("verify:verify", { address: deployedVerifyContract.address, constructorArguments: [], }); } function sleep(ms) { return new Promise((resolve) => setTimeout(resolve, ms)); } // 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 open the
hardhat.config.js
file, we will add themumbai
network here so that we can deploy our contract to mumbai and anetherscan
object so that we can verify our contract onpolygonscan
. Replace all the lines in thehardhat.config.js
file with the given below lines.require("@nomicfoundation/hardhat-toolbox"); require("dotenv").config({ path: ".env" }); const ALCHEMY_API_KEY_URL = process.env.ALCHEMY_API_KEY_URL; const MUMBAI_PRIVATE_KEY = process.env.MUMBAI_PRIVATE_KEY; const POLYGONSCAN_KEY = process.env.POLYGONSCAN_KEY; module.exports = { solidity: "0.8.4", networks: { mumbai: { url: ALCHEMY_API_KEY_URL, accounts: [MUMBAI_PRIVATE_KEY], }, }, etherscan: { apiKey: { polygonMumbai: POLYGONSCAN_KEY, }, }, };
-
Compile the contract, open up a terminal pointing at
hardhat-verification
directory and execute this commandnpx hardhat compile
-
To deploy, open up a terminal pointing at
hardhat-tutorial
directory and execute this commandnpx hardhat run scripts/deploy.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 🚀🚀🚀
To pass the skill test for this level, select the Mumbai
test network below and input the contract address you have verified on the Polygon Mumbai Testnet.
Share the link in Discord, and as always, feel free to ask any questions!