0

Given a call to an external contract,

(bool success, bytes memory returnData) = addr.call(abiEncoding);

how do I read a single value of a given type from the return data, without using assembly?

The assembly version would be, e.g. for bool return type,

if (success) {
    bool boolVal;
    if (returnData.length >= 1) {
        assembly {
            boolVal := mload(add(returnData, 32))
        }
    } else {
        boolVal = false;
    }
}

This sort of pattern seems to be needed for any return type other than string, because (as far as I can tell) you can't accomplish this by merely casting, e.g. if you expect a bytes4 as the return value, you can't just go bytes4(returnData).

However assembly sections are always flagged in security audits as potential issues, and additionally, assembly potentially makes your code unportable in future to other blockchains or other VMs, etc. -- so surely there is another way to do this?

1 Answer 1

1

Returndata will be abi encoded bytes. You must decode these first providing the correct types.

Example:

(uint256 a, address b) = abi.decode(returnData, (uint256, address));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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