// SPDX-LICENSE: UNLICENSED
pragma solidity ^0.8.0;
contract DeployedContract {
uint public result = 0;
function add(uint256 input) public {
result = result + input;
}
}
contract CallerContract {
DeployedContract deployed_contract;
constructor(DeployedContract deployedContract_) {
deployed_contract = deployedContract_;
}
// see examples below of different types
// of low level call
}
1. Calldata by creating the bytes4 selector manually with keccak256 using abi.encodePacked
function callWithFunctionSignatureFromHash(uint256 input) public {
bytes memory calldataPayload = abi.encodePacked(
bytes4(keccak256("add(uint256)")),
input
);
(bool success, ) = address(deployed_contract).call(calldataPayload);
}
2. Calldata using abi.encodeWithSignature
function callWithEncodeWithSignature(uint256 input) public {
bytes memory calldataPayload = abi.encodeWithSignature("add(uint256)", input);
(bool success, ) = address(deployed_contract).call(calldataPayload);
}
3. Calldata using abi.encodeWithSelector
+bytes4 value
function callWithEncodeWithSelectorAsLiternal(uint256 input) public {
bytes memory calldataPayload = abi.encodeWithSelector(0x1003e2d2, input);
(bool success, ) = address(deployed_contract).call(calldataPayload);
}
4. Calldata using abi.encodeWithSelector
+functionName.selector
function callWithEncodeWithSelectorAsReference(uint256 input) public {
bytes memory calldataPayload = abi.encodeWithSelector(deployed_contract.add.selector, input);
(bool success, ) = address(deployed_contract).call(calldataPayload);
}
5. Calldata using abi.encodeCall
function callWithABIEncodecCall(uint256 input) public {
function (uint256) external functionToCall = deployed_contract.add;
bytes memory calldataPayload = abi.encodeCall(functionToCall, input);
(bool success, ) = address(deployed_contract).call(calldataPayload);
}