Development of ERC20 for Beginners
1 — What is an ERC20 Token
2 — How to Create Your Own ERC20 Token
January 20, 2023
What is an ERC20 Token
First, let's understand what ERC20 is and why there's so much talk about it. An ERC20 token is a program for tracking users' balances. Nothing more. But there are several features that have made it so popular among the masses. Let's go through them.
— Community
— Transparency
— Simplicity
Community
The ERC20 standard was officially introduced by the core team of Ethereum (you can read more about the specification here, which means it was more easily and quickly recognized and adopted by the community. The founder himself, Vitalik Buterin, also participated in its development.
It quickly became used to create in-game currencies, gamification systems, and later in more serious financial structures, such as ICOs and other investment formats. The rise of stablecoins, which were an extension of the classic ERC20, also contributed to its widespread adoption.
Cryptocurrency wallet providers had no choice but to natively support basic operations related to sending and receiving such crypto assets.
Supervised Learning
Transparency is the main marketing slogan of all crypto projects. This principle is at the core of the cryptocurrency and blockchain protocol boom.
The same transparency of smart contracts — where every recorded byte is synchronized across thousands of computers and visible to all users — has helped microfinance organizations earn the trust of clients worldwide, regardless of jurisdictions.
Transparency is the main marketing slogan of all crypto projects. This principle is at the core of the cryptocurrency and blockchain protocol boom.
How to Create Your Own ERC20 Token
We won't discuss the reasons for creating your own token. If you're here — you already know why and what for. Instead, let's get to work! ERC20 is a smart contract on the Ethereum blockchain. This means we need to understand what a smart contract and blockchain development are in general: let's first go over the basic terms, tools, and technologies.
How Smart Contracts Work
The bright, new, and mysterious term 'smart contract' is just marketing. In fact, it’s no different from an ordinary program, except for how the instructions are executed and stored.
A smart contract is a high-level program that allows creating, reading, and writing information about something. The executable bytecode is permanently recorded on the blockchain.
It is executed using the Ethereum Virtual Machine (EVM).
The blockchain is also used as the data storage.
Several programming languages are used to describe instructions, including: Solidity (similar to C or JavaScript), Vyper and Serpent (similar to Python), LLL (low-level version of Lisp), and Mutan.
The Solidity Programming Language
Solidity is an object-oriented programming language for self-executing contracts on the Ethereum platform.
It is statically typed, and its syntax is inspired by C-like languages, specifically JavaScript. The developers designed it to help web programmers quickly adopt it. The programs are translated into EVM bytecode.
Example of a program in the language
// SPDX-License-Identifier: MIT pragma solidity 0.8.13; contract HelloWorld { function sayHelloWorld() public pure returns (string memory) { return "Hello World"; } }
This smart contract has a method 'sayHelloWorld', which would return the string 'Hello World' when called.
Development Tools
There are many tools for convenient development, testing, debugging, and publishing. Today, we'll talk about Remix https://remix.ethereum.org— the official IDE from Ethereum. It's a great option for beginner developers and/or designing simple smart contracts.
Next, we need to look at existing libraries, so we don’t reinvent the wheel. For example, OpenZeppelin offers a huge amount of tested and optimized code for all major ERC standards.
Writing Code
So, the structure of your project looks as follows:
Create a file in the /contracts directory and name it MyToken.sol
// SPDX-License-Identifier: MIT pragma solidity ^0.8.4; contract MyToken { }
In the first line, we specify the license. In this case, it's MIT. You can read more about licenses here https://spdx.org/licenses.
The next line tells the compiler which version of Solidity the code should be compiled under. In this example, it’s version 0.8.4 or higher.
Blog.articles.article4.sections.section8.p5
An ERC20 contract specification must have several methods and events, which we will discuss next.
event Transfer(address indexed from, address indexed to, uint256 value)
An event is emitted when tokens are successfully transferred from one account to another
event Approval(address indexed owner, address indexed spender, uint256 value)
An event is emitted when the 'approve' method is called, which allows a specific address to perform operations on behalf of the approver for a certain amount of tokens
function totalSupply() external view returns (uint256)
The method should return the total number of tokens in circulation
unction balanceOf(address account) external view returns (uint256)
The method should return the balance of the account address
function transfer(address to, uint256 amount) external returns (bool)
The method transfers the specified amount of tokens to the address 'to'. The tokens are deducted from the caller’s balance
function allowance(address owner, address spender) external view returns (uint256)
The method returns the allowed number of tokens for managing the spender by the owner. This can be described legally as a power of attorney for making transactions on behalf of the owner to the spender
function approve(address spender, uint256 amount) external returns (bool)
The method allows the caller to give the spender address permission to make transactions with the specified amount of tokens
function transferFrom(address from, address to, uint256 amount) external returns (bool)
The method transfers the specified amount of tokens from the address 'from' to the address 'to'. The allowed amount for the calling address must be less than or equal to the amount
You can study the interface and implementation in more detail via the link
Since we are using a library with pre-written implementation, we don’t need to write the methods ourselves. We just need to import the contracts (classes).
// SPDX-License-Identifier: MIT pragma solidity ^0.8.4; import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; contract MyToken { }
Since we are using a library with pre-written implementation, we don’t need to write the methods ourselves. We just need to import the contracts (classes).
- After that, we just extend our MyToken class with the imported ERC20 contract. Then we pass parameters to the parent constructor to assign the name, symbol, decimals, and total supply.
- Name: MyToken
- Symbol: MTN
- Decimals: 8
// SPDX-License-Identifier: MIT pragma solidity ^0.8.4; import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; contract MyToken is ERC20 { constructor() ERC20("MyToken", "MTN") { _mint(msg.sender, 1000000 * (10 ** decimals())); } }
You won’t believe it, but that’s all! Of course, this is the most standard token without any custom logic, but it's enough to start with! Now, right-click on the file and choose 'Compile'.
What to do next?
Deploying the Smart Contract
Or, as it’s also called — deployment. For now, it's just the business logic described in code. We need to compile the code into a format understandable by the EVM (Ethereum Virtual Machine) and send the bytecode to the blockchain, where it will remain permanently.
Go to the 'DEPLOY & RUN TRANSACTIONS' section, as shown in the screenshot.
In the 'Environment' tab, select 'Remix VM (London)', which is a test EVM. You can select any suitable account. In the 'CONTRACT' tab, select the compiled contract — in this case, MyToken.
Click the 'Deploy' button. Your contract is now in the test VM! MyToken should appear in the 'Deployed Contracts' list.