[Ethereum] How to get ETH contract balance with Ethers.js

balancescontract-invocationetherethers.js

I am trying to get the ETH balance of a contract:

const balance = await contract.getBalance();

but it fails:

TypeError: contract.getBalance is not a function

What I do wrong and how to fix the error?

Best Answer

getBalance is a function of the Ether.js blockchain provider object, it is used this way :

const balance = await provider.getBalance("address");

Note that you can use contract.address to obtain the address of the contract instance.

Related Topic