Creating an ERC20 Token: A Step-by-Step Guide
ERC20 is a standard for tokens on the Ethereum blockchain, and it has become the de facto standard for Ethereum tokens. ERC20 tokens are contracts that implement the ERC20 standard. Operations handled by these contracts include getting the total supply and balance of tokens, and the methods used to transfer them Source 1.
Prerequisites
Before we start, it's important to note that Ethereum development is conducted in Solidity, a contract-oriented programming language inspired by JavaScript, Python, and C++ Source 1. You should have a basic understanding of blockchain technology, Solidity, and how Ethereum works.
Creating an ERC-20 Token
Let's start with creating a simple token. We'll call it "MyToken":
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract MyTokenERC20 {}
In the first line of the code, we set the license identifier and the version of Solidity the code was written for. Here, our Solidity code is for Solidity v0.7.0–0.9.0. We declared our contract by using the contract keyword and then giving it the name MyTokenERC20 Source 3.
Setting the Events
Next, we declare the events Transfer
and Approval
inside our contract:
event Transfer(address indexed from, address indexed to, uint tokens);
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
Setting the Token Details
We then set the name, symbol, and decimal of our token:
string public constant name = "My Token";
string public constant symbol = "MYT";
uint8 public constant decimals = 18;
Our token name is "My Token" and our symbol is "MYT". We set the decimals to be 18 Source 3.
Approving a Token Transfer
Next, we define a function to approve a token transfer:
function approve(address delegate, uint numTokens) public returns (bool) {
allowed[msg.sender][delegate] = numTokens;
emit Approval(msg.sender, delegate, numTokens);
return true;
}
In this function, delegate
is the address we want to set the number of tokens that the deployer can send to it, and numTokens
is the number of tokens the deployer can send to the delegate Source 3.
Getting the Allowance Status
Next, we define a function to get the allowance status of an account:
function allowance(address owner, address delegate) public view returns (uint) {
return allowed[owner][delegate];
}
This function returns the current approved number of tokens by an owner to a specific delegate, as set in the approve
function Source 1.
Getting Total Token Supply
We also need a function to get the total token supply:
function totalSupply()
public
view
returns (uint)
{
return _totalSupply - balances[address(0)];
}
This function will return the number of all tokens allocated by this contract regardless of owner Source 6.
Getting Token Balance of Owner
Finally, we define a function to get the token balance of an owner:
function balanceOf(address tokenOwner)
public
view
returns (uint balance)
{
return balances[tokenOwner];
}
This function will return the current token balance of an account, identified by its owner’s address Source 6.
And that's it! You've created your own ERC20 token. Remember, ERC20 tokens follow a list of rules so that they can be shared, exchanged for other tokens, or transferred to a crypto-wallet. If you include certain functions in the token’s smart contract, you are ERC20 compliant Source 6.