21

I writing hardhat/ethers/waffle unit tests for my contract and need to know Ether balance of my contract.

Chai matcher changeEtherBalance needs Signer object to check balance, but I have address of my contract only.

I know, what ethers.js has provider object with getBalance(address) method, so I have installed nomiclabs-hardhat-ethers plugin and can call ethers.provider.getBalance(address)

It works well for addresses from signers ethers.getSigners() but returns 0 for mycontract.address.

How I can know my contract Ethereum balance? Please advice.

3
  • Are you sure that your contract is deployed and funded with ETH at the time of calling getBalance on it? Mar 29, 2021 at 9:58
  • 1
    Also posting the same question in the Hardhat Discord server might help. Mar 29, 2021 at 9:58
  • @PaulRazvanBerg yes, contract is deployed and other tests passed well (sender's balances decreased after sending ethers and increased after withdrawals). Thank you for pointing me to discord channel. Mar 30, 2021 at 12:00

9 Answers 9

19
const { ethers, waffle} = require("hardhat");
const provider = waffle.provider;
const balance0ETH = await provider.getBalance(user1.address);
1
  • 2
    also you can get only using const balance0ETH = await ethers.provider.getBalance(owner.address); Mar 22, 2023 at 3:42
15

Maybe you can try:

await contract.provider.getBalance(contract.address)

contract is a 'Contract' class in @ethersproject.

2
  • 1
    Did not work for me in the context of testing a smart contract in Hard Hat.
    – MetaZebre
    Mar 4, 2022 at 21:46
  • Finally !!! Not a single soul is giving this information in the whole f0ing internet,
    – BOOMHUNK
    Jul 27, 2022 at 15:52
6

You can obtain the ETH balance of your Contract directly using the following code snippet:

import {ethers} from "hardhat"

const contractBalance = await ethers.provider.getBalance("CONTRACT_ADDRESS")
4

Updated 2022

contractFactory = await ethers.getContractFactory("MySmartContract");
contract = await contractFactory.deploy();    
await contract.deployed();

const contractBalance = await ethers.provider.getBalance(contract.address);
3

This way is work for me

const { ethers } = require('hardhat')
async function getBalance(address) {
  const balance = await ethers.provider.getBalance(address)
  return hre.ethers.utils.formatEther(balance)
}
1
  • This is what works in 2023. Thanks
    – Udo E.
    Jul 17, 2023 at 9:56
2

{Update 2023}

This Is a hardhat test case where i am testing if ethers are transfering or not.

    it("Should Transfer Ethers to the seller", async function () { 
   
    ///its basically approving NFT to marketplace
    await nftContract.approve(marketplace.address, 1);
    /// Listing NFT into marketplace (tokenID, price)
    await marketplace.createMarketItem(1, ethers.utils.etherParse("1");
    
    const prevBalance = await ethers.provider.getBalance(seller.address);
    const prevBalanceInEth = ethers.utils.formatEther(prevBalance)
    //passing 1.1 Ether bcoz its needed gas fee also
    await marketplace.connect(buyer).BuyFlowerNFT(1, { value: ethers.utils.etherParse("1") }); 
    
    const balance = await ethers.provider.getBalance(seller.address);
    const balanceInEth = ethers.utils.formatEther(balance)
    //when a buyer buy nft then in seller account ether balance will be added 
    //so for that we stored  diff between prev balance and after purchasing
    const differenceInEtherBal = balanceInEth - prevBalanceInEth;

    expect(differenceInEtherBal).to.equal(1);
})
1
  • You should add a text explaining what are you trying to achieve.
    – Ismael
    Sep 28, 2022 at 4:25
1

Have you tried setting an ethers provider first?

prov = ethers.getDefaultProvider();

const balance = await prov.getBalance(address);

https://docs.ethers.io/v5/api/providers/#providers-getDefaultProvider

2
  • 2
    this was very unhelpfull for me (testing smart contracts with Hard Hat) as the default provider sent me some weird false values.
    – MetaZebre
    Mar 4, 2022 at 21:45
  • I had garbage results getting the balance of a contract using ethers.getDefaultProvider(), but it worked as expected using provider = waffle.provider or provider = ethers.provider
    – sola24
    Jun 22, 2022 at 20:26
0

For Ethers.js v6 and Hardhat 2.17 (August 2023)

This is how I was able to read the ETH balance of a contract in my unit tests:

const ethBalance = await myContract
  .runner?
  .provider?
  .getBalance(myContract.getAddress());
0

Ethers V6 introduced new breaking changes and moved ethers.provider.* into ethers.*. I have used the following code in my tests:

const [user] = await ethers.getSigners();
const provider = await user.provider;
const balance = await provider.getBalance(owner);

I suppose there is a more elegant solution.

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.