How to create and implement ERC-20 tokens on the Ethereum blockchain
How to create and implement ERC-20 tokens on the Ethereum blockchain. At the current rate of development, the blockchain is gradually conquering the world. Bitcoin, the most popular cryptocurrency, is the product of the world’s first and best-known blockchain technology. Ethereum is a by-product of the blockchain and was born when Bitcoin began to thrive.
While Bitcoin has only moved towards a currency, Ethereum has gone even further with its vision of providing virtual machines (EVMs) and smart contracts that allow users to create tokens on the network.
Tokens can represent money, gold, lottery tickets … you can develop all types of tokens on the Ethereum blockchain, but this tutorial will focus on creating and deploying ERC-20 tokens.
Before creating a token, you must have at least a basic understanding of blockchain technology, solidity language, and how Ethereum works.
What is blockchain and method How does it work?
In short, blockchain is a record of transactions in a distributed ledger or database for many network participants. This ledger contains a record of the transactions carried out on the network.
A transaction is the act of transferring a specific currency or amount from one user to another on the network. Suppose Alice transmits 30BLC to Bob. This transaction is cryptographically hashed by a specific node on the network and recorded in the general ledger.
This node sends the transaction to other nodes in the network, ie it propagates the transaction to the network. Other nodes receive the transaction, review it using the standard method, and then add it to the ledger.
The nodes on the network receive the newly transmitted transaction on the network and then add the transaction to the ledger. Every node on the network owns or owns a copy of the ledger. This results in the decentralized character of the blockchain.
The word “blockchain” is derived from the fact that transactions or records within a ledger are linked to form a chain. As we all know, the transaction represents the currency exchange between two parties in the node, which can be represented in JSON as follows:
{
"to": "0xalice",
"from": "0xbob",
"amount": "30BLC"
}
This is a simple record or transaction. Bob transferred 30BLC to Alice.
This transaction is recorded in a block written using the following JSON data format:
[
{
"to": "0xalice",
"from": "0xbob",
"amount": "30BLC"
}
]
A block is like a sequence that contains many transaction objects. This block can therefore contain several transactions:
[
{
"to": "0xalice",
"from": "0xbob",
"amount": "30BLC"
},
{
"to": "0xtheresa",
"from": "0xarinze",
"amount": "5BLC"
}
]
You can see the location of the block that the transaction will be added to. Chains are made up of interconnected blocks. Each blockchain starts with a Genesis block, which the creator adds and distributes to the network.
Each block also has a cryptographic hash that acts as a unique identifier on the network. No two blocks have the same hash.
When a block is verified by nodes and added to the network, it has a pointer to the hash of the last block on the network.
[
{
"hash": "0x0",
"prevHash": "",
"txns": [
{
"to": "0xalice",
"from": "0xbob",
"amount": "30BLC"
},
{
"hash": "0x1",
"prevHash": "0x0",
"to": "0xtheresa",
"from": "0xarinze",
"amount": "5BLC"
}
]
},
{
"hash": "0x1",
"prevHash": "0x0",
"txns": [
{
"to": "0xalice",
"from": "0xbob",
"amount": "30BLC"
},
{
"to": "0xtheresa",
"from": "0xarinze",
"amount": "5BLC"
}
]
}
]
The first block with hash 0x0 is the original block. The next block with hash 0x1 has a prevHash that points to 0x0, i.e. to the first block in the network.
In this way, each new block that is inserted into the network points to the adjoining newest block first. This creates a “chain” in the blockchain.
What is ethereum
Ethereum is a blockchain with its own digital currency called ETH. Just like other blockchains, transactions are stored in a ledger.
What sets Ethereum apart from other blockchains is its flexibility. While many blockchain platforms only support money transfers, Ethereum allows all data to be transferred over the blockchain and fees to be paid in ETH.
How does Ethereum work?
As mentioned above, we can transfer any data in the Ethereum blockchain and pay fees in the ETH.
Just like the BLC transaction in the example above, the Ethereum blockchain supports ETH transactions. Suppose Alice transfers 1 ETH to Bob. This transaction is validated by the nodes in the network and added to the block in the blockchain.
In addition, Ethereum has mining, which requires work to get ETH. This task involves solving a difficult calculation by trying many different ways until you find the correct answer. Any node in the network can participate. A node that successfully solves the calculation is rewarded with a certain amount of ETH. The difficulty of the calculation increases the more transactions are reduced.
Whenever a transaction is triggered in the Ethereum blockchain, a mining node in the network mints this transaction. The sender of the transaction must agree to pay a certain ETH amount to this node. This is known as the gas price.
What is a smart contract?
A smart contract is a tool that contains code that runs on the Ethereum blockchain. Smart contracts are written in the Solidity language and compiled into ABI code. This ABI code is provided on the Ethereum blockchain. The smart contract takes the sender’s external address and mixes it with a nonce (unique number) to form an address on the Ethereum blockchain.
Smart contracts enable the creation of digital contracts. Like real contracts, digital contracts help set up a transaction between two or more parties within the Ethereum blockchain.
A smart contract is a type of account in Ethereum, which means that it is not controlled by the user and can send transactions on the blockchain. Since it is an account, the smart contract has a balance and contains the EVM code.
What are ERC-20 tokens?
The Ethereum Virtual Machine (EVM) is a virtual machine that runs compiled Solidity ABI code. Smart contracts in Ethereum have become the global standard for creating a range of tokens. These standards are known as the Ethereum Request for Comment (ERC) standard.
Ethereum has many default, but the most commonly used and widely used are ERC-20 and ERC-721. ERC-20 is used for token generation while ERC-721 is used for NFT development.
ERC-20 is the standard proposed by Fabian Vogelsteller, a smart contract that contains a number of APIs. ERC20 is a set of rules that applies to all tokens that choose the ERC-20 standard.
As mentioned above, ERC-20 can be used to create virtual currencies like Bitcoin and ETH. Some of the most famous tokens created according to the ERC-20 standard are Binance Coin (BNB) and Shiba Shabu (KOBE).
Users can send and receive ERC-20 tokens. These tokens are fungible, which means that their value is the same everywhere on the blockchain.
follow Blockchain.com, Wallets and exchanges use this standard to integrate various ERC-20 tokens into the platform and to facilitate their exchange and many other cryptocurrencies.
Now that we understand what the ERC-20 standard is, let’s take a look at the contents of the ERC-20 token.
Content of the token ERC-20
The ERC-20 token contains the methods and events that an ERC-20 token must have.
ERC-20 tokens must be able to:
- Transfer tokens from one account to another
- Return the account balance
- Returns the total number of tokens available in tokens
- Transfer tokens to your account
In fact, if it were written in Solidity, the ERC-20 would look like this:
function name() public view returns (string)
function symbol() public view returns (string)
function decimals() public view returns (uint8)
function totalSupply() public view returns (uint256)
function balanceOf(address _owner) public view returns (uint256 balance)
function transfer(address _to, uint256 _value) public returns (bool success)
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success)
function approve(address _spender, uint256 _value) public returns (bool success)
function allowance(address _owner, address _spender) public view returns (uint256 remaining)
An ERC-20 token can have the following methods:
- name – returns the name of the token (e.g. Binance Coin)
- symbol – returns the symbol of the token (e.g. BNB)
- Decimal Places – Returns the number of decimal places used by the token
- totalSupply – returns the entire initial stock of the token
- balanceOf – returns the account balance
- transfer – transfer a certain amount of tokens to an address
- transferFrom – transfers a certain number of tokens from the recipient address to the recipient address
- approve – withdraw tokens up to a certain number of tokens from the owner’s address
- Exemption – returns the amount of tokens that can be withdrawn from the owner’s account
Events can also be registered on the token in order to be captured in time when a signal is sent. ERC-20 tokens have the following events:
Event transfer (address indexed _from, address indexed _to, uint256 _value)
Event approval (address indexed _owner, address indexed _spender, uint256 _value)
- Transfer – activated when transferring tokens
- Approval – activated when the account has been approved to receive a certain number of tokens
Generate ERC 20 token
To make it easier to understand in this section, the article shows how to write a simple token and call it an ND Coin.
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract NDCoinERC20 {
event Transfer(address indexed from, address indexed to, uint tokens);
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
string public constant name = "ND Coin";
string public constant symbol = "NDN";
uint8 public constant decimals = 18;
mapping(address => uint256) balances;
mapping(address => mapping (address => uint256)) allowed;
uint256 totalSupply_;
constructor(uint256 total) {
totalSupply_ = total;
balances[msg.sender] = totalSupply_;
}
function totalSupply() public view returns (uint256) {
return totalSupply_;
}
function balanceOf(address tokenOwner) public view returns (uint) {
return balances[tokenOwner];
}
function transfer(address receiver, uint numTokens) public returns (bool) {
require(numTokens <= balances[msg.sender]);
balances[msg.sender] -= numTokens;
balances[receiver] += numTokens;
emit Transfer(msg.sender, receiver, numTokens);
return true;
}
function approve(address delegate, uint numTokens) public returns (bool) {
allowed\[msg.sender\][delegate] = numTokens;
emit Approval(msg.sender, delegate, numTokens);
return true;
}
function allowance(address owner, address delegate) public view returns (uint) {
return allowed\[owner\][delegate];
}
function transferFrom(address owner, address buyer, uint numTokens) public returns (bool) {
require(numTokens <= balances[owner]);
require(numTokens <= allowed\[owner\][msg.sender]);
balances[owner] -= numTokens;
allowed\[owner\][msg.sender] -= numTokens;
balances[buyer] += numTokens;
emit Transfer(owner, buyer, numTokens);
return true;
}
}