Solidity-057 UsingCallFunction

本文介绍了如何在Ethereum区块链上使用Solidity编写一个简单的智能合约EtherBox,展示了如何进行余额操作、调用其他合约函数以及使用不同类型的调用(如普通call、delegatecall和staticcall),并涉及gas限制的应用。
摘要由CSDN通过智能技术生成

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

// Contract EtherBox is a simple contract to demonstrate how to manipulate and access a balance.

contract EtherBox {

    uint balance; // Variable to store the balance

    // Function to increase the balance by 10 units.

    function SetBalance() public {

        balance = balance + 10;

    }

   

    // Function to return the current balance.

    // Note: This function is marked as `payable` unnecessarily since it doesn't need to receive ether.

    function GetBalance() public payable returns(uint) {

        return balance;

    }

}

// Contract UsingCall demonstrates various ways to interact with contracts using low-level calls.

contract UsingCall {

    // Payable constructor allows this contract to receive and store ether upon deployment.

    constructor() payable {

    }

    // Demonstrates a simple call to another contract to execute its function and return a value.

    function SimpleCall() public returns (uint) {

        EtherBox eb = new EtherBox(); // Create a new EtherBox instance

        address myaddr = address(eb); // Get the address of the new EtherBox instance

        // Encode the function signature to call SetBalance on EtherBox

        bytes memory payload = abi.encodeWithSignature("SetBalance()");

        (bool success, ) = myaddr.call(payload); // Perform the call

        require(success); // Ensure the call was successful

        return eb.GetBalance(); // Return the balance from EtherBox

    }

    // Demonstrates a call to another contract with a specific amount of gas.

    function SimpleCallwithGas() public returns (bool) {

        EtherBox eb = new EtherBox();

        address myaddr = address(eb);

        bytes memory payload = abi.encodeWithSignature("SetBalance()");

        // Call with specified gas limit

        (bool success, ) = myaddr.call{gas: 200000}(payload);

        return success; // Return the success status of the call

    }

    // Demonstrates a call to another contract with both gas and ether value specified.

    function SimpleCallwithGasAndValue() public returns (bool) {

        EtherBox eb = new EtherBox();

        address myaddr = address(eb);

        bytes memory payload = abi.encodeWithSignature("GetBalance()");

        // Call with specified gas limit and 1 ether value

        (bool success, ) = myaddr.call{gas: 200000, value: 1 ether}(payload);

        return success; // Return the success status of the call

    }

    // Demonstrates a delegate call, which executes code in the context of the caller's state.

    function SimpledelegateCallwithGas(address libAddress) public returns (bool) {

        bytes memory payload = abi.encodeWithSignature("sum(uint256,uint256)", 10, 10);

        // Perform delegate call with specified gas limit

        (bool success, ) = libAddress.delegatecall{gas: 2000000}(payload);

        return success; // Return the success status of the call

    }

    // Demonstrates a static call, which is a call that cannot alter the state.

function SimpleStaticCallwithGas(address contractAddress) public view returns (bool) {

    bytes memory payload = abi.encodeWithSignature("sum(uint256,uint256)", 10, 10);

    // Perform static call with specified gas limit

    (bool success, ) = contractAddress.staticcall{gas: 2000000}(payload);

    return success; // Return the success status of the call

}

}

  • 12
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值