ERC721 由 Axiom Zen 公司的技术总监 Dieter Shirley 提出,Axiom Zen 公司正是 CryptoKitties 背后的公司,因此 CryptoKitties 也是第一个实现 ERC721 标准的 NFT。
主要合约信息,和 ERC20 基本相同
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);
}
如果 NFT 在转移时,_to
的对象是一个合约地址的话(比如 NFT 挖矿,NFT 交易),那么需要实现以下方法
interface ERC721TokenReceiver {
function onERC721Received(address _operator, address _from, uint256 _tokenId, bytes _data) external returns(bytes4);
}
由于 NFT 可能包含多媒体信息(图片、视频、音频等),由于信息太大,无法全部存储在以太坊中,所以这些信息(Metadata,元数据)会通过远端存储的方式,存储在中心化或去中心化存储服务中。NFT 协议只需要规定 Metadata 信息指向哪里即可。
interface ERC721Metadata{
function name() external view returns (string _name);
function symbol() external view returns (string _symbol);
function tokenURI(uint256 _tokenId) external view returns (string);
}
ERC721Metadata
接口中的 tokenURI
指向远端服务器(存储)文件链接,这个链接通常会指向一个 JSON 文件,721 标准中规范了 JSON 文件的标准
{
"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."
}
}
}
此外 ERC721Enumerable
接口还提高了 NFT 的可访问性,比如 NFT 发行总量,返回指定 TokenID 的 NFT,以及返回 _owner 拥有的 Collection 下的所有 NFT 的 TokenID。
interface ERC721Enumerable {
function totalSupply() external view returns (uint256);
function tokenByIndex(uint256 _index) external view returns (uint256);
function tokenOfOwnerByIndex(address _owner, uint256 _index) external view returns (uint256);
}
其实简单来说,ERC721 规范了一套创建 NFT 的标准,这个标准主要分为两个主要部分,其一是类似于 ERC20,规范了转账,授权,Owner 所有者等接口,其二是设定了 ToeknURI 接口,制定了 Metadata 相关规范。