1

I'm creating a test contract for calling external ones & ran into an weird issue with call() where it just keeps returning false. Being new to solidity, I'm not sure what is failing it. Here is the following code.

contract Main {
    event Response(bool successful, bytes data);

    function testCallSetInt(uint v) external {
        address helloContract = 0x809d550fca64d94Bd9F66E60752A544199cfAC3D;
        (bool success, bytes memory data) = helloContract.call(
            abi.encodeWithSignature("set(uint)", v)
        );
        console.log(success);
        emit Response(success, data);
    }
} 

The deployed contract I'm trying to call is definied as such

contract HelloWorld {
  uint storedValue;

  function set(uint x) public {
    storedValue = x;
  }

  function get() public view returns (uint) {
    return storedValue;
  }
}

When I log success I only get false & no other indicators as to why this is failing. Any suggestions?

2 Answers 2

3

It's because you need to use full name for types. Just use uint256 instead of uint like this:

(bool success, bytes memory data) = helloContract.call(
      abi.encodeWithSignature("set(uint256)", v)
  );

and this should work.

Hope this helps!

1

Adding to what OliverD said, this works in Remix:

pragma solidity 0.8.17;

contract Main {
    event Response(bool successful, bytes data);

    function testCallSetInt(uint v) external {
        address helloContract = 0x809d550fca64d94Bd9F66E60752A544199cfAC3D;
        (bool success, bytes memory data) = helloContract.call(
            abi.encodeWithSignature("set(uint256)", v)
        );
        require(success, "call failed");
        emit Response(success, data);
    }
}

See: https://stackoverflow.com/questions/54863261/solidity-abi-encodewithsignature-did-not-work-with-diffienet-parameters

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.