> ## 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](https://hardhat.org/docs) 是一个全面的开发环境,由用于编辑、编译、调试和部署智能合约的不同组件组成。

## 要求

在开始之前,请安装您 Hardhat 版本所支持的 Node.js 版本:

* Hardhat 2:Node.js v20.0.0 或更高版本
* Hardhat 3:Node.js v22.13.0 或更高版本

<Note>
  如果您使用 Windows,我们强烈建议您在遵循本指南时使用 [WSL 2](https://learn.microsoft.com/en-us/windows/wsl/about)。
</Note>

<Warning>
  部署到 Monad 时,请在 Hardhat 的 Solidity 编译器设置中设置 `evmVersion: "osaka"`。例如:

  ```ts theme={null}
  solidity: {
    settings: {
      evmVersion: "osaka",
    },
  },
  ```
</Warning>

<Tabs>
  <Tab title="Hardhat 2">
    ## 1. 创建新的 Hardhat 项目

    <Tip>
      您可以使用 `hardhat-monad` 模板创建一个已经设置好 Monad 配置的新项目。

      *[hardhat-monad](https://github.com/monad-developers/hardhat-monad) 是一个带有 Monad 配置的 Hardhat 模板。*
    </Tip>

    使用以下命令将仓库克隆到您的机器:

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

    ## 2. 安装依赖

    ```sh theme={null}
    npm install
    ```

    ## 3. 创建 .env 文件

    ```sh theme={null}
    cp .env.example .env
    ```

    用您的私钥编辑 `.env` 文件:

    ```
    PRIVATE_KEY=your_private_key_here
    ```

    <Warning>
      请妥善保护您的私钥。切勿将其提交到版本控制、在公共仓库中分享,或在客户端代码中暴露。您的私钥可完全访问您的资金。
    </Warning>

    ## 4. 部署智能合约

    以下命令使用 [Hardhat Ignition](https://hardhat.org/ignition/docs/getting-started#overview):

    ### 部署到本地 hardhat 节点

    通过运行以下命令启动 hardhat 节点:

    ```bash theme={null}
    npx hardhat node
    ```

    要将示例合约部署到本地 hardhat 节点,请在另一个终端中运行以下命令:

    ```bash theme={null}
    npx hardhat ignition deploy ignition/modules/Counter.ts
    ```

    ### 部署到 Monad 测试网

    确保您的私钥已在 `.env` 文件中设置。

    将合约部署到 Monad 测试网:

    ```bash theme={null}
    npx hardhat ignition deploy ignition/modules/Counter.ts --network monadTestnet
    ```

    将相同的代码重新部署到不同的地址:

    ```bash theme={null}
    npx hardhat ignition deploy ignition/modules/Counter.ts --network monadTestnet --reset
    ```

    ### 部署到 Monad 主网

    确保您的私钥已在 `.env` 文件中设置。

    将合约部署到 Monad 主网:

    ```bash theme={null}
    npx hardhat ignition deploy ignition/modules/Counter.ts --network monadMainnet
    ```

    将相同的代码重新部署到不同的地址:

    ```bash theme={null}
    npx hardhat ignition deploy ignition/modules/Counter.ts --network monadMainnet --reset
    ```
  </Tab>

  <Tab title="Hardhat3">
    ## 1. 创建新的 Hardhat3 项目

    <Tip>
      您可以使用 `hardhat3-monad` 模板创建一个针对 Hardhat3 已经设置好 Monad 配置的新项目。

      *[hardhat3-monad](https://github.com/monad-developers/hardhat3-monad) 是一个带有 Monad 配置的 Hardhat3 模板。*

      要了解有关 Hardhat3 的更多信息,请访问[开始使用指南](https://hardhat.org/docs/getting-started#getting-started-with-hardhat-3)。
    </Tip>

    使用以下命令将仓库克隆到您的机器:

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

    ## 2. 安装依赖

    ```sh theme={null}
    npm install
    ```

    ## 3. 设置您的私钥

    在项目根目录中创建一个 `.env` 文件:

    ```sh theme={null}
    PRIVATE_KEY=your_private_key_here
    ETHERSCAN_API_KEY=your_etherscan_api_key_here
    ```

    <Warning>
      请妥善保护您的私钥。切勿提交 `.env` 文件或泄漏您的私钥。您的私钥可完全访问您的资金。
    </Warning>

    ## 4. 部署智能合约

    以下命令使用 [Hardhat Ignition](https://hardhat.org/ignition/docs/getting-started#overview):

    ### 部署到本地链

    ```bash theme={null}
    npx hardhat ignition deploy ignition/modules/Counter.ts
    ```

    ### 部署到 Monad 测试网

    确保您的 `.env` 文件中已设置私钥。

    ```bash theme={null}
    npx hardhat ignition deploy ignition/modules/Counter.ts --network monadTestnet
    ```

    ### 部署到 Monad 主网

    确保您的 `.env` 文件中已设置私钥。

    ```bash theme={null}
    npx hardhat ignition deploy ignition/modules/Counter.ts --network monadMainnet
    ```
  </Tab>
</Tabs>

## 后续步骤

查看[如何在 MonadVision 上验证已部署的智能合约](/zh/guides/verify-smart-contract/hardhat)。
