0

Trying to launch a simple ERC20 Token.

I'm finding OpenZeppelin's extensive library - where every Contract seems to import about 4 other Contracts - to be both wonderfully well organized - but also totally confusing.

Here's a BIG question: their basic ERC20.sol contract has no constructor() function.

1) So where/how do you for example set the totalSupply of Tokens you want your contract to have when you deploy it?

2) where/how do you declare the name or symbol you want your Token to have?

For Q#2: I see you can "tack on" the name, symbol and decimals properties to your contract by also importing the ERC20Detailed contract - which by the way does have a constructor() - into your Contract's declaration, but then what? This still doesn't let you access totalSupply or the other properties of the original basic ERC20 contract.

So how are you supposed to be doing this?

Here's what I've got so far:

pragma solidity ^0.5.2;


import "./ERC20Detailed.sol";


contract MyToken is ERC20, ERC20Detailed {       

    constructor(string memory _name, string memory _symbol, uint8 _decimals) 
        ERC20Detailed(_name, _symbol, _decimals)
        public
    {

    }


}

NOTE: when I try to set a value for totalSupply in the public { .... } section of the constructor, I get all sorts of errors...

Very confused!

1

Also included in the library is a couple of examples - you can find them in their github repo. One, called SimpleToken looks like this:

pragma solidity ^0.4.24;

import "../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 {

  string public constant name = "SimpleToken";
  string public constant symbol = "SIM";
  uint8 public constant decimals = 18;

  uint256 public constant INITIAL_SUPPLY = 10000 * (10 ** uint256(decimals));

  /**
   * @dev Constructor that gives msg.sender all of existing tokens.
   */
  constructor() public {
    _mint(msg.sender, INITIAL_SUPPLY);
  }

}

Notice how the INITIAL_SUPPLY is set. Notice too that there is a constructor. This means if you wish to pass in the number of tokens you wish to mint in say, a truffle environment, you could rewrite that as something like:

  //uint256 public constant INITIAL_SUPPLY = 10000 * (10 ** uint256(decimals));
  // REMOVE THE ABOVE LINE

  constructor(uint256 _amount) public {
    _mint(msg.sender, _amount * 10 ** uint256(decimals));
  }

Then, once deployed, you'll notice that the totalSupply amount will reflect whatever you created as the initial_supply.

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.