ERC-20 tokens are a type of fungible token standard on the Ethereum blockchain. The term "ERC" stands for "Ethereum Request for Comments," and "20" is the unique identifier for this specific proposal. ERC-20 tokens are widely used for various applications, including Initial Coin Offerings (ICOs), decentralized finance (DeFi) projects, and as utility tokens within decentralized applications (dApps).
1. **Key Features of ERC-20 Tokens**
- Fungibility: Each ERC-20 token is identical and interchangeable with another token of the same type, making them fungible.
- Standardization: ERC-20 provides a standard interface for tokens, allowing developers to create and interact with tokens in a predictable manner.
- Interoperability: ERC-20 tokens can be easily integrated into various wallets, exchanges, and dApps due to their standardized nature.
2. **Common Functions of ERC-20 Tokens**
The ERC-20 standard defines a set of mandatory and optional functions that a token contract must implement. Here are the most common functions:- totalSupply: Returns the total supply of the token.
- balanceOf: Returns the balance of a specific address.
- transfer: Transfers a specified amount of tokens from the sender's address to a recipient's address.
- approve: Allows a spender to withdraw tokens from the sender's account, up to a specified amount.
- transferFrom: Transfers tokens from one address to another, using the allowance set by the
approve
function. - allowance: Returns the amount of tokens that a spender is allowed to withdraw from a sender's account.
3. **Sample Code: An ERC-20 Token Implementation**
Below is a simple implementation of an ERC-20 token in Solidity:
solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract MyToken {
string public name = "MyToken";
string public symbol = "MTK";
uint8 public decimals = 18;
uint256 public totalSupply;
mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
constructor(uint256 initialSupply) {
totalSupply = initialSupply * (10 ** uint256(decimals));
balanceOf[msg.sender] = totalSupply; // Assign the total supply to the contract's creator
}
function transfer(address _to, uint256 _value) public returns (bool success) {
require(balanceOf[msg.sender] >= _value, "Insufficient balance");
balanceOf[msg.sender] -= _value;
balanceOf[_to] += _value;
emit Transfer(msg.sender, _to, _value);
return true;
}
function approve(address _spender, uint256 _value) public returns (bool success) {
allowance[msg.sender][_spender] = _value;
emit Approval(msg.sender, _spender, _value);
return true;
}
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
require(balanceOf[_from] >= _value, "Insufficient balance");
require(allowance[_from][msg.sender] >= _value, "Allowance exceeded");
balanceOf[_from] -= _value;
balanceOf[_to] += _value;
allowance[_from][msg.sender] -= _value;
emit Transfer(_from, _to, _value);
return true;
}
}
This MyToken
contract implements the basic ERC-20 functionalities:
- Constructor: Initializes the token with a specified total supply and assigns it to the contract creator.
- transfer: Allows users to transfer tokens to another address.
- approve: Allows users to set an allowance for a spender.
- transferFrom: Enables a spender to transfer tokens on behalf of the owner, up to the approved amount.
4. **Use Cases
ERC-20 tokens have a wide range of use cases, including:
- Initial Coin Offerings (ICOs): Many projects use ERC-20 tokens to raise funds through ICOs, allowing investors to purchase tokens in exchange for Ether or other cryptocurrencies.
- Decentralized Finance (DeFi): ERC-20 tokens are commonly used in DeFi applications for lending, borrowing, and trading, providing liquidity and enabling various financial services.
- Utility Tokens: Many dApps utilize ERC-20 tokens as utility tokens, granting users access to specific features or services within the application.
5. **Conclusion**
ERC-20 tokens are a fundamental part of the Ethereum ecosystem, providing a standardized way to create and manage fungible tokens. Their versatility and interoperability have made them a popular choice for developers and projects across various sectors in the blockchain space.