0

pragma solidity ^0.4.24;

I'm trying to call another contract's functions to return values.

I have the below which successfully returns a true/false

_childAddress.call(bytes4(keccak256("isValidChild(uint256, uint256)")), _childModule6, 6) == true)

But when I try to return an actual value like so:

uint256 tokenId = _childAddress.call(bytes4(keccak256("totalSupply()")));

I get the following error:

Type bool is not implicitly convertible to expected type uint256. tokenId

I can't find the syntax anywhere for returning a value.

Any help appreciated.

2
  • You should share the relevant code of each one of these contracts, since no one here can guess what it is! Jun 5, 2019 at 13:48
  • add more code please, and use truffle, cause remix does not work always as expected
    – Majd TL
    Jun 5, 2019 at 14:45

2 Answers 2

1

With solc v0.5 the signature of call changed it returns a tuple (bool, bytes). The first parameter indicates if the call successful and the second parameter is the result of the function.

    bool success;
    bytes memory result;
    uint totalSupply;

Make the call using abi.encodeWithSignature to encode parameters and function signature

    (success, result) = childContract.call(abi.encodeWithSignature("totalSupply()"));
    if (success) {

If the call was successful decode the result of the function

        (totalSupply) = abi.decode(result, (uint));
        return totalSupply;
    }

With v0.4 either you create an interface for your child contract or use assemply to obtain the value returned by the call.

0

The other answer is correct, and also widely used. however if you do a address.call the compiler will not allow to mark the function as view even if you're just reading the value. That's because address.call could be used to write the state as well.

if you want a view function with a low level call to totalSupply() do this

function readTotalSupply() external view returns(uint) {
    (, bytes memory data) = childContract.staticcall(abi.encodeWithSignature("totalSupply()"));
    return abi.decode(data, (uint));
}

Your Answer

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

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