0

I have a proxy contract that delegate calls to a target contract. The proxy is similar to DSProxy.

In it, there is an execute function:

function execute(address target, bytes memory data) external payable returns (bytes memory response) {
    // ...
    (bool success, bytes memory returndata) = target.delegatecall(data);
    // ...
}

Whose job is to delegate call to the provided target and return the response, which is of type bytes memory.

Now, say that I am calling a target contract that returns a struct:

contract TargetEcho {
    struct TargetStruct {
        uint256 foo;
        uint256 bar;
    }

    function echoStruct(TargetStruct calldata input) external pure returns (TargetStruct calldata) {
        return input;
    }
}

The Solidity compiler must do something to the TargetStruct calldata return type. It must decode it somehow. How is it done?

1 Answer 1

0

Let's figure this out empirically with ethers.js:

const input = { foo: BigNumber.from("3"), bar: BigNumber.from("1") };
const data: string = targetEcho.interface.encodeFunctionData("echoStruct", [input]);
const response = await proxy.callStatic.execute(target, data);
console.log({ input, response });

That produces the following output:

{
  input: {
    foo: BigNumber { _hex: '0x03', _isBigNumber: true },
    bar: BigNumber { _hex: '0x01', _isBigNumber: true },
  },
  response: '0x00000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000001'
}

Thus the answer is that Solidity unpacks the struct fields and concatenates their values, disregarding the keys. This makes it compatible with the bytes memory type of the response variable defined in the proxy contract.

Luckily ethers.js is able to decode the response string:

const decodedResponse = targetEcho.interface.decodeFunctionResult("echoStruct", response);

Output:

{
  decodedResponse: [
    [
      [BigNumber],
      [BigNumber],
      foo: [BigNumber],
      bar: [BigNumber],
    ]
  ]
}

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.