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. 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.
  2. 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)
  • It is the second scenario I am interested in, thanks for figuring that out since I did not phrase it very well Oct 5 '21 at 11:44
  • 1
    I added an untested code example. Oct 5 '21 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.