How to validate and publish contracts with multiple dependencies on Etherscan

Published on Oct 29, 2021

I recently dove into the rabbit hole of blockchain development on Ethereum and love it! This article assumes that you are developing contracts in the hardhat framework.

Verifying contracts on Etherscan

Verifying and uploading contracts directly on Etherscan is easy enough, especially when you are coding up a simple one-file contract in remix. When you have just one file, it's a matter of simply uploading your compiled code and you're good to go.

Once you start developing more complex contracts that use dependencies like the openzeppelin contracts you'll notice that you can't easily upload those dependencies via the Etherscan website.

Solution: use hardhat-etherscan

Luckily for us, there is a hardhat plugin we can use to upload and validate and upload our contracts to Etherscan. The plugin will run a simulation of the verification process locally, allowing it to detect and communicate any mistakes during the process. After that, your contracts will be uploaded and verified by using the Etherscan API.

1 — Generate an API key on etherscan.io

Sign up for an account on https://etherscan.io/register and generate your API key there.

2 — Install the plugin into your hardhat project

Installation is straightforward. Just follow the installation instructions as mentioned in the documentation.

3 — Verify your contracts

You will now be able to run the verification step of your contracts by issuing the following command:

npx hardhat verify --network mainnet DEPLOYED_CONTRACT_ADDRESS

For me, the command looked as follows because I was verifying my contract on the rinkeby network.

npx hardhat verify --network rinkeby 0xcf503dD4c3b761F4984E18f3959E8fEe8650F546

4 — Issues you might encounter

If all goes well, your contracts will display the verified badge and you'll be able to call read and write operations on it.

Each situation is different and I too ran into a little issue when trying to verify my contracts. I got the following error:

[Error: ENOENT: no such file or directory, open '/Users/Goodbytes/.../artifacts/build-info/4e7389b25e253b27f945b011dc8f896b.json']

A little digging gave me the following solution. Run npx hardhat clean to clean out old artifacts and let the verification step rebuild your contracts and you'll be good to go.

npx hardhat clean

Please note that every contract is unique and if your contracts constructor required arguments, look into the docs of the plugin to see how you can pass in those arguments.

Good luck and have fun coding!

No comments? But that’s like a Gin & Tonic without the ice?

I’ve removed the comments but you can shoot me a message on LinkedIn to keep the conversation going.