9

When someone transfers ether to a contract address the payable callback (without name) function is called automatically. What I don't understand is why are there many examples with multiple payable functions ? Are all of them automatically called? Why do they have to be payable and why don't I call them normally from the callback function?

In my examples I noticed that if I have only one payable function and it has a name, the contract can't receive ether so it is somehow not automatically called but only if the payable function has no name. Is that normal or did I do a mistake?

3 Answers 3

3

Anyone can call any public function on your contract, by sending a transaction to that contract and including "data" that names that function.

If someone sends a transaction with ether to a function, and the contract did not mark the function as payable, then the call will be rejected. If the contract did mark the function as payable, though, that ether will be deposited into the contract's control. (and whatever code is in the function will run)

The fallback function is called when you send a transaction to a contract, and don't identify any function (in other words, "data" is empty). All the payable rules in the previous paragraph apply. Nothing prevents you from sending 0 ether to the fallback function.

1

When someone transfer ether to a contract address the payable callback (without name) function is called automatically.

This is not entirely true. There is only one fallback function (which can also be payable). But the payable modifier does not make a function a fallback, i.e. payable functions are not called automatically. Which function is to be called is encoded in a transaction's data field. The only thing payable does is that it allows a non-zero value.

1
  • Ok. Thanks. I'll try all of this and after that I hope everything is more clear.
    – Dany D
    Sep 2, 2017 at 6:40
0

In my examples I noticed that if I have only one payable function and it has a name, contract can't receive ether

The contract can receive ether when that payable function is called.

Contracts may have many payable functions so that it can receive ether whenever any of those functions are called.

Why do they have to be payable and why don't I call them normally from the callback function?

Because the fallback function usually only gets 2300 gas, explained as Case1 of How much computation can be done in a fallback function?

1
  • Thank you for the explanation. It is more clear now. What I still don't understand is if these payable functions should be called from the outside or from the inside? From the outside something like: mycontract c = contract (contract.address); c.payableFunction(); Most of times an EOA send eth to the contract address and this way the payable function should be called automatically. This is the only way. Am I write?
    – Dany D
    Sep 2, 2017 at 5:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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