Step-by-Step Guide to Verifying Smart Contracts with Hardhat
A Beginner's Guide to Verifying Smart Contracts with Hardhat
Table of contents
Introduction :
Imagine you're buying a house. You wouldn't just take the seller's word for it that everything is in order, right? You'd want to see the deed, inspect the property, and make sure everything is legit. That's kind of what we do with smart contracts in the blockchain world.
Trust: It's like showing your work in a math problem. People can see exactly what you did and trust the result.
Safety: It lets other smart people check for any problems, kind of like having a home inspector look at a house before you buy it.
Teamwork: It helps other developers understand how to work with your contract, like sharing a recipe so others can cook the same dish.
Before we start, you'll need a few things:
A well tested smart contract
Hardhat (a tool for developing smart contracts) set up on your system
An Etherscan API key (think of it like a special key for using Etherscan's services)
Some test ETH (fake money for practice)
Now, let's walk through how to verify your smart contract step by step!
Understanding Hardhat Ignition Modules:
Before we dive in, let's talk about something called Hardhat Ignition modules. These are special files that tell the computer how to deploy (put online) your smart contract. Think of them like a set of instructions for baking a cake. Here's what a simple one looks like:
import { buildModule } from "@nomicfoundation/hardhat-ignition/modules";
const MyContractModule = buildModule("MyContractModule", (m) => {
const myContract = m.contract("MyContract");
return { myContract };
});
export default MyContractModule;
Step 1: we need to get a special key from a website called Etherscan:
Go to Etherscan.io and sign up (or log in if you already have an account).
Find the "API Keys" section.
Click "Add" and give your key a name (like "Hardhat").
Copy the key they give you.
Now, we'll store this key safely in our project:
npx hardhat vars set ETHERSCAN_API_KEY
When it asks, paste in the key you copied.
Step 2: Set Up Hardhat
Open your Hardhat settings file (it's called hardhat.config.js or hardhat.config.ts) and add this:
import { vars } from "hardhat/config";
const ETHERSCAN_API_KEY = vars.get("ETHERSCAN_API_KEY");
export default {
// ... other settings ...
etherscan: {
apiKey: ETHERSCAN_API_KEY,
},
};
Step 3: Set Up Network Settings
We're going to use a test network called Sepolia. Add these settings to your Hardhat file:
const ALCHEMY_API_KEY = vars.get("ALCHEMY_API_KEY");
const SEPOLIA_PRIVATE_KEY = vars.get("SEPOLIA_PRIVATE_KEY");
module.exports = {
// ... other settings ...
networks: {
sepolia: {
url: `https://eth-sepolia.g.alchemy.com/v2/${ALCHEMY_API_KEY}`,
accounts: [SEPOLIA_PRIVATE_KEY],
},
},
etherscan: {
apiKey: {
sepolia: ETHERSCAN_API_KEY,
},
},
};
Make sure to set the ALCHEMY_API_KEY
and SEPOLIA_PRIVATE_KEY
just like you did with the Etherscan key.
Step 4: Prepare Your Contract
To make your contract unique, add a comment with something special, like your name:
// Author: @your_name_here
contract MyContract {
// ... your contract code ...
}
Step 5: Deploy Your Contract
Now we'll use Hardhat Ignition to put your contract online:
npx hardhat ignition deploy ignition/modules/MyContract.js --network sepolia --deployment-id sepolia-deployment
This is like telling the computer, "Hey, use these instructions to put my contract on the Sepolia test network, and call it 'sepolia-deployment'."
Step 6: Verify Your Contract
After it's deployed, we'll verify it:
npx hardhat ignition verify sepolia-deployment
Or, you can do both steps at once:
npx hardhat ignition deploy ignition/modules/MyContract.ts --network sepolia --verify
If it works, you'll get a link to see your verified contract on Etherscan.
Once the contract is verified you will see a green tick above the Contract button:
Conclusion :
Verifying your smart contracts is super important. It's like showing your work in a math test – it proves you did everything right and helps others understand what you did.
By using Hardhat and Ignition modules, we've made it easier to put your contract online and show everyone it's legit. It's like having a recipe and a helper in the kitchen – they make baking that cake (or in our case, deploying and verifying a contract) much simpler!
Remember, verified contracts are like houses with open doors – people can look inside and trust what they see. Always verify your contracts before asking people to use them in the real world.
Thank you for reading the blog . Meet you next time with some other blog. Do give me a follow on twitter @0xvarad and share this article with your friends . 👋👋