TRC-721
1 TRC-721协议标准
TRC-721是在波场公链上发行非同质化代币(non-fungible token, NFT)一套标准接口,与ERC-721完全兼容。
1.1 TRC-721智能合约接口
每个符合TRC-721标准的智能合约都必须实现TRC721与TRC165接口,并可以根据业务需要实现其他扩展接口。
1.1.1 TRC-721 & TRC-165接口
pragma solidity ^0.4.20;
interface TRC721 {
// Returns the number of NFTs owned by the given account
function balanceOf(address _owner) external view returns (uint256);
//Returns the owner of the given NFT
function ownerOf(uint256 _tokenId) external view returns (address);
//Transfer ownership of NFT
function safeTransferFrom(address _from, address _to, uint256 _tokenId, bytes data) external payable;
//Transfer ownership of NFT
function safeTransferFrom(address _from, address _to, uint256 _tokenId) external payable;
//Transfer ownership of NFT
function transferFrom(address _from, address _to, uint256 _tokenId) external payable;
//Grants address ‘_approved’ the authorization of the NFT ‘_tokenId’
function approve(address _approved, uint256 _tokenId) external payable;
//Grant/recover all NFTs’ authorization of the ‘_operator’
function setApprovalForAll(address _operator, bool _approved) external;
//Query the authorized address of NFT
function getApproved(uint256 _tokenId) external view returns (address);
//Query whether the ‘_operator’ is the authorized address of the ‘_owner’
function isApprovedForAll(address _owner, address _operator) external view returns (bool);
//The successful ‘transferFrom’ and ‘safeTransferFrom’ will trigger the ‘Transfer’ Event
event Transfer(address indexed _from, address indexed _to, uint256 indexed _tokenId);
//The successful ‘Approval’ will trigger the ‘Approval’ event
event Approval(address indexed _owner, address indexed _approved, uint256 indexed _tokenId);
//The successful ‘setApprovalForAll’ will trigger the ‘ApprovalForAll’ event
event ApprovalForAll(address indexed _owner, address indexed _operator, bool _approved);
}
interface TRC165 {
//Query whether the interface ‘interfaceID’ is supported
function supportsInterface(bytes4 interfaceID) external view returns (bool);
}
合约如果需要接受安全转账,必须实现TRC721TokenReceiver接口:
interface TRC721TokenReceiver {
//This method will be triggered when the ‘_to’ is the contract address during the ‘safeTransferFrom’ execution, and the return value must be checked, If the return value is not bytes4(keccak256("onTRC721Received(address,address,uint256,bytes)")) throws an exception. The smart contract which can receive NFT must implement the TRC721TokenReceiver interface.
function onTRC721Received(address _operator, address _from, uint256 _tokenId, bytes _data) external returns(bytes4);
}
注意
bytes4(keccak256("onTRC721Received(address,address,uint256,bytes)))
的hash与以太坊版本的bytes4(keccak256("onERC721Received(address,address,uint256,bytes)))
不同。请使用0x5175f878
,而不是0x150b7a02
。
1.1.2 metadata extension接口(可选)
metadata extension接口对于TRC-721智能合约来说是可选的,用户可以查询智能合约的名称以及NFT代表的资产的详细信息。
interface TRC721Metadata {
//Return the token name
function name() external view returns (string _name);
//Return the token symbol
function symbol() external view returns (string _symbol);
//Returns the URI of the external file corresponding to ‘_tokenId’. External resource files need to include names, descriptions and pictures.
function tokenURI(uint256 _tokenId) external view returns (string);
}
URI 是描述_tokenId资产的URI链接,指向一个符合TRC721元数据描述结构的JSON文件,铸造代币时需要给每个代币指定唯一的URI:
{
"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",
"description": "A URI pointing to a resource with mime type image/* representing the asset to which this NFT represents. Consider making any images at a width between 320 and 1080 pixels and aspect ratio between 1.91:1 and 4:5 inclusive."
}
}
}
1.1.3 enumeration extension接口(可选)
enumeration extension对于TRC-721智能合约是可选的,允许用户的智能合约发布其NFT的完整列表并使其可见。
interface TRC721Enumerable {
//Return the total supply of NFT
function totalSupply() external view returns (uint256);
//Return the corresponding ‘tokenId’ through ‘_index’
function tokenByIndex(uint256 _index) external view returns (uint256);
//Return the ‘tokenId’ corresponding to the ‘_index’ in the ‘_owner' owned NFT list.
function tokenOfOwnerByIndex(address _owner, uint256 _index) external view returns (uint256);
}
Updated about 2 years ago