call, staticcall, delegatecall
Call
bytes memory data = abi.encodeWithSignature( "approve(address,uint256)", address(this), type(uint256).max ); (bool success, bytes memory data) = target.call{value: 111, gas: 5000}(data)
Example
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract TestCall {
string public message;
uint public x;
event Log(string message);
fallback() external payable {
emit Log("Fallback was called");
}
function foo(string memory _message, uint256 _x) public payable returns (bool, uint) {
message = _message;
x = _x;
return (true, 999);
}
}
contract Call {
bytes public data;
function callFoo(address _test) external {
(bool succes, bytes memory _data) = _test.call{value: 111, gas: 5000}(
abi.encodedWithSignature("foo(string,uint256)", "call Foo", 123));
require(success, "call failed");
data = _data;
}
}
Staticcall
Delegatecall
Last updated