ERC-20 defines a common list of rules that all fungible Ethereum tokens should adhere to. Consequently, this token standard empowers developers of all types to accurately predict how new tokens will function within the larger Ethereum system. This simplifies and eases developers’ tasks, because they can proceed with their work, knowing that each and every new project won’t need to be redone every time a new token is released, as long as the token follows the rules. Here is, presented as an interface, the functions an ERC-20 must implement. If you’re not sure about what is an interface: check our article about OOP programming in Solidity.
Here is a line-by-line explainer of what every function is for. After this we’ll present a simple implementation of the ERC-20 token. Getters
Returns the amount of tokens in existence. This function is a getter and does not modify the state of the contract. Keep in mind that there are no floats in Solidity. Therefore most tokens adopt 18 decimals and will return the total supply and other results as followed 1000000000000000000 for 1 token. Not every token has 18 decimals and this is something you really need to watch for when dealing with tokens.
Returns the amount of tokens owned by an address ( 0). This function is a getter and does not modify the state of the contract.
The ERC-20 standard allows an address to give an allowance to another address to be able to retrieve tokens from it. This getter returns the remaining number of tokens that the 1 will be allowed to spend on behalf of 2. This function is a getter and does not modify the state of the contract and should return 0 by default. Functions
Moves the 3 of tokens from the function caller address ( 4) to the recipient address. This function emits the 5 event defined later. It returns true if the transfer was possible.
Set the amount of 6 the 1 is allowed to transfer from the function caller ( 4) balance. This function emits the Approval event. The function returns whether the allowance was successfully set.
Moves the 3 of tokens from 0 to 1 using the allowance mechanism. amount is then deducted from the caller’s allowance. This function emits the 5 event. Events
This event is emitted when the amount of tokens (value) is sent from the 3 address to the 4 address. In the case of minting new tokens, the transfer is usually 3 the 0x00..0000 address while in the case of burning tokens the transfer is 4 0x00..0000.
This event is emitted when the amount of tokens ( 7) is approved by the 2 to be used by the 1. A basic implementation of ERC-20 tokensHere is the most simple code to base your ERC-20 token from:
Another excellent implementation of the ERC-20 token standard is the OpenZeppelin ERC-20 implementation. |