详解函数动态调用的作用——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;
    }
}

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

  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值