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.

Bogdoslavik
  • 331
  • 1
  • 2
  • 6

9 Answers9

19
const { ethers, waffle} = require("hardhat");
const provider = waffle.provider;
const balance0ETH = await provider.getBalance(user1.address);
Russo
  • 1,784
  • 1
  • 21
  • 30
15

Maybe you can try:

await contract.provider.getBalance(contract.address)

contract is a 'Contract' class in @ethersproject.

amber
  • 151
  • 1
  • 4
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")

Shariq Anwar
  • 61
  • 1
  • 1
5

Updated 2022

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

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

Aaron Ullal
  • 151
  • 1
  • 2
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)
}
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

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

djn
  • 21
  • 1
  • 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 04 '22 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 '22 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());
klugjo
  • 113
  • 4
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.

d3mage
  • 11
  • 4