- Lab
- Core Tech

Guided: Design, Develop, and Deploy an ERC-20 Token on Ethereum
Ethereum, Tokens, and Cryptocurrency – what do these terms entail? This lab serves to advance your Solidity development skills while acquainting you with the ERC-20 Token standard. If you're well-versed in the blockchain domain, the expression "ERC-20 compatible token" would likely be familiar. Prepare to craft your own fungible ERC-20 token, laying the foundation for a more comprehensive understanding of this concept.

Path Info
Table of Contents
-
Challenge
Introduction
Welcome to the Guided: Design, Develop, and Deploy an ERC-20 Token on Ethereum lab,
Step into the world of ERC-20 tokens, where innovation meets utility. Whether you're already familiar with terms like "hodl'ing bags of coins" or find them a bit perplexing, this lab serves as your gateway to uncovering the mechanics of ERC-20 tokens. These tokens form the backbone of Ethereum's thriving token ecosystem, and whether you're a newcomer or a seasoned blockchain enthusiast, this lab will unveil the core features of ERC-20 compatible tokens.
ERC-20 tokens have the unique ability to digitally represent ownership of diverse assets. For instance, Ethereum's ETH token serves as the currency for covering transaction fees within the Ethereum network. Similarly, stablecoins like USDC and DAI aim to maintain a value equivalent to the U.S. Dollar.
The foundation of these tokens rests on the ERC-20 standard, a meticulously crafted set of protocols. This standard ensures their compatibility across various platforms, wallets, and services, fostering seamless and widespread usability.
At the heart of our exploration lies the concept of fungibility, which means that each unit of an ERC-20 token is interchangeable, just like conventional currency notes. This characteristic enables seamless and efficient transactions, unlike non-fungible tokens (NFTs), which are known for their uniqueness. NFTs represent individual, irreplaceable assets, much like homes and cars.
You are invited to embark on a journey of discovering the characteristics of the ERC-20 protocol that enable third-party applications to work seamlessly across all tokens adhering to this standard.
When you are ready to begin, click the Next step > button.
-
Challenge
Designing Token Value through `totalSupply`
Understanding the
totalSupply
value in an ERC-20 compatible token is fundamental to grasping the intricacies of token economics. ThetotalSupply
value in an ERC-20 compatible token serves as the linchpin for crafting a token's economic narrative, ultimately driving its value proposition. This pivotal parameter forms the basis for shaping the token's economic landscape, but its importance extends beyond mere technical detail. It's the cornerstone upon which a token's purpose and value are built.This design of a token's economic model is commonly referred to as Tokenomics. While beyond the scope of this lab, Tokenomics is a great area of study as you continue your blockchain learning journey.
For now, just realize the importance of identifying a limited
totalSupply
of tokens. Notice thattotalSupply()
is returning an unsigned integer type, so it is not able to handle fractional amounts. The ETH token handles this scenario by dividing 1 ETH into 1 quintillion WEI. Similar to how $1 can be broken down into 100 cents. So thetotalSupply
value of ETH is actually stored as thetotalSupply
of available WEI. This value is calculated by multiplying the total available ETH by 1 * 10^18 (that is 1 followed by 18 0s). Similar to how the total number of cents is calculated by multiplying the total number of dollars by 1 * 10^2 (or 100) This allows fractional ownership of ETH while not being able to maintain fractional numbers in the Solidity contract.Since you are defining your own token, you are free to decide how deep you would like to be able to subdivide your Token. To help keep your contract simple, just consider that the token is the smallest divisible unit. This keeps your contract simple, but might become confusing when interacting with other contracts that are divisible down to the quintillionth or some other decimal.
To help inform users about your token's divisibility, the standard provides the optional
decimals
property, which helps identify how many decimal points the tokens are divisible down to. The value of a token primarily depends on a users' ability to securely store and transfer ownership. In the next step, you will implement the functions of the ERC-20 standard that address token ownership. When you are ready, click the Next step > button. -
Challenge
Managing Token Ownership
The primary purpose of a token is to track the number of tokens held by specific accounts. The
balanceOf
function is a crucial part of the ERC-20 standard, enabling you to retrieve the token balance of any account. Before you can implement this function, you need to establish a data structure to manage balances associated with each account. Solidity'smapping
data type serves as an excellent choice for this purpose. With these steps completed, you have laid the foundation for managing account balances within our token contract. Now, you will implement thetransfer
function, which will enable users to transfer tokens between accounts.Before diving into the code, here is an outline of what the
transfer
function needs to achieve:- Verify the sender's account balance.
- Reduce the sender's balance by the transferred amount.
- Increase the receiver's balance by the transferred amount.
- Emit the
Transfer
event, as per the ERC-20 standard. - Return the successful status. Before continuing the implementation of the rest of the standard, you may be wondering how the contracts
totalSupply
gets distributed to the different accounts. Currently, no account owns any of the available supply of tokens. Different tokens manage the distribution of the total supply in various ways based on their Tokenomics plan. To keep things simple for this contract, you can assign thetotalSupply
to the account that deploys of this contract.
With these implementations, your token contract is now equipped to handle fundamental features, such as checking balances and transferring tokens among various accounts. Next, you'll explore the advanced capability of allowing one user to delegate transfers to another user. Click the Next step > button to continue.
-
Challenge
Enable Token Delegation for Interoperability
In your ERC-20 contract, you've already implemented core features for storing and transferring token balances directly between accounts. Now, delve into a key aspect of the ERC-20 standard—token transfer delegation.
Imagine you want to exchange your TokenX for TokenY. Here's the challenge: your TokenX balance is managed in a different contract from TokenY. To facilitate this trade, you must typically transfer TokenX to another user and trust that they reciprocate with the agreed on transfer on the TokenY contract. This trust-based system has its risks.
To address this, exchange services were developed. These services can autonomously handle token exchanges between users, eliminating the need for trust. You can delegate these exchange services to conduct transactions on your behalf.
Before the ERC-20 standard, each token required custom programming for exchange services, limiting their scalability. However, the ERC-20 standard changed this by providing a common interface for all Ethereum based tokens. This standardization streamlines the addition of new tokens to exchange services, making token exchanges more efficient.
To enable delegated functionality, you need to implement three essential functions:
approve
,allowance
, andtransferFrom
. You will be implementing these over the next several tasks. In considering what data structure to use to support this feature, consider the parameters that need to be mapped together:- The owner of the tokens (the caller)
- The address these tokens are being delegated to
- The number of tokens the owner is allowing
This structure requires just one additional parameter than the
balances
variable, so a nestedmapping
makes sense. The second function within the ERC-20 standard for delegation, known asallowance
, serves the purpose of verifying how many tokens the owner has approved the delegate to manage. The last function of the standard performs the actual transfer of the delegated tokens. At a minimum, thetransferFrom()
function needs to perform the following actions:- Verify that the owner actually owns the tokens.
- Verify that the delegate, who is calling the function, has been approved to that many tokens.
- Once verified, decrement the
amount
from thesender
in both thebalances
and theallowed
variables. - Add the
amount
to therecipient
's balance. - Emit the
Transfer
event and return atrue
value as a success acknowledgment to the caller. Congratulations! You've successfully implemented all the functions required to enable delegated token transfers in your contract. Now, it's time to put your ERC-20 compatibleToken
smart contract to the test in your local development environment. Click the Next Step > button to explore its functionality further.
-
Challenge
Exploring Token Interactions in the HardHat Console
Congratulations, you have just created a simple ERC-20 compatible token. In this step, you will use the hardhat console in the Terminal tab to see how these functions can be used together to help support the transferring functionality. To get started you will first need to compile your contract using the command:
npx hardhat compile
Next, you can run the following command to open the HardHat console:
npx hardhat console
Deploy your
Token
contract to this test environment using the following commands:const ContractFactory = await ethers.getContractFactory("Token") const contract = await ContractFactory.deploy()
You can now use this
contract
object to call the variousToken
functions. Before calling any functions you will need references to several accounts to help demonstrate various the various types of transfers. In this test environment these accounts are easily retrieved using theethers.getSigners()
, which returns an array of accounts. You can assign the first several accounts into variables using the following destructuring assignment:const [deployer, delegate, acct1, acct2, acct3] = await ethers.getSigners()
Now you can verify the total supply of tokens by calling the
totalSupply()
function:await contract.totalSupply()
You can also verify that all of the tokens have been transferred to the deployment account. Note that in this test environment, the contract is deployed to that first signer account, which has been assigned to the
deployer
account:await contract.balanceOf(deployer)
A portion of the the
deployer
s tokens can be transferred toacct1
using the following command:await contract.transfer(acct1, 100)
Remember that the first signer account is used as the default in this test environment. If you want to transfer 10 of
acct1
tokens toacct2
, you will need to connect to that account and then call thetransfer
function:await contract.connect(acct1).transfer(acct2, 10)
You can also verify the balances of those accounts using the following:
await contract.balanceOf(acct1) await contract.balanceOf(acct2)
Note: The
balanceOf
function is not required to be called by a specific account.Now you can delve into the allowance feature, which is a bit more intricate. Follow these steps to understand how this workflow functions. Imagine the scenario where
acct1
wants to grant thedelegate
account the ability to transfer 10 tokens on their behalf toacct3
.Note: These functions are caller specific, so you do need to
connect
to the specific account.First
acct1
will need to approve thedelegate
to be able to transfer these 10 tokens on their behalf:await contract.connect(acct1).approve(delegate, 10)
Now anyone can verify how many tokens the
delegate
is allowed to transfer on behalf ofacct1
:await contract.allowance(acct1, delegate)
Finally, the
delegate
is free to initiate that transfer to whatever account they deem appropriate:await contract.connect(delegate).transferFrom(acct1, acct3, 10)
Great job, you have walked through several of the scenarios available while using and ERC-20 feel free to continue walking through other scenarios you may consider.
-
Challenge
Next Steps
Congratulations on your successful design, development, and deployment of your own ERC-20 token within your local Ethereum development environment.
This walkthrough aims to provide you with a foundational understanding of ERC-20 compatible tokens, albeit in a simplified implementation. It's important to note that while this example serves as a valuable starting point, there is significant room for improvement in real-world applications. Remember that smart contracts are immutable, meaning they cannot be altered once deployed. In the event of discovering bugs or deficiencies later on, you would need to abandon the original contract and redeploy a new one. Transitioning to a new contract can be complex and sometimes disruptive to the community using the existing contract, as demonstrated by the Ethereum Classic (ETC) and Ethereum (ETH) fork.
As you continue your blockchain learning journey, consider delving deeper into topics like Tokenomics and Security, which are essential for creating robust and secure blockchain solutions.
Wishing you success in your future blockchain endeavors! Excited to see what you create next!
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the author’s guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.