> ## Documentation Index
> Fetch the complete documentation index at: https://docs.monad.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# 使用 Hardhat 在 Monad 上验证智能合约

一旦您的合约部署到线上网络，下一步就是在区块浏览器上验证其源代码。

验证合约意味着将其源代码以及编译该代码时使用的设置上传到一个仓库
（通常由区块浏览器维护）。这样任何人都可以编译它并将生成的
字节码与链上部署的进行比较。在像 Monad 这样的开放平台上，这样做极其重要。

在本指南中，我们将解释如何使用 [Hardhat](https://hardhat.org/) 来完成此操作。

> [!TIP]
>
> 验证命令可能会显示错误消息，但这通常是误导性的 —— 合约通常已在 Sourcify 和 MonadScan 上成功验证。请查看浏览器链接以确认验证情况。

> [tab] Hardhat 2
>
> ## Hardhat 2 验证
>
> `hardhat-monad` 模板已预配置为同时在 MonadVision（Sourcify）和 Monadscan（Etherscan）上验证合约。
>
> 如果您使用该模板，您的 `hardhat.config.ts` 应该已经包含：
>
> ```ts theme={null}
> import type { HardhatUserConfig } from "hardhat/config";
> import "@nomicfoundation/hardhat-toolbox-viem";
> import "@nomicfoundation/hardhat-ignition-viem";
> import "dotenv/config";
>
> const PRIVATE_KEY = process.env.PRIVATE_KEY || "";
> const ETHERSCAN_API_KEY = process.env.ETHERSCAN_API_KEY || "";
>
> const config: HardhatUserConfig = {
>   solidity: {
>     version: "0.8.30",
>     settings: {
>       evmVersion: "osaka",
>       metadata: {
>         bytecodeHash: "ipfs", // Required for Sourcify verification
>       },
>     },
>   },
>   networks: {
>     monadTestnet: {
>       url: "https://testnet-rpc.monad.xyz",
>       accounts: [PRIVATE_KEY],
>       chainId: 10143,
>     },
>     monadMainnet: {
>       url: "https://rpc.monad.xyz",
>       accounts: [PRIVATE_KEY],
>       chainId: 143,
>     },
>   },
>   sourcify: {
>     enabled: true,
>     apiUrl: "https://sourcify-api-monad.blockvision.org",
>     browserUrl: "https://monadvision.com",
>   },
>   etherscan: {
>     enabled: true,
>     apiKey: {
>       monadMainnet: ETHERSCAN_API_KEY,
>       monadTestnet: ETHERSCAN_API_KEY,
>     },
>     customChains: [
>       {
>         network: "monadMainnet",
>         chainId: 143,
>         urls: {
>           apiURL: "https://api.etherscan.io/v2/api?chainid=143",
>           browserURL: "https://monadscan.com",
>         },
>       },
>       {
>         network: "monadTestnet",
>         chainId: 10143,
>         urls: {
>           apiURL: "https://api.etherscan.io/v2/api?chainid=10143",
>           browserURL: "https://testnet.monadscan.com",
>         },
>       },
>     ],
>   },
> };
>
> export default config;
> ```
>
> **在主网上验证：**
>
> ```sh theme={null}
> npx hardhat verify <contract_address> --network monadMainnet
> ```
>
> **在测试网上验证：**
>
> ```sh theme={null}
> npx hardhat verify <contract_address> --network monadTestnet
> ```
>
> 这将在 MonadVision 和 Monadscan 上都验证您的合约。验证完成后，您可以在相应的浏览器上查看您的合约。

> [tab] Hardhat 3
>
> ## Hardhat 3 验证
>
> > [!TIP]
> >
> > 验证命令可能会显示错误消息，但这通常是误导性的 —— 合约通常已在 Sourcify 和 MonadScan 上成功验证。请查看浏览器链接以确认验证情况。
>
> `hardhat3-monad` 模板已预配置为在 MonadVision（Sourcify）和 Monadscan（Etherscan）上都验证合约。Hardhat 3 使用不同的配置结构，采用 `verify` 键和 `chainDescriptors`。
>
> 如果您使用该模板，您的 `hardhat.config.ts` 应该已经包含：
>
> ```ts theme={null}
> import hardhatToolboxViemPlugin from "@nomicfoundation/hardhat-toolbox-viem";
> import { configVariable, defineConfig } from "hardhat/config";
> import "dotenv/config";
>
> export default defineConfig({
>   plugins: [hardhatToolboxViemPlugin],
>   solidity: {
>     version: "0.8.31",
>     settings: {
>       evmVersion: "osaka",
>       optimizer: {
>         enabled: true,
>         runs: 200,
>       },
>     },
>   },
>   networks: {
>     hardhat: {
>       type: "edr-simulated",
>     },
>     monadTestnet: {
>       type: "http",
>       url: "https://testnet-rpc.monad.xyz",
>       accounts: [configVariable("PRIVATE_KEY")],
>       chainId: 10143,
>     },
>     monadMainnet: {
>       type: "http",
>       url: "https://rpc.monad.xyz",
>       accounts: [configVariable("PRIVATE_KEY")],
>       chainId: 143,
>     },
>   },
>   verify: {
>     blockscout: {
>       enabled: false,
>     },
>     etherscan: {
>       enabled: true,
>       apiKey: configVariable("ETHERSCAN_API_KEY"),
>     },
>     sourcify: {
>       enabled: true,
>       apiUrl: "https://sourcify-api-monad.blockvision.org",
>     },
>   },
>   chainDescriptors: {
>     10143: {
>       name: "MonadTestnet",
>       blockExplorers: {
>         etherscan: {
>           name: "Monadscan",
>           url: "https://testnet.monadscan.com",
>           apiUrl: "https://api.etherscan.io/v2/api",
>         },
>       },
>     },
>     143: {
>       name: "MonadMainnet",
>       blockExplorers: {
>         etherscan: {
>           name: "Monadscan",
>           url: "https://monadscan.com",
>           apiUrl: "https://api.etherscan.io/v2/api",
>         },
>       },
>     },
>   },
> });
> ```
>
> **在主网上验证：**
>
> ```sh theme={null}
> npx hardhat verify <contract_address> --network monadMainnet
> ```
>
> **在测试网上验证：**
>
> ```sh theme={null}
> npx hardhat verify <contract_address> --network monadTestnet
> ```
>
> 这将在 MonadVision 和 Monadscan 上都验证您的合约。验证完成后，您可以在相应的浏览器上查看您的合约。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.