JavaScript 不可用。

我们检测到浏览器禁用了 JavaScript。请启用 JavaScript 或改用支持的浏览器来继续访问

Hardhat 项目测试的常见操作

作者:Anban Chu

发表日期:2023年12月27日

所属目录:合约开发

标签:
  • Hardhat: 基于 2.19 版本的
    • contract.target 是合约地址。
  • ethers: ^6.4.0
    • ethers.parseEther('1')

console

import "hardhat/console.sol";
console.log(amount);

test

import { time, loadFixture } from "@nomicfoundation/hardhat-network-helpers";
// import { anyValue } from '@nomicfoundation/hardhat-chai-matchers/withArgs'
import { expect } from "chai";
import { ethers } from "hardhat";
import { BigNumber } from "ethers";

获取地址

const address = await ethers.getSigners(); // address 是一个地址集合

// 地址展开
const [owner, user1, user2, user3, user4, user5, user6, user7, user8, user9, user10] = await ethers.getSigners();

如果需要多账号多金额

		hardhat: {
			allowUnlimitedContractSize: true,
			chainId: 22,
			accounts: {
				count: 100,
				accountsBalance: "88880000000000000000000000",
			},
		},

部署合约

const Lock = await ethers.getContractFactory("Lock");
const lock = await Lock.deploy(unlockTime, { value: lockedAmount });

时间穿梭

await time.increaseTo(unlockTime); // 穿梭时间
await time.advanceBlockTo(20); // 穿梭区块

异常判断

// 相同
expect(await lock.owner()).to.equal(owner.address);

// reverted
await expect(contract.call()).to.be.reverted; // reverted
await expect(lock.withdraw()).not.to.be.reverted; // not reverted

// reverted 预期错误信息
await expect(contract.call()).to.be.revertedWith("Some revert message");
await expect(contract.call()).not.to.be.revertedWith("Another revert message");

// 自定义错误
await expect(contract.call()).to.be.revertedWithCustomError(contract, "SomeCustomError");
await expect(contract.call()).to.be.revertedWithCustomError(contract, "SomeCustomError").withArgs(anyValue, "some error data string");

// 事件触发
await expect(lock.withdraw()).to.emit(lock, "Withdrawal").withArgs(lockedAmount, anyValue);

// ETH余额变化
await expect(lock.withdraw()).to.changeEtherBalances([owner, lock], [lockedAmount, -lockedAmount]);
await expect(() => sender.sendTransaction({ to: someAddress, value: 200 })).to.changeEtherBalance(sender, "-200");
await expect(token.transfer(account, 1)).to.changeTokenBalance(token, account, 1);
// 多个地址同时检查
await expect(() => sender.sendTransaction({ to: receiver, value: 200 })).to.changeEtherBalances([sender, receiver], [-200, 200]);
await expect(token.transferFrom(sender, receiver, 1)).to.changeTokenBalances(token, [sender, receiver], [-1, 1]);

ether.js

转账给其他地址

// 转账给其他地址
const [owner, user1, user2, user3, user4, user5, user6, user7, user8, user9, user10] = await ethers.getSigners();
await user10.sendTransaction({
	to: user1.address,
	value: ethers.utils.parseEther("1.0"), // Sends exactly 1.0 ether
});

更多参考资料

相关的命令

Command Line

  • install :
    • npm i
  • create .env file in project root path
    • mirror .env.example
  • compile :
    • npm run compile
    • if got Error HH12: run command npm install --save-dev hardhat
  • test :
    • npm run test
  • coverage :
    • npm run coverage
  • view methods gas :
    • npm run gas
  • deploy on hardhat temp network
    • npm run d
  • deploy on localhost network
    • Terminal 1: npm run node
    • Terminal 2: npm run d:l
  • deploy on blockchain network
    • ✅ Product: npm run d:fbchain

All scripts is in package.json file.

Open in remixd web

  1. npm install -g @remix-project/remixd
  2. remixd / npm run remixd




以上就是本篇文章的全部内容了,希望对你有帮助。

>> 本站不提供评论服务,技术交流请在 Twitter 上找我 @anbang_account