// 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
}
}