Lesson 4 of 4 Advanced Solidity

Token Standards

Token standards let any wallet, exchange or contract work with any token. This lesson explains ERC-20 fungible tokens and the approve and transferFrom pattern, ERC-721 non-fungible tokens, ERC-1155, and how to build tokens with OpenZeppelin.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe the functions and events of ERC-20.
  • Explain the approve and transferFrom pattern.
  • Compare ERC-20, ERC-721 and ERC-1155.
  • Calculate token amounts from decimals.
  • Describe how to build tokens with OpenZeppelin.

Why standards matter

An Ethereum Request for Comments (ERC) is an application-level standard proposed through the Ethereum Improvement Proposal process. Because every ERC-20 token has the same functions and events, a wallet can display any token's balance, and an exchange can list any token, without custom code. Implementing a standard exactly, including its events, is therefore a functional requirement.

ERC-20 fungible tokens

ERC-20 tokens are fungible: every unit is interchangeable, like currency. The following table lists the standard's interface.

Function or eventPurpose
totalSupply()Total tokens in existence
balanceOf(account)Tokens held by an account
transfer(to, amount)Move the caller's tokens
approve(spender, amount)Allow a spender to move up to an amount of the caller's tokens
allowance(owner, spender)Remaining amount a spender may move
transferFrom(from, to, amount)Move tokens on an owner's behalf, within the allowance
Transfer, Approval eventsEmitted on every transfer (including mint and burn) and approval

The optional name, symbol and decimals describe the token. Amounts are integers: with 18 decimals, 1.5 tokens are stored as 1,500,000,000,000,000,000. A contract cannot take a user's tokens on its own; the user first calls approve, then the contract calls transferFrom. This two-step pattern underlies decentralised exchanges and staking contracts.

Put in order

Depositing tokens into a contract

Exam skills 2.2.4

Arrange the steps of the approve and transferFrom pattern.

Log in to try this activity Interactive activities are free with an account.

ERC-721 and ERC-1155

ERC-721 tokens are non-fungible: each token ID is unique and has exactly one owner, which suits collectibles, tickets and property titles. The following comparison sets out how the three token standards differ.

ERC-20
Token typeFungible
Ownership querybalanceOf(account)
Transfertransfer, transferFrom
Metadataname, symbol, decimals
ERC-721
Token typeNon-fungible
Ownership queryownerOf(tokenId)
TransfertransferFrom, safeTransferFrom
MetadatatokenURI(tokenId)
ERC-1155
Token typeBoth, in one contract
Ownership querybalanceOf(account, id)
TransfersafeTransferFrom, safeBatchTransferFrom
Metadatauri(id)

ERC-721's safeTransferFrom calls onERC721Received on a receiving contract and reverts unless the expected value is returned, preventing tokens from being locked in contracts that cannot move them. setApprovalForAll authorises an operator, such as a marketplace, to move all of an owner's tokens.

Sort into groups

Which token standard?

Exam skills 2.2.4

Choose the token standard that fits each use case.

Log in to try this activity Interactive activities are free with an account.

Building tokens with OpenZeppelin

OpenZeppelin Contracts provides audited implementations. A complete capped-supply token with an owner who can mint looks like this (OpenZeppelin v5):

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";

contract ClubToken is ERC20, Ownable {
    uint256 public constant MAX_SUPPLY = 1_000_000 * 10 ** 18;

    constructor(address initialOwner) ERC20("Club Token", "CLUB") Ownable(initialOwner) {}

    function mint(address to, uint256 amount) external onlyOwner {
        require(totalSupply() + amount <= MAX_SUPPLY, "cap exceeded");
        _mint(to, amount);
    }
}
Fill in the number

Token amounts

Exam skills 2.2.4

Convert token amounts into the integers a contract works with.

Log in to try this activity Interactive activities are free with an account.
Worked example

A deposit that fails

A user calls a staking contract's stake(100e18), which calls token.transferFrom(msg.sender, address(this), amount). The transaction reverts with "insufficient allowance" although the user holds 500 tokens. What must the user do?

Call token.approve(stakingContract, 100e18) first (or a larger amount). transferFrom requires an allowance from the owner to the spender; holding tokens is not enough. DApps therefore show two wallet prompts: approve, then stake.

Worked example

Decimals

A token has 6 decimals. A script must send 25.5 tokens. What amount is passed to transfer?

25.5 × 106 = 25,500,000. Decimals only affect how amounts are displayed; the contract works with integers. Using 1018 by habit would try to send a trillion times too much and revert for insufficient balance.

Check yourself

Which two ERC-20 functions let a contract move a user's tokens, and who calls each?

Show answer

The user calls approve(spender, amount) to grant an allowance; the spender contract then calls transferFrom(user, to, amount) within that allowance.

Exam Tips

Remember for the exam

  • ERC-20: transfer, approve, allowance, transferFrom, with Transfer and Approval events.
  • Contracts move users' ERC-20 tokens only after approve; then they call transferFrom.
  • ERC-721 tokens are unique; safeTransferFrom checks the receiver can handle them.
  • Token amounts are integers scaled by decimals.
  • Inherit audited OpenZeppelin implementations instead of writing tokens from scratch.