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;
    }
}
相关推荐
jrjrgood2 小时前
现货黄金和黄金期货的区别有哪些?如何投资?
大数据·人工智能·区块链
ellenwan20265 小时前
期货跨期价差程序化怎么做:天勤 SP 合约与腿比例核对
python·区块链
CTA终结者6 小时前
期货量化合约代码写错:天勤 symbol 格式与 silent 订阅坑
python·区块链
LadenKiller8 小时前
期货多品种轮动标的池:天勤 query_quotes 筛品种写法
python·区块链
lizhihai_999 小时前
股市学习心得-六月的股市怎么应对
大数据·人工智能·科技·学习·区块链
nanawinona9 小时前
只会用 K 线算期货信号下一步怎么接到交易
python·区块链
2601_9602058810 小时前
2026年6月,中国品牌咨询行业正经历一场深刻的范式转移
大数据·人工智能·区块链
CTA终结者1 天前
期货量化主力换月程序怎么移仓:天勤 underlying_symbol 与任务切换
python·区块链
CTA量化套保1 天前
期货量化临期合约还能不能做:程序化到期禁开与强平写法
python·区块链
下午写HelloWorld1 天前
【概念与应用】轻量级加密算法LEA、动态脱敏算法DDA、零知识证明ZKP和优化协同交互协议OCIP
算法·区块链·密码学·安全架构·零知识证明