# 使用fallback传递参数

By [web3zoom](https://paragraph.com/@web3zoom) · 2025-07-10

---

巧妙的使用fallback函数传递函数选择器的参数，实现对方法的调用并计算结果。

    // SPDX-License-Identifier: GPL-3.0
    
    
    // 测试 TestFallback -> FallbackInputOutput -> Counter
    pragma solidity >=0.4.16 <0.9.0;
    
    contract FallbackInputOutput{
        address immutable target;
    
        constructor(address _target){
            target = _target;
        }
    
        fallback(bytes calldata data)external payable returns(bytes memory){
            (bool success, bytes memory res) = target.call{value: msg.value}(data);
            require(success,"call failed");
            return res;
        }
    }
    
    contract Counter{
        uint256 public count;
    
        function inc() external returns(uint256){
            count +=1;
            return count;
        }
    }
    
    contract TestFallback{
        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){
            // 函数选择器Counter.inc
            return abi.encodeCall(Counter.inc,());
        }
    }
    

知识点：

1、函数选择器的使用

2、fallback的使用

3、abi.encodeCall的使用

另外，不能使用receive方法，因为receive没有返回值

---

*Originally published on [web3zoom](https://paragraph.com/@web3zoom/fallback)*
