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;
    }
}
相关推荐
Shota Kishi2 天前
Solana Stream SDK 的 UDP Shred 接收:data-only 与 recover 两种模式的取舍与实测
rust·区块链·solana
阿菜ACai2 天前
20261203-APE Chain 2.68 亿代币增发事件:深藏于链层面的回滚缺陷
区块链·智能合约·漏洞分析
木西4 天前
深度拆解与复刻 o1.exchange:基于 Solidity 0.8.28、OpenZeppelin v5 与 CIEM 架构的智能合约实战
web3·智能合约·solidity
OxYGC4 天前
[Web3] 钱包业务第十六篇: Solana 钱包链调研,钱包 SDK 开发,Staking 开发
web3·区块链·以太坊·比特币
大今野6 天前
人工智能与区块链核心考点全解析
人工智能·区块链·uml·专转本
莫叫石榴姐6 天前
现货黄金技术分析:3427破而复收之后,关键价位与下周情景推演
区块链·可信计算技术
OxYGC6 天前
[Web3] 钱包业务第十五篇: Bitcoin 链调研与钱包开发
web3·区块链·以太坊·比特币
OxYGC6 天前
[Web3] 钱包业务第十三篇: Ethereum 钱包链调研实战
web3·区块链·以太坊·比特币
大今野8 天前
2026 专转本计算机|区块链典型框架:以太坊与超级账本 Fabric 考点全解(附例题)
运维·区块链·fabric·专转本
OxYGC13 天前
[Web3] 智能合约 19: NFT 知识与 Web3 求职指南
区块链·智能合约·求职招聘·nft