【问题标题】:Issue with returning uints in solidity在solidity中返回uint的问题
【发布时间】:2021-08-31 19:21:27
【问题描述】:

所以我开始学习solidity,我想创建一个函数来返回创建的硬币总量。

这里是合同的重要部分

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;
| ^^^^^^^^^^^^^

【问题讨论】:

  • return 而不是 returns

标签: blockchain solidity cryptocurrency


【解决方案1】:

最好使用标准 ERC20 或您想要创建的任何类型的代币。 here 是标准的 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 中指定的顺序返回值

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-12
    • 1970-01-01
    相关资源
    最近更新 更多