Hardhat 寫測試
獲取地址餘額
const ownerAddress = await owner.getAddress();
const initialOwnerBalance = await ethers.provider.getBalance(ownerAddress);獲取 function gas 與 gas price
// 呼叫 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 參數
測試 Require
Last updated