An Introduction to ERC-20 Contracts
To understand an ERC-20 contract, it goes without saying that one should understand what a contract (smart contract) and ERC is.
What is a smart contract?
Smart contracts are digital contracts stored on a blockchain that are automatically executed when predetermined terms and conditions are met.
Smart contracts are typically used to automate the execution of an agreement so that all participants can be immediately certain of the outcome, without any intermediary’s involvement or time loss. They can also automate a workflow, triggering the next action when predetermined conditions are met. To learn more about smart contracts you can check out this page.
What are ERCs?
ERC which stands for Ethereum Request for Comments is a proposal document developers write. It is then reviewed by the Ethereum community using a process called "Ethereum Improvement Proposal" or EIP.
what is ERC-20?
ERC-20 is the 20th proposal, proposed in 2015 by Fabian Vogelsteller, and integrated in Ethereum in 2017. It is the most commonly used token standard in the Ethereum ecosystem.
The ERC-20 standard allows for the implementation of a standard API for tokens within smart contracts. This standard provides basic functionality to transfer tokens, as well as allow tokens to be approved so they can be spent by another on-chain third party.
An ERC20 Contract...
is a smart contract on the Ethereum blockchain that implements the ERC-20 standard - a set of rules and guidelines for creating and managing fungible tokens: any one token is equal to any other token; like currencies (e.g., 1 ETH is the same as another 1 ETH). The ERC-20 provides an interface for creating and managing fungible tokens on the Ethereum blockchain. By following this standard, developers ensure that their tokens can interact seamlessly with various wallets, exchanges, and decentralized applications (DApps).
Properties Of An ERC-20 Token Contract
Tokens created with the ERC-20 Contract consist of three optional and six mandatory functions that a smart contract should implement to create a fungible token.
The following specifications use syntax from Solidity 0.4.17
(or above)
- Optional functions variables: These store the essential data related to the token to the state. This method can be used to improve usability, but interfaces and other contracts MUST NOT expect these values to be present. They include
function name() public view returns (string)
- The name of the erc-20 token (e.g., "MyToken"). This returns a
string
- The name of the erc-20 token (e.g., "MyToken"). This returns a
function symbol() public view returns (string)
- The ticker symbol or abbreviation for the token (e.g., "MTK").
function decimals() public view returns (uint8)
- The number of decimal places the token can be divided into. Returns the number of decimals the token uses - e.g.
8
, means to divide the token amount by100000000
to get its user representation.
- The number of decimal places the token can be divided into. Returns the number of decimals the token uses - e.g.
Functions: These are the methods that can be called to interact with the token contract.
function totalSupply() public view returns (uint256)
- Returns the total number of tokens in existence.
balanceOf(address owner) public view returns (uint256):
- Returns the balance of tokens held by the specified address.
transfer(address to, uint256 value) public returns (bool success):
- Transfers
_value
_to
another address. This decreases the balance of the sender and increases the balance of the recipient. The function SHOULDthrow
if the message caller’s account balance does not have enough tokens to spend.
- Transfers
approve(address spender, uint256 value) public returns (bool):
Allows the owner to approve a spender to transfer up to a specified number of tokens on their behalf.
Clients SHOULD make sure to create user interfaces in such a way that they set the allowance first to
0
before setting it to another value for the same spender. This is to prevent attack Vectors on the Approve/TransferFrom Methods. See here for a full explanation of such attacks
transferFrom(address from, address to, uint256 value) public returns (bool):
- Allows a spender to transfer tokens from one address to another, based on the allowance:
_value
previously set by the owner. This references theapprove()
function.
- Allows a spender to transfer tokens from one address to another, based on the allowance:
allowance(address owner, address spender) (public view returns (uint256)):
- Returns the remaining number of tokens that a spender is allowed to transfer on behalf of the owner.
Events: Events are used to log activities on the blockchain, allowing external applications to track changes in the contract.
event Transfer(address indexed from, address indexed to, uint256 _value)
This event MUST be Emitted whenever tokens are transferred from one address to another including zero-value transfers.
A token contract that creates new tokens SHOULD trigger a Transfer event with the
_from
address set to0x0
when tokens are created.
event Approval(address indexed owner, address indexed spender, uint256 _value)
Emitted whenever an owner approves a spender to manage their tokens.
ie: a response to this successful call to
approve(address _spender, uint256 _value)
Example implementations of ERC-20 contracts are available at
The Need for ERC-20
The need for ERC-20 spins from the necessity to standardize the creation and management of fungible tokens on the Ethereum blockchain. This ensures that tokens can interact seamlessly with various wallets, exchanges, and decentralized applications (DApps).
While an interface doesn't specify how you should implement the functions or events, it warrants that you should implement the manadatory functions and events. Hence by following the ERC-20 standard, developers can create tokens that are compatible with the Ethereum ecosystem. This brings about uniformity and prevents collision in the ecosystem.
Subscribe to my newsletter
Read articles from Nwachukwu directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Nwachukwu
Nwachukwu
I am a skilled full-stack developer experienced in JavaScript, TypeScript, react, PHP, and Laravel. I create visually appealing and responsive user interfaces. I have a strong background in back-end development with proficiency in Laravel, enabling me to build robust and scalable server-side solutions. My skills extend to data management using tools like SQL, and POSGRESQL for efficient storage and real-time communication. Additionally, I actively contribute to open-source projects and have a knack for BAAS like supabase.