Hardhat开发框架 #
一、Hardhat简介 #
Hardhat是以太坊开发的专业框架,提供编译、测试、部署等完整功能。
1.1 特点 #
| 特点 | 说明 |
|---|---|
| TypeScript支持 | 完整的TypeScript支持 |
| 插件系统 | 丰富的插件生态 |
| 测试网络 | 内置Hardhat Network |
| 调试功能 | 强大的调试工具 |
二、安装与配置 #
2.1 安装 #
bash
# 创建项目
mkdir my-hardhat-project
cd my-hardhat-project
# 初始化
npm init -y
# 安装Hardhat
npm install --save-dev hardhat
# 初始化项目
npx hardhat init
2.2 配置文件 #
javascript
// hardhat.config.js
require("@nomicfoundation/hardhat-toolbox");
module.exports = {
solidity: {
version: "0.8.20",
settings: {
optimizer: {
enabled: true,
runs: 200
}
}
},
networks: {
hardhat: {},
localhost: {
url: "http://127.0.0.1:8545"
},
sepolia: {
url: process.env.SEPOLIA_RPC_URL || "",
accounts: process.env.PRIVATE_KEY ? [process.env.PRIVATE_KEY] : []
}
},
paths: {
sources: "./contracts",
tests: "./test",
cache: "./cache",
artifacts: "./artifacts"
}
};
三、项目结构 #
text
my-hardhat-project/
├── contracts/ # 合约目录
│ └── MyContract.sol
├── scripts/ # 脚本目录
│ └── deploy.js
├── test/ # 测试目录
│ └── MyContract.js
├── hardhat.config.js # 配置文件
└── package.json
四、编写合约 #
4.1 创建合约 #
solidity
// contracts/Token.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract Token is ERC20 {
constructor(string memory name, string memory symbol, uint256 supply)
ERC20(name, symbol)
{
_mint(msg.sender, supply * 10 ** decimals());
}
}
4.2 编译合约 #
bash
npx hardhat compile
五、测试合约 #
5.1 编写测试 #
javascript
// test/Token.js
const { expect } = require("chai");
const { ethers } = require("hardhat");
describe("Token", function () {
let token;
let owner;
let addr1;
beforeEach(async function () {
[owner, addr1] = await ethers.getSigners();
const Token = await ethers.getContractFactory("Token");
token = await Token.deploy("MyToken", "MTK", 1000000);
await token.deployed();
});
it("Should have correct name and symbol", async function () {
expect(await token.name()).to.equal("MyToken");
expect(await token.symbol()).to.equal("MTK");
});
it("Should assign total supply to owner", async function () {
const balance = await token.balanceOf(owner.address);
expect(balance).to.equal(ethers.utils.parseEther("1000000"));
});
it("Should transfer tokens", async function () {
await token.transfer(addr1.address, ethers.utils.parseEther("100"));
expect(await token.balanceOf(addr1.address)).to.equal(
ethers.utils.parseEther("100")
);
});
});
5.2 运行测试 #
bash
npx hardhat test
六、部署合约 #
6.1 部署脚本 #
javascript
// scripts/deploy.js
const hre = require("hardhat");
async function main() {
const Token = await hre.ethers.getContractFactory("Token");
const token = await Token.deploy("MyToken", "MTK", 1000000);
await token.deployed();
console.log("Token deployed to:", token.address);
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
6.2 部署命令 #
bash
# 本地部署
npx hardhat run scripts/deploy.js
# 测试网部署
npx hardhat run scripts/deploy.js --network sepolia
七、常用命令 #
| 命令 | 说明 |
|---|---|
npx hardhat compile |
编译合约 |
npx hardhat test |
运行测试 |
npx hardhat run scripts/deploy.js |
部署合约 |
npx hardhat node |
启动本地节点 |
npx hardhat clean |
清理编译文件 |
npx hardhat console |
打开控制台 |
八、总结 #
Hardhat要点:
| 功能 | 说明 |
|---|---|
| 编译 | 自动编译合约 |
| 测试 | 强大的测试框架 |
| 部署 | 灵活的部署脚本 |
| 网络 | 内置测试网络 |
接下来,让我们学习Foundry框架!
最后更新:2026-03-27