The question in the title says it all.

To the best of my understanding, the only way to deposit ether into a contract is by calling a payable function, and set msg.value larger than 0 (and of course, the function should complete successfully without reverting).

But if there are no payable functions to begin with, is it ever possible that the ether balance of the contract will be larger than 0?

Thank you!


Yes, a contract can have Ether balance without any payable function.

There are three ways to do it:

1) selfdestruction. Another contract self destructrs and sends its remaining Ether to your contract

2) Target of mining. Ether rewarded from mining can't be refused.

3) Ether sent to the contract before the contract exists.

More details about these alternatives can found for example here: https://medium.com/@alexsherbuck/two-ways-to-force-ether-into-a-contract-1543c1311c56

  • Great, thank you! Question on bullet #3: So this is just "a long shot in the dark"? I send ether to some public address without knowing it's private key, and at a later point in time, somebody deploys a contract which happens to "fall" on the same address - is that correct, or have I misinterpreted you intention? – goodvibration Dec 12 '18 at 12:56
  • 2
    @goodvibration The chance of someone generating any completely random address (either through a private key or contract deployment) is incredibly small. However, contract addresses are not random, they are deterministic. For any given address, you can know in advance the contract addresses it will generate: ethereum.stackexchange.com/questions/9776/… – Jesse Busman Dec 12 '18 at 14:29
  • 2
    Additionally, a trivial case: before Solidity 0.4 payable didn't exist, so all those Solidity contracts have ether without a payable function :) – eth Dec 13 '18 at 2:52

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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