简体   繁体   English

为什么 truffle 控制台没有在 Solidity 智能合约中打印正确的变量?

[英]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.我是使用 Solidity 和 truffle 编写以太坊智能合约的初学者。 I made a simple smart contract to deploy that sets a variable equal to 1000000, as shown below:我做了一个简单的智能合约来部署,它设置了一个等于 1000000 的变量,如下所示:

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?但是,不是按照代码返回 1000000,而是返回 0。我不确定为什么会发生这种情况,有人可以帮忙吗?

You can try replacing你可以尝试更换

function ClesteToken()public{
    totalSupply = 1000000;
}

to

constructor () public {
    totalSupply = 1000000;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 测试我的 Solidity 智能合约时遇到问题(带松露) - Having a Problem Testing my Solidity Smart Contract (w/ Truffle) Solidity 智能合约迁移错误的 Truffle 和 Ganache 教程 - Truffle and Ganache tutorial for a Solidity smart contract migration error (ethereum/solidity/truffle) 从测试/客户问题调用智能合约方法 - (ethereum/solidity/truffle) calling smart contract method from test/client question 在 Truffle 上测试部署的智能合约 - Testing Deployed Smart Contract on Truffle Solidity:更改已发布的智能合约中变量的值 - Solidity: change a the value of a variable in a smart contract already published 使用 javascript 调用 Solidity 智能合约来铸造代币似乎不起作用 - Calling solidity smart contract with javascript to mint token does not seem to work 松露固体-从不同合约调用功能 - Truffle Solidity - Calling function from different contract 为什么将松露控制台打印功能输出给屏幕,以便进行回调? - Why does truffle console print function outputs to screen when they are meant for callback? 松露智能合约错误:参数数量无效 - Truffle Smart Contract Error: Invalid number of parameter 在 Solidity 中部署智能合约后调用智能合约的 function - Call function of smart contract after deploying smart contract in Solidity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM