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;
    }
}
相关推荐
天涯学馆13 小时前
Solidity中实现安全的代币转账
智能合约·solidity·以太坊
yiyesushu21 小时前
solidity+chainlink 项目实例
solidity
zhang_xiaoyu582 天前
第十届99全球链商节重点项目“全球纸基生态战略联盟”正式签约
区块链
文火冰糖的硅基工坊2 天前
《投资-54》数字资产的形式有哪些?
人工智能·区块链
木西2 天前
React Native DApp 开发全栈实战·从 0 到 1 系列(兑换-合约部分)
web3·智能合约·solidity
凡哥btczf6663 天前
Antminer S19 Pro 92T矿机详细参数解析与挖矿能力分析
区块链
qq_508823403 天前
金融数据---股票筹码数据
金融·区块链
致***锌4 天前
50期权日内交易技巧
区块链
CodingBrother4 天前
ABI解析智能合约
区块链·智能合约
.刻舟求剑.4 天前
solidity得高级语法3
区块链·solidity·语法笔记