Search code examples
tokenethereumsolidityerc20

Issues setting a maximum amount of tokens in ERC20 contract


I've been trying to create a very simple ERC20 token with truffle in the rinkeby network. I placed the following code into my .sol file but the max supply doesnt seem to match.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract Artytoken is ERC20 {

    address public admin;
    uint private _totalSupply;

    constructor() ERC20('ArtyToken', 'AATK') {
        admin = msg.sender;
        _totalSupply = 1000000;
        _mint(admin, _totalSupply);
    }
}

In my Metamask address shows that i own "0.000000000001" and I've seen that in Etherscan it shows that the max total supply its "0.000000000001". What im doing wrong? Thank you in advance! :)


Solution

  • The EVM does not support decimal numbers (to prevent rounding errors related to the network nodes running on different architectures), so all numbers are integers.

    The ERC-20 token standard defines the decimals() function that your contract implements, effectively shifting all numbers by the declared number of zeros to simulate decimals.

    So if you wanted to mint 1 token with 18 decimals, you'd need to pass the value 1000000000000000000 (18 zeros). Or as in your case, 1 million tokens (6 zeros) with 18 decimals is represented as 1000000000000000000000000 (24 zeros). Same goes the other way around, 0.5 of a token with 18 decimals is 500000000000000000 (17 zeros).

    You can also use underscores (they effectively do nothing but visually separate the value) and scientific notation to mitigate human error while working with such large amount of zeros:

    // 6 zeros and 18 zeros => 24 zeros
    _totalSupply = 1_000_000 * 1e18;