ERC20 Token – Troubleshooting Zero Balance in Rinkeby Using Solidity

erc-20metamaskrinkebysoliditytokens

I'm writing a pre-minted ERC20 token contract derived from openzeppelin-solidity like:

pragma solidity >=0.4.24;

import "../node_modules/openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";
import "../node_modules/openzeppelin-solidity/contracts/token/ERC20/ERC20Detailed.sol";

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

I'm deploying it to the Rinkeby network, using truffle. My account has enough balance.

After running truffle deploy --reset --network rinkeby, I get:

   Replacing 'SampleToken'
   -----------------------
   > transaction hash:    0x126c13b5fc64dbd90db60dfff754a7a6825add3138e69e1ab27ac90a8fe166bf
   > Blocks: 0            Seconds: 9
   > contract address:    0x557858779C6C5240B05005bCbd60dD06fBcCF479
   > block number:        4377271
   > block timestamp:     1557788535
   > account:             0xceB4c079Dd21494E0bc99DA732EAdf220b727389
   > balance:             7.326879674
   > gas used:            1111289
   > gas price:           20 gwei
   > value sent:          0 ETH
   > total cost:          0.02222578 ETH

   Pausing for 2 confirmations...
   ------------------------------
   > confirmation number: 1 (block: 4377272)
   > confirmation number: 2 (block: 4377273)

   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:          0.02222578 ETH

If I go to EtherScan, I can see the contract was deployed, the event of the first coinbase transaction was generated:

Contract events

However, after adding the token to my Metamask Wallet, it still shows my balance as zero:

Metamask


I've also tried to change the visibility of _balances and _totalSupply in openzeppelin-solidity/contracts/token/ERC20/ERC20.sol and assign them directly from my contract, but the behavior I saw was still the same.

Best Answer

I suspect this is just a rounding problem. MetaMask doesn't show enough decimal places. The account that created the token has a balance of 0.000000000000001.

The token has been created with 18 decimal places, so it takes a balance of 10^18 token units to equal one token. If you want to have 1000 tokens, you need to pass in 1000 * 10^18 (a 1 with 21 zeroes after it).