A Study of ERC-20 Tokens: What They Are and How They Work
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:
totalSupply()
- A view function that takes no arguments and returns the total supply of tokens that exist in the contract.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.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 theTransfer
event, even for transfers involving0
values.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 anApproval
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 theapprove
function on the DAI token contract.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 defaulttransferFrom(address _from, address _to, uint256 _amount)
- transferFrom moves_amount
of tokens from the_from
address to the_to
address, returns a boolean and emits theTransfer
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.
Subscribe to my newsletter
Read articles from Levi Francis directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by