Hardhat 寫測試
獲取地址餘額
const ownerAddress = await owner.getAddress();
const initialOwnerBalance = await ethers.provider.getBalance(ownerAddress);獲取 function gas 與 gas price
現在的 etherjs v6 版本使用 js 原生 BIgInt 即可。
// 呼叫 function 後獲取 receipt,即可獲取到 gasUser, gasPrice
const stakeTx = await staker.connect(owner).stake();
const stakeReceipt = await stakeTx.wait();
const stakeGasUsed = BigInt(stakeReceipt.gasUsed) * BigInt(stakeReceipt.gasPrice);it("Should unstake correctly after the unbonding period", async function () {
const ownerAddress = await owner.getAddress();
const initialOwnerBalance = await ethers.provider.getBalance(ownerAddress);
// Stake
const stakeTx = await staker.connect(owner).stake({ value: ethers.parseEther("1") });
const stakeReceipt = await stakeTx.wait();
const stakeGasUsed = BigInt(stakeReceipt.gasUsed) * BigInt(stakeReceipt.gasPrice);
const afterStakeBalance = BigInt(initialOwnerBalance) - BigInt(ethers.parseEther("1")) - BigInt(stakeGasUsed);
expect(await ethers.provider.getBalance(ownerAddress)).to.equal(afterStakeBalance);
});時間機器
可設置當前時間
Event 參數
記得 await 放在 expect 前
測試 Require
Last updated
Was this helpful?