[Ethereum] How to retrieve the Contract account balance in web3 or in geth command line

balancesgo-ethereumsolidity

I have created a private network with miner node using Ethereum. Then I deployed an Ethereum smart contract in the network. I have got the contract address as "0x045bfe22453a9ca06aff4bdc5d7f5870eba121bd"

Now from my eth.coinbase I would like to transfer some ethers to the contract account.

After unlocking the account, I used the code

  eth.sendTransaction({from:eth.coinbase,to:"0x045bfe22453a9ca06aff4bdc5d7f5870eba121bd",value:web3.toWei(3,"ether"),gas:'1000000'});

After doing this, I have checked for eth.pendingtransactions, those got mined and added to blocks.

I debugged the transactionReceipt, could not able to get the data anything.

How to get the balance in contract account?

Best Answer

To check the balance you can do:

web3.eth.getBalance(contract.options.address)

Or:

web3.eth.getBalance("0x045bfe22453a9ca06aff4bdc5d7f5870eba121bd")

To transfer balance on a contract without calling a function the contract must have a payable fallback function.

http://solidity.readthedocs.io/en/develop/contracts.html#fallback-function

Related Topic