Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
4.0k views
Welcome To Ask or Share your Answers For Others

1 Answer

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{
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share
...