A Study of ERC-20 Tokens: What They Are and How They Work

Levi FrancisLevi Francis
3 min read

ERC-20 tokens are smart contract-enabled tokens on the Ethereum blockchain that implement the ERC-20 standard. The ERC-20 standard is a technical standard for fungible tokens that could represent any real-world or virtual asset like stablecoins, utility tokens, security tokens, or cryptocurrencies to name a few. In this article, I'll be discussing how ERC-20 tokens work, and I'll explain their core functions and the functionalities they offer to contracts that implement the standard.

How ERC-20 tokens work

ERC-20 tokens operate based on a specific standard defined by the Ethereum Improvement Proposal(EIP)-20. This standard defines six mandatory functions and three optional events that smart contracts must implement to be considered ERC-20 compliant. The key functions are:

  1. totalSupply() - A view function that takes no arguments and returns the total supply of tokens that exist in the contract.

  2. balanceOf(address _account) - Also a view function that takes one argument which is the address to an account and returns the total balance of the tokens held by that account.

  3. transfer(address _to, uint256 _amount) - This function moves the specified amount of tokens in _amount from the caller's account to the recipient's address specified in _to. It returns a boolean to signify whether the transfer was successful or not and it must emit the Transfer event, even for transfers involving 0 values.

  4. approve(address _spender, uint _value) - This function allows _spender to withdraw from the owner's account, up to the _value amount, returns a boolean response, and emits an Approval event. If the function is called again, it overwrites the current value of _value. An example of how the approve function works is when you interact with a DApp that does something with your token, like a Decentralized Exchange. When you interact with a DEX's interface let's say Uniswap and you're trying to swap 100 DAI tokens for another token, you go through a process where the DApp(_spender) asks you to approve the 100 DAI(_value). You confirm this action through your wallet, which then sends a transaction to the blockchain calling the approve function on the DAI token contract.

  5. allowance(address _owner, address _spender) - A view function that returns the amount that _spender is still allowed to withdraw from _owner. The value is zero by default

  6. transferFrom(address _from, address _to, uint256 _amount) - transferFrom moves _amount of tokens from the _from address to the _to address, returns a boolean and emits the Transfer event. The function is used for the withdrawal workflow, this function is used when a smart contract transfers tokens on the owner's behalf as long as the contract is approved, and should throw an error if the _from account has not deliberately authorized the sender of the message.

In Summary

ERC-20 tokens are a major part of the Ethereum blockchain, allowing for a wide range of digital assets. By following a standard set of rules, these tokens can be easily created and managed across different Ethereum wallets and apps. Whether it’s keeping track of balances, approving token transfers, or interacting with DApps, the ERC-20 standard makes it all possible. Understanding these functions helps you implement the token standard in a way that doesn't allow them to be exploited due to mistakes in development.

0
Subscribe to my newsletter

Read articles from Levi Francis directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Levi Francis
Levi Francis