0

I have an ERC20 token contract (say at address 0x0a6ebb3690b7983e470D3aBFB86636cf64925B98) with the totalSupply() function:

function totalSupply() constant returns (uint256) {
           return _totalSupply;
}

I want to call this function from another contract and assign the value of _totalSupply that is returned to a totalSupplyTest variable, eg:

uint256 public totalSupplyTest

totalSupplyTest = 0x0a6ebb3690b7983e470D3aBFB86636cf64925B98.call(bytes4(keccak256("totalSupply(uint256)")),"");

This results in the error:

Type bool is not implicitly convertible to expected type uint256

There must be something wrong in the syntax I am using - in this case how should the totalSupply() function in the external contract be called?

1

From solidity you can't obtain the returned value of a call (or delegatecall) they are low level interfaces that only return a boolean indicating if the call was successful or not.

You have to define an interface a make the call through it

interface MyToken {
    function totalSupply() constant returns (uint256);
}

Now you can cast the address to the interface and make the call normaly

function Test() public {
    uint256 public totalSupplyTest;
    address myAddress = 0x0a6ebb3690b7983e470D3aBFB86636cf64925B98;

    totalSupplyTest = MyToken(myAddress).totalSupply();
}
0

call returns a bool indicating if the call was correctly formed. You have to query the result from the callback.

So, in e.g. web3:

contract.totalSupply.call().then(res => console.log(res))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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