Solidity Minimalist Primer | Lecture 19: Receiving ETH Receive and Fallback

22-08-15 11:04
Read this article in 6 Minutes
总结 AI summary
View the summary 收起
原文标题:《 Solidity 极简入门: 19. 接收 ETH receive 和 fallback 》
Original author: 0xAa 


I've been relearning Solidity recently to consolidate the details and also write a "Solidity Minimality Primer" for kids to use (programming geeks can find another tutorial) and update 1-3 sessions every week.


All open source code and tutorial on making: github.com/AmazingAng/WTFSolidity


The callback function


Solidity supports two special callbacks, receive() and fallback(), which are used mainly in two situations:


- receive ETH 

- Handles function calls that do not exist in the contract (proxy contract)


This lecture will focus on receiving ETH.


Receive the ETH function receive


Receive () is used only to process the received ETH. A contract can have at most one receive() function, payable in the same way as normal functions, without the function keyword: receive() external Payable {... }


The receive() function can't take any arguments, can't return any value, must include External and Payable.


When the contract receives ETH, receive() is triggered. Receive () is best not to do too much logic because if someone sends ETH using send or transfer, the gas limit is 2300. Complex receive() may trigger an Out of gas error. If you use Call, you can customize GAS to perform more complex logic (these three ways of sending ETH will be covered later).


We can send an event in the receive() line, for example:



Some malicious contracts embed malicious gas consumption content in the receive() function, making some refund contracts not work: the Akutar NFT project was permanently locked up with 11,539 ETH, or nearly $200 million! So be aware of this when writing contracts that include logic such as refunds.


Fallback function


The fallback() function is fired when a function whose contract does not exist is called. It can be used to receive ETH or proxy contract. Fallback () declaration does not require function keyword, must be decorated by external, and will usually be decorated by Payable, to receive ETH:fallback() External Payable {... }.


We define a fallback() function that, when fired, releases the fallbackCalled event and outputs msg.sender, msg.value, and msg.data:



Receive and fallback


Both Receive and fallback can be used to receive ETH. They trigger the following rules:



Simply put, when the contract receives ETH, receive() is triggered if msg.data is empty and receive() exists; When msg.data is not empty or receive() does not exist, Fallback () is triggered, then Fallback () must be Payable.


When receive() and Payable fallback() are not present, an error will be reported for sending ETH to the contract.


conclusion


In this lecture I introduced two special functions in Solidity, receive() and fallback(), which are mainly used in two situations, they are mainly used for dealing with receiving ETH and proxy contracts.


The original link


欢迎加入律动 BlockBeats 官方社群:

Telegram 订阅群:https://t.me/theblockbeats

Telegram 交流群:https://t.me/BlockBeats_App

Twitter 官方账号:https://twitter.com/BlockBeatsAsia

举报 Correction/Report
Choose Library
Add Library
Cancel
Finish
Add Library
Visible to myself only
Public
Save
Correction/Report
Submit