ERC20 token with supply less than 1

I am using OpenZeppelin to make an ERC20 token. Everything seems to work fine with exception of the totalSupply.

I made the following contract:

contract SampleToken is ERC20 {
    constructor(string memory _name, string memory _symbol, uint256 _initialSupply) 
    ERC20(_name, _symbol) public {
        require(_initialSupply > 0, "INITIAL_SUPPLY has to be greater than 0");
        _mint(msg.sender, _initialSupply);
    }
}

And I deploy it as:

module.exports = function(deployer) {
  deployer.deploy(SampleToken, "TestToken", "ST", 100000000);
};

The name “TestToken” and the symbol “ST” are fine at contract creation. The total supply however isn’t 100.000.000, but when I look at the deployed contract the total supply is 0.0000000001 which is not what I expected.

What I think happens is that not 100.000.000 tokens are added but 100.000.000 decimals. This explains the final total supply I get. However, when I look at the _mint function, I don’t see decimals being added. From the code of ERC20.sol I expect the totalsupply to be 100.000.000.

Anyone can explain what is happening here and how this can be fixed ?

1 Like

Hi @Diego24,

Welcome to the community :wave:

The token is created with default decimals of 18 (https://docs.openzeppelin.com/contracts/3.x/api/token/erc20#ERC20-constructor-string-string-).

Which means that for a supply of 1 token you would need to set the supply to 1,000,000,000,000,000,000 or 1 * 10^18.

As you are setting the token supply to less than that, your total supply is less than 1 token.

For more details see the documentation on decimals:

The following SimpleToken example mints 1000 tokens, with the total supply being 1000 * 10^18

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

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.2.0/contracts/token/ERC20/ERC20.sol";

/**
 * @title SimpleToken
 * @dev Very simple ERC20 Token example, where all tokens are pre-assigned to the creator.
 * Note they can later distribute these tokens as they wish using `transfer` and other
 * `ERC20` functions.
 */
contract SimpleToken is ERC20 {

    /**
     * @dev Constructor that gives msg.sender all of existing tokens.
     */
    constructor() public ERC20("SimpleToken", "SIM") {
        _mint(msg.sender, 1000 * (10 ** uint256(decimals())));
    }
}
1 Like

Thanks a lot. It works now.

1 Like

Hi @Diego24,

Glad that resolved your issue. Feel free to ask all the questions that you need.