2

The following is my code in solidity

pragma solidity ^0.4.2;

contract DappToken {
  //Constructor
  // set no of Tokens
  // Read the total number of Tokens
  uint256 public totalSupply;

  function DappToken () public {
    totalSupply = 10000000;
  }
}

Now i am going to interact with Truffle 1) I have done Truffle migrate

2) In Truffle console i used DappToken.deployed().then(function(i) {token = i ;})

3)deployed() returns a promise and then takes a call back (correct me if I am wrong )

4) Next i used

 token.totalSupply 
{ [Function]
  call: [Function],
  sendTransaction: [Function],
  request: [Function: bound ],
  estimateGas: [Function] }

// It is showing the following output

totalSupply is the name of the variable which i have given but it is showing Function in square brackets can any one explain how truffle is compiling inside and why it is showing me as Function I tried token.DappToken.totalSupply it is showing me error I thought it will go inside the contract and call the variable

1

Solidity do not expose variables directly, but it creates a public getter function.

Your example will behave like this.

contract DappToken {
  uint256 public _totalSupply;

  function DappToken () public {
    _totalSupply = 10000000;
  }
  function totalSupply() public view returns (uint256) {
    return _totalSupply;
  }
}

To get the total supply you have to make a call like regular function.

token.totalSupply().then((result) => { console.log(result); } )
0

Here is documentation. I think you need to call token.totalSupply inside then() similar to the example below:

var account_one = "0x1234..."; // an address

var meta;
MetaCoin.deployed().then(function(instance) {
  meta = instance;
  return meta.getBalance.call(account_one, {from: account_one});
}).then(function(balance) {
  // If this callback is called, the call was successfully executed.
  // Note that this returns immediately without any waiting.
  // Let's print the return value.
  console.log(balance.toNumber());
}).catch(function(e) {
  // There was an error! Handle it.
})

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.