Photo by Thor Alvis on Unsplash
An Interactive Explainer for ERC-20 Tokens
Putting the Fun in Fungible
Intro 🍼
If you're not familiar with ERC-20, it's the standard for creating Tokens on Ethereum. ERC-20 Tokens can represent a lot of things, like points, tickets, or currency. Currency is definitely the most common use-case.
Some examples of well-known ERC-20 tokens are:
- USD Coin (USDC)
- Chainlink (LINK)
- Tether (UDST)
If you're here, chances are you've heard of at least one of these before.
This interactive guide will help get your started on your path to using and creating your own ERC-20 Tokens. The interactive explainers were made with developers in mind, but they'll be useful for anyone curious about this technology.
Heads up! While you learn, you'll get to play around with fun stuff like this:
What’s a Token? 💎
At a high-level, in the blockchain world, token is another word for cryptocurrency. Not all cryptocurrencies are the same, though.
Bitcoin, for example, is a cryptocurrency that exists on a network whose purpose is to record and regulate the supply of Bitcoin. Something like Ether, on the other hand, is a transactional token that’s used to “power” the Ethereum Network, which is a network that allows you to build decentralized apps (dApps) on top of it.
You pay "gas" fees in Ether whenever you want to update in-app data on Ethereum, or even transfer some Ether to another address. This in turn gives it some inherent value.
But, going a step further, there are also tokens that live within the Ethereum network itself, such as ERC721 Tokens, also known as NFTs, and ERC-20 tokens.
What’s ERC-20? 📜
ERC-20 refers to a smart contract standard for creating fungible tokens on the Ethereum network.
Basically, if you want to create your own fungible token on Ethereum, you should be using the ERC20 standard.
Fungibility 🤝
Anything that’s fungible can be replaced with another one of its kind. You can give your friend a $1 bill, and she can give you a different $1 bill in return, and they will work the same. That’s fungibility.
But NFTs, or Non-Fungible Tokens (ERC-721), can’t be swapped 1-for-1. Each one is unique and therefore lacks fungibility. So that DeveloperDAO NFT you just bought is one-of-a-kind and shows that you're the only owner!
You can see the differences by clicking or swiping on the stacks of tokens above.
What the Code Kinda Looks Like 👓
Below is some pseudo Solidity code. It's a stripped-down version of what a typical ERC-20 smart contract looks like.
We will discuss the functionality of ERC-20's core features in more detail below. This code snippet is just here so you can keep everything in context.
contract ERC20 {
mapping(address => uint256) _balances;
mapping(address => mapping(address => uint256)) _allowances;
uint256 _totalSupply;
function balanceOf(address account) returns (uint256) { };
function transfer(address to, uint256 amount) returns (bool) { };
function approve(address spender, uint256 amount) returns (bool) { }; ;
function transferFrom(address from, address to, uint256 amount) returns (bool) { };
}
transfer
So, we want to be able to use our ERC-20 Token to pay for stuff! With the transfer
function, we can pay users -- also referred to as Externally Owned Accounts -- as well as other smart contracts!
You'll notice that an ERC-20 contract is like a simple database. We're not really "transferring" anything across the network, we're just incrementing and decrementing balances within the contract. Have a try!
If you call the transfer
function when you're connected to a dApp, the function will automatically know that you're the sender, so it only has to pass the address it's going to
, along with the amount
, as arguments.
Decimals in Solidity
Before we continue, it's important to mention that Solidity does not support floating-points numbers.
So we can't input a number like 1.50
into our contract to get fractional amounts. Instead, we have to express these amounts in whole numbers.
What does this mean in practice then? Well, in Solidity, because we can't use decimal points, we have to represent all numbers in their smallest unit.
Most all ERC-20 Tokens use the same approach as Ether, and Ether's smallest unit is the wei, which is 1^18. So 1 Ether gets represented in Solidity as:
1000000000000000000
The numbers we were using in the transfer
example above, like 50
or 100
, are actually just 50 wei. 🤔
Keep this in mind, because you'll have to convert these numbers to a human readable format on your UI, and vice-versa.
transferFrom
The transferFrom
method is really cool. It allows a “spender” to move some of the tokens in our balance to another address for us.
This works because the ERC-20 smart contract comes with:
- an
allowance
mapping - an
approve
function.
mapping(address => mapping(address => uint256)) _allowances;
function approve(address spender, uint256 amount) returns (bool) { };
function transferFrom(address from, address to, uint256 amount) returns (bool) { };
As users, we always have to approve
before someone else can move our tokens with the transferFrom
method. This is smart, we don't want someone to be able to access all of our funds!
Approving
simply updates the _allowances
mapping above. We do this by passing two arguments to the function:
- address of the
spender
amount
we want to allow them to spend
In the end, the _allowances
mapping is saying: "For our address, let this spender
address access a certain amount
of our tokens only -- nothing more!"
Again, our address gets sent from our connected wallet when we call it, so it knows who we are as approvers and don't have to pass it as an argument.
In the component below, we're imaging that Bob and Alice are saving money to throw a party. Bob approves
Alice to spend some of his tokens. Alice is only allowed to spend the amount Bob allows. Alice can then call transferFrom
and send some of Bob's tokens to the Party Fund 👯♂️
This feature is pretty powerful, but it's often misunderstood by end-users. Next time you're using a dApp and you have to approve
before you can transfer
, you'll know that the dApp is using the transferFrom
method internally in their smart contract. 😎
What Next? ⏭
This was just the start. If you want to explore actual code, then check out OpenZeppelin's:
There's definitely no substitute for playing with the code yourself. So open up a new tab with Remix, import the ERC-20 code above, and have fun! You can come back to this guide if things aren't making sense.
Also, keep your eye out for more Interactive Explainers, like a guide to Automated Market Makers ⚖️, and thanks for reading 🙏.
If you found this helpful, or would like to provide some feedback, drop me a line on twitter @hot_town!