1

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.

1

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).

  • 1
    You can just put it in quotes. "1000000000000000000000" (if I counted right). But yes, I assume Truffle uses either BigNumber or BN. – user19510 May 14 at 22:17
  • You nailed it! Problem is that 1000 * 1e18 apparently won't fit into the uint256: "invalid number value (arg="_initialSupply", coderType="uint256", value=1e+21)" – Henrique Barcelos May 14 at 22:22
  • 1
    That's not right. A uint256 can store values up to 2^256, which is around 10^77. – user19510 May 14 at 22:24
  • Oh, I see... I must declare it as a string. Nice... Only problem is that this way I can't declare it using a shorter scientific notation as I tried above. – Henrique Barcelos May 14 at 22:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.