Introduction
The ERC-20 introduces a standard for Fungible Tokens, in other words they have a property that makes each Token be exactly the same (in type and value) as another Token.
For example, an ERC-20 Token acts just like ETH, meaning that 1 Token is and will always be equal to all the other Tokens.
- Example functionalities ERC-20 provide are to:
- transfer tokens from one account to another
- get the current token balance of an account
- get the total supply of the token available on the network
- approve whether an amount of token from an account can be spent by a third-party account
ERC20 Contracts
ERC20 standard interface has following functions and events:
- Total Supply: The total number of tokens that will ever be issued
- Balance Of: The account balance of a token owner's account
- Transfer: Automatically executes transfers of a specified number of tokens to a specified address for transactions using the token
- Transfer From: Automatically executes transfers of a specified number of tokens from a specified address using the token
- Approve: Allows a spender to withdraw a set number of tokens from a specified account, up to a specific amount
- Allowance: Returns a set number of tokens from a spender to the owner
- Transfer: An event triggered when a transfer is successful (an event)
- Approval: A log of an approved event (an event)
Creating Custom Tokens
Creating an ERC20 token is quite simple since templates for all different types of tokens are available on the OpenZeppelin allowing us to create tokens effortlessly.
In order to create an ERC20 token, you need the following:
- The Token’s Name
- The Token’s Symbol
- The Token’s Decimal Places
- The Total Number of Tokens in Circulation
Please set the below variables in your contract with relevant values
string public constant name;
string public constant symbol;
uint8 public constant decimals;
uint256 totalSupply_;
constructor(uint256 total) public {
totalSupply_ = total;
balances[msg.sender] = _totalSupply;
}
Let us extend the standard ERC20 contract from the Openzeppelin library to create our own custom token. You can customize your token by giving its name, symbol, decimals, etc.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v4.0.0/contracts/token/ERC20/ERC20.sol";
contract Mtoken is ERC20 {
constructor(string memory name, string memory symbol) ERC20(name, symbol) {
// Mint 100 tokens to msg.sender
// Similar to how
// 1 dollar = 100 cents
// 1 token = 1 * (10 ** decimals)
_mint(msg.sender, 100 * 10**uint(decimals()));
}
}
Comments
Post a Comment