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?

ZhouW
  • 1,348
  • 2
  • 18
  • 35
  • 1
    per https://ethereum.stackexchange.com/questions/9733/calling-function-from-deployed-contract call only returns a bool; how should this be done (or can it be done) to return a value of a uint256 type from another contract? – ZhouW Dec 12 '17 at 08:16

2 Answers2

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();
}
Ismael
  • 30,570
  • 21
  • 53
  • 96
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))
Mundi
  • 152
  • 4