Eth Of Erc20 And Erc721

Solidity common keywords

  • public, variables and functions can be modified, the modified function or variable can be called (or accessed) by any contract, default variables and functions use this property.
  • private, variables and functions can be modified, and the modified one can only be called (or accessed) by the code inside the current contract, and cannot be called (or accessed) by external contracts or sub-contracts that inherit it.
  • external, can only modify functions, and the modified functions can be called (or accessed) by contracts other than the current contract, but cannot be called (or accessed) by themselves and contracts that inherit it.
  • internal, variables and functions can be modified, and the modified one can be called (or accessed) by the current contract and the contracts that inherit it, but cannot be called (or accessed) by external contracts.
  • View can only modify functions, and the function can read external variables, but cannot modify them.
  • pure, can only modify the function, the external variables cannot be read and modified inside the function, 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 strings, and there are the following Keys in Json:

name: 字符串类型,对应的是当前项的名称。name只知道这个项的名称,究竟对于它是函数function还是uint8变量还是什么,并不清楚。
type: 字符串类型,标明当前项的项是什么类型(具体是一个函数还是一个变量)。常见的type有下面的取值
		1.function 函数
		2. construct 构造函数
		3. event 事件
		4. 变量类型,如: address, uint8, bool ...
constant: 布尔类型,代表当前项的操作结果是否会被写进区块链上,是则为true,否则为false。
stateMutability: 字符串类型。stateMutability有如下取值
		1. pure: 代表不会读和写区块链状态
		2. view: 代表会读区块链状态,但是不会改写区块链状态
		3. nonpayable: 代表会改写区块链状态,如转账transfer和授权approve这连个ERC20标准的函数就是可以用来改写区块链
payable: 布尔类型,代表当前的函数function是否可以接收ETH Token,可以则为true,否则为false
inputs: 其类型是Json数组,代表当前项入参信息,内部会把每个参数的名称及其所对应的类型列出。一般来说,input会跟随type是function或者事件event而含有值。inputs中的Json变量除了name和type外,还含有如下变量:
		1. Indexed,在Solidity代码事件event中,其入参有设置为Indexed关键字,此时Json中的这个变量对应的值为true,反之为false
		2. components: 该变量的类型是Json数组,当参数的typestruct结构类型是,该变量就会出现
outputs:  和inputs的含义类似,其类型也是Json数组,代表的是当前项的返回值,内部表达是返回值的名称和类型。inputs和outputs如果没有值,便默认是[]。
anonymous: 布尔类型,它和"标准的事件(Event)"的Indexed的设置有关,当为true的时候,在event中的入参即使是属于Indexed关键字的形式也不会保存到Topic中,为false则会。

Look for an ABI

[
    {
    
    
        "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 standard

There are many types of smart contracts, but the current token contract standards are mainly based on ERC20 and ERC721 .
insert image description here

ERC20 standard

The ERC20 standard includes member variables , functions and events .
(1) Standard member variables

  • string public name, this is the name of the token, 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 supply

(2) Standard functions and standard events

interface IERC20 {
    
    
	// 这是token的名称,全称,Ethereum
	string public name;
	// 这是简称,如ETH
	string public symbol;
	// 精度,即小数点的位数
	uint8 decimals;
	// 总发行量
	uint256 totalSupply;

	/* 
		标准的函数
	*/
	// 查询totalSupply函数
    function totalSupply() external view returns (uint256);
    // 查询某用户Token余额函数
    function balanceOf(address account) external view returns (uint256);
    // 转账函数
    function transfer(address recipient, 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 sender, address recipient, uint256 amount ) external returns (bool);
	/* 
		标准的事件
		// ERC20标准中,规定了在编写转账、授权函数代码时,必须在成功转账后触发事件。
	*/
	// Transfer在transfer和transferFrom函数内触发
    event Transfer(address indexed from, address indexed to, uint256 value);
    // 在成功调用approve函数后对应的一个事件
    event Approval(address indexed owner, address indexed spender, uint256 value );
}

The following is a standard ERC20 contract

contract ERC20 {
    
    
    // 这是token的名称,全称,Ethereum
	string public name;
	// 这是简称,如ETH
	string public symbol;
	// 精度,即小数点的位数
	uint8 decimals;
	// 总发行量
	uint256 totalSupply;
	/* 
		标准的函数
	*/
	// 查询totalSupply函数
    function totalSupply() external view returns (uint256){
    
    };
    // 查询某用户Token余额函数
    function balanceOf(address account) external view returns (uint256){
    
    };
    // 转账函数
    function transfer(address recipient, 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 sender, address recipient, uint256 amount ) external returns (bool){
    
    };
	/* 
		标准的事件
		// ERC20标准中,规定了在编写转账、授权函数代码时,必须在成功转账后触发事件。
	*/
	// Transfer在transfer和transferFrom函数内触发
    event Transfer(address indexed from, address indexed to, uint256 value);
    // 在成功调用approve函数后对应的一个事件
    event Approval(address indexed owner, address indexed spender, uint256 value );
}

ERC721 standard

The ERC721 standard includes member variables , functions and events . The contract is mainly based on a one-to-one relationship.
(1) Member variables
ERC721 and ERC20 member variables are basically the same, but ERC721 does not require decimal variables.
(2) Standard functions and standard events

	/* 
		标准的函数
	*/
	// 查询totalSupply函数
    function totalSupply() external view returns (uint256){
    
    };
    // 查询某用户Token余额函数
    function balanceOf(address account) external view returns (uint256){
    
    };
    // 转账函数
    function transfer(address recipient, uint256 amount) external returns (bool){
    
    };
    // 所有权查询
    function ownerOf(uint256 _tokenId) external view returns (uint256 balance){
    
    };
    // 授权函数
    function approve(address spender, uint256 amount) external returns (bool){
    
    };
    // 转账函数
    function transferFrom(address sender, address recipient, uint256 amount ) external returns (bool){
    
    };

    // 该标准主要的作用是用来检测当前智能合约实现了哪些接口,可以根据interfaceID来查询接口ID,存在返回true,否则返回false
    // 该标准函数会消耗gas,至少消耗30000Gas
    function supportsInterface(bytes4 _interfaceID) external view returns (bool){
    
    };

    // 可选实现函数
    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);

Compared with ERC20, ERC721 mainly has the following functions

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

The function of this function is to query the owner of the token according to the tokenId .

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

The function of this function is to query the tokenId owned by the address according to an address

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

Inside this function is a custom string, which is some basic information. For example, what is written here is url, which can be understood as a link to an NFT storage location.

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. The interface ID can be queried according to the interfaceID. If it exists, it will return true, otherwise it will return false. This standard function consumes gas, at least 30000Gas.

The following is the ERC721 smart contract

contract ERC721 {
    
    
    // 这是token的名称,全称,Ethereum
	string public name;
	// 这是简称,如ETH
	string public symbol;
	// 精度,即小数点的位数
	uint8 decimals;
	// 总发行量
	uint256 totalSupply;
	/* 
		标准的函数
	*/
	// 查询totalSupply函数
    function totalSupply() external view returns (uint256){
    
    };
    // 查询某用户Token余额函数
    function balanceOf(address account) external view returns (uint256){
    
    };
    // 转账函数
    function transfer(address recipient, uint256 amount) external returns (bool){
    
    };
    // 所有权查询
    function ownerOf(uint256 _tokenId) external view returns (uint256 balance){
    
    };
    // 授权函数
    function approve(address spender, uint256 amount) external returns (bool){
    
    };
    // 转账函数
    function transferFrom(address sender, address recipient, uint256 amount ) external returns (bool){
    
    };

    // 该标准主要的作用是用来检测当前智能合约实现了哪些接口,可以根据interfaceID来查询接口ID,存在返回true,否则返回false
    // 该标准函数会消耗gas,至少消耗30000Gas
    function supportsInterface(bytes4 _interfaceID) external view returns (bool){
    
    };

    // 可选实现函数
    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标准中,规定了在编写转账、授权函数代码时,必须在成功转账后触发事件。
	*/
	// Transfer在transfer和transferFrom函数内触发
    event Transfer(address indexed from, address indexed to, uint256 value);
    // 在成功调用approve函数后对应的一个事件
    event Approval(address indexed owner, address indexed spender, uint256 value );
}

Guess you like

Origin blog.csdn.net/wjl__ai__/article/details/125123264