Erc20 interpretation

Confucian 2022-04-04 22:25:27 阅读数:47

erc20 erc interpretation

ERC20 Reading

Reference resources OpenZepplin file and Ethereum official developer documentation , Combine your own understanding .

What is? ERC20

ERC20(Ethereum Request for Comments 20) A token standard . It has some typical functions .

ERC20 Token contract tracking can replace tokens : Any token is exactly the same as any other token ; No token has any special rights or actions related to it . This makes ERC20 Tokens can be used to exchange money 、 Right to vote 、 Pledge and other media .

Why abide by ERC20

All applications on Ethereum support... By default ERC20 , If you want to issue your own money , Then your code must follow ERC20 standard , Such Wallet ( Such as MetaMask) Wait for the app to display your currency .

Code implementation

You need to implement the following functions and events :

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)event Transfer(address indexed _from, address indexed _to, uint256 _value)event Approval(address indexed _owner, address indexed _spender, uint256 _value)

Use OpenZeppllin The library provided can be built easily and quickly ERC20 Token .

Fast build

This is a GLD token .

// contracts/GLDToken.sol// SPDX-License-Identifier: MITpragma solidity ^0.8.0;import "@openzeppelin/contracts/token/ERC20/ERC20.sol";contract GLDToken is ERC20 { constructor(uint256 initialSupply) ERC20("Gold", "GLD") { _mint(msg.sender, initialSupply); }}

Usually , We define the issue of tokens and the name and symbol of tokens .

IERC20

Let's start with ERC20 The interface of (IERC20), This makes it easy for us to directly define ERC20 Tokens, .

similarly ,OpenZepplin It provides us with the corresponding library , Easy for developers to import and use .

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

EIP As defined in ERC20 Standard interface :

pragma solidity ^0.8.0;interface IERC20 { event Transfer(address indexed from, address indexed to, uint256 value); event Approval(address indexed owner, address indexed spender, uint256 value); function totalSupply() external view returns (uint256); function balanceOf(address account) external view returns (uint256); function transfer(address to, uint256 amount) external returns (bool); function allowance(address owner, address spender) external view returns (uint256); function approve(address spender, uint256 amount) external returns (bool); function transferFrom( address from, address to, uint256 amount ) external returns (bool);}

Analyze one by one

function :

  • totalSupply() : Returns the total number of tokens .
  • balanceOf(address account) : return account The number of tokens owned by the address .
  • transfer(address to, uint256 amount) : take amount Number of tokens sent to to Address , Returns a boolean indicating whether the execution was successful . Trigger Transfer event .
  • allowance(address owner, address spender) : Returns the authorized consumer spender adopt transferFrom Represents the amount of remaining tokens spent by the owner . Zero by default . When approve and transferFrom When called , The value will change .
  • approve(address spender, uint256 amount) : to grant authorization spender It can cost amount The number of tokens , Returns a boolean indicating whether the execution was successful . Trigger Approval event .
  • transferFrom(address from, address to, uint256 amount) : take amount The number of tokens from from Address to to Address , Returns a boolean indicating whether the execution was successful . Trigger Transfer event .

event ( In the definition indexed Easy to find and filter ):

  • Transfer(address from, address to, uint256 value) : Triggered when contemporary coins are transferred from one address to another . Be careful : The transferred value may be 0 .
  • Approval(address owner, address spender, uint256 value) : Trigger when the contemporary coin owner authorizes others to use the token , That is to call approve Method .

Metadata

Generally, in addition to the above functions that must be implemented , There are other ways :

  • name() : Return token name
  • symbol() : Returns the token symbol
  • decimals() Returns the number of digits after the decimal point of the token

ERC20

Take a look ERC20 How exactly is the token written .

Again ,OpenZepplin Provide ready-made contract code :

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

Here's a GitHub Source link OpenZepplin ERC20

Function Overview

constructor(name_, symbol_)name()symbol()decimals()totalSupply()balanceOf(account)transfer(to, amount)allowance(owner, spender)approve(spender, amount)transferFrom(from, to, amount)increaseAllowance(spender, addedValue)decreaseAllowance(spender, subtractedValue)_transfer(from, to, amount)_mint(account, amount)_burn(account, amount)_approve(owner, spender, amount)_spendAllowance(owner, spender, amount)_beforeTokenTransfer(from, to, amount)_afterTokenTransfer(from, to, amount)

event ( Same as IERC20)

Transfer(from, to, value)Approval(owner, spender, value)

Analyze one by one

  • constructor(string name, string symbol) : Set the name and symbol of the token .decimals The default is 18 , To change to a different value, you should reload it . These two values are constant , Assign values only once during construction .
  • name() : Returns the name of the token .
  • symbol() : Returns the symbol of the token , Usually an abbreviation of a name .
  • decimals() : Returns the number of digits after the decimal point , Usually 18 , imitation Ether and wei . To change, rewrite it .

totalSupply()、balanceOf(address account)、transfer(address to, uint256 amount)、 allowance(address owner, address spender)、approve(address spender, uint256 amount)、transferFrom(address from, address to, uint256 amount) All references IERC20 .

  • increaseAllowance(address spender, uint256 addedValue) : Increase in the form of atoms spender quota . Returns a boolean indicating whether the execution was successful , Trigger Approval event .
  • _transfer(address from, address to, uint256 amount) : Transfer accounts . This internal function is equivalent to transfer , It can be used, for example, to implement automatic token fees , Reduction mechanism, etc . Trigger Transfer event .
  • _mint(address account, uint256 amount) : Casting amount Amount of tokens to account Address , Increase total circulation . Trigger Transfer event , The parameter from It's zero address .
  • _burn(address account, uint256 amount) : from account Burned in the address amount The number of tokens , Reduce total circulation . Trigger Transfer event , The parameter to It's zero address .
  • _approve(address owner, uint256 spender, uint256 amount) : Set allow spender cost owner Number of tokens . This internal function is equivalent to approve , It can be used, for example, to set automatic quota for some subsystems .
  • spendAllowance(address owner, address spender, uint256 amount) : cost amount In quantity owner to grant authorization spender Token of . In infinity allowance Without updating allowance amount of money . If there is not enough margin , Then recover . Could trigger Approval event .
  • _beforeTokenTransfer(address from, address to, uint256 amount) : Before any token transfer Hook . It includes coinage and burning . Call conditions :

    • When from and to When they are not zero addresses ,from in the hand amount The number of tokens will be sent to to .
    • When from When it's zero address , Will give to Casting amount The number of tokens .
    • When to When it's zero address ,from in the hand amount The number of tokens will be burned .
    • from and to Cannot be both zero addresses .
  • _afterTokenTransfer(address from, address to, uint256 amount) : After any token transfer Hook . It includes coinage and burning . Call conditions :

    • When from and to When they are not zero addresses ,from in the hand amount The number of tokens will be sent to to .
    • When from When it's zero address , Will give to Casting amount The number of tokens .
    • When to When it's zero address ,from in the hand amount The number of tokens will be burned .
    • from and to Cannot be both zero addresses .

Summary

ERC20 In code _transfer_mint_burn_approve_spendAllowance_beforeTokenTransfer_afterTokenTransfer All are internal function ( Others are public ), That is, they can only be called by derived contracts .

summary

ERC20 In fact, it is one of the most common token standards , It defines the classic functions of tokens and standardizes what developers write token Code for , So as to facilitate the adaptation of various applications .

版权声明:本文为[Confucian]所创,转载请带上原文链接,感谢。 https://netfreeman.com/2022/04/202204042220157629.html