简体   繁体   中英

Why does the truffle console not print correct variable in solidity smart contract?

I am a beginner in programming Ethereum smart contracts using solidity and truffle. I made a simple smart contract to deploy that sets a variable equal to 1000000, as shown below:

pragma solidity >=0.4.22 <0.8.0;
contract CelesteToken{
uint256 public totalSupply;
  function ClesteToken()public{
    totalSupply = 1000000;
}
}

In the truffle console I used the following commands:

CelesteToken.deployed().then(function(i){token =i;})
token.totalSupply().then(function(s){totalSupply =s;})
totalSupply.toNumber()

However instead of returning 1000000 as per the code, it returns 0. I am not sure why this is happening, can anyone help?

You can try replacing

function ClesteToken()public{
    totalSupply = 1000000;
}

to

constructor () public {
    totalSupply = 1000000;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM