Understand the ERC-20 token smart contract
One of the most significant smart contract standards on Ethereum is known as ERC-20, which has emerged as the technical standard used for all smart contracts on the Ethereum blockchain for fungible token implementations.
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(opens in a new tab).
1pragma solidity ^0.6.0;23interface IERC20 {45 function totalSupply() external view returns (uint256);6 function balanceOf(address account) external view returns (uint256);7 function allowance(address owner, address spender) external view returns (uint256);89 function transfer(address recipient, uint256 amount) external returns (bool);10 function approve(address spender, uint256 amount) external returns (bool);11 function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);121314 event Transfer(address indexed from, address indexed to, uint256 value);15 event Approval(address indexed owner, address indexed spender, uint256 value);16}Prikaži sveKopiraj
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
1function totalSupply() external view returns (uint256);Kopiraj
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.
1function balanceOf(address account) external view returns (uint256);Kopiraj
Returns the amount of tokens owned by an address (account
). This function is a getter and does not modify the state of the contract.
1function allowance(address owner, address spender) external view returns (uint256);Kopiraj
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 spender
will be allowed to spend on behalf of owner
. This function is a getter and does not modify the state of the contract and should return 0 by default.
Functions
1function transfer(address recipient, uint256 amount) external returns (bool);Kopiraj
Moves the amount
of tokens from the function caller address (msg.sender
) to the recipient address. This function emits the Transfer
event defined later. It returns true if the transfer was possible.
1function approve(address spender, uint256 amount) external returns (bool);Kopiraj
Set the amount of allowance
the spender
is allowed to transfer from the function caller (msg.sender
) balance. This function emits the Approval event. The function returns whether the allowance was successfully set.
1function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);Kopiraj
Moves the amount
of tokens from sender
to recipient
using the allowance mechanism. amount is then deducted from the caller’s allowance. This function emits the Transfer
event.
Events
1event Transfer(address indexed from, address indexed to, uint256 value);Kopiraj
This event is emitted when the amount of tokens (value) is sent from the from
address to the to
address.
In the case of minting new tokens, the transfer is usually from
the 0x00..0000 address while in the case of burning tokens the transfer is to
0x00..0000.
1event Approval(address indexed owner, address indexed spender, uint256 value);Kopiraj
This event is emitted when the amount of tokens (value
) is approved by the owner
to be used by the spender
.
A basic implementation of ERC-20 tokens
Here is the most simple code to base your ERC-20 token from:
1pragma solidity ^0.8.0;23interface IERC20 {45 function totalSupply() external view returns (uint256);6 function balanceOf(address account) external view returns (uint256);7 function allowance(address owner, address spender) external view returns (uint256);89 function transfer(address recipient, uint256 amount) external returns (bool);10 function approve(address spender, uint256 amount) external returns (bool);11 function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);121314 event Transfer(address indexed from, address indexed to, uint256 value);15 event Approval(address indexed owner, address indexed spender, uint256 value);16}171819contract ERC20Basic is IERC20 {2021 string public constant name = "ERC20Basic";22 string public constant symbol = "ERC";23 uint8 public constant decimals = 18;242526 mapping(address => uint256) balances;2728 mapping(address => mapping (address => uint256)) allowed;2930 uint256 totalSupply_ = 10 ether;313233 constructor() {34 balances[msg.sender] = totalSupply_;35 }3637 function totalSupply() public override view returns (uint256) {38 return totalSupply_;39 }4041 function balanceOf(address tokenOwner) public override view returns (uint256) {42 return balances[tokenOwner];43 }4445 function transfer(address receiver, uint256 numTokens) public override returns (bool) {46 require(numTokens <= balances[msg.sender]);47 balances[msg.sender] = balances[msg.sender]-numTokens;48 balances[receiver] = balances[receiver]+numTokens;49 emit Transfer(msg.sender, receiver, numTokens);50 return true;51 }5253 function approve(address delegate, uint256 numTokens) public override returns (bool) {54 allowed[msg.sender][delegate] = numTokens;55 emit Approval(msg.sender, delegate, numTokens);56 return true;57 }5859 function allowance(address owner, address delegate) public override view returns (uint) {60 return allowed[owner][delegate];61 }6263 function transferFrom(address owner, address buyer, uint256 numTokens) public override returns (bool) {64 require(numTokens <= balances[owner]);65 require(numTokens <= allowed[owner][msg.sender]);6667 balances[owner] = balances[owner]-numTokens;68 allowed[owner][msg.sender] = allowed[owner][msg.sender]-numTokens;69 balances[buyer] = balances[buyer]+numTokens;70 emit Transfer(owner, buyer, numTokens);71 return true;72 }73}Prikaži sveKopiraj
Another excellent implementation of the ERC-20 token standard is the OpenZeppelin ERC-20 implementation(opens in a new tab).
Posljednje uređivanje: @nhsz(opens in a new tab), 15. kolovoza 2023.