我试着比较了几种方法。我真的很想用ETH来比较。我试过使用formatEther,但它返回一个字符串。我怎么能用ETH来比较呢?
const ownerBal = await ethers.provider.getBalance(accounts[0].address);
expect(BigNumber.from(ownerBal)).to.be.closeTo(
BigNumber.from(1000),
BigNumber.from(1)
);
最后从字符串中解析浮点数。然后将其四舍五入并将其作为数字进行比较。
async function formatBalance(
address: string,
fractionDigits: number
): Promise<number> {
const bn = await ethers.provider.getBalance(address);
const bal = formatEther(bn);
return parseFloat(parseFloat(bal).toFixed(fractionDigits));
}
const contractBalNew = await formatBalance(contractA.address, 2);
expect(contractBalNew).to.be.equal(0);
expect(contractBalNew).to.be.within(10003, 10004);