ERC721介绍

数字加密货币大致可以分为原生币(coin)和代币(token)两大类。前者如BTC、ETH等,拥有自己的区块链。后者如Tether、TRON、ONT等,依附于现有的区块链。市场上流通的基于以太坊的代币大都遵从ERC20协议。最近出现了一种被称为ERC721的数字加密资产,例如CryptoKitties。

ERC20可能是其中最广为人知的标准了。它诞生于2015年,到2017年9月被正式标准化。协议规定了具有可互换性(fungible)代币的一组基本接口,包括代币符号、发行量、转账、授权等。所谓可互换性(fungibility)指代币之间无差异,同等数量的两笔代币价值相等。交易所里流通的绝大部分代币都是可互换的,一单位的币无论在哪儿都价值一单位。

与之相对的则是非互换性(non-fungible)资产。比如CryptoKitties中的宠物猫就是典型的非互换性资产,因为每只猫各有千秋,而且由于不同辈分的稀缺性不同,市场价格也差异巨大。这种非标准化资产很长时间内都没有标准协议,直到2017年9月才出现ERC721提案,定义了一组常用的接口。ERC721至今仍旧处于草案阶段,但已经被不少dApp采用,甚至出现了专门的交易所。

ERC721标准

下面先给出ERC721标准的具体内容,后面会讲解。

每个符合ERC721标准的合约必须实现ERC721ERC165接口。

pragma solidity ^0.4.20;

/// @title ERC-721 Non-Fungible Token Standard
/// @dev See https://eips.ethereum.org/EIPS/eip-721
/// Note: the ERC-165 identifier for this interface is 0x80ac58cd.
interface ERC721 /* is ERC165 */ {
/// @dev This emits when ownership of any NFT changes by any mechanism.
/// This event emits when NFTs are created (`from` == 0) and destroyed
/// (`to` == 0). Exception: during contract creation, any number of NFTs
/// may be created and assigned without emitting Transfer. At the time of
/// any transfer, the approved address for that NFT (if any) is reset to none.
event Transfer(address indexed _from, address indexed _to, uint256 indexed _tokenId); /// @dev This emits when the approved address for an NFT is changed or
/// reaffirmed. The zero address indicates there is no approved address.
/// When a Transfer event emits, this also indicates that the approved
/// address for that NFT (if any) is reset to none.
event Approval(address indexed _owner, address indexed _approved, uint256 indexed _tokenId); /// @dev This emits when an operator is enabled or disabled for an owner.
/// The operator can manage all NFTs of the owner.
event ApprovalForAll(address indexed _owner, address indexed _operator, bool _approved); /// @notice Count all NFTs assigned to an owner
/// @dev NFTs assigned to the zero address are considered invalid, and this
/// function throws for queries about the zero address.
/// @param _owner An address for whom to query the balance
/// @return The number of NFTs owned by `_owner`, possibly zero
function balanceOf(address _owner) external view returns (uint256); /// @notice Find the owner of an NFT
/// @dev NFTs assigned to zero address are considered invalid, and queries
/// about them do throw.
/// @param _tokenId The identifier for an NFT
/// @return The address of the owner of the NFT
function ownerOf(uint256 _tokenId) external view returns (address); /// @notice Transfers the ownership of an NFT from one address to another address
/// @dev Throws unless `msg.sender` is the current owner, an authorized
/// operator, or the approved address for this NFT. Throws if `_from` is
/// not the current owner. Throws if `_to` is the zero address. Throws if
/// `_tokenId` is not a valid NFT. When transfer is complete, this function
/// checks if `_to` is a smart contract (code size > 0). If so, it calls
/// `onERC721Received` on `_to` and throws if the return value is not
/// `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`.
/// @param _from The current owner of the NFT
/// @param _to The new owner
/// @param _tokenId The NFT to transfer
/// @param data Additional data with no specified format, sent in call to `_to`
function safeTransferFrom(address _from, address _to, uint256 _tokenId, bytes data) external payable; /// @notice Transfers the ownership of an NFT from one address to another address
/// @dev This works identically to the other function with an extra data parameter,
/// except this function just sets data to "".
/// @param _from The current owner of the NFT
/// @param _to The new owner
/// @param _tokenId The NFT to transfer
function safeTransferFrom(address _from, address _to, uint256 _tokenId) external payable; /// @notice Transfer ownership of an NFT -- THE CALLER IS RESPONSIBLE
/// TO CONFIRM THAT `_to` IS CAPABLE OF RECEIVING NFTS OR ELSE
/// THEY MAY BE PERMANENTLY LOST
/// @dev Throws unless `msg.sender` is the current owner, an authorized
/// operator, or the approved address for this NFT. Throws if `_from` is
/// not the current owner. Throws if `_to` is the zero address. Throws if
/// `_tokenId` is not a valid NFT.
/// @param _from The current owner of the NFT
/// @param _to The new owner
/// @param _tokenId The NFT to transfer
function transferFrom(address _from, address _to, uint256 _tokenId) external payable; /// @notice Change or reaffirm the approved address for an NFT
/// @dev The zero address indicates there is no approved address.
/// Throws unless `msg.sender` is the current NFT owner, or an authorized
/// operator of the current owner.
/// @param _approved The new approved NFT controller
/// @param _tokenId The NFT to approve
function approve(address _approved, uint256 _tokenId) external payable; /// @notice Enable or disable approval for a third party ("operator") to manage
/// all of `msg.sender`'s assets
/// @dev Emits the ApprovalForAll event. The contract MUST allow
/// multiple operators per owner.
/// @param _operator Address to add to the set of authorized operators
/// @param _approved True if the operator is approved, false to revoke approval
function setApprovalForAll(address _operator, bool _approved) external; /// @notice Get the approved address for a single NFT
/// @dev Throws if `_tokenId` is not a valid NFT.
/// @param _tokenId The NFT to find the approved address for
/// @return The approved address for this NFT, or the zero address if there is none
function getApproved(uint256 _tokenId) external view returns (address); /// @notice Query if an address is an authorized operator for another address
/// @param _owner The address that owns the NFTs
/// @param _operator The address that acts on behalf of the owner
/// @return True if `_operator` is an approved operator for `_owner`, false otherwise
function isApprovedForAll(address _owner, address _operator) external view returns (bool);
} interface ERC165 {
/// @notice Query if a contract implements an interface
/// @param interfaceID The interface identifier, as specified in ERC-165
/// @dev Interface identification is specified in ERC-165. This function
/// uses less than 30,000 gas.
/// @return `true` if the contract implements `interfaceID` and
/// `interfaceID` is not 0xffffffff, `false` otherwise
function supportsInterface(bytes4 interfaceID) external view returns (bool);
}

如果钱包程序接受安全转账,它必须实现钱包接口。

/// @dev Note: the ERC-165 identifier for this interface is 0x150b7a02.
interface ERC721TokenReceiver {
/// @notice Handle the receipt of an NFT
/// @dev The ERC721 smart contract calls this function on the recipient
/// after a `transfer`. This function MAY throw to revert and reject the
/// transfer. Return of other than the magic value MUST result in the
/// transaction being reverted.
/// Note: the contract address is always the message sender.
/// @param _operator The address which called `safeTransferFrom` function
/// @param _from The address which previously owned the token
/// @param _tokenId The NFT identifier which is being transferred
/// @param _data Additional data with no specified format
/// @return `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`
/// unless throwing
function onERC721Received(address _operator, address _from, uint256 _tokenId, bytes _data) external returns(bytes4);
}

ERC721接口解析

先来一波名词解释:

  • NFT: 非互换性资产(non-fungible token)
  • tokenId: NFT的id,类型为uint256
  • owner: NFT的拥有者,类型为address
  • balance: 用户拥有的NFT数量,类型为uint256
  • approved: NFT的管理者,只有NFT的owner和approved可以对NFT进行操作,类型为address
  • operator:operator拥有一个用户所有NFT的管理权限,类型为address

接下来解释下接口内的事件和方法:

event Transfer(address indexed _from, address indexed _to, uint256 indexed _tokenId);

当NFT被转移时会触发Transfer事件,在NFT被创建和销毁时也会触发此事件。在NFT被转移时,他的approved会被重置为零地址。

event Approval(address indexed _owner, address indexed _approved, uint256 indexed _tokenId);

当NFT的管理者,也就是approved被改变的时候,会触发Approval事件。如果approved是零地址,说明NFT没有管理者。

event ApprovalForAll(address indexed _owner, address indexed _operator, bool _approved);

当NFT的owner指定一个用户拥有他所有NFT的管理权限时,会触发ApprovalForAll事件。operator拥有owner所有NFT的管理权限。

function function balanceOf(address _owner) external view returns (uint256);

查询一个用户拥有的NFT数量。

function ownerOf(uint256 _tokenId) external view returns (address);

查询一个NFT的拥有者。

function safeTransferFrom(address _from, address _to, uint256 _tokenId) external payable;

NFT的拥有者或管理者把一个NFT转移给别人,当to是一个合约的地址时,这个方法会调用onERC721Received方法。

function safeTransferFrom(address _from, address _to, uint256 _tokenId, bytes data) external payable;

NFT的拥有者或管理者把一个NFT转移给别人,这个方法有一个额外的data参数,上面那个safeTransferFrom方法会调用这个方法,然后把data置为""。

function transferFrom(address _from, address _to, uint256 _tokenId) external payable;

NFT的拥有者或管理者把一个NFT转移给别人,可以看到,这个方法比起上面两个要少了个safe,说明是不安全的转移,转移者要自己验证to是不是一个正确的地址,如果不是的话,NFT可能会永久丢失。

function approve(address _approved, uint256 _tokenId) external payable;

NFT的拥有者把NFT的管理权限授予一个用户。

function setApprovalForAll(address _operator, bool _approved) external;

NFT的拥有者授权一个用户是否拥有自己所有NFT的管理权限。合约必须允许一个用户有多个operator。

function getApproved(uint256 _tokenId) external view returns (address);

查询一个NFT的管理者

function isApprovedForAll(address _owner, address _operator) external view returns (bool);

查询指定的operator是否拥有owner所有NFT的管理权限。

function supportsInterface(bytes4 interfaceID) external view returns (bool);

查询一个合约是否实现了一个接口。

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

这个方法处理NFT的转移,当一笔转移被发起时,这个方法可能会被调用,目的是拒绝这次转移。

ERC721的实现

Github上已经有ERC721的实现了,地址:https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol

不过这是solidity的实现,并且基于公链,如果要基于联盟链,可以看一下我的这篇文章基于Hyperledger Fabric实现ERC721

参考

https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md

https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/ERC721.sol

https://zhuanlan.zhihu.com/p/112275276

初探区块链数字加密资产标准ERC721的更多相关文章

  1. Panda Global获悉,美国承诺4年内明确区块链数字资产监管方式!

    近日,美国商品期货交易委员会(CFTC)宣布,在4年内将会全面把加密货币监管列为优先事项.Panda Global从7月8日公布的新战略中获悉,此次CFTC公布了自己接下来的新框架,并且在框架中承诺: ...

  2. 为什么区块链和加密行业需要 Web 3?

    为什么区块链和加密行业需要 Web 3? “由于人们都想从互联网上获得好处,互联网已经演变成了一个导致不公平和分裂的引擎,它被强大的力量所支配,并且任由其摆布.“——万维网之父.互联网先驱 Tim B ...

  3. 沪苏浙皖共同打造区块链数字经济发展高地,Panda Global表示区块链真的来了!

    近日,在长三角一体化发展重大合作事项签约仪式上,沪苏浙皖经信部门共同签约,推进长三角区块链数字经济一体化发展,共同打造数字经济发展高地.从此次签约活动也能看出来,区块链数字现金的发展已经得到了认可,早 ...

  4. NGK公链如何构建区块链数字经济商业帝国?

    2020年对于区块链市场来说,重大的利好消息莫过于NGK公链的上线了.NGK公链其广泛的市场前景.顶尖的技术,一直备受众多大型机构以及投资者所看好.同时,NGK公链也不负众望,在上线以后,就开始落地到 ...

  5. [币严BIZZAN区块链]数字货币交易所钱包对接之比特币(BTC)

    在币严BIZZAN开发数字货币交易所的过程中,一共有两大难点,一个是高速撮合交易引擎,另一个是钱包对接,这两者是我们团队以前没有接触过的.这个系列的文章主要介绍数字货币交易所钱包对接实现技术.第一个要 ...

  6. 重磅!Panda Global获悉立陶宛下周将发行区块链数字货币!

    近日,Panda Global从路透社获悉,立陶宛将在下周开始预售2.4万枚由央行发行的数字货币.该名为LBCoin的数字货币基于区块链技术生产,也是该国试点具有国家支持背景的数字货币和区块链技术的项 ...

  7. [区块链|非对称加密] 对数字证书(CA认证)原理的回顾

    摘要:文中首先解释了加密解密的一些基础知识和概念,然后通过一个加密通信过程的例子说明了加密算法的作用,以及数字证书的出现所起的作用.接着对数字证书做一个详细的解释,并讨论一下windows中数字证书的 ...

  8. [币严区块链]数字货币交易所之比特币(BTC)钱包对接 | 自建节点JSON-RPC访问

    BTC钱包对接流程 一.   部署BTC钱包节点 二.   分析BTC钱包的API 三.   通过JSON-RPC访问BTC钱包API 四.   部署测试 一.部署钱包节点 交易平台对接BTC之前,要 ...

  9. [币严区块链]数字货币交易所之以太坊(ETH)钱包对接(四) 使用web3j对接以太坊钱包

    本文给大家介绍了 Web3j Java 版本的框架的基本使用,大家可根据本文的内容进行扩展性的练习,对其他 API 的使用进行尝试. 使用web3j对接以太坊钱包 一.开发准备事项 启动 Geth 此 ...

随机推荐

  1. Flink的CheckPoint

    Checkpoint checkpoint是Flink容错的核心机制.它可以定期的将各个Operator处理的数据进行快照存储(Snapshot). 如果Flink程序出现宕机,可以重新从这些快照中恢 ...

  2. golang中的三个点 ‘...‘ 的用法

    '-' 其实是go的一种语法糖. 它的第一个用法主要是用于函数有多个不定参数的情况,可以接受多个不确定数量的参数. 第二个用法是slice可以被打散进行传递. 下面直接上例子: func test1( ...

  3. Node.js开发博客系统

    数据库设计 用户表: id phone password nickname head_img personal_sign level_id create_time update_time is_del ...

  4. Servlet学习笔记(一)之Servlet原理、初始化、生命周期、结构体系

    Servlet是用java语言编写的应用到Web服务器端的扩展技术,与java对象的区别是,Servlet对象主要封装了对HTTP请求的处理,并且它的运行需要Servlet容器的支持(以下会介绍原因, ...

  5. Java最大栈深度有多大?-从一道面试题开始学习JVM

    一.问题:Java最大支持栈深度有多大? 1.分析 有JVM的内存结构我们可知: 随着线程栈的大小越大,能够支持越多的方法调用,也即是能够存储更多的栈帧: 局部变量表内容越多,那么栈帧就越大,栈深度就 ...

  6. shell脚本书写

    #!/bin/bash #指定脚本默认使用的命令解释器 第1行 幻数 #!/usr/bin/python #!/bin/awk #!/bin/sed

  7. junit5 yaml和json 数据驱动

    使用yaml 数据驱动测试 新建用例数据类,用于动态修改测试数据 对应yaml文件,第一层结构为列表 第二层结构为具体序列化class对象 测试成功 public class TestYaml { @ ...

  8. WEB安全性测试之拒绝服务攻击

    1,认证 需要登录帐号的角色 2,授权 帐号的角色的操作范围 3,避免未经授权页面直接可以访问 使用绝对url(PS:绝对ur可以通过httpwatch监控每一个请求,获取请求对应的页面),登录后台的 ...

  9. ubantu硬盘不足,无法启动

    我的ubantu虚拟机经过我一顿操作后,就起不来了.然后经过多方询问,广集天下良方,最终发现是由于分配的硬件空间不足导致的.现象如下: 通过查看 root@ubantu:/snap# df -h Fi ...

  10. epoll代码框架

    epoll代码实现框架: #define MAX_EVENTS 10 struct epoll_event ev, events[MAX_EVENTS]; int listen_sock, conn_ ...