How to Verify a Smart Contract on EtherScan with HardHat
--
Verifying your smart contract on EtherScan using HardHat.
Verifying a smart contract is an essential part for any serious crypto project. It allows the community to check out the code themselves to make sure nothing nefarious is happening when they interact with it.
It also allows them to easily call view
functions, for example to see who owns a specific token. Or, my favorite, they might use your smart contract to learn to create their own.
This article will go over everything from creating and deploying a smart contract to verifying it. If you only want to know about verifying the contract, please scroll down to the ‘verifying’ header.
Creating a Smart Contract
To verify a smart contract, we first need to create one. I will create a very minimal NFT smart contract that implements ERC721 from OpenZeppelin. It’s an easy way to get used to verifying a contract that doesn’t require too much work to try. Let’s get started.
Setting up
First, let’s set up a HardHat environment by running npx hardhat
in the terminal. If you get an error, try updating Node.
- Create a TS project
- Install all dependencies. (This includes
@nomiclabs/hardhat-etherscan
which is what we need to verify the contract.
After setting up HardHat, we still need OpenZeppelin’s ERC721 contract. We can get it by running npm i @openzeppelin/contracts
.
You can now create a new file in /contracts
and create our simple smart contract.
Creating
The smart contract we’re going to create only imports ERC721 from OpenZeppelin and has a mint
function that calls _safeMint
. We don’t actually need any functionality for verifying the contract, but this way you can use it for following other tutorials.
After adding this file, NFT.sol
to the /contracts
folder, you can run npx hardhat compile
to see if there aren’t any errors. We can then get on to deploying the smart contract to the Goerli testnet.
Deploying
To deploy a smart contract we need four things.
- Get a wallet with testnet Ether. You can get testnet Ether from faucets like the Goerli faucet. Some faucets require you to verify yourself in one way or another. Some require you to tweet, others to register an account, or simply doing a captcha.
- Get an Ethereum RPC url. The easiest way is just registering at Infura.io. Registering is free, and you can use the services for free as well for small (hobby) projects.
- Update the
hardhat.config.ts
file to include your private key and the correct network settings. - Create a deploy script and run it.
The first two you have to do by yourself. But updating the config file is something I can show.
We add a network with our node url, our account with tesnet ETH to deploy from, and a variable for gas that is optional (but sometimes fixes issues).
We can now create our deploy script inside /scripts
. Again, we’re using a very simple script that only deploys the smart contract to our default network. Once deployed it outputs the address of the smart contract.
Once everything is set up, we should be able to deploy the contract by running the following commands.
npx hardhat compile
npx hardhat run scripts/deploy_NFT.ts
Once deployed, we can move on to verifying the smart contract. But you can already view the contract on EtherScan by inserting the address of the contract into the search bar on EtherScan.
Verifying
If you’ve come so far already, well done! And if you’ve skipped ahead, welcome! You haven’t missed much if you already have a smart contract deployed. Let’s verify it.
First, we need to import the hardhat-etherscan
package in our config file.
Then, we need to add our EtherScan API key to our config. If you don’t have one, you need to create an account and create a new API key. It’s free for small projects and isn’t a hassle at all.
After we’ve set up absolutely everything, we can finally run the command to verify our smart contract.
npx hardhat verify <your deployed contract address>
And then boom.
We’re verified.
If you want to specify any more settings for verifying such as constructor parameters you’ve set, you can check out the official docs.
Conclusion
Thank you so much for reading and have an excellent day.
Consider supporting me by getting a Medium membership. It helps me out a lot, it won’t cost you anything extra, and you can read as many Medium articles as you like!
Follow me on Twitter and gm.xyz to keep up with me and my projects.
Check out Pixel Pizzas on the Polygon Blockchain.