hardhat集成了编译、测试、部署等功能,支持TypeScript,还提供了本地开发网络和调试工具。这篇文章详细介绍了hardhat的特点以及核心功能。
Hardhat 是一个用于以太坊智能合约的开发环境,它集成了编译、测试、部署等功能,支持 TypeScript,还提供了本地开发网络和调试工具。
特点
核心功能
8545
。你需要确保已经安装了 Node.js 和 npm(Node 包管理器)。 最好你的 node 版本 >= 18,然后执行一下命令安装 hardhat。
npm install --save-dev hardhat
npx hardhat init
然后根据自己的需要选择合适的模板即可(Javascript/typescript)。
contracts/ # Solidity 合约
scripts/ # 部署脚本
test/ # 测试用例
hardhat.config.js # 配置文件
自定义任务
Hardhat 的核心是一个任务运行器,可让您自动化开发工作流程。它自带一些内置任务,例如 compile
和 test
,但您也可以添加自己的自定义任务。
比如我们可以自己写一个打印可用账号列表的任务:
task(
"accounts",
"Prints the list of accounts",
async (taskArgs, hre) => {
const accounts = await hre.ethers.getSigners();
for (const account of accounts{
console.log(account.address);
}
}
);
执行npx hardhat accounts
即可运行。
task参数说明:
npx hardhat help
中打印出来在 test
目录下编写合约的测试用例,例如 sample-test.js
:
const { expect } = require("chai");
const { ethers } = require("hardhat");
describe("Greeter", function () {
it("Should return the new greeting once it's changed", async function () {
const Greeter = await ethers.getContractFactory("Greeter");
const greeter = await Greeter.deploy("Hello, world!");
await greeter.deployed();
expect(await greeter.greet()).to.equal("Hello, world!");
const setGreetingTx = await greeter.setGreeting("Hola, mundo!");
// wait until the transaction is mined
await setGreetingTx.wait();
expect(await greeter.greet()).to.equal("Hola, mundo!");
});
});
上述测试用例使用了 ethers.js
库和 chai
断言库,用于测试 Greeter
合约的 greet
和 setGreeting
方法。
npx hardhat test
如果所有测试用例都通过,你会看到类似以下的输出:
Greeter
√ Should return the new greeting once it's changed (102ms)
1 passing (123ms)
在 scripts
目录下创建一个部署脚本,例如 deploy.js
:
console.log("Account balance:", (await deployer.getBalance()).toString());
const Greeter = await ethers.getContractFactory("Greeter");
const greeter = await Greeter.deploy("Hello, Hardhat!");
console.log("Greeter address:", greeter.address);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
使用以下命令来部署合约:
npx hardhat run scripts/deploy.js --network localhost
这里的 --network localhost
表示使用 Hardhat 提供的本地开发网络进行部署。
如果跨链部署只需修改为自己配置的网络即可,如:
npx hardhat run scripts/deploy.js --network sepolia
调试技巧
console.log
:在 Solidity 中使用(需 HardhatConsole
导入)。你可以在 hardhat.config.js
中配置不同的网络,例如以太坊主网、sepolia 测试网等。以下是一个示例配置:
require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config(); // 用于环境变量配置
module.exports = {
solidity: "0.8.20",
networks: {
sepolia: {
url: process.env.ALCHEMY_URL,
accounts: [process.env.PRIVATE_KEY]
},
localhost: {
url: "http://127.0.0.1:8545"
}
},
etherscan: {
apiKey: process.env.ETHERSCAN_KEY
}
};
在使用不同网络之前,你需要在 .env
文件中设置相应的环境变量,例如 ROPSTEN_URL
、PRIVATE_KEY
和 ETHERSCAN_API_KEY
。
如果你想在 Etherscan 等区块链浏览器上验证合约,可以使用 Hardhat 提供的插件。首先安装 @nomiclabs/hardhat-etherscan
插件:
npm install --save-dev @nomiclabs/hardhat-etherscan
然后在 hardhat.config.js
中配置 Etherscan API 密钥。最后,使用以下命令验证合约:
npx hardhat verify --network ropsten <DEPLOYED_CONTRACT_ADDRESS> "Hello, Hardhat!"
将 <DEPLOYED_CONTRACT_ADDRESS>
替换为你实际部署的合约地址。
@nomicfoundation/hardhat-verify
:验证合约源码。hardhat-gas-reporter
:分析 Gas 消耗。hardhat-deploy
:管理部署脚本。Gas Reporter 是一个 Hardhat 插件,用于分析智能合约的 Gas 消耗,帮助开发者:
这里涉及的内容比较多,如果想知道更多详细的内容,请移步gas优化报告的详细使用方法
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!