What is ERC-721. Now we see a variety of encryption cats and dogs are based on ERC-721 created, each is a unique ERC-721 tokens, but ERC-721 in the block chain of the world far more than cats and dogs, it is more space to imagine the physical world of the assets map to the block chain. This article is to analyze what is ERC721. What's ERC721?
In the creation of a token, we talked about ERC20 tokens,
Like ERC20, ERC721 is also a token of the standard, ERC721 official brief explanation is non-fungible tokens, short for nfts, many translation is not the same quality token.
ERC721 was presented by Dieter Shirley in September 2017. Dieter Shirley is the technical director of the company Axiom Zen behind the Mystery cat cryptokitties. So the mystery cat is also the first to achieve the ERC721 standard of the central application. The ERC721 proposal has been accepted by the ether as standard, but the standard is still in draft stage. The ERC721 standards described in this article are based on the latest (2018/03/23 official proposal).
How do you understand the non homogeneous tokens?
Non-homogeneous represents unique, mystery cat for example, each cat is endowed with the gene, is unique (a cat is a nfts), the cat can not be replaced. This uniqueness makes some rare cats valuable for collection and is therefore sought after.
The ERC20 token is replaceable and can be subdivided into n parts (1 = 10 * 0.1), while the ERC721 token smallest unit is 1 and can no longer be segmented.
If the two items of the same set have different characteristics, the two objects are not homogeneous, and the homogeneity is a part or quantity that can be replaced by another equal part or quantity.
Heterogeneity in fact widely exists in our life, such as every book in the library, pet store each pet, the singer sings the song, The flower Shop different flower and so on, therefore the ERC721 contract must have the widespread application scene. By such a standard, a cross-functional nfts management and sales platform (like an exchange and wallet supported by ERC20) can be built to make the ecology more powerful. ERC721 Standard
ERC721 most of the contract standards, provided in the implementation of ERC721 tokens must comply with the protocol, requires each ERC721 standard contracts need to implement ERC721 and ERC165 interface, interface definition is as follows:
1
2
3
4
5
6
7 8 9
20
|
pragma solidity ^0.4.20;
Interface ERC721 * is ERC165/{event Transfer (address indexed _from, address indexed _to, uint256 _tokenid);
Event Approval (address indexed _owner, address indexed _approved, uint256 _tokenid);
Event Approvalforall (address indexed _owner, address indexed _operator, bool _approved);
function balanceof (address _owner) external view Returns (UINT256);
function Ownerof (uint256 _tokenid) External view Returns (address);
function Safetransferfrom (address _from, address _to, uint256 _tokenid, bytes data) external payable;
function Safetransferfrom (address _from, address _to, uint256 _tokenid) external payable;
function Transferfrom (address _from, address _to, uint256 _tokenid) external payable;
function approve (address _approved, uint256 _tokenid) external payable;
function Setapprovalforall (address _operator, bool _approved) external;
function getapproved (uint256 _tokenid) External view Returns (address); function Isapprovedforall (address _owner, address _operator) external view Returns (bool);
} |
Interface Description: balanceof (): Returns the number of Nfts held by _owner. Ownerof (): Returns the address of the holder of the Tokenid token. Approve (): The address _to has _tokenid control, and the method succeeds in triggering the approval event. Setapprovalforall (): The address _operator has control over all nfts and the Approvalforall event is triggered after success.
Getapproved (), Isapprovedforall (): Used to query authorization.
Safetransferfrom (): Transfer NFT ownership, a successful transfer operation must initiate a Transer event. The implementation of the function needs to do several checks: The caller Msg.sender should be the owner of the current Tokenid or the authorized address _from must be _tokenid's owner _tokenid should be any nfts address in the _to that the current contract is monitoring should not be 0 if _to is a contract, it should call its Onerc721received method and check its return value if the return value is not BYTES4 (keccak256 ("onerc721received (address,uint256,bytes)") ) throws an exception.
A contract that can receive NFT must implement the Erc721tokenreceiver interface:
1
2
3
4
|
Interface Erc721tokenreceiver {
///@return ' Bytes4 (keccak256 ("onerc721received (address,uint256,bytes)")) '
function onerc721received (address _from, uint256 _tokenid, bytes data) external returns (BYTES4);
|
Transferfrom (): Used to transfer nfts, the success of the method should trigger the transfer event. The caller himself confirms that the _to address can receive NFT correctly, otherwise this NFT will be lost. The first 4 of the above conditions need to be checked when this function is implemented.
ERC165 Standard
The ERC721 standard also requires that the ERC165 standard be met, with the following interface:
1
2
3
|
Interface ERC165 {
function supportsinterface (bytes4 interfaceid) external view Returns (bool);
}
|
ERC165 is also a contract standard that requires the contract to provide what interfaces it implements so that it can be invoked to query the contract when interacting with it.
InterfaceID is a function selector, there are two methods of calculation, such as: Bytes4 (keccak256 (' Supportsinterface (BYTES4) '); Or ERC165.supportsInterface.selector, the interface ID of multiple functions is the difference or value of the function selector.
About ERC165, there is no in-depth introduction, interested students can read the official proposal. Optional Implementation interface: Erc721metadata
The Erc721metadata interface is used to provide metadata for the contract: name, Symbol, and URI (the corresponding resource for NFT).
The interface is defined as follows:
1
2
3
4
5
|
Interface Erc721metadata * is ERC721/{
function name () external pure Returns (string _name);
function symbol () external pure Returns (string _symbol);
function Tokenuri (uint256 _tokenid) external view returns (string);
|
Interface Description: name (): Returns the contract name, although it is optional, but it is strongly recommended to implement, even if it returns an empty string. Symbol (): Returns the contract token, which, although optional, is strongly recommended, even if it returns an empty string. Tokenuri (): Returns the URI (usually the IPFs or HTTP (S) path) of an external resource file that corresponds to _tokenid. The external resource file needs to contain a name, description, picture, and its format requires the following:
1 2 3 4 5 6 7 8 9 a |
{
"title": "Asset Metadata", "
Type": "Object",
"Properties": {
"name": {
"type": "string",
" Description ":" Identifies the asset to which this NFT represents ",
},
" description ": {
" type ":" String ", "
description": "describes the asset to which this NFT represents",
},
"image": {
"type": "String", c13/> "description": "A URI pointing to a resource with MIME type image/* representing the asset to which this NFT Represen Ts. Consider making any images at a width between 1080 and pixels aspect and ratio between 1.91:1 4:5 and inclusive. ",
}
}
}
|
Tokenuri is usually called by the web3 to make the corresponding query and presentation at the application layer. Optional Implementation interface: Erc721enumerable
The main purpose of erc721enumerable is to improve the accessibility of ntf in the contract, and its interface is defined as follows:
1
2
3
4
5
|
Interface Erc721enumerable * is ERC721/{
function totalsupply () external view Returns (UINT256);
function Tokenbyindex (uint256 _index) external view Returns (UINT256);
function Tokenofownerbyindex (address _owner, uint256 _index) external view Returns (UINT256);
}
|
Interface Description: totalsupply (): Return NFT Total Tokenbyindex (): Returns the corresponding Tokenid by index. Tokenofownerbyindex (): The owner can have more than one nft at a time, which returns Tokenid of the corresponding index in the NFT list owned by _owner. Supplementary Notes ntf IDs
The ntf ID, the Tokenid, is identified in the contract with a unique uint265, and each NFT ID is not allowed to change within the life cycle of the smart contract. Recommended implementations are: Starting from 0, each new nft,ntf ID plus 1 uses SHA3 after the UUID to convert to ntf ID and ERC-20 compatibility