How to implement ERC20 tokens?

I was looking at ERC20 tokens and it’s not even become a week…
I want to make a game where I can give various roles to various people(Addresses) and make the tokens transfer between them… like:
one is a token factory owner… he deploys the tokenfactory.sol and then anyone can create tokens from there by calling a createToken() fxn…
Then the deployed contract(token.sol) is where all events happen…
The fxn caller then becomes the manager of the deployed token.sol contract…
Then there are general players who are distributed the tokens by the manager who go to another roleplayer (merchant) to buy various goods from the merchant by transferring their tokens to the merchant…

The token.sol contract inherits ERC20 contract from openzeppelin-contracts-ethereum-package, (I’m trying look for the ERCDetailed and ERC20PresetMinterPauser and implement them too) .
But since I have so many other roles and functions and state to include in my contract, and I don’t see where I have to use approve(), allowance() and transferFrom() functions in my token, I haven’t used them… and its compiling fine…

But I want to know if this is allowed(cause manadatory function need to be implemented in an ERC20 inherited token, no?) if I want to make my contract a token and that too an ERC20 token…

Is there any way I can break my token contract that inherits ERC20 standard and other game functionalities in my contract? Since some of them include transfer funtions…
I am heavily confused in here…

Since I didn’t have a nice idea about tokens, I was even confused as to which ERC standard I should implement for this particular case to begin with… Can you suggest me on that too?

1 Like

@abcoathup are u packed these days?

1 Like

Hi @asmeedhungana,

Sorry I haven’t gotten back to you sooner. It’s a busy week.

If I understand correctly you have three contracts:

  • A Token Factory
  • Tokens
  • And a merchant contract where the token can be used.

If you inherit from the OpenZeppelin Contracts implementation of ERC20 then you have a compatible ERC20 which implements all the required functions. If you use ERC20PresetMinterPauser then you would just need to add the account you want to be able to mint from. (the deployer of the contract, I assume the factory, would be the minter.)

For the Token Factory you could inherit from ProxyFactory

Given you are working on a game with multiple tokens you might want to look at using multi-token standard ERC1155 which is part of OpenZeppelin Contracts v3.1

1 Like

@abcoathup, it’s no problem, really! I can only wonder how busy your life might be…

Nope, it’s only two contracts…

Token Factory (which creates new Token contracts)

Does this mean I don’t have to define any functions related to ERC20 and overwrite them in my token contract other than giving the total supply as the basic requisite and all other gaming functions in the token.sol contract as per my like?

ohh… alright! Why would I need to inherit from ProxyFactory though? I’ll make sure to see it myself too, but I get it better when you lay it out in simpler terms… :smiley: so make sure to explain the gist when you get the time to do so… :grin:

1 Like

Hi @asmeedhungana,

An example ERC20 with fixed supply is shown in Constructing an ERC20 Token Contract.

For a fixed supply token you can have an ERC20 token just by specifying the name, symbol and minting the supply of tokens.

You could use ProxyFactory if you wanted to create Proxies (for upgradeable contracts) on-chain.

For a game I would consider using ERC1155.
See Constructing an ERC1155 Token Contract for an example.

1 Like