With a scenario that goes as follows: I want a Contract A that allows me to call functions from other contracts, different each time, using the storage and code of the target contract. Essentially, I want to use the target contract but without interacting directly with it, instead having Contract A interact with it for me. Is that something that delegate call would make possible, or would that always use Contract A storage? If delegatecall is not the appropriate function, what could be used instead? Thanks.
1 Answer
- If you want contract A to call a function of contract B and use the storage of contract B, that's simply calling a function on contract B. There is nothing fancy about it, as this is the basis of contract interactions. Under the hood, the Solidity compiler transforms your function call into a
CALL
. - Now, if what you want to do is call contract B's function and use the storage of contract B but prevent any potential change to B's storage, I believe you need to use
STATICCALL
. Unfortunately, Solidity does not provide a way to easily write that.
Which one is it that you want to do?
Edit: You want case 2. Here is a quick function that could be doing what you want.
function doStatic(address target, bytes memory dataIn, uint256 outSize) public view returns(bytes memory dataOut) {
uint256 gasToSend = gasleft() - 50 * outSize;
uint256 dataLength = dataIn.length;
bool success;
assembly {
success := staticcall(gasToSend, target, dataIn, dataLength, dataOut, outSize)
}
require(success);
}
-
It is the second scenario I am interested in, thanks for figuring that out since I did not phrase it very well Commented Oct 5, 2021 at 11:44
-
1