Search code examples
node.jsethereumsoliditysmartcontractshardhat

Hardhat, giving unrealistic balance


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

balanceBeforeand balanceAfter are BigNumber and without .toString() they give me the hex code.

The fact is, those number are unrealistic... why ?


Solution

  • import { ethers } from "ethers";
    
    let price = ethers.utils.formatUnits(balance.toString(), "ether");