logoAcademy

ERC-20 Tokens

Learn about ERC-20 Standard for tokens

There is only a single native token to a chain. However, to allow to represent a wide range of assets on EVM-chains, the ERC-20 token standard was developed. "ERC" stands for Ethereum Request for Comment, and "20" is the proposal identifier.

ERC-20 tokens are fungible, meaning each token is identical and can be exchanged on a one-to-one basis. These tokens are typically created and managed through smart contracts that adhere to the ERC-20 standard, which defines a set of rules and functions that ensure interoperability between different tokens and decentralized applications (DApps).

At the base of an ERC-20 token smart contract, there is a simple mapping of addresses to numbers, which represent the amount of tokens an address holds (balance).

abstract contract ERC20 is Context, IERC20, IERC20Metadata, IERC20Errors {
    
  mapping(address account => uint256) private _balances;
	
  //...
}

These addresses may belong to EOA or contract accounts. Both of these can hold and transfer ERC-20 tokens.

ERC-20 tokens can represent various assets, from digital currencies to tokenized assets, and they play a crucial role in the crypto ecosystem by enabling the creation of decentralized applications with diverse functionalities, such as token sales, decentralized finance (DeFi) protocols, and more.

Interface

To maintain compatibility with dApps designed to work with the ERC20 standard, all ERC20 tokens implement the following interface:

interface IERC20 {
 
    function name() external view returns (string memory);
 
    function symbol() external view returns (string memory);
 
    function decimals() external view returns (uint8);
 
    function totalSupply() external view returns (uint256);
 
    function balanceOf(address _owner) external view returns (uint256 balance);
 
    function transfer(address _to, uint256 _value) external returns (bool success);
 
    function transferFrom(address _from, address _to, uint256 _value) external returns (bool success);
 
    function approve(address _spender, uint256 _value) external returns (bool success);
 
    function allowance(address _owner, address _spender) external view returns (uint256 remaining);
}

Click here to review the ERC20 standard in full detail.

Transfer Tokens

Transferring ERC-20 tokens between accounts involves calling the 'transfer' function, specifying the recipient’s address and the amount to transfer.

To facilitate more complex interactions, such as those involving smart contracts, the ERC-20 standard includes the approve() and transferFrom() functions. We will check how to implement those so smart contracts can use funds according to its code.

approve()

Since all balances are just a mapping of amount values to addresses, the owner of each address can 'approve' some other account as a _spender of their funds up to some limit determined by the approved _amount. The spender then will be able to withdraw from your account multiple times, up to the value set in amount.

allowance()

Returns the amount which _spender is still allowed to withdraw from _owner.

transferFrom()

Transfers _value amount of tokens from address _from to address _to

Loading...

On this page