2

If I call method x, which returns an address, in the Ethereum contract with address addr:

(bool success, bytes memory returnVal) = addr.call(abi.encodeWithSignature("x()"));
require(success, "Could not call x()");

I then need to decode the result in returnBytes to get an address. Is this the correct way to do it?

(address returnedAddr) = abi.decode(returnVal, (address));

1 Answer 1

2

No.

The call data must be composed of a function identifier (4 first bytes of keccak256 of your function signature) followed by the encoded parameters if any. addr.call(abi.encodePacked("x()")) is incorrect. The best way is to use the provided abi functions, such as encodeWithSignature, it's just more readable.

Like in the following example. I just changed call to staticcall to be able to keep the test function view. You can change it to call if that's a better fit for your use case.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract A {
    function getAddress() public pure returns (address) {
        return 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4;
    }
}
contract B {
    address addr;
    constructor(address _addr) {
        addr = _addr;
    }

    function test() public view returns (address returnValue) {
        (bool success, bytes memory returnBytes) = addr.staticcall(abi.encodeWithSignature("getAddress()"));
        require(success == true, "Call to getAddress() failed");
        returnValue = abi.decode(returnBytes, (address));
    }

}
1
  • Thanks, yes, encodePacked was a typo, I'm still learning the API, and didn't look at the code closely enough when typing out the example. However thank you for confirming the point I was asking about, which is whether the returned bytes should be decoded with abi.decode. That's what I needed to know. I'll update the question to use encodeWithSignature so that I don't lead anyone astray! Commented Dec 14, 2021 at 19:51

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.