payable() function In solidity

Viewed 12748

I dont really understand payable() should i use

contract(){
receive() {
 somefunction()
}

somefunction() payable external{
}

or it is enough to use

contract(){

somefunction() payable external{}
}

do they need to be used together - we use fallback function and some normal payable modifier what is the difference of using them both or just fallback or just somefunction? What if there are 2 functions with modifier payable can you give me some examples?

1 Answers

The presence of the payable modifier means that the function can process transactions with non-zero Ether value. If a transaction that transfers Ether comes to the contract and calls some function X, then if this function X does not have the payable modifier, then the transaction will be rejected. The fallback function is designed to handle the situation when no function is called at all in a transaction comes to the contract (for example, the transaction simply transfers ether), or a function is called that is not in the contract. In this case, similar to the one described above - if the fallback function has the payable modifier, then it will be able to accept transactions with the transfer of Ether, if not, then they will be rejected.

Accordingly, in your case, if Ether is being transmitted in the transaction, the function somefunction will be called successfully, and when the receive function is called, the transaction will be rejected.

contract(){
receive() {
 somefunction()
}

somefunction() payable external{
}
Related