Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@nomicfoundation/hardhat-verify

NomicFoundation575.3kMIT3.0.2TypeScript support: included

Hardhat plugin for verifying contracts

ethereum, smart-contracts, hardhat, verify, etherscan, blockscout, sourcify

readme

hardhat-verify

Hardhat plugin to verify the source of code of deployed contracts.

Installation

This plugin is part of Viem Hardhat Toolbox and Ethers+Mocha Hardhat Toolbox. If you are using any of those toolboxes, there's nothing else you need to do.

To install this plugin, run the following command:

npm install --save-dev @nomicfoundation/hardhat-verify

In your hardhat.config.ts file, import the plugin and add it to the plugins array:

import hardhatVerify from "@nomicfoundation/hardhat-verify";

export default {
  plugins: [hardhatVerify],
};

Usage

Verifying on Etherscan

You need to add the following Etherscan config in your hardhat.config.ts file

export default {
  verify: {
    etherscan: {
      // Your API key for Etherscan
      // Obtain one at https://etherscan.io/
      apiKey: "<ETHERSCAN_API_KEY>",
    },
  },
};

We recommend using a configuration variable to set sensitive information like API keys.

import { configVariable } from "hardhat/config";

export default {
  verify: {
    etherscan: {
      // Your API key for Etherscan
      // Obtain one at https://etherscan.io/
      apiKey: configVariable("ETHERSCAN_API_KEY"),
    },
  },
};

Run the verify task passing the network where it's deployed, the address of the contract, and the constructor arguments that were used to deploy it (if any):

npx hardhat verify --network mainnet DEPLOYED_CONTRACT_ADDRESS "Constructor argument 1"

Programmatic verification

You can also verify contracts programmatically by using the verifyContract function from the plugin:

import hre from "hardhat";
import { verifyContract } from "@nomicfoundation/hardhat-verify/verify";

await verifyContract(
  {
    address: "DEPLOYED_CONTRACT_ADDRESS",
    constructorArgs: ["Constructor argument 1"],
    provider: "etherscan", // or "blockscout" for Blockscout-compatible explorers
  },
  hre,
);

Note: The verifyContract function is not re-exported from the Hardhat toolboxes, so you need to install the plugin and import it directly from @nomicfoundation/hardhat-verify/verify.

Build profiles and verification

When no build profile is specified, this plugin defaults to production. However, tasks like build and run default to the default build profile. If your contracts are compiled with a different profile than the one used for verification, the compiled bytecode may not match the deployed bytecode, causing verification to fail.

To avoid this, make sure to build and verify using the same profile:

npx hardhat build --build-profile production
npx hardhat verify --network mainnet DEPLOYED_CONTRACT_ADDRESS "Constructor argument 1"

If you're using the verifyContract function programmatically through a script, pass the build profile when running it:

npx hardhat run --build-profile production scripts/verify.ts

How it works

The plugin works by fetching the bytecode in the given address and using it to check which contract in your project corresponds to it. Besides that, some sanity checks are performed locally to make sure that the verification won't fail.

changelog

@nomicfoundation/hardhat-verify

3.0.2

Patch Changes

  • a475780: Added automatic proxy detection for hardhat-verify and fixed case-insensitive proxy environment variables for network requests (#7407)

3.0.1

Patch Changes

  • 0016b57: Fix ContractInformationResolver to use optional chaining when accessing compiler output contracts to prevent potential TypeError (#7291)

3.0.0

Major Changes

  • 29cc141: First release of Hardhat 3!

2.1.1

Patch Changes

  • 11ee260: Don't use undici's global dispatcher, making Hardhat more stable across Node.js versions

2.1.0

Minor Changes

  • 14b3042: Updated the minimal supported version of Node to v20 (#6982)

2.0.14

Patch Changes

  • d0c3dcf: Support Etherscan API v2 (#6716)
  • Updated dependencies [9b75f5d]
  • Updated dependencies [a8ad44c]

2.0.13

Patch Changes

  • 0469eb2: Ink networks added

2.0.12

Patch Changes

  • f571670: Replace chalk with picocolors

2.0.11

Patch Changes

  • 913b5a1: Added Blockscout as a verification provider

2.0.10

Patch Changes

  • efa905d: Fix for corrupted Hardhat peer dependency version from pnpm.

2.0.9

Patch Changes

  • 88e57fa: Make the --force flag override the check of any existing verification, even in the presence of errors.

2.0.8

Patch Changes

  • 73d5bea: Improved validation of constructor arguments (thanks @fwx5618177!)

2.0.7

Patch Changes

  • f186e1a: Improved error handling and messaging for errors from the block explorer
  • e7b12df: Added Polygon Amoy testnet (thanks @FournyP!)
  • b9aada0: Added --force flag to allow verification of partially verified contracts (thanks @rimrakhimov!)

2.0.6

Patch Changes

  • 62d24cd: Added baseSepolia (thanks @hironate)

2.0.5

Patch Changes

  • 91d035e: Updated polygonZkEVMTestnet to point to cardona testnet

2.0.4

Patch Changes

  • fb673f2be: Added holesky and arbitrumSepolia, and removed arbitrumTestnet and arbitrumGoerli from hardhat-verify chains.
  • 11043e96a: Added support for programmatic verification in Sourcify

2.0.3

Patch Changes

  • e77f1d8a0: Add apiUrl and browserUrl to Sourcify configuration.

2.0.2

Patch Changes

  • 5cab65fb7: Updated chiado urls to avoid redirect

2.0.1

Patch Changes

  • c2155fb26: Added polygonZkEVM and polygonZkEVMTestnet
  • c7d87c41a: Fixed case-sensitive address comparison for detecting verified contracts

2.0.0

Major Changes

  • a32e68589: - Added Sourcify as a verification provider.

1.1.1

Patch Changes

  • 4ed196924: Added base mainnet

1.1.0

Minor Changes

  • e2fc27766: Exposed the Etherscan class as a public API for third-party consumers.

1.0.4

Patch Changes

  • 0f4411ce0: Added baseGoerli testnet.

1.0.3

Patch Changes

  • efe7824e0: Removed the rinkeby, ropsten and kovan deprecated test networks (thanks @pcaversaccio!)

1.0.2

Patch Changes

  • 4028c6e24: Fix URLs for the Aurora networks (thanks @zZoMROT and @ZumZoom!)
  • 4028c6e24: Fixed a problem where the --list-networks flag wasn't working without passing an address (thanks @clauBv23!)
  • 72162dcc7: Success messages are now more generic (thanks @clauBv23!).

1.0.1

Patch Changes

  • 40b371bca: Removed the compilation step from the verify task, and removed the noCompile flag