8

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?

11

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

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 {}
}
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.

  • Simple calling eth.getBalance("0x045bfe22453a9ca06aff4bdc5d7f5870eba121bd") Return Zero. How can I restrict the "invalid jump destination error". Thanks. – Seetharaman GR Mar 27 '17 at 1: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 5:02
  • solidity.readthedocs.io/en/develop/… 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 5: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 6: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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.