How to get a contract's balance in Solidity?

  • How do I get the balance of a contract with solidity? I know geth has web3.eth.getBalance(), but that's to get the contract balance after it has been deployed.

    Is there a standard way of doing this, or do I have to have a separate var that uint256 that keeps track of the total balance?

  • 0xcaff

    0xcaff Correct answer

    4 years ago

    You can do this by calling address.balance. To get the contract's balance, just do this.balance. Read the docs.

    Update: As of Solidity ^0.4.24, you need to do:

    address(this).balance
    

    (Copied from answer by Paul Berg)

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM