In this tutorial, we will introduce the ERC20 token standard on Ethereum and issue our own test tokens.
ERC20 is the token standard on Ethereum, proposed by Vitalik Buterin in November 2015 as part of the EIP20 proposal. It implements the basic logic of token transfer:
IERC20 is the interface contract for the ERC20 token standard, which specifies the functions and events that an ERC20 token needs to implement. The reason for defining an interface is that with a standard, there are common function names, input parameters, and output parameters that all ERC20 tokens share. In the interface functions, only the function name, input parameters, and output parameters need to be defined, and the implementation of the function is not relevant. Therefore, functions can be divided into internal and external contents, one focusing on implementation and the other on external interfaces, agreeing on common data. This is why we need two files, ERC20.sol and IERC20.sol, to implement a contract.
IERC20 defines two events: the Transfer event and the Approval event, which are released when a transfer and approval are made, respectively.
/**
* @dev Emits when `value` tokens are moved from one account (`from`) to another (`to`).
*/
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* @dev Emits when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
IERC20 defines six functions that provide the basic functionality for transferring tokens and allow tokens to be approved for use by third parties on other chains:
totalSupply()
: Returns the total token supply /**
* @dev Returns the amount of tokens in existence.
*/
function totalSupply() external view returns (uint256);
balanceOf()
: Returns the account balance /**
* @dev Returns the amount of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
transfer()
: Transfers tokens