乐闻世界logo
搜索文章和话题

面试题手册

如何在 Hardhat 中编写智能合约测试?

Hardhat 提供了强大的测试框架,基于 Mocha 和 Chai,以下是编写测试的核心要点:测试文件结构:const { expect } = require("chai");const { ethers } = require("hardhat");describe("MyContract", function () { beforeEach(async function () { const MyContract = await ethers.getContractFactory("MyContract"); this.contract = await MyContract.deploy(); await this.contract.deployed(); }); it("should return the correct value", async function () { expect(await this.contract.getValue()).to.equal(42); });});核心功能:合约部署const Contract = await ethers.getContractFactory("ContractName");const contract = await Contract.deploy();await contract.deployed();函数调用和断言const tx = await contract.someFunction(param1, param2);await tx.wait(); // 等待交易确认expect(await contract.someViewFunction()).to.equal(expectedValue);事件监听await expect(contract.someFunction()) .to.emit(contract, "EventName") .withArgs(arg1, arg2);交易回滚测试await expect(contract.failingFunction()) .to.be.revertedWith("Error message");快照功能const snapshot = await ethers.provider.send("evm_snapshot", []);// 执行一些操作await ethers.provider.send("evm_revert", [snapshot]);时间操作await ethers.provider.send("evm_increaseTime", [3600]); // 增加1小时await ethers.provider.send("evm_mine"); // 挖掘新区块最佳实践:使用 beforeEach 和 afterEach 管理测试状态为每个测试用例提供清晰的描述测试正常路径和异常路径使用有意义的测试数据保持测试的独立性和可重复性
阅读 0·2月21日 15:59

Hardhat 如何支持 TypeScript 和类型安全?

Hardhat 对 TypeScript 提供了原生支持,以下是使用 TypeScript 的主要优势和方法:1. 项目初始化使用 TypeScript 模板创建项目:npx hardhat init# 选择 "Create a TypeScript project"2. 类型安全的合约交互Hardhat 自动生成类型定义:import { ethers } from "hardhat";async function main() { const Contract = await ethers.getContractFactory("MyContract"); const contract = await Contract.deploy() as MyContract; // 类型安全的函数调用 await contract.setValue(42); const value = await contract.value(); console.log("Value:", value.toNumber());}3. 使用 TypeChainTypeChain 为合约 ABI 生成类型定义:npm install --save-dev typechain @typechain/ethers-v5在 hardhat.config.ts 中配置:import "@typechain/hardhat";生成的类型定义:import { MyContract } from "../typechain-types";const contract: MyContract = await contractFactory.deploy();4. 测试中的类型安全import { expect } from "chai";import { ethers } from "hardhat";import { MyContract } from "../typechain-types";describe("MyContract", function () { let contract: MyContract; beforeEach(async function () { const ContractFactory = await ethers.getContractFactory("MyContract"); contract = await ContractFactory.deploy(); await contract.deployed(); }); it("should set value correctly", async function () { await contract.setValue(100); expect(await contract.value()).to.equal(100); });});5. 配置文件类型安全使用 TypeScript 配置文件 hardhat.config.ts:import { HardhatUserConfig } from "hardhat/config";import "@nomicfoundation/hardhat-toolbox";const config: HardhatUserConfig = { solidity: "0.8.19", networks: { sepolia: { url: process.env.SEPOLIA_RPC_URL || "", accounts: process.env.PRIVATE_KEY ? [process.env.PRIVATE_KEY] : [] } }};export default config;6. 环境变量类型定义// .env.d.tsdeclare namespace NodeJS { interface ProcessEnv { SEPOLIA_RPC_URL: string; PRIVATE_KEY: string; ETHERSCAN_API_KEY: string; }}优势:编译时类型检查智能代码补全重构更安全减少运行时错误更好的代码文档团队协作更高效
阅读 0·2月21日 15:59

Hardhat 主网分叉功能如何使用?

Hardhat 主网分叉功能允许开发者基于以太坊主网或测试网的当前状态创建本地开发环境,这对于测试 DeFi 协议和与现有合约交互非常有用。基本用法:在 hardhat.config.js 中配置分叉:networks: { hardhat: { forking: { url: process.env.MAINNET_RPC_URL, blockNumber: 15000000 // 可选:指定分叉区块 } }}使用场景:测试与主网合约的交互const uniswapRouter = await ethers.getContractAt( "IUniswapV2Router02", "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D");模拟真实市场条件// 获取主网上的代币价格const dai = await ethers.getContractAt("IERC20", DAI_ADDRESS);const price = await someOracle.getPrice(dai.address);测试 DeFi 协议集成// 在分叉环境中测试 Aave 集成const pool = await ethers.getContractAt( "IPool", "0x87870Bca3F3fD6335C3F4ce8392D69350B4fA4E2");高级配置:networks: { hardhat: { forking: { url: process.env.MAINNET_RPC_URL, blockNumber: 15000000, enabled: true }, chainId: 1 // 保持与主网相同的 chainId }}测试中的使用:describe("Mainnet Fork Tests", function () { it("should interact with Uniswap", async function () { // 获取测试账户 const [signer] = await ethers.getSigners(); // 获取主网 USDC const usdc = await ethers.getContractAt("IERC20", USDC_ADDRESS); const balance = await usdc.balanceOf(signer.address); console.log("USDC Balance:", balance.toString()); });});注意事项:需要 RPC 节点支持归档数据分叉会增加内存使用确保有足够的测试 ETH考虑使用固定的区块号以确保测试可重复性最佳实践:使用环境变量存储 RPC URL指定固定的区块号以确保测试稳定性在 CI/CD 中使用归档节点定期更新分叉区块号
阅读 0·2月21日 15:59

什么是 Hardhat Ignition 及其使用方法?

Hardhat Ignition 是 Hardhat 的声明式部署系统,提供了更强大和可维护的部署方式:核心概念:模块化部署使用模块定义部署逻辑支持模块间的依赖关系声明式配置而非命令式脚本部署状态管理自动跟踪部署状态支持增量部署避免重复部署基本使用:创建部署模块:const { buildModule } = require("@nomicfoundation/hardhat-ignition/modules");module.exports = buildModule("TokenModule", (m) => { const token = m.contract("MyToken", ["MyToken", "MTK", 18]); return { token };});高级功能:参数化部署module.exports = buildModule("TokenModule", (m) => { const name = m.getParameter("name", "MyToken"); const symbol = m.getParameter("symbol", "MTK"); const token = m.contract("MyToken", [name, symbol, 18]); return { token };});依赖管理module.exports = buildModule("DAppModule", (m) => { const token = m.contract("MyToken"); const sale = m.contract("TokenSale", [token]); // 调用 token 合约的函数 m.call(token, "transferOwnership", [sale]); return { token, sale };});现有合约使用module.exports = buildModule("Module", (m) => { const existingContract = m.contractAt( "ExistingContract", "0x1234..." ); return { existingContract };});部署命令:# 部署到本地网络npx hardhat ignition deploy ./ignition/modules/Module.js# 部署到测试网npx hardhat ignition deploy ./ignition/modules/Module.js --network sepolia# 使用参数部署npx hardhat ignition deploy ./ignition/modules/Module.js --parameters name:CustomToken,symbol:CTK# 验证部署npx hardhat ignition deploy ./ignition/modules/Module.js --verify部署计划:Ignition 会生成部署计划,显示将要执行的操作:npx hardhat ignition plan ./ignition/modules/Module.js优势:声明式配置更易理解自动处理部署依赖支持部署验证更好的错误处理适合复杂的多合约部署便于团队协作
阅读 0·2月21日 15:59

Hardhat 中的调试技巧有哪些?

Hardhat 提供了强大的调试功能,以下是主要的调试技巧:1. Console.log 调试在 Solidity 合约中使用 console.log:import "hardhat/console.sol";contract MyContract { function setValue(uint256 _value) public { console.log("Setting value to:", _value); value = _value; console.log("Value set successfully"); }}2. 交易回溯使用 hardhat --verbose 查看详细交易信息:npx hardhat test --verbose在测试中捕获错误堆栈:try { await contract.someFunction();} catch (error) { console.log(error);}3. Hardhat Network 控制台启动交互式控制台:npx hardhat console在控制台中执行命令:const Contract = await ethers.getContractFactory("MyContract");const contract = await Contract.deploy();await contract.setValue(42);console.log(await contract.value());4. 调试测试使用 --debug 标志运行测试:npx hardhat test --debug5. 查看事件监听合约事件:contract.on("EventName", (arg1, arg2, event) => { console.log("Event emitted:", arg1, arg2);});6. 状态快照使用快照功能快速重置状态:const snapshot = await ethers.provider.send("evm_snapshot", []);// 执行操作await ethers.provider.send("evm_revert", [snapshot]);7. Gas 分析使用 gas-reporter 插件分析 Gas 使用:npx hardhat test --gas最佳实践:在开发阶段大量使用 console.log生产环境移除 console.sol 导入使用有意义的日志信息结合事件和日志进行调试利用快照功能提高测试效率
阅读 0·2月21日 15:58