Using HardHat
Learn how to verify a smart contract using Hardhat.
This tutorial assumes that the contract was deployed using Hardhat and that all Hardhat dependencies are properly installed.
After deploying a smart contract one can verify the smart contract on Snowtrace in three steps:
- Flatten the Smart Contract
- Clean up the flattened contract
- Verify using the Snowtrace GUI
Flatten Smart Contract using Hardhat
To flatten the contract, run the command: npx hardhat flatten <path-to-contract> >> <flat-contract-name>.sol
Cleanup the Flattened Smart Contract
Some clean-up may be necessary to get the code to compile properly in the Snowtrace Contract Verifier
- Remove all but the top SPDX license.
- If the contract uses multiple SPDX licenses, use both licenses by adding AND:
SPDX-License-Identifier: MIT AND BSD-3-Clause
Verify Smart Contract using Snowtrace UI
Snowtrace is currently working on a new user interface (UI) for smart contract verification. Meanwhile, you may consider using their API for a seamless smart contract verification experience.
Verify Smart Contract Programmatically Using APIs
Ensure you have Postman or any other API platform installed on your computer (or accessible through online services), along with your contract's source code and the parameters utilized during deployment.
Here is the API call URL to use for a POST request: https://api.snowtrace.io/api?module=contract&action=verifysourcecode
Please note that this URL is specifically configured for verifying contracts on the Avalanche C-Chain Mainnet. If you intend to verify on the Fuji Testnet, use: https://api-testnet.snowtrace.io/api?module=contract&action=verifysourcecode
Here's the body of the API call with the required parameters:
Verifying with Hardhat-Verify
This part of the tutorial assumes that the contract was deployed using Hardhat and that all Hardhat dependencies are properly installed to include '@nomiclabs/hardhat-etherscan'
.
You will need to create a .env.json
with your Wallet Seed Phrase. You don't need an API key to verify on Snowtrace.
Example .env.json
:
Below is a sample hardhat.config.ts
used for deployment and verification:
Once the contract is deployed, verify with hardhat verify by running the following:
Example:
You can also verify contracts programmatically via script. Example:
First create your script, then execute it via hardhat by running the following:
Verifying via terminal will not allow you to pass an array as an argument, however, you can do this when verifying via script by including the array in your Constructor Arguments. Example: