所以我已经开始学习固体,我想做一个函数,返回创建的硬币总数。
以下是合同的重要部分
address public owner;
mapping(address => uint) public balances;
uint totalSupply;
function mint(address receiver, uint amount) public {
require(msg.sender == owner);
// balances[receiver] = balances[receiver] + amount;
totalSupply += amount;
balances[receiver] += amount;
}
function CheckTotalSupply(uint supply) public {
returns supply;
}
当我编译时,它给了我这个错误。
ParserError: Expected primary expression.
--> subcurrency.sol:47:9:
|
47 | returns supply;
| ^^^^^^^
有什么问题吗?
如果我使用return它会显示
TypeError: Different number of arguments in return statement than in returns declaration.
--> subcurrency.sol:47:9:
|
47 | return supply;
| ^^^^^^^^^^^^^
最好使用标准ERC20或您想要创建的任何类型的令牌。这是标准的ERC20合同。要使用标准的ERC20合约,您必须导入它,并且您的令牌合约应该继承它。这样的:
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol";
contract YOUR_TOKEN_CONTRACT is ERC20{
constructor() ERC20(YOU_TOKEN_NAME, YOUR_TOKEN_SYMBOL){
//code here
//for example _mint(msg.sender, 100000);
}
//code here
}
标准ERC20合约有一个返回总供给的函数,称为totalSupply()
。因此,它使编写代币合约变得更容易。
你的代码也有语法错误!
返回一个值必须遵循以下语法:
function CheckTotalSupply(uint supply) public retuns(uint){
return supply;// ^ this is returns with (s) at its end
//^ this is return whitout (s) at its end
}
在retuns
中指定要返回的变量类型然后在return
中按returns
中指定的顺序返回值