Ethereum--ERC standard

1. ERC20 standard

It is a standard token released on Ethereum. Anyone can issue their own ERC20 token, as long as it meets the ERC20 standard on Ethereum, that is, it implements the specified 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)

If the above functions and events are implemented, the corresponding tokens can be issued on Ethereum. Currently, there are many ERC20 tokens on Ethereum.

The ERC721 standard is a non-fungible token, and the corresponding standard released by NFT is similar to ERC20.

2. Problems with ERC20

However, the ERC20 annotation itself has many limitations. It is very difficult to add additional functions based on the ERC20 token == standard, and many times it cannot be completed within one transaction.
The main problems are:

  • If a token transfer is received, the ERC20 standard cannot record in the contract who sent the token.
  • The ERC20 standard does not have a transfer notification mechanism
  • When transferring ERC20 tokens, no additional information can be carried.

At this time, the emergence of the ERC777 standard better solved this problem.

3. ERC1820 – Universal registry contract

ERC1820 is compatible with ERC165. The ERC165 standard itself standardizes how to identify interfaces and how to detect whether a contract implements a certain standard interface.
The ERC1820 standard defines what functions smart contracts and ordinary user accounts can publish to the registry.
Anyone can query this registry to ask which address implements a given interface and the smart contract handles the implementation logic.
ERC1820 mainly implements two interfaces:

  • setInterfaceImplementer(address _addr, bytes32 _interfaceHash, address _implementer)
    is used to set which contract implements the interface (_interfaceHash interface name of keccak256) of the address (_addr) (_implementer).
  • getInterfaceImplementer(address _addr, bytes32 _interfaceHash) external view returns (address)
    This function is used to query which contract implements the interface of the address (_addr).

3.ERC777

The main advantage:

  1. ERC777 is compatible with ERC20. Based on ERC20, it defines **send(dest, value, data)** to transfer tokens. Additional parameters are used to carry other information. The send function will detect whether the holder and the recipient are The corresponding hook function is implemented. If it is implemented, the corresponding hook function is called.
    When ERC777 uses send to transfer, it will use the getInterfaceImplementer function of ERC1820 on the holder and recipient addresses to query whether there is a corresponding implementation contract.
    The interface and function names are predetermined in the ERC777 standard specification. If there is an implementation, the corresponding call will be made.
  2. Both contracts and ordinary addresses can control and refuse to send which tokens by registering the tokensToSend hook function (rejection of sending is achieved by reverting in the hook function tokensToSend).
  3. Both contracts and ordinary addresses can control and refuse to accept which tokens by registering the tokensReceived hook function (rejection is achieved by reverting in the hook function tokensReceived).
  4. tokensReceived can send tokens and notify the contract to accept tokens in one transaction through the hook function, unlike ERC20, which must be completed through two calls (approve/transferFrom).

3-1 Interface description and implementation conventions

ERC777 contracts must register the ERC777Token interface and ERC20Token interface through ERC1820.

3-2 Operator

ERC777 defines a new role of operator. An operator is an address that acts as a mobile token. Each address moves its own tokens intuitively, separating the concepts of holder and operator.

3-3Send tokens

ERC777 sends tokens using the following two methods:

send(address to, uint256 amount, bytes calldata data) external

function operatorSend(
    address from,
    address to,
    uint256 amount,
    bytes calldata data,
    bytes calldata operatorData
) external

operatorSend can carry the operator's information through the parameter operatorData. In addition to executing the balance addition and subtraction of the corresponding account and triggering events, sending tokens also has additional provisions:

  1. If the holder has registered the ERC777TokensSender implementation interface through ERC1820, the token contract must call its tokensToSend hook function.
  2. If the receiver has registered the ERC777TokensRecipient implementation interface through ERC1820, the token contract must call its tokensReceived hook function.
  3. If there is a tokensToSend hook function, it must be called before modifying the balance status.
  4. If there is a tokensReceived hook function, it must be called after modifying the balance status.

ERC777TokensSender

For all ERC777 contracts, a holder address can only register one ERC777TokenSender interface implementation. The ERC777TokenSender implementation will be called by multiple ERC777 contracts. In the implementation contract of the ERC777TokenSender interface, msg.sender is the ERC777 contract address, not the operator.

ERC777TokensRecipient

If the recipient is a contract address, the ERC777TokensRecipient interface must be registered and implemented (this can prevent the token from being locked). If it is not implemented, the ERC777 token contract must revert back to the transaction state.

Vulnerabilities caused by some issues with the ERC721 and ERC777 standards will be introduced later.

A detailed introduction to ERC777 tokens can be found in the following document: Introduction to the ERC777 standard
Detailed descriptions of the above-mentioned ERC721, 20, 165, 777, 1820 and other standards can be found in this document: Ethereum EIP Description

Guess you like

Origin blog.csdn.net/m0_53689197/article/details/134043116