Delegatecall
TestDelegateCall.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract TestDelegateCall {
// Storage layout must be the same as contract A
uint public num;
address public sender;
uint public value;
function setVars(uint _num) external payable {
num = _num;
sender = msg.sender;
value = msg.value;
}
function setNum(uint _num) external {
num = _num;
}
}
DelegateCall.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract DelegateCall {
uint public num;
address public sender;
uint public value;
function setVars(address _test, uint _num) external payable {
// This contract's storage is updated, TestDelegateCall's storage is not modified.
(bool success, bytes memory data) = _test.delegatecall(
abi.encodeWithSignature("setVars(uint256)", _num)
);
require(success, "tx failed");
}
function setNum(address _test, uint _num) external {
(bool success, ) = _test.delegatecall(
abi.encodeWithSignature("setNum(uint256)", _num)
);
require(success, "tx failed");
}
}
Last updated
Was this helpful?