fallback是特殊的函数,无参数,无返回值。
fallback是特殊的函数,无参数,无返回值
何时会被调用:
当使用transfer或者send对合约进行转账时,fallback函数的gaslimit限定为2300 gas
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
pragma solidity ^0.8.9;
contract Fallback {
event Log(uint gas);
fallback() external payable {
//gasleft 当前还剩的gas
emit Log(gasleft());
}
function getBalance() public view returns (uint) {
return address(this).balance;
}
}
contract SendToFallback {
function transferToFallback(address payable _to) public payable {
_to.transfer(msg.value);
}
function callFallback(address payable _to) public payable {
(bool sent, ) = _to.call{value: msg.value}("");
require(sent, "Failed to send Ether");
}
function callNoExistFunc(address payable _to) public payable {
(bool sent, ) = _to.call{value: msg.value}(
abi.encodeWithSignature("noExistFunc()")
);
require(sent, "Failed to call");
}
}
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!