2

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);
1

2 Answers 2

1

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().

1
  • I think it's better to avoid using .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. Nov 11, 2021 at 15:19
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.