> ## 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 启动一个 Hardhat 3 项目，或将 Monad 添加到现有项目中。

[Hardhat 3](https://hardhat.org/docs) 是一个用于编译、测试、部署和验证智能合约的开发环境。您可以从 Monad 模板开始，也可以将 Monad 添加到现有的 Hardhat 3 项目中。

<Note>
  Hardhat 3 需要 Node.js v22.13.0 或更高版本。
</Note>

选择一种设置路径：

* **新项目：** 克隆 Monad 模板并在克隆的仓库中工作。
* **现有项目：** 请勿将模板克隆到您的项目中。仅将下方所示的配置合并到您现有的 `hardhat.config.ts` 中。

## 从 Monad 模板开始

[Monad Hardhat 3 模板](https://github.com/monad-developers/hardhat3-monad) 包含：

* Monad 测试网和主网的网络配置
* 兼容 Foundry 的 Solidity 测试，以及使用 `node:test` 的 TypeScript 测试
* [Viem](https://viem.sh/)，用于 TypeScript 交互
* [Hardhat Ignition](https://hardhat.org/ignition/docs/getting-started) 部署模块
* Sourcify 和 MonadScan 合约验证

克隆并测试该模板：

```sh theme={null}
git clone https://github.com/monad-developers/hardhat3-monad.git
cd hardhat3-monad
npm install
npx hardhat test
```

请参阅该模板的 [README](https://github.com/monad-developers/hardhat3-monad#readme) 了解本地、测试网和主网的部署命令。

## 配置现有的 Hardhat 3 项目

将以下编译器和网络设置合并到您的 `hardhat.config.ts` 中。保留文件中已有的任何插件和其他项目特定设置。

```ts theme={null}
import { configVariable, defineConfig } from "hardhat/config";

export default defineConfig({
  solidity: {
    version: "0.8.31",
    settings: {
      evmVersion: "osaka",
    },
  },
  networks: {
    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,
    },
  },
});
```

<Warning>
  切勿将私钥直接放入 `hardhat.config.ts`，也不要将其提交到版本控制中。请通过 Hardhat 的[配置变量系统](https://hardhat.org/docs/guides/configuration-variables) 提供 `PRIVATE_KEY`，可使用 `PRIVATE_KEY` 环境变量或 Hardhat 密钥库。
</Warning>

为 Monad 编译合约时必须使用 `osaka` EVM 目标,并且 Solidity 0.8.31 需要 Hardhat 3.1.0 或更高版本。有关规范的 RPC URL、chain ID 和浏览器 URL，请参阅 [网络信息](/zh/developer-essentials/network-information) 和 [网络信息 - 测试网](/zh/developer-essentials/testnet)。

确认更新后的项目仍可编译并通过其测试：

```sh theme={null}
npx hardhat compile
npx hardhat test
```

## 部署与验证

* [使用 Hardhat 部署合约](/zh/guides/deploy-smart-contract/hardhat)
* [使用 Hardhat 验证合约](/zh/guides/verify-smart-contract/hardhat)
