3分钟Solidity: 5.3 Fallback 回退

如需获取本内容的最新版本,请参见 Cyfrin.io 的Fallback(代码示例)

fallback函数是一种特殊函数,在以下情况下会被执行:

  • 调用了不存在的函数时
  • 以太币被直接发送到合约但 receive() 函数不存在 或 msg.data 不为空时

为了更好地理解 Solidity 在什么情况下会调用 receive 或 fallback 函数,请参考下面的流程图:

scss 复制代码
                 send Ether
                      |
           msg.data is empty?
                /           \
            yes             no
             |                |
    receive() exists?     fallback()
        /        \
     yes          no
      |            |
  receive()     fallback()

当通过transfer或send调用时,fallback函数有2300的gas限制。

solidity 复制代码
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;

contract Fallback {
    event Log(string func, uint256 gas);

    // 回退函数必须声明为外部函数。
    fallback() external payable {
        // 发送 / 转账(向该回退函数转发2300 gas)
        // 调用(转发这些所有gas)
        emit Log("fallback", gasleft());
    }

    // Receive 是 fallback 的一种变体,当 msg.data 为空时触发
    receive() external payable {
        emit Log("receive", gasleft());
    }

    // 用于检查该合约余额的辅助函数
    function getBalance() public view returns (uint256) {
        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, "以太币发送失败");
    }
}

Remix Lite 尝试一下

fallback 操作可选择性地接受 bytes 作为输入和输出

solidity 复制代码
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;

// TestFallbackInputOutput -> FallbackInputOutput -> Counter
contract FallbackInputOutput {
    address immutable target;

    constructor(address _target) {
        target = _target;
    }

    fallback(bytes calldata data) external payable returns (bytes memory) {
        (bool ok, bytes memory res) = target.call{value: msg.value}(data);
        require(ok, "call failed");
        return res;
    }
}

contract Counter {
    uint256 public count;

    function get() external view returns (uint256) {
        return count;
    }

    function inc() external returns (uint256) {
        count += 1;
        return count;
    }
}

contract TestFallbackInputOutput {
    event Log(bytes res);

    function test(address _fallback, bytes calldata data) external {
        (bool ok, bytes memory res) = _fallback.call(data);
        require(ok, "call failed");
        emit Log(res);
    }

    function getTestData() external pure returns (bytes memory, bytes memory) {
        return
            (abi.encodeCall(Counter.get, ()), abi.encodeCall(Counter.inc, ()));
    }
}

Remix Lite 尝试一下


END

相关推荐
OxYGC5 小时前
[Web3] 钱包业务第十三篇: Ethereum 钱包链调研实战
web3·区块链·以太坊·比特币
大今野2 天前
2026 专转本计算机|区块链典型框架:以太坊与超级账本 Fabric 考点全解(附例题)
运维·区块链·fabric·专转本
OxYGC7 天前
[Web3] 智能合约 19: NFT 知识与 Web3 求职指南
区块链·智能合约·求职招聘·nft
OxYGC8 天前
[Web3] 智能合约 18: 借贷之 Compound V2 源码解读
web3·智能合约
OxYGC8 天前
[Web3] 智能合约 00: 系列总览与目录
web3·区块链·智能合约·solidity
木西8 天前
别再只听概念了!手把手手搓图币二象性合约|DN-404 极简实战
web3·智能合约·solidity
Bczheng19 天前
五十六.网络(6)--广播tx测试
区块链
OxYGC9 天前
[Web3] 智能合约 15: 项目实战⑦ 从0到1 体育投注
web3·智能合约
OxYGC10 天前
[Web3] 智能合约 13: 项目实战⑤ DappLink Oracle
oracle·web3·智能合约
木西13 天前
深度拆解 MyShell 底层:AI Agent 生态结算合约,如何实现安全防阻塞
web3·智能合约·solidity