No history yet

ERC20 Token Basics

The Universal Language of Tokens

Imagine trying to plug a European charger into a U.S. wall outlet. It doesn't work without an adapter. In the early days of Ethereum, digital tokens had a similar problem. Developers created them in all sorts of custom ways, meaning every new wallet or exchange had to build custom adapters to support each new token. It was messy and inefficient.

To solve this, the Ethereum community proposed a standard called ERC-20. Think of it as a universal adapter for tokens. It's a list of rules and functions that a token's smart contract must follow. If a token adheres to the ERC-20 standard, it can automatically work with any dApp, wallet, or exchange that also understands the standard. This simple idea unlocked a massive wave of innovation, creating the ecosystem of tokens we see today.

Fungible

adjective

Describes an item or asset that is interchangeable with another identical item. One dollar is fungible because it can be replaced by any other dollar; a unique painting is not.

The ERC-20 standard is built around a handful of core functions that every token contract must include. Let's walk through the most important ones.

Checking the Supply and Balances

Two of the simplest functions are for looking up information. They don't change anything on the blockchain; they just read data.

First is totalSupply(). This function returns the total number of tokens that have been created. If a project mints 100 million tokens, totalSupply() will always report 100 million.

Next is balanceOf(address). This tells you how many tokens a specific Ethereum address holds. You can use it to check your own balance or anyone else's, since the blockchain is public.

// A simplified view of these functions in a contract
contract MyToken {
    mapping(address => uint256) public balances;
    uint256 public totalSupply;

    function balanceOf(address account) external view returns (uint256) {
        return balances[account];
    }
}

Moving Tokens Around

The most basic action is a direct transfer. The transfer(recipient, amount) function allows a token owner to send tokens directly from their wallet to someone else's. It's straightforward: you specify who you're sending tokens to and how many. The function automatically subtracts the tokens from your balance and adds them to the recipient's.

But what if you want to let a decentralized application (dApp) spend tokens on your behalf? For example, when you want to trade Token A for Token B on a decentralized exchange (DEX). You can't just send your tokens to the DEX and hope for the best. That would require trusting the exchange's contract completely.

This is where the real power of ERC-20 comes in, with a two-step process: approve and transferFrom.

  1. approve(spender, amount): You call this function to give a specific address (the spender, like a DEX's smart contract) permission to withdraw up to a certain amount of your tokens.
  2. transferFrom(sender, recipient, amount): The spender contract then calls this function to execute the transfer. It moves the amount of tokens from your address (the sender) to the final recipient.

This approval system is a core part of decentralized finance (DeFi). It allows you to interact with smart contracts without sending them your assets directly, which is much more secure. You grant limited permission, and the contract can only use what you've approved.

Lesson image

Announcing Changes

Functions change the state of the blockchain, but how does the outside world know what happened? ERC-20 contracts use events to announce these changes. Think of them as public receipts.

The two key events are:

  • Transfer(from, to, value): Emitted whenever tokens are moved, including minting (from address 0) and burning (to address 0).
  • Approval(owner, spender, value): Emitted whenever an approve() function is successfully called.

Wallets, block explorers, and dApps listen for these events to update their own records and show users accurate information about their balances and allowances.

ERC-20 defines a common list of rules that all fungible Ethereum tokens should adhere to.

By providing a shared blueprint, the ERC-20 standard ensures that tokens are predictable and reusable across the entire Ethereum ecosystem. This interoperability is what allows developers to build complex applications that can interact with any token, without needing to write custom code for each one.

Quiz Questions 1/5

What was the primary problem that the ERC-20 standard was created to solve?

Quiz Questions 2/5

You want to check how many tokens a specific Ethereum wallet address holds. Which ERC-20 function would you use?

That covers the fundamental building blocks of the most common token type on Ethereum. Understanding this standard is the first step to understanding how DeFi and other decentralized applications truly work.