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;
    }
}
相关推荐
央链知播1 天前
重磅新书 | 《链改2.0:从数字资产到RWA》
金融·区块链·业界资讯
Webb Yu1 天前
加密货币学习路径
学习·区块链
openHiTLS密码开源社区1 天前
椭圆曲线密码学的效率核心:单标量与多标量乘法详解
区块链·零知识证明·rsa·隐私保护·ecc·多标量·单标量
本郡主是喵1 天前
基于区块链的商品溯源平台的设计与实现(源码+文档)
区块链
数据与人工智能律师1 天前
数据淘金时代的法治罗盘:合法收集、使用与变现数据的边界与智慧
大数据·网络·人工智能·云计算·区块链
大桔骑士v2 天前
【区块链学习笔记】17:以太坊中的GHOST协议
区块链·以太坊·比特币·ghost
wangchenggong19882 天前
solidity中的函数总结
区块链·1024程序员节
狙击主力投资工具2 天前
期货看盘和下单简要说明
区块链
0132 天前
013的加密世界权威指南_第二部分
区块链
Web3_Daisy3 天前
冷换仓的隐性代价:从安全策略到地址信誉体系的重新思考
大数据·安全·web3·区块链·比特币·1024程序员节