1

I can send 1 parameter to Contract A (otherContract) and it works well. But when I send 2 parameters, it fails. What do I do wrong?

Contract A:

function sumTwoParams (uint256 _num1, uint256 _num2) pure public returns (uint256) {
        return _num1 + _num2;
    } 

Contract B:

function sumTwoParams (uint256 _num1, uint256 _num2) public {     
        bytes memory payload = abi.encodeWithSignature("sumTwoParams(uint256, uint256)", _num1, _num2);
        (bool success, bytes memory returnData) = otherContract.call(payload);
        require(success, "Failed!");
        
        (sumResult) = abi.decode(returnData, (uint256));
    } 

sumResult is a member of Contract B, tx reverts at require(success, "Failed!"); in Contract B.

2 Answers 2

1

You should use "sumTwoParams(uint256,uint256)" (no whitespaces after ,) when encoding the signature.

1
  • Oh my lord! Thanks a lot! That solved my headache! :D Sep 14, 2022 at 14:29
1

The problem is that you are adding an space to the signature parameters, hence, it produces a different signature.

Take a look at this code:

function encodeWithSignatureWithSpaceInParams() public pure returns(bytes memory) {
        // This produces: 0x60fb6acb00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002
        return abi.encodeWithSignature("sumTwoParams(uint256, uint256)", 1, 2);
    }

    function encodeWithSignatureWithoutSpaceInParams() public pure returns(bytes memory) {
        // This produces: 0xff19a36e00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002
        return abi.encodeWithSignature("sumTwoParams(uint256,uint256)", 1, 2);
    }

Remove the space. It should be like sumTwoParams(uint256,uint256)

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.