Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Understanding ERC-20 Tokens: Purpose and Functions

Hello! Welcome to Module 4. In the previous modules, we've covered the fundamentals of Ethereum and built a solid foundation in the Solidity programming language. You've learned how to structure contracts, manage data, control access, and even handle Ether securely. Now, it's time to apply those skills to your primary goal: building a cryptocurrency token.

This module is all about the ERC-20 token, the most widely used standard for creating fungible tokens on the Ethereum blockchain. Today's lesson is the first and most crucial step. Your learning outcome is to explain the purpose and functions of the ERC-20 token standard. We'll explore what it is, why it's so important, and the core components that make it work. Think of this as learning the blueprint before we start construction.


1. What is a Token and Why Do We Need a Standard?

On Ethereum, a "token" is a digital asset managed by a smart contract. Tokens can represent almost anything of value: a currency, a share in a company, loyalty points, or even ownership of a physical object.

The ERC-20 standard specifically defines fungible tokens. Fungibility is an economic term meaning that each unit of the asset is identical and interchangeable. For example, one US dollar is exactly the same as any other US dollar. Similarly, one ERC-20 token of a specific type is exactly the same as any other token of that same type. This is in contrast to Non-Fungible Tokens (NFTs, or ERC-721), where each token is unique.

So, why not just let every developer create their token contract however they see fit? This is where the power of a standard comes in.

ERC20 Token Standard Explained

To start, let's watch this video from Moralis for Developers. It provides an excellent explanation of what an ERC-20 token can represent and uses a great analogy to illustrate why a standard is so essential for the ecosystem.

Please watch the following segments: What an ERC-20 Token Is (00:00 - 02:04): Understand the variety of assets a token can represent. The Need for a Standard (02:04 - 05:14): Pay close attention to the analogy of electrical plugs. This is the key concept of interoperability. Where the Name Comes From (06:15 - 08:02): A brief history lesson on Ethereum Improvement Proposals (EIPs) and how ERC-20 got its name.

As the video explained, the ERC-20 standard ensures interoperability. With your background as a backend developer, you can think of it as a mandatory API specification. Just as developers agree on RESTful API conventions (GET, POST, DELETE) to allow different clients to interact with a server predictably, the ERC-20 standard defines a common interface for token contracts.

This means any application—like a wallet (MetaMask), a decentralized exchange (Uniswap), or a lending protocol (Aave)—can automatically support and interact with any ERC-20 token without needing custom code for each one. This standardization is what allows the DeFi ecosystem to flourish.


2. The ERC-20 Interface: A Required Set of Functions

To be "ERC-20 compliant," a smart contract must implement a specific set of functions and events. Think of this as implementing an interface in an object-oriented language like Java.

The standard defines six mandatory functions, two mandatory events, and three optional (but highly recommended) functions for metadata.

ERC-20 Token Mandatory Functions
This table outlines the six mandatory functions that every ERC-20 token contract must include to comply with the standard.

Let's briefly go over the core components.

ERC-20 Token Standard

The official Ethereum documentation provides a concise definition of the ERC-20 standard. It's a great reference to have.

Read the 'Introduction' to reinforce the concept of fungibility and the 'Body' section, which lists the required methods and events. This will serve as our definitive reference for the interface.

Mandatory Functions:

  • totalSupply(): Returns the total number of tokens that exist.
  • balanceOf(address _owner): Returns the token balance of a specific account address.
  • transfer(address _to, uint256 _value): Transfers a specified number of tokens from the msg.sender's account to another account (_to).
  • approve(address _spender, uint256 _value): Allows an account (_spender) to withdraw up to a certain number of tokens (_value) from the msg.sender's account. This is a crucial function for dApp interactions.
  • allowance(address _owner, address _spender): Returns the remaining number of tokens that _spender is still allowed to withdraw from _owner.
  • transferFrom(address _from, address _to, uint256 _value): Used by a _spender to transfer tokens on behalf of the _from account to a _to account, but only if the _from account has previously approved it via the approve function.

Optional Functions:

These are not technically required but are essential for usability and are implemented by virtually every ERC-20 token.

  • name(): Returns the name of the token (e.g., "My Awesome Token").
  • symbol(): Returns the symbol of the token (e.g., "MAT").
  • decimals(): Returns the number of decimal places the token uses. This is important for display purposes. For example, ETH has 18 decimals.

Mandatory Events:

  • Transfer(address indexed _from, address indexed _to, uint256 _value): Emitted when tokens are transferred, including for minting (creating) and burning (destroying) tokens.
  • Approval(address indexed _owner, address indexed _spender, uint256 _value): Emitted when the approve function is successfully called.

3. How to Use an ERC-20 Token: Two Transfer Mechanisms

These functions enable two primary ways to move tokens:

  1. Direct Transfer: The token owner sends tokens directly to another address.
  2. Delegated Transfer: The token owner gives permission to another address (usually a smart contract) to transfer tokens on their behalf.

This second mechanism is the cornerstone of decentralized finance, enabling everything from automated trades on exchanges to collateralized loans.

How to Use an ERC-20 Token | Solidity (0.6)

This video from Smart Contract Programmer clearly demonstrates how these two transfer mechanisms work using a simple Alice and Bob example. It will help solidify your understanding of the core functions.

Focus on these key parts: Function Overview (00:00 - 01:33): A quick recap of what each function does. Direct Transfer (01:33 - 02:12): See how the transfer function works. Delegated Transfer (02:12 - 03:04): This is the most important part. Pay close attention to the approve and transferFrom interaction. Summary (08:44 - 09:11): A final recap of the two methods.

Let's review the Delegated Transfer flow, as it's a new and vital concept:

  1. Approval Step: You want to use a Decentralized Exchange (DEX) to swap 100 of your MAT tokens for ETH. You can't just "send" the tokens to the DEX contract because the contract needs to be the one to initiate the pull of funds as part of an atomic swap.

    • You call the approve() function on the MAT token contract.
    • You set the _spender address to the DEX's contract address.
    • You set the _value to 100.
    • The MAT contract records that the DEX is now allowed to withdraw up to 100 MAT from your balance.
  2. Transfer Step: You then interact with the DEX contract to execute the swap.

    • The DEX contract, as part of its internal logic, calls transferFrom() on the MAT token contract.
    • It sets _from to your address, _to to its own address (or a liquidity pool), and _value to 100.
    • The MAT contract checks if the DEX (the msg.sender of this call) has a sufficient allowance from you. Since you approved it, the check passes, and the transfer succeeds.

This two-step process allows you to give smart contracts permission to use your tokens without giving them control of your entire wallet.

Test your understanding!

Imagine you want to deposit your ERC-20 tokens into a staking contract to earn rewards. The staking contract needs to pull the tokens from your wallet and hold them.

Which function(s) must you call first on the token contract to make this possible? And which function will the staking contract call later to complete the deposit?

Show answer
  1. You must first call approve() on the token contract. You would pass the staking contract's address as the _spender and the amount you want to stake as the _value.
  2. After you've given approval, you will call a function on the staking contract (e.g., stake()). The staking contract will then call transferFrom() on the token contract to pull the approved amount of tokens from your address into its own.

Conclusion

You now have a solid grasp of the ERC-20 standard, the "API specification" for all fungible tokens on Ethereum. This is the blueprint we will use to build your own token.

Key Takeaways:

  • Purpose of the Standard: ERC-20 ensures interoperability, allowing wallets, exchanges, and other dApps to seamlessly interact with any token that follows the rules.
  • Fungibility: Each unit of an ERC-20 token is identical and interchangeable with another.
  • Core Interface: The standard mandates six key functions (totalSupply, balanceOf, transfer, approve, allowance, transferFrom) and two events (Transfer, Approval).
  • Two Transfer Patterns:
    1. transfer() for direct, user-initiated payments.
    2. approve() + transferFrom() for delegated transfers, where you grant permission to a smart contract to move tokens on your behalf.

Preview of the Next Lesson:
The approve and transferFrom pattern is so fundamental to DeFi that it deserves its own deep dive. In the next lesson, we will focus exclusively on this mechanism. You will describe the 'approve' and 'transferFrom' pattern for delegated token transfers in greater detail, exploring its importance, common use cases, and security considerations.

Can't find a good explanation? Sign up and we'll make it for you

Sign up