logo头像
Snippet 博客主题

hardhat3开发

初始化

1
2
3
npm init -y
npm install --save-dev hardhat@latest
npx hardhat --init

常用命令

编译所有合约

编译contracts目录下的所有Solidity文件,生成artifacts和cache目录。

1
npx hardhat compile

清理缓存

如果遇到编译问题,可以清理缓存:

1
npx hardhat clean

然后重新编译。
显示Gas报告:

设置环境变量REPORT_GAS=true,然后运行测试:

REPORT_GAS=true npx hardhat test

脚本测试

1
npx hardhat run scripts/send-op-tx.ts

部署到特定网络,可以添加–network参数:

1
npx hardhat run scripts/send-op-tx.ts --network sepolia

运行测试用例

1
npx hardhat test

运行特定的测试用例

1
npx hardhat test ts文件路径

部署

传统部署

1
2
3
4
5
6
# 部署到本地网络
npx hardhat run scripts/send-op-tx.ts
# 部署到Sepolia测试网
npx hardhat run scripts/send-op-tx.ts --network sepolia
# 部署到主网
npx hardhat run scripts/send-op-tx.ts --network mainnet

ignition部署

1
2
3
4
5
6
7
8
9
# 部署到本地网络
npx hardhat ignition deploy ignition/modules/Counter.ts
# 部署到Sepolia测试网
npx hardhat ignition deploy ignition/modules/Counter.ts --network sepolia
# 查看部署状态
npx hardhat ignition status chain-11155111
# 验证已部署的合约
npx hardhat ignition verify chain-11155111

chain-11155111对应Sepolia测试网,不同的网络有不同的链ID。

部署后浏览器查看

https://sepolia.etherscan.io/

带参数部署

1
npx hardhat ignition deploy ignition/modules/NfgAuctionUpgradeWithAddressModule.ts --network sepolia --parameters params.json

多合约部署

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import { buildModule } from "@nomicfoundation/hardhat-ignition/modules";

export default buildModule("VaultSystem", (m) => {
// 先部署Token合约
const token = m.contract("Token");

// 然后部署Vault合约,传入Token地址
const vault = m.contract("Vault", {
args: [token],
});

// 可选:给某个地址转账一些Token
const deployer = m.getAccount(0);
m.call(token, "transfer", [deployer, 1000n]);

return { token, vault };
});

依赖处理:

Vault合约依赖于Token合约
Ignition会自动先部署Token,然后部署Vault
正确传递依赖关系,无需手动管理顺序

参数化部署

对于不同网络,可能需要不同的参数。Ignition支持参数化部署。

使用参数文件:

在ignition/parameters目录下创建参数文件:

sepolia.json:

1
2
3
4
5
6
7
{
"SimpleStorageModule": {
"SimpleStorage": {
"args": [200]
}
}
}

mainnet.json:

1
2
3
4
5
6
7
{
"SimpleStorageModule": {
"SimpleStorage": {
"args": [100]
}
}
}

部署时,Ignition会自动读取对应网络的参数文件,使用指定的参数进行部署。

敏感信息保存

为了安全地管理敏感信息,我们需要使用环境变量。

创建.env文件:

在项目根目录创建.env文件,存储敏感信息:

1
2
3
4
5
6
7
8
9
10
# RPC节点URL
SEPOLIA_URL=https://sepolia.infura.io/v3/your-project-id
MAINNET_URL=https://mainnet.infura.io/v3/your-project-id

# 私钥(敏感信息,切勿泄露)
PRIVATE_KEY=your-private-key-here

# API密钥
ETHERSCAN_API_KEY=your-etherscan-api-key
COINMARKETCAP_API_KEY=your-coinmarketcap-api-key

安装dotenv:

1
npm install dotenv --save-dev

在配置文件中,导入dotenv/config,这会自动加载.env文件中的环境变量:
import “dotenv/config”;
然后就可以使用process.env.VARIABLE_NAME访问这些变量。

网络相关

启动本地节点:

npx hardhat node
这会启动一个HTTP和WebSocket JSON-RPC服务器,默认地址是http://127.0.0.1:8545/。

在本地节点上运行脚本:

npx hardhat run scripts/send-op-tx.ts –network localhost

获取RPC节点URL

Infura:

访问infura.io
注册账号并创建项目
获取项目ID
RPC URL格式:https://sepolia.infura.io/v3/YOUR-PROJECT-ID
Alchemy:

访问alchemy.com
注册账号并创建应用
获取API密钥
RPC URL格式:https://eth-sepolia.g.alchemy.com/v2/YOUR-API-KEY
公共RPC节点:

也可以使用公共RPC节点,但可能有速率限制:

Sepolia: https://rpc.sepolia.org
Mainnet: https://eth.llamarpc.com

获取测试ETH
在测试网上部署合约需要测试ETH。获取方式:

Sepolia水龙头:

Alchemy Sepolia Faucet:

1
2
访问:https://www.alchemy.com/faucets/ethereum-sepolia
连接钱包 每天可以领取0.1 ETH 需要有0.001的真实eth