Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

13.4. ERC721 - Non-Fungible Tokens

13.4.1. 

			
interface ERC721 {

    event Transfer(address indexed _from, address indexed _to, uint256 indexed _tokenId);
    event Approval(address indexed _owner, address indexed _approved, uint256 indexed _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 ERC165 {
    function supportsInterface(bytes4 interfaceID) external view returns (bool);
}

interface ERC721TokenReceiver {
    function onERC721Received(address _operator, address _from, uint256 _tokenId, bytes _data) external returns(bytes4);
}			
			
			

13.4.2. ERC721Metadata (可選)

可選實現介面,ERC721Metadata 介面用於提供合約的元數據:name , symbol 及 URI(NFT所對應的資源)。

			
其介面定義如下:

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);
}

介面說明:
name(): 返回合約名字,儘管是可選,但強烈建議實現,即便是返回空字元串。
symbol(): 返回合約代幣符號,儘管是可選,但強烈建議實現,即便是返回空字元串。
tokenURI(): 返回_tokenId所對應的外部資源檔案的URI(通常是IPFS或HTTP(S)路徑)。外部資源檔案需要包含名字、描述、圖片,其格式的要求如下:			
			
			

13.4.3. ERC721Enumerable (可選)

可選實現介面,ERC721Enumerable的主要目的是提高合約中NTF的可訪問性,其介面定義如下:

			
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);
}
介面說明:

totalSupply(): 返回NFT總量
tokenByIndex(): 通過索引返回對應的tokenId。
tokenOfOwnerByIndex(): 所有者可以一次擁有多個的NFT, 此函數返回_owner擁有的NFT列表中對應索引的tokenId。