详解函数动态调用的作用——call

动态调用的作用

  • 类似于其他语言的反射
  • 能够开发框架性代码

Call调用语法

(bool success, bytes data) = <address>.call(bytes calldata)
  • call是address的方法
  • call返回值(bool success, bytes data)
  • 忽视返回值success,会造成严重问题

calldata的结构

  • call的参数是calldata
  • calldata的前四个字节是selector,剩下的是参数编码
  • selector = bytes(keccak256())
  • keccak256:哈希sha3->256

Abi工具函数

  • calldata = abi.encodeWithSignature(sig, ps)–会用
  • 返回值解码abi.decode(bytes)

编码、解码 decoder.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

contract AbiDecode {
    
    
    struct MyStruct {
    
    
        string name;
        uint[2] nums;
    }

    function encode(
        uint x,
        address addr,
        uint[] calldata arr,
        MyStruct calldata myStruct
    ) external pure returns (bytes memory) {
    
    
        return abi.encode(x, addr, arr, myStruct);
    }

    function decode(
        bytes calldata data
    )
        external
        pure
        returns (uint x, address addr, uint[] memory arr, MyStruct memory myStruct)
    {
    
    
        // (uint x, address addr, uint[] memory arr, MyStruct myStruct) = ...
        (x, addr, arr, myStruct) = abi.decode(data, (uint, address, uint[], MyStruct));
    }
}

效果:
在这里插入图片描述
Callee.sol

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.8.2 <0.9.0;

contract Callee {
    
    
    uint public x;
    function setX(uint _x)public returns(uint){
    
    
        x = _x;
        return x;
    }
}

Caller.sol

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.8.2 <0.9.0;

contract Caller{
    
    
    address calleeAddress;
    uint public xx;
    constructor(address _calleeAddress){
    
    
        calleeAddress = _calleeAddress;
    }
    function setCalleeX(uint _x)public{
    
    
        bytes memory cd = abi.encodeWithSignature("setX(uint256)", _x);
        (bool suc, bytes memory rst) = calleeAddress.call(cd);
        if(!suc){
    
    
            revert("call failed");
        }
        (uint x) = abi.decode(rst, (uint));
        xx = x;
    }
}

效果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_37117521/article/details/139682912