Verifying a contract
When you deployed the Counter contract to the Sepolia network, Etherscan only showed you the raw bytecode of the contract. To see its source code, you need to verify it.
Getting an Etherscan API key
Section titled “Getting an Etherscan API key”Before you can verify the contract, add an Etherscan API key to your configuration. If you don’t have one, learn how to obtain it here.
You need to add the API key to your config using a Configuration Variable:
import hardhatToolboxViemPlugin from "@nomicfoundation/hardhat-toolbox-viem";import { configVariable, defineConfig } from "hardhat/config";
export default defineConfig({ plugins: [hardhatToolboxViemPlugin], solidity: { version: "0.8.28", }, networks: { sepolia: { type: "http", chainType: "l1", url: configVariable("SEPOLIA_RPC_URL"), accounts: [configVariable("SEPOLIA_PRIVATE_KEY")], }, }, verify: { etherscan: { apiKey: configVariable("ETHERSCAN_API_KEY"), }, },});As you did before, add the ETHERSCAN_API_KEY variable to your keystore:
npx hardhat keystore set ETHERSCAN_API_KEYpnpm hardhat keystore set ETHERSCAN_API_KEYyarn hardhat keystore set ETHERSCAN_API_KEYVerifying your contract with Hardhat Ignition
Section titled “Verifying your contract with Hardhat Ignition”Execute the Ignition module again with the --verify flag. Since you’ve already deployed the contracts, this won’t re-deploy them, but it will submit the source code to Etherscan for verification:
npx hardhat ignition deploy ignition/modules/Counter.ts --network sepolia --verifypnpm hardhat ignition deploy ignition/modules/Counter.ts --network sepolia --verifyyarn hardhat ignition deploy ignition/modules/Counter.ts --network sepolia --verifyIf everything goes well, you’ll see output indicating successful verification and a link to the verified contract on Sepolia Etherscan.