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?


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:


(Copied from answer by Paul Berg)

  • Can i set address(this).balance = 0 in withdraw function to prevent re-enterancy before transfer call. It gives me " Expression has to be an lvalue" on doing so
    – Coderboi
    Oct 23 '21 at 13:51

Small update from my side:

enter image description here

You want to use the following, address(this).balance;


As of Solidity ^0.4.24, you need to do:


Find an example here.


In case of invalid opcode, use a local variable of type address payable as workaround:

        address payable self = address(this);
        uint256 balance = self.balance;

In solidity 0.5.14, I get an invalid opcode, debugging showed me, that it is exactly here:


Test result:

     Error: Returned error: VM Exception while processing transaction: invalid opcode
     at PromiEvent (node_modules/truffle/build/webpack:/packages/contract/lib/promievent.js:6:1)                                                              
      at TruffleContract.destroy (node_modules/truffle/build/webpack:/packages/contract/lib/execute.js:158:1)                                                 
      at Context.it (test/1_PactaTest.js:108:17)                                                                                                              
      at web3.eth.getBlockNumber.then.result (node_modules/truffle/build/webpack:/packages/core/lib/testing/testrunner.js:161:1)                              
      at <anonymous>                                                                                                                                          
      at process._tickCallback (internal/process/next_tick.js:188:7)                                                                                          




115:     // move whole balance to the owner
116:     function withdraw() public managers isActive {
117:         uint256 part = address(this).balance / beneficiaries.length;


Transaction halted with a RUNTIME ERROR.

This is likely due to an intentional halting expression, like assert(), require() or revert(). It can also be due to out-of-gas exceptions. Please inspect your transaction parameters and contract code to determine the meaning of this error.
  • This solution throws an error TypeError: Type address is not implicitly convertible to expected type address payable. address payable self = address(this);
    – Dziugas
    Apr 4 '20 at 21:01
  • 7
    use payable(address(this)).balance Jun 7 '20 at 6:20

uint256 contractBalance = address(this).balance;


Create a simple function which accept the address and in return it just return back amount of eth u hold

function balance(address owner) public view returns(uint accountBalance)
            accountBalance = owner.balance;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy