Search code examples
blockchainsoliditycryptocurrency

Issue with returning uints in solidity


So I've started learning solidity and I want to make a function that return the total amount of coins created.

Here are the important parts of the contract

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

When I compile it gives me this error.

ParserError: Expected primary expression.
--> subcurrency.sol:47:9:
|
47 | returns supply;
| ^^^^^^^

What is the issue?

Also if I use return it says

TypeError: Different number of arguments in return statement than in returns declaration.
--> subcurrency.sol:47:9:
|
47 | return supply;
| ^^^^^^^^^^^^^

Solution

  • It is better to use standard ERC20 or whatever kind of token you want to create. here is the standard ERC20 contract. To use the standard ERC20 contract, you have to import it and your token contract should inherit it. Like this:

    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
    }
    

    Standard ERC20 contract has a function to return total supply called totalSupply(). So it makes it easier to write token contracts.

    Also you have syntax error in your code!

    To return a value you have to follow this syntax:

    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
        }
    

    in retuns you specify what kind of variable you want to return then in return you return the values in order you specified in returns