Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.6k views
in Technique[技术] by (71.8m points)

ethereum - payable() function In solidity

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...