If Function in contract A delegatecall
function in contract B which calls function in contract C. What will be the msg.sender
for function in contract C?
1 Answer
The msg.sender
according to the test scenario below is Contract A
The Change to state happens in Contract A and Contract C
For me it make sense because contract B has nothing to do with the transaction except running the logic. However, you need to verify the result by your own and use it on your own risk (I could be wrong or missed some edge cases!)
pragma solidity 0.8.12;
contract StorageA {
uint public val = 1;
event Test(string class, address sender);
constructor() { }
function setValue(uint v, address storageB, address storageC) external {
emit Test("Contract Storage A", msg.sender);
(bool success, bytes memory result) = storageB.delegatecall(abi.encodeWithSignature("setValue(uint256,address)", v, storageC));
require(success == true);
}
}
contract StorageB {
uint public val = 2;
event Test(string class, address sender);
constructor() { }
function setValue(uint v, address storageC) external {
emit Test("Contract Storage B", msg.sender);
storageC.call(abi.encodeWithSignature("setValue(uint256)", v));
val = v + 9;
}
}
contract StorageC {
uint public val = 3;
event Test(string class, address sender);
constructor() { }
function setValue(uint v) external {
emit Test("Contract Storage C", msg.sender);
val = v + 7;
}
}
Deploying Contract A at 0xd9145CCE52D386f254917e481eB44e9943F39138
with val = 1
Deploying Contract B at 0xd8b934580fcE35a11B58C6D73aDeE468a2833fa8
with val = 2
Deploying Contract C at 0xf8e81D47203A594245E36C48e151709F0C19fBe8
with val = 3
Now calling the method in Contract A like this:
It will give you the following events
The val values is changed as the following