Creating an ERC-20 token with OpenZeppelin v3.x?

Hi everyone. So, I have developed both an ERC20 and ERC721 token using OpenZeppelin 2.x but now, I am onto something where I have to build an ERC20 token using OpenZeppelin 3.x. Can someone point me in the right direction? Also, I wanna understand OpenZeppelin 3.x more as I am on a project which is at a much larger scale and I will be developing and maintaining a couple of tokens in the next 7-8 months.

Edit:- I can create a basic ERC-20 token with it but I struggle with things like using functionalities from ‘access’ to make it ownable, making it mintable, and things like that.

1 Like

Hi @PradhumnaPancholi,

I suggest looking at AccessControl rather than Ownable so that you can define roles for specific actions (such as minting).
See the documentation for details: https://docs.openzeppelin.com/contracts/3.x/access-control#role-based-access-control

ERC20PresetMinterPauser is an example of using AccessControl for minting and pausing:

As well as access control, you could look at using hooks (depending on what functionality you need): https://docs.openzeppelin.com/contracts/3.x/extending-contracts#using-hooks

Whenever someone is creating a token, I suggest they look at: Points to consider when creating a fungible token (ERC20, ERC777)

Feel free to ask all the questions that you need.

I spent a quite a good amount of time in Access Control and I think I get most of it now but for this project, I think Ownable is better way bcoz we are allowing very few things that can be done by user.To follow up on that, if I out “onlyOwner” on my constrcutor, does it apply on all methods? and second question is more basic, if I wanna sell a token as single unit i.e no “1.5455”, it’s just 1000 tokens sold as single units, does my decimal value needs to be “0”?

1 Like

Hi @PradhumnaPancholi,

My suggestion is to use Access Control, even if the only privileged function is mint. Where possible it is best to avoid having an all encompassing privileged function.

You don’t need onlyOwner on your constructor, you only need to add the modifier on functions that you only want the owner to call. (see the example in the documentation: https://docs.openzeppelin.com/contracts/3.x/access-control#ownership-and-ownable)

If you want no decimals, then set decimals to zero. You will need to call _setupDecimals if you aren’t using the default of zero.

1 Like