I'm actually trying to do some unit tests with Hardhat/Ether/Chai, I try to test the balance decrease after a buy.
There is my code :
it('Should buy the nft', async function () {
const [buyerAddress] = await ethers.getSigners()
const provider = ethers.provider
const balanceBefore = await provider.getBalance(buyerAddress.address)
await marketReady
.connect(buyerAddress)
.buyNFTFromEscrow(nftContractAddress, nftTokenId, { value: ethers.utils.parseEther('6') })
const balanceAfter = await provider.getBalance(buyerAddress.address)
console.log(balanceBefore.toString(), balanceAfter.toString())
})
The console log output :
9999986719269931896192 9999986523302573800272
balanceBefore
and balanceAfter
are BigNumber and without .toString()
they give me the hex code.
The fact is, those number are unrealistic... why ?
CodePudding user response:
The Ethereum balance is expressed as wei units. You need to convert them to human readable decimals to make sense out of them. Ethers library should have an utility function for it, or you can just divide the number with 10 exp 18
.
CodePudding user response:
import { ethers } from "ethers";
let price = ethers.utils.formatUnits(balance.toString(), "ether");