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
  • 1
    You can use ethers (docs.ethers.io) library. ethers.utils.formatEther(Bignumber here) Commented Nov 9, 2021 at 10:00

3 Answers 3

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. Commented 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

0

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

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.