ERC20 Token Creation Step by Step Guide

0xandyeth
CoinsBench
Published in
5 min readJun 4, 2021

--

Summary

After the Ethereum’s Co-Founder Viktalik Buterin launched the Ethereum Network and Ethereum smart contract In 2014, and The ERC-20(Ethereum Request fo Comments 20) was proposed by Fabian Vogelsteller who is one of early Ethereum developers In 2015. An ERC20 token is a standard used for creating and issuing smart contracts on the Ethereum blockchain.So all tokens which can create and issue on Ethereum blockcain can be inherited from ERC20 standard.Finally , we can exchange our tokens each other on the exchange sites which is supporting Ethereum blockchain platform such as Uniswap,Sushiswap. Some new developers are not understanding ERC20 standard conception deeply because they prefer to train to import various libraries such as openzeppelin. But in my opinion, we have to know the basic conception correctly so that we can use it skillfully as we wish.

ERC20 (Ethereum Request for Comments 20) Token

  • Conception
  • Writing an ERC20 token in Solidity
  • Simple Example

1.Conception

Token is a fuel in the decentralized network.We can not think the decentralization without the token.An ERC20 token is a standard used for creating and issuing smart contracts on the Ethereum blockchain.In other hand , we can think we are printing a regular money by the special regulation in the Ethereum kingdom. It also will be the standard used for sharing each other.

2.Overview an ERC20 token in Solidity

pragma solidity >=0.6.0 <0.8.0;
/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20{
// code implementation inside here}/*
* @dev Provides information about the current execution context, * including the sender of the transaction and its data.
*/
contract Context {// code implementation inside here}/*
* @dev Implements the ERC20 standard token contract
*/
contract ERC20 is Context,IER20{ // code implementation inside here}

Ethereum blockchain platform is being managed by Ethereum community and the community made EIP(Ethereum Improvement Proposal) for adding more features in the future.The Context is the current execution one to provide information including the sender of the transaction and it’s data.

1).IERC20 contract as defined in EIP

interface IERC20 {function totalSupply() external view returns(uint256);function balanceOf(address account) external view returns(uint256);function transfer(address receiver,uint256 amount) external view returns(bool);function allowance(address owner, address spender) external view returns (uint256);function approve(address spender, uint256 amount) external returns (bool);function transferFrom(address sender, address receiver, uint256 amount) external returns (bool);event Transfer(address indexed from, address indexed to, uint256 value);event Approval(address indexed owner, address indexed spender, uint256 value);}
  • totalSupply function: Returns the amount of token in existence .
  • balanceOf function : Returns the amount of tokens owned by user.
  • transfer function: Moves amount tokens from sender’s account to receiver.
  • allowance function: Returns the remaining number of tokens that spender will be allowed to spend on behalf of owner through {transferFrom}.This is zero by default.
  • approve function:Sets amount as the allowance of spender over the sender’s tokens.
  • transferFrom function:Moves amount tokens from sender to receiver using the allowance mechanism.

The totalSupply ,transfer and balanceOf functions can be used with sender’s call directly and the allowance,approve and transferFrom functions can be used with other contract.

2). Context contract

contract Context{function _msgSender() internal view virtual returns (address payable) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes memory) {
return msg.data;
}
}
  • msg.sender: current sender who is interacting with the smart contract.
  • msg.data: current sender’s data

3). ERC20 standard contract

library safeMath{function add(uint256 a, uint256 b) internal pure returns (uint256) {

uint256 c = a + b;

require(c >= a, "SafeMath: addition overflow");

return c;
}
function sub(uint256 a, uint256 b) internal pure returns (uint256) {

return _sub(a, b, "SafeMath: subtraction overflow");
}
function _sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {

require(b <= a, errorMessage);

uint256 c = a - b;
return c;
}
}contract ERC20 is Context,IERC20{using safeMath for uint256;mapping(address => uint256) private _balances;mapping(address => mapping(address => uint256) private _allowances;uint256 private _totalSupply;uint256 private _name;
uint256 private _symbol;
uint256 private _decimal;
constructor (string memory name_, string memory symbol_) public {
_name = name_;
_symbol = symbol_;
_decimals = 18;
}
function name() public view returns (string memory) {
return _name;
}
function symbol() public view returns (string memory) {
return _symbol;
}
function decimals() public view returns (uint8) {
return _decimals;
}
.....*** transfer,allowance,totalSupply,approve and transferFrom functions will be overriede here from IERC20 interface***...../*
* @dev Creates amount tokens and assigns them to account,increasing *the totalSupply.
*
*/
function _mint(address account,uint256 amount) internal {

require(account != address(0), "ERC20: mint to the zero address");
_beforeTokenTransfer(address(0), account, amount);_totalSupply = _totalSupply.add(amount);_balances[account] = _balances[account].add(amount);emit Transfer(address(0), account, amount);}/*
* @dev Destroys amount tokens from account ,reducing the *totalSupply
*
*/
function _burn(address account, uint256 amount) internal virtual {require(account != address(0), "ERC20: burn from the zero address");_beforeTokenTransfer(account, address(0), amount);_balances[account] = _balances[account].sub(amount, "ERC20: burn amount exceeds balance");_totalSupply = _totalSupply.sub(amount);

emit Transfer(account, address(0), amount);
}function _beforeTokenTransfer(address from, address to, uint256 amount) internal { }}

Ethereum’s totalSupply has unlimited supply. In other hand, it is Inflationary. So we need to burn the tokens to keep high-price value in marketing .

Here are the functions we are going to focus:

  • _mint function: Create the amount tokens from creator.
  • _burn function: Destroy the amount tokens from creator.

3.Simple Example

We are going to create our own token by ERC20 standard now. Let’s say we have previous defined totalSupply. So we will have the limited supply to 200M. Our token contract will be inherited from the ERC20 contract.

contract SimpleToken is ERC20{constructor(string memory _name,string memory _symbol,uint256 _initSupply) ERC20(_name,_symbol) public{   _mint(msg.sender,_initSupply);
}
}

When we deploy the SimpleToken contract , constructor function will call initially. We will pass _name,_symbol and _initSupply parameters at that time.You can give name and symbol whatever you wish but those don’t might repeat on Ethereum platform. Those has unique name and symbol.

So we minted 200 M now and 200 M will be in the creator’s wallet. We maybe burn some tokens to incentive the marketing and to keep the high-price value of token.

Conclusion

Ethereum blockchain platform has powerful innovation to be driven Defi space and Crypto technology. Also we are excited to create our own ERC20 tokens quickly and exchange them each other .You can create meme coins with ERC20 standard tokens too.I did mentioned the main functionalities of ERC20 standard token in detail above cover letters. This article will help the beginners who are learning ERC20 smart contracts. Thanks for reading my article in the end.Cheer Up!!😀😀😀

--

--