I have read many examples of payable functions, but I didn't understand any of them. Also, they talked about modifier in payable. My questions are:
- What exactly does payable() function do?
- Does this function take arguments?
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.
Sign up to join this communityI have read many examples of payable functions, but I didn't understand any of them. Also, they talked about modifier in payable. My questions are:
- What exactly does payable() function do?
- Does this function take arguments?
First, payable
is a modifier that can be added to a function. What you are most likely misinterpreting is a use case like:
function () public payable {}
It's impossible to have payable()
as a function name as it is a reserved keyword. You may use payable
only in addition to existing functions like:
function deposit() payable {}
function register(address sender) payable {}
Second, payable
allows a function to receive ether while being called as stated in docs. It's mandatory to include the payable
keyword from Solidity 0.4.x. If you try to send ether using call, as follows:
token.foo.call.value("ETH_TO_BE_SENT")("ADDITIONAL_DATA")
to a function without a payable
keyword, the transaction will be rejected.
Usually, there is a no name function to accept ether to be sent to a contract which is called a fallback function:
function () payable {}
But you may have more than one payable
annotated functions that are used to perform different tasks, like registering a deposit to your contract:
function deposit() payable {
deposits[msg.sender] += msg.value;
};
When someone transfers funds to your contract, the function with payable
modifier executes automatically. Here is an example of a payable
function.
contract token {
function () payable {
create(msg.sender);
}
function create(address _beneficiary) payable{
uint256 amount = msg.value;
/// your logic
}
}
payable
)? So create gets called twice?
– David 天宇 Wong
Sep 27 '17 at 12:31
Each function with the payable modifier can receive funds. But what if funds are sent to your contract to a non payable function? For this the fallback payable function was defined, which can receive funds in any case funds are sent to the contract. This is why can see in many contracts some version of a function with noname and a payable modifier. Notice - this is a fallback payable function. But payable is not the function name, it has no name, its the function modifier.
function *noname* () payable { }
A heads up for Solidity 0.6.0: there have been some pretty major changes to the way a fallback payable function is defined.
From the docs:
The unnamed function commonly referred to as “fallback function” was split up into a new fallback function that is defined using the fallback keyword and a receive ether function defined using the receive keyword.
So what you have to do is replace this:
function () external [payable] { ... }
With either:
receive() external payable { ... }
Or:
fallback() external [payable] { ... }
But ideally you should go with receive
.