β What is an ERC-20
β Make your own implementation of an ERC-20
β Deploy an ERC-20 on Ethereum Sepolia Testnet
ERC-20, which stands for "Ethereum Request for Comments 20," is a technical standard used to create and manage tokens based on the Ethereum blockchain. ERC-20 tokens are digital assets that can represent various forms of value, such as financial assets, loyalty points, digital goods, and more.
The primary advantage of ERC-20 lies in its ease of adoption and interoperability. This standard defines a set of rules and functions that allow developers to create tokens compatible with the Ethereum network and existing Ethereum wallets. This means that users can store, transfer, and exchange different types of ERC-20 tokens using the same wallets and interfaces.
Solidity is a programming language specifically designed for developing smart contracts on the Ethereum blockchain. It enables developers to create self-executing, autonomous, and verifiable contracts that define rules and interactions within a decentralized environment. Solidity is based on JavaScript-like syntax and offers features such as state management, control structures, events, and calls to other contracts, enabling the creation of complex and secure solutions on the Ethereum blockchain. If you've never done Solidity before, take a look at the Solidity Essentials to understand the basics. You can also look at the official documentation.
Please refer to the SETUP.md file.
In this step, you will put in place the necessary foundations for the creation of an ERC-20.
- First, delete the
script
folder we will not need it as well as theCounter.sol
andCounter.t.sol
files. - Create a folder
interface
in thesrc
folder. - Copy the
IERC20.sol
file which is in theutils
folder to theinterface
folder, previously created.- This file contains the ERC20 interface, all the functions we are going to implement.
- Now, create the
ERC20.sol
file insrc
, it is in this file that we will create our ERC-20. - Finally, create the
ERC20
contract inheriting theIERC20
interface.
β οΈ Don't forget the import and the header of the file
In this step, you will create the variables, the mappings and the constructor necessary for an ERC-20.
- Create in the
ERC20
contract the following variables:- All variables except
_totalSupply
will be initialized on declaration name
which corresponds to the name of the tokensymbol
which corresponds to the abbreviation of the token namedecimals
which corresponds to the number of decimals that the token can have_totalSupply
which corresponds to the total token created
- All variables except
β οΈ Take care about the visibility of your variables! The names of the variables can give you clues π
- Create in the
ERC20
contract the following mappings:_balances
which corresponds to the balance of all wallets_allowances
which corresponds to the number of tokens whose owner has authorized the spender to use
β οΈ Take care about the visibility of your mappings!
I give you the last one, it's a little more complicated:
mapping(address owner => mapping(address spender => uint256)) private _allowances;
- Initialize
_totalSupply
in the constructor of the contract.
β οΈ Don't forget to assign_totalSupply
to a wallet and to emit an event. All the events you need to emit are already defined in the interface. Find the right one.
In this step, you will do the most important part: the implementation of the standard functions. Implementing standard functions in your ERC-20 token contract is a crucial element to ensure compatibility, interoperability, and adoption of your token within the Ethereum ecosystem.
- Implement in the
ERC20
contract all the functions present in theIERC20
interface.- You can copy function prototypes directly into the contract.
β οΈ Don't forget to update the visibility of the functions and to add theoverride
modifier
- Read the doc of the ERC-20: Token Standard, it will help you to understand the purpose of the different functions.
- Take care about the visibility of your functions!
- Use if into revert or require, to handle error cases.
In this step, you will test, with foundry, the functions that you have implemented previously. Any contract published on the blockchain is immutable, it cannot therefore be modified after its deployment. Testing the implementation of an ERC-20 contract is therefore essential to ensure the safety, compliance and reliability.
- Copy the
ERC20.t.sol
file which is in theutils
folder to thetest
folder.- This file contains the foundry tests that allow you to test all the functions of an ERC-20.
- Execute the command
forge test
to run the tests.- You can use
forge test -vvvv
to show more information.
- You can use
If you have correctly implemented the functions, you should have something like this:
β οΈ To validate a test, you must validate the previous one
Running 5 tests for test/ERC20.t.sol:ERC20Test
[PASS] testApproveAndAllowance() (gas: 31150)
[PASS] testBalanceOf() (gas: 11065)
[PASS] testTotalSupply() (gas: 10862)
[PASS] testTransferFrom() (gas: 49405)
[PASS] testTransfer() (gas: 36491)
Test result: ok. 5 passed; 0 failed; finished in 1.72ms
If not, fix the function implementation.
In this step, you will deploy your ERC-20 on Ethereum Sepolia Testnet. Testing our contract on a testnet allows us to deploy and test our application on the Ethereum network, without having to spend real money because we use test tokens. Be careful, this step requires a lot of tools. The first part of the tasks consists of the installation of these tools and the second one of the deployment of the ERC-20.
- Download Metamask and create an account, if you don't already have one. It is the most popular Ethereum wallet and allows you to interact with the Ethereum blockchain.
- Create your API key on Alchemy.
- Sign in.
- Click on
Create new app
. - Enter a name for your API key.
- Select
Ethereum
chain andEthereum Sepolia
network. - Click on
Create app
. - Now, click on
API Key
and you can see your API key as well as your RPC URL inHTTPS
.
- Add Sepolia Testnet network to Metamask, here a doc to explain how to do it.
- Go to Sepolia faucet, enter your wallet address and send you ETH.
β οΈ You need to have some ETH on mainnet to give you test tokens. If you don't have any, call the workshop manager.
- Copy the
.env
file which is in theutils
folder to your project and complete the variables except the last one.
The setup is now finished, let's go to the interesting part: deploy our ERC-20. We will again use foundry, the forge create command to deploy the contract and the cast command to interact with it.
- Load the environment variables:
source .env
- Deploy your ERC-20:
forge create src/ERC20.sol:ERC20 --private-key "$PRIVATE_KEY" --rpc-url $RPC_URL --constructor-args 100000 --legacy
π‘ You can replace
100000
by the total number of tokens you want
-
Copy the address of
Deployed to
and paste it into the.env
file in theCONTRACT_ADDRESS
variable. -
Load again the environment variables:
source .env
- Check that the contract has been deployed correctly:
cast call $CONTRACT_ADDRESS "totalSupply()" --rpc-url $RPC_URL
π‘ This should display the total supply of your tokens in wei.
Now, you can go in Metamask on Sepolia Testnet, click on Import tokens
and put the CONTRACT_ADDRESS
. Congratulation! You have your own token in your metamask wallet. You can now do what you want with your tokens.
Some examples of possible interactions:
- To transfer tokens to another address:
cast send $CONTRACT_ADDRESS "transfer(address, uint256)" $WALLET 100 --private-key $PRIVATE_KEY --rpc-url $RPC_URL
- To see the balance of an address:
cast call $CONTRACT_ADDRESS "balanceOf(address)" $WALLET --rpc-url $RPC_URL
- Approve an address to transfer tokens:
cast send $CONTRACT_ADDRESS "approve(address, uint256)" $WALLET 200 --private-key $PRIVATE_KEY --rpc-url $RPC_URL
- Ethereum Sepolia Testnet
- Metamask
- Add Sepolia Testnet to Metamask
- Sepolia faucet
- Foundry deploy
- Cast command
You just created your own ERC-20, well done! During this workshop, you learned what an ERC-20 was but you also made your own implementation. You can find a famous implementation here, if you want, you can compare your implementation with and possibly add features on yours.
I hope you enjoyed the workshop!
You have discovered what an ERC-20 is but there are still many other concepts to discover, here are some examples:
- Discovering what is an ERC-721 contracts (NFT)
- Deploy your ERC-20 on other blockchain, like Polygon Amoy
![]() Nathan FLATTIN |
---|
π Don't hesitate to follow us on our different networks, and put a star π on
PoC's
repositories.