JSPM

  • Created
  • Published
  • Downloads 117229
  • Score
    100M100P100Q177121F
  • License MIT

Hardhat plugin for verifying contracts

Package Exports

  • @nomicfoundation/hardhat-verify
  • @nomicfoundation/hardhat-verify/verify

Readme

Hardhat Verify plugin

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

Installation

To install this plugin, run the following command:

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

and add the following statements to your hardhat.config.ts file:

// ...
import hardhatVerifyPlugin from "@nomicfoundation/hardhat-verify";

// ...

export default {
  // ...
  plugins: [
    // ...
    hardhatVerifyPlugin,
  ],

  // ...
};

Usage

Verifying on Etherscan

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

import { configVariable } from "hardhat/config";

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

Run the verify task, passing the address of the contract, the network where it's deployed, 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(
  {
    contractAddress: "DEPLOYED_CONTRACT_ADDRESS",
    constructorArguments: ["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 the production build profile. However, tasks like compile and run default to default. 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 compile and verify using the same build profile:

npx hardhat compile --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 --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.