> For the complete documentation index, see [llms.txt](https://docs.getlaika.app/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.getlaika.app/docs/docs-cn/shi-yong-an-li/yu-hardhat-yi-qi-shi-yong/she-zhi-xiang-mu.md).

# 设置项目

## 启动并运行 Hardhat 项目 🛠

在我们了解 Hardhat 之后，下一步我们将去安装Hardhat。

第一步我们需要安装 Node.js 版本 12 或更高

![https://nodejs.org/](/files/uxLxbqwbuFDlDS9Vvnao)

然后我们将通过打开文本编辑器构建我们的项目Hardhat。这里我们将选择Visual Studio Code。当我们打开Visual Studio Code时，让我们选择一个目录。我将目录的名称定义为HARDHAT-LAIKA-TUTORIAL

之后，我们将打开 Terminal，并使用命令`npx hardhat init` 来创建 hardhat 项目 。

![](/files/VlkIhT4ZdSAkSLIOuV3z)

当我们运行Hardhat命令时。这将由我们来选择我们想要创建什么样的项目。在这里，我们将选择创建一个基本示例项目。接下来，我们必须将项目的根目录分配到我们想要用于安装 hardhat 的位置，在这种情况下，我们将选择默认目录。它会问我们是否想要 gitignore。我们想要使用 Git，因此我会回答Yes，将 .gitignore 添加到项目中。接下来，它将询问我们是否想要安装任何依赖项，我们将回答是，并等待安装。

```
√ What do you want to do? · Create a basic sample project
√ Hardhat project root: · /path/to/project/
√ Do you want to add a .gitignore? (Y/n) · y
√ Do you want to install this sample project's dependencies with npm (...)? (Y/n) · y 
```

安装后，Hardhat会为我们生成一个文件夹结构。将会有一个包含合约、脚本、测试和config.js文件的文件夹，这些文件将是Hardhat项目所需的文件。

![](/files/aNBIO4M49GMRevImO8t1)

正如我所说，Hardhat可以编译脚本、测试或部署任何东西。Hardhat 另外一个很酷的功能就是运行本地链。运行本地链后，我们会看到如何让本地链连接到MetaMask。运行 hardhat 本地链的命令很简单。

```
npx hardhat node
```

这里Hardhat会在本地机器上创建一个节点来运行这些帐户，并为不同的帐户提供私钥，它说:在默认设置下，它将在端口8545运行，但这里没有什么，只是运行它。

接下来，我们将配置这里运行的本地链，以便它可以连接MetaMask。

![](/files/iiIgRmPSWSOhABGFDIw6)

我们转到 web.getlaika.app

![](/files/z5mn9z3HcNynyK1SzvHZ)

连接到右上角的MetaMask。

## **添加到 Metamask 网络🌏**

在我们连接到MetaMask之后，我们将能够选择网络。在这种情况下，任何以前从未使用过Hardhat的人，网络将没有Hardhat网络可供选择。我们必须点击添加网络。

![](https://lh5.googleusercontent.com/r6oC3RbcAD09BF6ch2H_2U8YGWyV4q-7wS0vI0OyFeIy_PUfA8tBkVZ10Pc4R2mA6cV6JT69C7v0KKJmMwMT3K1JetATSwSlvwHQB9k9XvnETsg8ONiu6JFM9GariMkyt5-OgdtB)

当我们选择添加网络时，我们将进入一个页面，在那里我们可以输入我们的网络信息。

之后，我们将转到 New RPC URL，把复制的 RPC 地址填进去。

![](https://lh5.googleusercontent.com/abMNw5kGnpZt0BdeBmKj4SXPAHMtK8TFBKSVCxeirg9PpgwWrzbxcb-sXXXYm8agSQwZ8QMPRkSs-gbgDwJfoXJuvP_hVkkBNL5OsqFRQWVwq_Y4iIgGjwrYJJrwSv4HF_E3fdGF)

![](https://lh6.googleusercontent.com/Xqu5zdWVY0wxDwrlUZBY8wqNrpswMOebqN275njWqn7epIacGwEn8HInbILHspWF8E5cO0qGYDPoRiMItHbCDJ3rp3amItAVQ4CM3Y-Wfio90i0C7jtZfpheJX9YK9zUYMOyaG1n)

### Hardhat网络

**Network Name** : Hardhat Network (Local)

**New RPC URL** : <http://127.0.0.1:8545/>

**Chain ID** : 31337

**Currency Symbol** :&#x20;

**Block Explorer URL** :&#x20;

货币符号和区块链浏览器部分可以留空。填写完网络信息后，单击“保存”。在我们添加一个新的链到MetaMask之后，我们将看到如何连接到新的网络，如何直接连接，在这里我们将打开Laika网站并连接钱包，然后点击 MetaMask 钱包。选择我们创建的 Hardhat 网络。

![](https://lh5.googleusercontent.com/RseCNf1IlE_MBsKfIeZUnC8zeno50IEN5Y4SIBC_lrVx8Krk9zHhqueO1Zi78nDj47NsR7LIuyXl8dl0mqfprevVPIJKU561PW8S4dgrYS2egUyCyZWJtQ1bXTaAsJJIHlHqy1Ds)

现在莱卡显示说我们已经在 Hardhat 网络上了。

![](https://lh6.googleusercontent.com/lPmhRNV2dM6e8BzhRPzSgC385BjPJqEZQgyrQJsaQ5ArzW_loE5j0pnGkksSDPcPwlC8vnecJQATy58fu9qiTiG4d6InYRSfilia6JQeX0Nvu1IxFsSE8X6ww362G8iKcxQxyAPq)

## 修改账户余额  👛

如果我们看一下钱包，我们可以看到我们有0 ETH，这意味着我们没办法发交易，所以我们将往钱包中添加更多的钱。

![](https://lh3.googleusercontent.com/eIBGFgwp3UpUeepigyGi4Yke9y4L20PFV4w4sl8Qcoeb_D3m_01VzQdyzsPPA3WC7ENPudnBLpq-mdoNLxr7EiflgyT2MsfA2f4GW7zjVyQmgBVNLFGPrDv32_PUa8AKjGMmBIz7)

我们将回到VS Code，并在不篡改正在运行Hardhat节点的终端的情况下打开一个新终端。

在这个终端中，我们将使用命令 `npx hardhat console -network localhost`。当我们运行这个命令时，它将打开控制台以与我们运行的链通信，我们需要在命令中使用flag `--network`，表示我们想要连接 Hardhat 所运行的本地网络。

在我们运行命令`npx hardhat console --network localhost`之后，hardhat将编译合约并将控制台显示给我们。

![](https://lh5.googleusercontent.com/iSO5m4bpoj2buhX864kZYvAVeMW4cjeNlDAiFNis7ZmoNf7lV8tozHBEhqJtwD6ERRmrjvQOgdURF5XxObXSBirSQqBoGv07MhGB3onuvOHKK1VuhKGdSnUJ_ctj4vjdb-R3sTQ0)

一旦控制台启动，我们就可以编写代码来连接到网络。 这里我们将使用命令`hre.network.provider.send(‘hardhat_setBalance’,[array])`&#x20;

在这个数组中，我们将输入 2 件事：

1\. 我们要将 ETH 放入钱包的地址

2\. 我们需要多少 ETH 我们将得到一个订单`hre.network.provider.send(‘hardhat_setBalance’,[‘0x5C314eCB105F3e7A78BD19EE86823e44fd81D3DF’,’0xffffffffff’])`

![](https://lh3.googleusercontent.com/5uCzQfSTIFetQHSmYNHyLf0tuRjwyCisv8B4G197UMyAZdr6lqqBzgHn5WlURhuAP7xHWKt6_UeA9-mWghDcGMxvejvPRcHVZRhKBXWmu1ONh3QN-Ri_3qTxBft3GV1NCx-dVltc)

之后，它返回一个 Promise，我们等待它运行一段时间。

![](https://lh5.googleusercontent.com/aMMrGlyGgiEOZSEZ2nqI1OrUtkf1LAhNKoOSsXVJ0MW8xpdet3Hhf1ZMKU08AR885akN0qt4kxUAh6ZQbd3_gSZw2XopDUUe_pG2zGTdecSXXR1UlMDq-z_IAY52Xc8xpLvKHJ4s)

让我们看看运行本地链的终端，会出现一个 setBalance。

![](https://lh5.googleusercontent.com/5pZmVzK_3eGTlWNwjPWzBMSFNNbuqQHJ3yOTZG_4LR4sm5y5vrGbUTeuTUHK94Jso_DlILSQXVtdvpLhkreRDzPWgRjSBaiRIKbl2mpP11miNynFsgb-owp3h9EmKAys0Wo2gaS0)

让我们看看我们的MetaMask，我们钱包里的ETH数量将会增加。我们用它来做 gas。

![](https://lh4.googleusercontent.com/6FSvrmFh0JXIdebNgRkmhZ9fDK9DtxTyBnRWLe2dl1qA3qWKcoRPn2mK8VDSBMo2CIxZ8LNRnGcPM8K-gxt1s4u0cHTNoJJ2KpVproz-t-1v6oLGWyzSiuKNzFVGeF92BkWNEGsm)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.getlaika.app/docs/docs-cn/shi-yong-an-li/yu-hardhat-yi-qi-shi-yong/she-zhi-xiang-mu.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
