Skip to main content

Verifying Contracts with Hardhat

Β· 3 min read
Josh Crites

header

Hardhat is one of the most popular developer tools for writing contracts for EVM compatible blockchains. Hardhat is a great tool for developing smart contracts for Celo--you can find more information about this in the Celo documentation here.

In this tutorial I will go over how to use the hardhat-deploy plugin for hardhat, specifically to verify deployed contracts on the Celo block explorer via Sourcify. You can verify contracts with the plugin whether you deployed them using the plugin or not.

In this post I will cover

  • setting up a hardhat project with the hardhat-deploy plugin
  • deploying contracts using the plugin
  • how to verify the contracts on sourcify

Verify contracts using hardhat-deploy​

Setup​

First, I will cover how to deploy contracts using the plugin. This will help provide some context around how a project using this plugin is different from a regular hardhat project. This guide assumes that you already have a hardhat project set up. If you don’t, refer to this page.

tip

You can reference the 'hardhat-deploy' branch of this github repo to see how the project is structured.

Install the plugin in your hardhat project:

npm install -D hardhat-deploy

Import the plugin in your hardhat.config.js file.

require("hardhat-deploy");

Since hardhat-deploy-ethers is a fork of @nomiclabs/hardhat-ethers and that other plugin might have an hardcoded dependency on @nomiclabs/hardhat-ethers the best way to install hardhat-deploy-ethers and ensure compatibility is the following:

npm install --save-dev  @nomiclabs/hardhat-ethers@npm:hardhat-deploy-ethers ethers

Which means you then need to do require("@nomiclabs/hardhat-ethers") instead of require("hardhat-deploy-ethers") in your hardhat.config.js file.

The plugin also supports Typescript, which you can find more information about here.

The plugin also has a concept of namedAccounts which makes it easier to reference available accounts. You can read more about the details here. For our purposes, keep it simple and add namedAccounts to the exports in hardhat.config.js like so

//hardhat.config.js
...
module.exports = {
defaultNetwork: "alfajores",
namedAccounts: {
deployer: 0
},
networks: {
...

You can see my source file here.

Deploy​

Deployment scripts used by hardhat-deploy live in a folder called deploy in the root of the project. The deployment scripts will save deployment information in a deployments folder in the project root as well.

Here is the deployment script for deploying the Greeter contract in the example repo.

// deploy/00_deploy_my_contract.js
module.exports = async ({ getNamedAccounts, deployments }) => {
const { deploy } = deployments;
const { deployer } = await getNamedAccounts();
await deploy("Greeter", {
from: deployer,
args: ["hello world"],
log: true,
});
};
module.exports.tags = ["Greeter"];

It is a simple function that takes the namedAccounts, configuration info and the contract constructor arguments and deploys the contract. You can read more about deploy scripts here.

To run the deployment, use the command

npx hardhat --network alfajores deploy

There should be terminal output similar to this:

Downloading compiler 0.8.4
Compiling 1 file with 0.8.4
Compilation finished successfully
deploying "Greeter" (tx: 0x7fedbd14877cdca23485a96108e22ae6764b65348eddbaa1cbec9504707b7186)...: deployed at 0x9F163C9138faA5cdc731b94E1e9632C05764C23e with 493178 gas

Verify​

Once the contract is deployed, you can verify it with the command:

npx hardhat --network alfajores sourcify

Which should output

verifying Greeter (0x9F163C9138faA5cdc731b94E1e9632C05764C23e on chain 44787) ...
=> contract Greeter is now verified

You can check the verification on the block explorer, https://alfajores-blockscout.celo-testnet.org/address/0x9F163C9138faA5cdc731b94E1e9632C05764C23e/contracts in this case.

I hope this is helpful and feel free to join the Celo Discord server and reach out if you have any questions. My handle is joshc#0001.

You can view my hardhat-deploy repository for reference on GitHub here: https://github.com/critesjosh/celo-hardhat/tree/hardhat-deploy