I'm trying to understand open zepplins crowd sale contract code: https://github.com/OpenZeppelin/zeppelin-solidity

I understand that if someone sends ether to the contract address that it responds with:

   // fallback function can be used to buy tokens
  function () public payable {

But I'm a little confused about how "payable" works. Can only one function per contract be "payable"? And is that function basically just an event listener for when the contract receives ether?


3 Answers 3


payable is a modifier that allow a function to be called with a non zero value (that you can access via msg.value ) .

function canReceiveEther() payable {
 /// some code here

Exemple above will be able to be called and deal with any amount of ether sent to it .

If you do not mark your function as payable but someone do a call to it it will be rejected .

Now the function that you mentionned in your post is kinda a special one, it's what we call the fallback function (see the official doc here) That's the only function that can be unnamed and will be executed if you call the contract but none of the function match the parameters or if you don't provide data.

So in short :

  • More than one function can be marked as payable but only one can be the fallback function .
  • The fallback function will be called when :
    • No functions match the data you provided .
    • You call the contract with no data .
    • You call the contract with send or transfer.

Note : fallback function only have access to a really limited amount of gas (2300 , see doc) . So it's better to keep the cost of the function as cheap as possible .

  • 1
    Thanks. The fallback makes sense, but I didn't realize that ether could be sent to a function. I thought ether could only be sent to an ether address?
    – mark
    Dec 9, 2017 at 19:35
  • Well your contract is deployed on the chain and thus it has an address and can have an ether balance (that you can access in solidity via {{address}}.balance or this.balance . Dec 9, 2017 at 19:38

The Solidity docs explain the fallback function well:

A contract can have exactly one unnamed function. This function cannot have arguments and cannot return anything. It is executed on a call to the contract if none of the other functions match the given function identifier (or if no data was supplied at all).

Furthermore, this function is executed whenever the contract receives plain Ether (without data). Additionally, in order to receive Ether, the fallback function must be marked payable. If no such function exists, the contract cannot receive Ether through regular transactions.

Additionally there is substantially less gas available to the fallback function as a result of the transaction gas costs.

The fallback function may be used to receive ether through a regular transaction but any function may be marked payable.

  • Fallback function and payable function are orthogonal concepts. Fallback function may be non-payable, and non-fallback function may be payable. In old versions of Solidity all functions, including constructor and fallback function, were implicitly payable. Dec 9, 2017 at 18:57
  • Thanks for the clarification @MikhailVladimirov, I've updated the answer. Dec 9, 2017 at 19:45
  • Also, your statement about gas limitation for callback function is not correct. 2300 gas limit applies only to contract invocations with no data supplied, and does apply to invocations of fallback function with non-empty message data. Dec 10, 2017 at 5:18

Sure, any public or external function may be payable, including fallback function, and there may be many payable functions inside one contract. Also, constructor may be payable.

You may attach some ether to any contract invocation, not only to the simple send without data. You may also attach some ether to contract creation.

Your Answer

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

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