13

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?

Badr Bellaj
  • 18,780
  • 4
  • 58
  • 75
Seetharaman GR
  • 851
  • 1
  • 7
  • 14

3 Answers3

19

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

nz_21
  • 279
  • 5
  • 10
Mikko Ohtamaa
  • 22,269
  • 6
  • 62
  • 127
1

The contract probably has a fallback function that generates an exception or uses more than 1000000 gas (which would itself cause an exception).The fallback function goes by:

function( ) {throw;}

You can check this by using following code snippet:

var status= debug.traceTransaction("your_transaction_hash);
if (status.structLogs.length > 0) {
  console.log(status.structLogs[status.structLogs.length-1].error)
}

And if you output as:

"invalid jump destination (PUSH1) 2"

Then your code throws.

What could be the way to get the balance in contract account. If you are comfortable using geth use :

eth.getBalance("0x045bfe22453a9ca06aff4bdc5d7f5870eba121bd")

Or you can also see your contract balance in Mist for that you have to watch the contract.
Solution:
From comments section :
Add payable modifier to the fallback function to allow contract to recieve ethers. You need to add payable modifier to any function that seeks to recieve ethers.

Prashant Prabhakar Singh
  • 8,026
  • 5
  • 40
  • 79
  • Simple calling

    eth.getBalance("0x045bfe22453a9ca06aff4bdc5d7f5870eba121bd")

    Return Zero.

    How can I restrict the "invalid jump destination error".

    Thanks.

    – Seetharaman GR Mar 27 '17 at 01:55
  • For that, you have to remove the fallback function from your contract that prevents contract from accidently accepting ethers. – Prashant Prabhakar Singh Mar 27 '17 at 05:02
  • http://solidity.readthedocs.io/en/develop/contracts.html#fallback-function

    As per this, we couldn't remove the callback function. How can I test the callback function execution costly less than 2300 gas.

    – Seetharaman GR Mar 27 '17 at 05:54
  • Hmm.. thanks for pointing out. I was not updated with changes in new version of solidity. As the doc says: "this function is executed whenever the contract receives plain Ether (without data)", so you can send some data along with sendTransaction. Also you can use buy function of token to send ethers to contract see here – Prashant Prabhakar Singh Mar 28 '17 at 06:15
  • 1
    Thanks. I have resolved it. Able to retrieve the contract balances by adding the payable modifier to the fallback function. – Seetharaman GR Mar 28 '17 at 16:47
  • Great. You can update the answer for others. – Prashant Prabhakar Singh Mar 29 '17 at 04:25
1

If you're using newer versions of solidity to compile your contract, you need to ensure your fallback function is defined, and has the payable modifier:

contract IsPayable {
  function () payable {}
}
DeviateFish
  • 1,128
  • 5
  • 11