solidity call使用

solidity call使用

复制代码
<address>.call(bytes memory payload) returns (bool, bytes memory)

call 仅发送ETH,不传入data

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

contract SendEther {
    // 构造函数,payable使得部署的时候可以转eth进去
    constructor() payable {}

    function callEther(address payable recipient, uint256 amount)
        public
        returns (bool)
    {
        (bool success, ) = recipient.call{value: amount}("");
        require(success, "Transfer failed.");
        return success;
    }
}

1、部署合约时附带 value

2、调用合约,给接收地址recipient转账

call 传入data,附带发送ETH

在合约中调用其他的合约的方法

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

contract Callee {
    uint256 value;

    function getValue() public view returns (uint256) {
        return value;
    }

    function setValue(uint256 value_) public payable {
        require(msg.value > 0, "msg.value must > 0");
        value = value_;
    }
    // 获取合约地址的eth余额
    function getBalance() public view returns (uint256) {
        return address(this).balance;
    }

}

contract Caller006 {

    // 构造函数,payable使得部署的时候可以转eth进去
    constructor() payable {}

    function callSetValue(address callee, uint256 value)
        public
        returns (bool)
    {
        Callee meta = Callee(callee);

        // 对函数签名和参数进行编码
        bytes memory methodop = abi.encodeWithSignature(
            "setValue(uint256)",
            value
        );
        (bool flg, ) = address(meta).call{value: 1 ether}(methodop);

        if (!flg) {
            revert("call function failed");
        }

        return flg;
    }

    // 获取合约地址的eth余额
    function getBalance() public view returns (uint256) {
        return address(this).balance;
    }

}

staticcall调用其他合约的方法

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

contract Callee {
    function getData() public pure returns (uint256) {
        return 42;
    }
}

contract Caller {
    function callGetData(address callee) public view returns (uint256 data) {
        // call by staticcall
        // 对函数签名和参数进行编码
        bytes memory methodop = abi.encodeWithSignature("getData()");
        (bool flg, bytes memory result) = callee.staticcall(methodop);

        if(!flg){
            revert("staticcall function failed");
        }
        data = bytesToUint(result);

        return data;
    }

    function bytesToUint(bytes memory b) public pure returns (uint256) {
        uint256 number;
        for (uint256 i = 0; i < b.length; i++) {
            number = number + uint8(b[i]) * (2**(8 * (b.length - (i + 1))));
        }
        return number;
    }
}
相关推荐
凡哥btczf6661 小时前
M61S 214T矿机详细参数解析与性能评估
区块链
MicroTech202511 小时前
微算法科技(NASDAQ: MLGO)采用分片技术(Sharding)与异步共识机制,实现节点负载均衡,提升交易处理效率
科技·区块链·分片技术
openHiTLS密码开源社区13 小时前
X448 算法签名验签流程深度解析及代码示例
物联网·区块链·签名·椭圆曲线·x448·密钥生成
微三云-轩2 天前
区块链系统:解决549 亿元积分商城是否违法的问题
大数据·小程序·重构·区块链·生活
晓宜2 天前
区块链—NFT介绍及发行
区块链
劲驰2 天前
基于智能合约实现非托管支付
区块链·智能合约
孙叫兽2 天前
区块链论坛社区
区块链
LHminer 凡3 天前
神马 M21 31T 矿机解析:性能、规格与市场应用
区块链
空中湖3 天前
去中心化投票系统开发教程
去中心化·区块链