57 lines
1.6 KiB
Markdown
57 lines
1.6 KiB
Markdown
---
|
|
title: Hardhat 相关知识
|
|
createTime: 2025/10/14 14:57:06
|
|
permalink: /programming/solidity/other/hardhat/
|
|
---
|
|
|
|
## HardHat2 部署
|
|
### Node.js v16 安装
|
|
HardHat2 需要 Node.js v16 及以上的版本
|
|
这里给出的是通过 npm 来将旧版本升级到 Node.js v16 的,如果想要直接安装的话通过 `yum install nodejs` 或者 `apt install nodejs` 来即可。
|
|
```bash
|
|
npm config set registry https://registry.npmmirror.com
|
|
sudo npm install -g n
|
|
sudo n 16
|
|
```
|
|
安装完成之后可以查看版本,如果没有更新可以重新刷新一下环境(直接退出重连最简单)
|
|
```bash
|
|
node -v
|
|
# v16.20.2
|
|
npm -v
|
|
# 6.14.15
|
|
```
|
|
### 安装 Hardhat2
|
|
创建一个 hardhat2-project 目录,初始化 npm 项目,注意这里不要用 hardhat 作为项目名
|
|
```bash
|
|
mkdir ~/hardhat2-project
|
|
cd ~/hardhat2-project
|
|
npm init -y
|
|
```
|
|
安装 Hardhat 2
|
|
```bash
|
|
npm install --save-dev hardhat@^2.23.0
|
|
```
|
|
初始化 Hardhat2 项目
|
|
```bash
|
|
npx hardhat --init
|
|
```
|
|
选择
|
|
```bash
|
|
✔ What do you want to do? · Create a JavaScript project
|
|
✔ Hardhat project root: · /root/hardhat2-project
|
|
✔ Do you want to add a .gitignore? (Y/n) · y
|
|
✔ Help us improve Hardhat with anonymous crash reports & basic usage data? (Y/n) · y
|
|
✔ Do you want to install this sample project's dependencies with npm (@nomicfoundation/hardhat-toolbox)? (Y/n) · y
|
|
```
|
|
## 使用 Hardhat2 编译测试合约
|
|
在 hardhat2-project 目录下,执行以下命令来编译合约
|
|
合约都会放在 `contracts` 目录下,这里我们选择了一个默认的合约,因此可以直接编译
|
|
```bash
|
|
# 测试编译
|
|
npx hardhat compile
|
|
# 运行测试
|
|
npx hardhat test
|
|
# 查看可用任务
|
|
npx hardhat help
|
|
```
|