Skip to content Skip to sidebar Skip to footer

How To Create ERC 20 Token on the Ethereum Network?

Ethereum is a blockchain which has digital currency known as Ether (ETH). But all the transactions of the coins are stored in a ledger. Ethereum is very flexible as you also transfer the data through the blockchain and you have to pay the price to Ether. All this transactions in the network are verified by the nodes. And node solves the hard computation and then it is awarded with ETH. Ethereum has Smart Contracts which contains the code and it is executed in the Ethereum blockchain. These Smart contracts are written in Solidity language and it is compiled into an ABI code.

Smart Contracts in Ethereum can create token known as ERC (Ethereum Request for Comment). ERC 20 token can be sent and received and are fungible tokens. The interface of the ERC 20 has methods and events. The body of ERC 20 consists of following methods which is in Solidity:

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)

This means:

name: name of the token

symbol: symbol of the token

decimals: number of decimals which the token uses

totalSupply: total number which is supplied to the token

balanceOf: balance of an account

transfer: transfers the amount of tokens to an address

transferFrom:  transfers the amount of tokens from one address to a recipient address

approve: the amount of token which is withdraw from the owner’s address

allowance: number of tokens which is withdrawable from the owner’s account

ERC 20 Token has events:

event Transfer(address indexed _from, address indexed _to, uint256 _value)

event Approval(address indexed _owner, address indexed _spender, uint256 _value)

Transfer: when the tokens are transferred

Approval: If the account is approved to collect amount of tokens

In this blog, you will read the method to create ERC 20 Token.

Method To Create ERC 20 Token:

  1. First, you should set the license identifier and also the version of Solidity in which the code was written. Then, declare the contract just by using the contract keyword and then you have to give it the name WMTCoinERC20.

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

contract WMTCoinERC20 {}

  1. Declare the Events:

After this, you should declare the events Transfer and Approval in your contract:

event Transfer(address indexed from, address indexed to, uint tokens);

event Approval(address indexed tokenOwner, address indexed spender, uint tokens);

  1. Set the Name, Symbol and Decimal of Token:

Now, you should set the name of your token, its symbol and also the decimal to use:

string public constant name = “WMT Coin”;

string public constant symbol = “WMT”;

uint8 public constant decimals = 18;

The token name is WMT Coin and the symbol is WMT. Here, you should set the decimals to be 18.

  1. Set Balances and Allowances Mapping:

Then, you should set two mappings:

mapping(address => uint256) balances;

mapping(address => mapping (address => uint256)) allowed;

Mapping in Solidity is basically a key-value pair. In balances, address is the key and the uint256 is the value. And address type is 160-bit value which is used for storing addresses of contracts. Allowed mapping is also a key-value pair, which allows you to store the number of tokens which you can transfer to a recipient.

  1. Declare Total Supply:

After this, you should declare the stored number of tokens which are available in your contract.

uint256 totalSupply_;

  1. Set Amount of Total Supply and Balances:

Then, you have the constructor – the total number of tokens which you want to be in your contract. After this, total is set to totalSupply_and then the balance of address is set to the total tokens. And the msg.sender is the Ethereum account which is presently executing contract function.

constructor(uint256 total) {

totalSupply_ = total;

balances[msg.sender] = totalSupply_;

}

  1. Balance of an Owner:

function balanceOf(address tokenOwner) public view returns (uint) {

return balances[tokenOwner];

}

TokenOwner is the address of the token owner to whom you want to return the balance of the token in the contract.

  1. Transfer tokens to Account:

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;

}

Receiver is the address of the account which will receive tokens

numTokens is the number of tokens which will be sent to the receiver account.

After this, the numTokens is subtracted from deployer’s balance and then credited to the receiver‘s balance. Then, a Transfer event is emitted. Lastly, the Boolean true is returned.

  1. Approve Token Transfer:

function approve(address delegate, uint numTokens) public returns (bool) {

allowed[msg.sender][delegate] = numTokens;

emit Approval(msg.sender, delegate, numTokens);

return true;

}

delegate is basically, the address that you want to set the number of tokens which the deployer can send to it

numTokens is generally, the number of tokens which the deployer can send to the delegate

Here in the body, you have to reference the delegate map in the allowed mapping should be set the number of tokens to it. Finally, emit the Approval event and then return true.

  1. Allowance Status of an Account:

function allowance(address owner, address delegate) public view returns (uint) {

return allowed[owner][delegate];

}

In this method, there is an owner and delegate. owner is the address of the owner to return the number of tokens which is transferable to the recipient in the delegate.

  1. Transfer Tokens from Account to Another Account:

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;

}

transferFrom has owner, buyer and numTokens.

owner is the address of the balances that will transfer the numTokens buyer is the address in the balances which will credit the numTokens numTokens is the number of tokens which is transferred from owner to buyer

In the body, you should check if the balance in the owner is enough and if the owner is approved to send the amount of tokens to the buyer.

Here, you will made the transfer by subtracting the number of tokens from the owner’s balance and from the allowed balance. After this, the number of tokens is added to the buyer’s balance. Lastly, the Transfer event is emitted and then the boolean true is returned.

  1. Deploy Token on the Ethereum Testnet:

You can deploy your token to the Ropsten test network and then compile the smart contract on Remix. Here, you can use MetaMask in order to create a wallet on the Ethereum test network. For this, you should install the MetaMask extension in your desktop browser. Then, you should Create an account. After this, you should go to Remix and then create a new .sol file. Just name it wmt_coin.sol and then paste your smart contract. Here, you can get free ETH from Ropsten Test network and then copy the account address from MetaMask, and just paste it on the https://faucet.ropsten.be page and than tap on the Send me test Ether. At this point, you will view 0.3 ETH in your MetaMask ETH account. Here, you can deploy your smart contract to the Ropsten test network. Just go back to the Remix dashboard and then tap on the Ethereum logo in the dashboard. It will load a page where you can deploy and just run transactions. Then, in the left sidebar, wmt_coin.sol contract is selected and then the account in your MetaMask is set as the deploying address. Next in the ENVIRONMENT, you will see JavaScript VM (London) is selected. Then, change your deploying network will be our Ropsten network. Just tap on the dropdown and choose Injected Web3. At this point, enter the original amount of tokens which will be traded in your token. You should enter 100 and tap on Deploy button. It will open MetaMask extension and then it will ask to confirm the pending transaction. Just hit on the Confirm button on the MetaMask popup. It will deploy the smart contract on the Ropsten test network. Then in Remix page, just scroll down in the left sidebar, you will view the names of methods in your smart contract. Here, you can check the balance of your deploying address and then it returned 100. Lastly, you have successfully created ERC-20 token in Ethereum network.

Through this process, you can create ERC 20 Token in the Ethereum Network. To learn about Ethereum network, subscribe to Cryptoowiki.com.

Email

Email

Cryptoowiki © 2022. All rights reserved.

Cryptoowiki © 2022. All rights reserved.