In this tutorial, we are going to get familiar with the methods and events of the ERC-20 token. In add-on to that, nosotros want to create an ERC-20 token on the Rinkeby Testnet. By writing the smart contract, the deployment python file, helpful_scripts.py, and other files that help the creation and deployment of the ERC-20 token smart contract. This tutorial volition go on in the next manufactures where nosotros volition explain the ERC-20 smart contract deeper and provide guidelines to add the created tokens to your Metamask wallet.
What Is ERC-xx?
The ERC-twenty is a token standard that implements an API for tokens within smart contracts. The acronym stands for Ethereum Request for Comments twenty. The ERC-20 proposes a standard for fungible tokens (every bit opposed to Non-Fungible Tokens or NFTs that use ERC- 721) meaning that they have a property that makes each of them the aforementioned. This standard provides the post-obit functionalities:
-
1. Transfering tokens from one account to another.
-
2. Tetting the balance of an business relationship.
-
3. Getting the total supply of the token available on the network.
-
4. Approving whether a tertiary party can spend tokens from an account.
Methods and Events of an ERC-xx Token
If a smart contract implements the events and methods of ERC-xx, it can exist chosen an ERC-xx token contract.
Methods:
function name() public view returns (cord) function symbol() public view returns (string) function decimals() public view returns (uint8) function totalSupply() public view returns (uint256) role balanceOf(address _owner) public view returns (uint256 balance) office transfer(address _to, uint256 _value) public returns (bool success) function transferFrom(accost _from, address _to, uint256 _value) public returns (bool success) part approve(address _spender, uint256 _value) public returns (bool success) function allowance(address _owner, accost _spender) public view returns (uint256 remaining)
Events:
event Transfer(accost indexed _from, address indexed _to, uint256 _value) event Approval(address indexed _owner, address indexed _spender, uint256 _value)
Getting Started with the ERC-20 Token in Brownie
At present, we begin our projection past typing in the terminal:
brownie init
And in our contracts binder, we create a file named OurToken.sol and paste the beneath contract we have copied from this link. You tin change the name of the token any y’all desire:
// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; contract OurToken is ERC20 { // wei constructor(uint256 initialSupply) ERC20("OurToken", "OT") { _mint(msg.sender, initialSupply); } }
We also create a deploy_token.py file in the scripts folder and paste the beneath code in it:
from brownie import accounts, config, TokenERC20, EasyToken from scripts.helpful_scripts import get_account initial_supply = 1000000000000000000000 # 1000 token_name = "Token" token_symbol = "TKN" def main(): account = get_account() erc20 = TokenERC20.deploy(initial_supply, token_name, token_symbol, {"from": account})
And helpful_scripts.py goes like this:
from brownie import network, accounts, config LOCAL_BLOCKCHAIN_ENVIRONMENTS = ["hardhat", "development", "mainnet-fork"] def get_account(): if network.show_active() in LOCAL_BLOCKCHAIN_ENVIRONMENTS: return accounts[0] if network.show_active() in config["networks"]: account = accounts.add(config["wallets"]["from_key"]) return business relationship return None
ERC-twenty Token Smart Contract
And finally this is the contract that contains the ERC-xx methods and events (TokenERC20.sol):
pragma solidity ^0.half dozen.0; interface tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes calldata _extraData) external; } contract TokenERC20 { // Public variables of the token string public name; string public symbol; uint8 public decimals = xviii; // 18 decimals is the strongly suggested default, avoid changing information technology uint256 public totalSupply; // This creates an array with all balances mapping (address => uint256) public balanceOf; mapping (address => mapping (address => uint256)) public allowance; // This generates a public event on the blockchain that will notify clients event Transfer(address indexed from, address indexed to, uint256 value); // This generates a public upshot on the blockchain that volition notify clients event Approving(address indexed _owner, address indexed _spender, uint256 _value); event Burn(address indexed from, uint256 value); constructor( uint256 initialSupply, string retention tokenName, string memory tokenSymbol ) public { totalSupply = initialSupply * 10 ** uint256(decimals); //Update total supply with the decimal amount balanceOf[msg.sender] = totalSupply; // Give the creator all initial tokens name = tokenName; // Ready the proper name for display purposes symbol = tokenSymbol; // Set the symbol for brandish purposes } function _transfer(address _from, address _to, uint _value) internal { // Forbid transfer to 0x0 accost. Utilise burn() instead crave(_to != accost(0x0)); // Check if the sender has enough require(balanceOf[_from] >= _value); // Cheque for overflows require(balanceOf[_to] + _value >= balanceOf[_to]); // Save this for an assertion in the time to come uint previousBalances = balanceOf[_from] + balanceOf[_to]; // Decrease from the sender balanceOf[_from] -= _value; // Add together the same to the recipient balanceOf[_to] += _value; emit Transfer(_from, _to, _value); // Asserts are used to use static assay to find bugs in your code. They should never //fail assert(balanceOf[_from] + balanceOf[_to] == previousBalances); } office transfer(accost _to, uint256 _value) public returns (bool success) { _transfer(msg.sender, _to, _value); return truthful; } function transferFrom(accost _from, accost _to, uint256 _value) public returns (bool success) { require(_value <= allowance[_from][msg.sender]); // Check allowance assart[_from][msg.sender] -= _value; _transfer(_from, _to, _value); render true; } office approve(address _spender, uint256 _value) public returns (bool success) { allowance[msg.sender][_spender] = _value; emit Approval(msg.sender, _spender, _value); render true; } role approveAndCall(address _spender, uint256 _value, bytes memory extraData) public returns (bool success) { tokenRecipient spender = tokenRecipient(_spender); if (approve(_spender, _value)) { spender.receiveApproval(msg.sender, _value, address(this), _extraData); render true; } } role fire(uint256 _value) public returns (bool success) { require(balanceOf[msg.sender] >= _value); // Cheque if the sender has enough balanceOf[msg.sender] -= _value; // Subtract from the sender totalSupply -= _value; // Updates totalSupply emit Burn(msg.sender, _value); render true; } function burnFrom(address _from, uint256 _value) public returns (bool success) { require(balanceOf[_from] >= _value); // Check if the targeted balance is enough require(_value <= assart[_from][msg.sender]); // Check allowance balanceOf[_from] -= _value; // Decrease from the targeted balance assart[_from][msg.sender] -= _value; // Subtract from the sender's allowance totalSupply -= _value; // Update totalSupply emit Burn(_from, _value); render truthful; } }
We are going to explicate the above contract deeper in the next article. But for at present, it is time to deploy the contract itself:
brownie run scripts/1_deploy_token.py
Upshot:
Brownie v1.18.1 - Python evolution framework for EthereumErc20BrownieProject is the active projection.Launching 'ganache-cli --chain.vmErrorsOnRPCResponse truthful --wallet.totalAccounts 10 --hardfork istanbul --miner.blockGasLimit 12000000 --wallet.mnemonic brownie --server.port 8545'...Running 'scripts/1_deploy_token.py::primary'... Transaction sent: 0xe677ec7a8e17a7e750bb65f908bf77c92b5bbd368d975f7c3b36131544c9cf02 Gas price: 0.0 gwei Gas limit: 12000000 Nonce: 0 TokenERC20.constructor confirmed Cake: ane Gas used: 670648 (5.59%) TokenERC20 deployed at: 0x3194cBDC3dbcd3E11a07892e7bA5c3394048Cc87Terminating local RPC client…
From the higher up results, we are going to use the address that the smart contract is deployed at the “TokenERC20” Which is 0x3194cBDC3dbcd3E11a07892e7bA5c3394048Cc87 in this case, and track in the Etherscan.
Final Give-and-take
In this commodity, we accept got familiar with the methods and events of an ERC-xx token. Besides, we have created a project containing the ERC-20 token smart contract, the deployment python files, and and so on to create a typical ERC-20 token with a sure number of tokens created in our Metamask wallet. In the tutorials, we will see how we can add these tokens in the Uniswap and too find them on the Etherscan.
Download this Commodity in PDF format
Care to Know Almost Metaverse?
In Arashtad, we are providing custom services on 3d developments such equally 3d websites, 3d models, metaverses and all 3d applications.
Arashtad Serivces
Drib us a bulletin and tell us nearly your ideas.
Tell Us What Yous Demand
Blockchain Development
Source: https://blog.arashtad.com/blockchain/ethereum/create-erc-20-token/