当前位置:网站首页>Eth Of Erc20 And Erc721

Eth Of Erc20 And Erc721

2022-06-11 04:03:00 Are earthlings from other planets aliens?

Solidity Common keywords

  • public, You can modify variables and functions , The modified function or variable can be called by any contract ( Or visit ), The default variables and functions use this property .
  • private, You can modify variables and functions , The decorated person can only be called by the code inside the current contract ( Or visit ), Cannot be called by an external contract or by a child contract that inherits it ( Or visit ).
  • extermal, Can only decorate functions , The modified function can be called by a contract other than the current contract ( Or visit ), Cannot be invoked by itself or by the contract that inherits it ( Or visit ).
  • internal, You can modify variables and functions , The decorated person can be called by the current contract and the contract that inherits it ( Or visit ), But it cannot be called by an external contract ( Or visit ).
  • view, Can only decorate functions , Function can read external variables , But it can't be changed .
  • pure, Can only decorate functions , Functions cannot read or modify external variables , It can only read and write the parameter quantity entered by the parameter .

ABI(Application Binary Interface): Application Binary Interface

Its intuitive form is a string of Json character string ,Json There are the following Key:

name:  String type , This corresponds to the name of the current item .name Only the name of this item is known , What is a function for it function still uint8 Variables or something , Not clear .
type:  String type , Indicates what type of item the current item is ( Is it a function or a variable ). common type I have the following values 
		1.function  function 
		2. construct  Constructors 
		3. event  event 
		4.  Variable type , Such as : address, uint8, bool ...
constant:  Boolean type , Whether the operation result of the current item will be written into the blockchain , Yes, it is true, Otherwise false.
stateMutability:  String type .stateMutability It has the following values 
		1. pure:  Represents that the blockchain state cannot be read or written 
		2. view:  Representatives can read blockchain status , But the blockchain status will not be overwritten 
		3. nonpayable:  The representative will overwrite the blockchain status , Such as transfers transfer And authorization approve It's not even ERC20 The standard function can be used to rewrite the blockchain 
payable:  Boolean type , Represents the current function function Can I receive ETH Token, Can be true, Otherwise false
inputs:  The type is Json Array , Represents the input parameter information of the current item , The internal will list the name of each parameter and its corresponding type . Generally speaking ,input Will follow type yes function Or events event With values .inputs Medium Json Variable except name and type Outside , It also contains the following variables :
		1. Indexed, stay Solidity Code event event in , The input parameter is set to Indexed keyword , here Json The corresponding value of this variable in is true, Instead of false
		2. components:  The type of the variable is Json Array , When the parameter is type yes struct The structure type is , This variable will appear 
outputs:   and inputs It means something like , Its type is also Json Array , Represents the return value of the current item , The inner expression is the name and type of the return value .inputs and outputs If there is no value , The default is [].
anonymous:  Boolean type , It and " Standard events (Event)" Of Indexed The settings of , When it comes to true When , stay event Even if it belongs to Indexed The form of keywords will not be saved to Topic in , by false will .

Find one. ABI have a look

[
    {
    
        "inputs":[
            {
    
                "internalType":"address",
                "name":"_logic",
                "type":"address"
            },
            {
    
                "internalType":"bytes",
                "name":"_data",
                "type":"bytes"
            }
        ],
        "stateMutability":"payable",
        "type":"constructor"
    },
    {
    
        "anonymous":false,
        "inputs":[
            {
    
                "indexed":false,
                "internalType":"address",
                "name":"previousAdmin",
                "type":"address"
            },
            {
    
                "indexed":false,
                "internalType":"address",
                "name":"newAdmin",
                "type":"address"
            }
        ],
        "name":"AdminChanged",
        "type":"event"
    },
    {
    
        "anonymous":false,
        "inputs":[
            {
    
                "indexed":true,
                "internalType":"address",
                "name":"beacon",
                "type":"address"
            }
        ],
        "name":"BeaconUpgraded",
        "type":"event"
    },
    {
    
        "anonymous":false,
        "inputs":[
            {
    
                "indexed":true,
                "internalType":"address",
                "name":"implementation",
                "type":"address"
            }
        ],
        "name":"Upgraded",
        "type":"event"
    },
    {
    
        "stateMutability":"payable",
        "type":"fallback"
    },
    {
    
        "stateMutability":"payable",
        "type":"receive"
    }
]

Contract Standards

There are many types of smart contracts , But at the moment token Contract standards are mainly based on ERC20 and ERC721 As the standard .
 Insert picture description here

ERC20 standard

ERC20 The standard includes Member variables function and event .
(1) Standard member variables

  • string public name, This is a token The name of , Full name ,Ethereum
  • string public symbol, This is the abbreviation , Such as ETH
  • uint8 decimals, precision , That is, the number of decimal places
  • uint256 totalSupply, Total circulation

(2) Standard functions and standard events

interface IERC20 {
    
	//  This is a token The name of , Full name ,Ethereum
	string public name;
	//  This is the abbreviation , Such as ETH
	string public symbol;
	//  precision , That is, the number of decimal places 
	uint8 decimals;
	//  Total circulation 
	uint256 totalSupply;

	/*  Standard functions  */
	//  Inquire about totalSupply function 
    function totalSupply() external view returns (uint256);
    //  Query a user Token Balance function 
    function balanceOf(address account) external view returns (uint256);
    //  Transfer function 
    function transfer(address recipient, uint256 amount) external returns (bool);
    //  Query the authorization quantity function according to the authorization relationship 
    function allowance(address owner, address spender) external view returns (uint256);
    //  Authorization function 
    function approve(address spender, uint256 amount) external returns (bool);
    //  Transfer function 
    function transferFrom(address sender, address recipient, uint256 amount ) external returns (bool);
	/*  Standard events  // ERC20 In the standard , It is stipulated that when preparing the transfer 、 When authorizing function code , The event must be triggered after a successful transfer . */
	// Transfer stay transfer and transferFrom Function triggers 
    event Transfer(address indexed from, address indexed to, uint256 value);
    //  On successful call approve The corresponding event after the function 
    event Approval(address indexed owner, address indexed spender, uint256 value );
}

Here's a standard ERC20 contract

contract ERC20 {
    
    //  This is a token The name of , Full name ,Ethereum
	string public name;
	//  This is the abbreviation , Such as ETH
	string public symbol;
	//  precision , That is, the number of decimal places 
	uint8 decimals;
	//  Total circulation 
	uint256 totalSupply;
	/*  Standard functions  */
	//  Inquire about totalSupply function 
    function totalSupply() external view returns (uint256){
    };
    //  Query a user Token Balance function 
    function balanceOf(address account) external view returns (uint256){
    };
    //  Transfer function 
    function transfer(address recipient, uint256 amount) external returns (bool){
    };
    //  Query the authorization quantity function according to the authorization relationship 
    function allowance(address owner, address spender) external view returns (uint256){
    };
    //  Authorization function 
    function approve(address spender, uint256 amount) external returns (bool){
    };
    //  Transfer function 
    function transferFrom(address sender, address recipient, uint256 amount ) external returns (bool){
    };
	/*  Standard events  // ERC20 In the standard , It is stipulated that when preparing the transfer 、 When authorizing function code , The event must be triggered after a successful transfer . */
	// Transfer stay transfer and transferFrom Function triggers 
    event Transfer(address indexed from, address indexed to, uint256 value);
    //  On successful call approve The corresponding event after the function 
    event Approval(address indexed owner, address indexed spender, uint256 value );
}

ERC721 standard

ERC721 The standard includes Member variables function and event . The contract mainly withdraws cash in a one-to-one relationship .
(1) Member variables
ERC721 and ERC20 The member variables are basically the same , however ERC721 Unwanted decimal Variable .
(2) Standard functions and standard events

	/*  Standard functions  */
	//  Inquire about totalSupply function 
    function totalSupply() external view returns (uint256){
    };
    //  Query a user Token Balance function 
    function balanceOf(address account) external view returns (uint256){
    };
    //  Transfer function 
    function transfer(address recipient, uint256 amount) external returns (bool){
    };
    //  Ownership inquiry 
    function ownerOf(uint256 _tokenId) external view returns (uint256 balance){
    };
    //  Authorization function 
    function approve(address spender, uint256 amount) external returns (bool){
    };
    //  Transfer function 
    function transferFrom(address sender, address recipient, uint256 amount ) external returns (bool){
    };

    //  The main function of this standard is to detect which interfaces are implemented by the current smart contract , According to interfaceID To query the interface ID, There is returned true, Otherwise return to false
    //  This standard function consumes gas, At least consume 30000Gas
    function supportsInterface(bytes4 _interfaceID) external view returns (bool){
    };

    //  Optional implementation functions 
    function name() public view returns (string name);
    function symbol() public view returns (string symbol);
    function tokenOfowner(address _owner) external view returns(uin256 [] tokenIds);
    function tokenMetadata(uint256 _tokenId, string _preferredTransport) public view returns (string infoUrl);

ERC721 and ERC20 comparison , The following functions are added

function ownerOf(uint256 _tokenId) external view returns (uint256 balance){};

The function of this function is based on tokenId Query the token Owner .

function tokenOfowner(address _owner) external view returns(uin256 [] tokenIds){};

The function of this function is based on an address , Query the address owned by tokenId

function tokenMetadata(uint256 _tokenId, string _preferredTransport) public view returns (string infoUrl){};

Inside this function is a custom string , Just some basic information . such as , What's written here is url, You can think of it as one NFT Store links .

function supportsInterface(bytes4 _interfaceID) external view returns (bool){};

The main function of the standard is to detect which interfaces are implemented by the current smart contract , According to interfaceID To query the interface ID, There is returned true, Otherwise return to false. This standard function consumes gas, At least consume 30000Gas.

Here are ERC721 Intelligent contract

contract ERC721 {
    
    //  This is a token The name of , Full name ,Ethereum
	string public name;
	//  This is the abbreviation , Such as ETH
	string public symbol;
	//  precision , That is, the number of decimal places 
	uint8 decimals;
	//  Total circulation 
	uint256 totalSupply;
	/*  Standard functions  */
	//  Inquire about totalSupply function 
    function totalSupply() external view returns (uint256){
    };
    //  Query a user Token Balance function 
    function balanceOf(address account) external view returns (uint256){
    };
    //  Transfer function 
    function transfer(address recipient, uint256 amount) external returns (bool){
    };
    //  Ownership inquiry 
    function ownerOf(uint256 _tokenId) external view returns (uint256 balance){
    };
    //  Authorization function 
    function approve(address spender, uint256 amount) external returns (bool){
    };
    //  Transfer function 
    function transferFrom(address sender, address recipient, uint256 amount ) external returns (bool){
    };

    //  The main function of this standard is to detect which interfaces are implemented by the current smart contract , According to interfaceID To query the interface ID, There is returned true, Otherwise return to false
    //  This standard function consumes gas, At least consume 30000Gas
    function supportsInterface(bytes4 _interfaceID) external view returns (bool){
    };

    //  Optional implementation functions 
    function name() public view returns (string name);
    function symbol() public view returns (string symbol);
    function tokenOfowner(address _owner) external view returns(uin256 [] tokenIds);
    function tokenMetadata(uint256 _tokenId, string _preferredTransport) public view returns (string infoUrl);

	/*  Standard events  // ERC721 In the standard , It is stipulated that when preparing the transfer 、 When authorizing function code , The event must be triggered after a successful transfer . */
	// Transfer stay transfer and transferFrom Function triggers 
    event Transfer(address indexed from, address indexed to, uint256 value);
    //  On successful call approve The corresponding event after the function 
    event Approval(address indexed owner, address indexed spender, uint256 value );
}
原网站

版权声明
本文为[Are earthlings from other planets aliens?]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110344300318.html