ERC20 is a standard interface for tokens on the Ethereum blockchain. It defines a common set of rules that all Ethereum-based tokens must adhere to, making it easier to interact with different tokens. ERC means Ethereum Request for Comment, it's fungible (each ERC20 token is are rather unique than identical).
Prior to the introduction of ERC-20, the process of creating tokens on the Ethereum network lacked standardization. Developers could create their own tokens, but these tokens were not always compatible with each other. This lack of a unified standard made it challenging to use or exchange tokens across different platforms and applications.
Creating your own token on the Ethereum blockchain is quite simple if you know what to do. All you need to do is to create and deploy your ERC20 contract. You can do this using Open Zeppelin's wizard or, you can write your ERC20 contract that inherits its features from an ERC20 interface.
But a simpler and more safe approach would be to use Open Zeppelin because their contract is audited and is bug free and has no security risks. Anyways, I wrote a simple ERC20 contract which is at https://github.com/victuk/ERC20-Assignment/blob/main/MyERC20.sol which I'll explain its features.
_name (private string variable): This represents the name of the token you are creating.
_symbol (private string variable): This represents the symbol of the token you are creating. It's usually the short form of the token name.
_balances (mapping): This represents the mapping of the token owner to the balance he/she holds.
_allowed (2D mapping): This represents the mapping of the maping of the owner of the token to the spender of the token and to the spendable amount that the spender is allowed to spend on behalf of the token owner.
Transfer (Event): This event is used to keep record of the activities/logs that involves transfer of tokens. This event takes in a from address which is the address of the initiator, the to address which is the address of the recepient and the amount of token being transferred to the recepient which has to be less than or equa to the initiator's balance.
Approval (Event): The approval event is used to keep logs of approvals that owners have approved for spenders to use. This event takes in an owner who is the owner of the token he/she wants to give permission to be spent, a spender who is approved to spend the owner's token and an amount which is the amount the owner has allocated that a spender can use from his wallet.
totalSupply (Function): This is the total amount of tokens that will ever exist for the ERC20 contract. If the smart contract is not mintable, then the total supply will always be a fixed amount.
balanceOf (Function): This function is used to retrieve the amount of token the address parameter of the function holds.
allowance (Funcion): This is the total amount of money a token owner has allowed a spender to use or spend on his behalf.
transfer (Function): This is used by the initiator of the transaction to transfer an amount from his/her token balance to another user's token balance.
approve (Function): This is used to approve a user to spend an amount of money on behalf of the owner. For example, I can approve you to spend 20000 of my token, that amount will be tracked on the token allowance mapping on the contract. The _allowance mapping is a mapping from the owner to the spender to the amount the spender is allowed to use.
transferFrom (Function): The transfer from function is used by the spender to move his assigned allowance from the owner's wallet to a recepient's wallet. Example, if I allow you to spend 20000 of my token, then calling transfer from with an amount less than or equal to 20000 to a recepient's address will move the amount from the allowance to the recepient's address specified.
With this article I hope it's clearer how each and every feature of an ERC20 contract is used and what they represent, thank you for reading, have a wonderful day/night.
Top comments (0)