23-03-02
Last Update:
2023-03-02
call与fallback
fallback函数被调用的情况
1 | Which function is called, fallback() or receive()? |
- 函数名称不存在
Receive函数没有 function 关键字,没有参数也没有返回值,且必须是 external 可见性(允许外部合约调用)并具有 payable 可支付属性。
转账的时候会先调用receive,没有receive的时候会调用fallback
address可以唯一标识一个合约,某种意义上来说,address就是合约
接收及发送以太币(send、transfer、call)
How to send Ether?
如何发送以太币?
You can send Ether to other contracts by
你可以通过以下方式发送以太币给其它合约
- transfer (2300 gas, throws error)
- send (2300 gas, returns bool)
- call (forward all gas or set gas, returns bool)
How to receive Ether?
如何接收以太币?
A contract receiving Ether must have at least one of the functions below
接收以太币的合约必须拥有至少一个以下函数:
- receive() external payable
- fallback() external payable
receive() is called if is empty, otherwise is called.msg.datafallback()
当receive()是空的的时候会被调用,否则msg.data
会调用fallback()
Which method should you use?
call in combination with re-entrancy guard is the recommended method to use after December 2019.
call函数是2019年12月份之后被推荐使用的,它结合了重入防御机制
Guard against re-entrancy by
通过以下方式防止重入
making all state changes before calling other contracts / 在调用其他合约之前先做完所有状态的改变
using re-entrancy guard modifier / 使用防重入修饰符
call能省一些gas(详见-> 视频)
如下代码:
1 | // SPDX-License-Identifier: MIT |