Assignment 6
Write two contracts caller and calleeDelegate.
The contract calleeDelegate should implement a function sum() that receives two parameters _a and _b of type uint256. It should calculate the sum and store the result in a public contract variable result.
The contract caller should implement a function invokeRemoteSum() that passes the arguments _a and _b to the function sum() in the calleeDelegate contract via a delegate call.
The contract caller should declare a public state variable result of type uint256;
Try the contracts out and check the values of both variables result if the function sum() is called directly from the contract calleeDelegate or via the caller contract.
Solution
calleeDelegate
pragma solidity 0.8.13;
contract callee{
uint256 public result;
function sum(uint256 _a, uint256 _b) public{
result = _a + _b;
}
}
Caller
pragma solidity 0.8.13;
contract callerDelegate{
uint256 public result;
address public callee = 0xDA07165D4f7c84EEEfa7a4Ff439e039B7925d3dF;
function invokeRemoteSum() public{
(bool success, bytes memory data) = callee.delegatecall(
abi.encodeWithSignature("sum(uint256,uint256)", 10,14)
);
}
}