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 天前
可信数据空间的分布式数字凭证和分布式数字身份
区块链·可信数据空间·分布式数字身份·分布式数字凭证
Wilson Chen2 天前
区块链与以太坊基础:环境搭建与智能合约部署
区块链·智能合约
许强0xq2 天前
Q6: 如何计算以太坊交易的美元成本?
面试·web3·区块链·智能合约·dapp
mit6.8242 天前
[AI tradingOS] AI决策引擎 | decision/engine.go | 交易哲学prompts
人工智能·区块链
Chef_Chen2 天前
数据科学每日总结--Day20--区块链
区块链
mit6.8242 天前
[AI tradingOS] 市场数据系统 | 多交易所交易接口 | 适配器模式
人工智能·区块链
Less^_^2 天前
DeFi 协议分析:Hyperliquid 一个去中心化的永续合约交易平台
web3·去中心化·区块链·defi
虫洞没有虫2 天前
Go语言学习笔记(一)
笔记·go·区块链
taxunjishu2 天前
Modbus RTU 转 Modbus TCP:物联网网关实现中药产线巴赫曼与三菱PLC互联
人工智能·物联网·tcp/ip·区块链·工业自动化
Web3VentureView3 天前
Synbo Protocol 受邀出席ETHShanghai 2025,以共识机制重构链上融资生态
金融·web3·去中心化·区块链