How do you return ETH balance in hardhat test?
This snippet works but returns a BigNumber hex value.
const balance = await ethers.provider.getBalance(owner.address);
console.log(balance);
How do you return ETH balance in hardhat test?
This snippet works but returns a BigNumber hex value.
const balance = await ethers.provider.getBalance(owner.address);
console.log(balance);
If the value is smaller than 64 bits, covert to a number: balance.toNumber()
. If it larger than 64 bits, cast it as a string to avoid bigger numbers from being cut off: balance.toString()
.
.toNumber
unless you have a really good reason to. The way I think about this is: use bignumbers for everything except to print something or show something in a UI, and in that case convert the number to a string.
Commented
Nov 11, 2021 at 15:19
ethers provides a function to format the BigNumber:
ethers.utils.formatEther(balance)
Link to docs (v5): https://docs.ethers.io/v5/api/utils/display-logic/#utils-formatEther
The snippet returns the ethereum balance of owner.address
in units of WEI
in order to transform this into units of ETH
, divide the number by 1000000000000000000