OpenZeppelin ERC721源碼分析

ERC721 官方簡介是:A standard interface for non-fungible tokens, also known as deeds.也叫非同質代幣,或者不可置換代幣(NFTs)。提到ERC721,一個好理解的例子就是CryptoKitties 迷戀貓,每一隻貓都是獨一無二的擁有不一樣基因,有收藏價值屬性。ERC721對於虛擬資產收藏品領域會有很好的應用價值和市場需求。javascript

它和我寫的上一篇《OpenZeppelin ERC20源碼分析》介紹的ERC20有所不一樣,ERC721最小的單位爲1沒法再分割,表明獨一無二的,針對不可置換的Token的智能合約標準接口。從 ERC721標準草案中能夠看到,兼容ERC20的方法有4個:namesymboltotalSupplybalanceOf 添加的新方法:ownerOftakeOwnership ERC721還重寫了approvetransferjava

分析OpenZeppelin ERC721源碼前一樣我畫了一個繼承和調用關係的思惟導圖,能夠幫助更容易地看源碼。git

OpenZeppelin ERC721源碼分析

ERC721Basic.sol

pragma solidity ^0.4.23;

/**
 * @title ERC721 標準的基本接口
 * @dev see https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md
 */
contract ERC721Basic {
  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) public view returns (uint256 _balance);
  function ownerOf(uint256 _tokenId) public view returns (address _owner);
  function exists(uint256 _tokenId) public view returns (bool _exists);

  function approve(address _to, uint256 _tokenId) public;
  function getApproved(uint256 _tokenId)
    public view returns (address _operator);

  function setApprovalForAll(address _operator, bool _approved) public;
  function isApprovedForAll(address _owner, address _operator)
    public view returns (bool);

  function transferFrom(address _from, address _to, uint256 _tokenId) public;
  function safeTransferFrom(address _from, address _to, uint256 _tokenId)
    public;

  function safeTransferFrom(
    address _from,
    address _to,
    uint256 _tokenId,
    bytes _data
  )
    public;
}

ERC721Basic 合約定義了基本的接口方法:github

  • balanceOf 返回_owner的代幣數量
  • ownerOf 根據_tokenId返回代幣持有者address
  • exists _tokenId是否存在
  • approve 受權_tokenId給地址to
  • getApproved 查詢_tokenId的受權人_operator address
  • setApprovalForAll 受權_operator具備全部代幣的控制權
  • isApprovedForAll
  • transferFrom 轉移代幣全部權
  • safeTransferFrom 轉移代幣全部權

同時還定義了Transfer Approval ApprovalForAll 在後面的ERC721實現的代碼中再來看事件的觸發。數組

ERC721.sol

pragma solidity ^0.4.23;

import "./ERC721Basic.sol";

/**
 * @title ERC-721 標準的基本接口, 可選的枚舉擴展
 * @dev See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md
 */
contract ERC721Enumerable is ERC721Basic {
  function totalSupply() public view returns (uint256);
  function tokenOfOwnerByIndex(
    address _owner,
    uint256 _index
  )
    public
    view
    returns (uint256 _tokenId);

  function tokenByIndex(uint256 _index) public view returns (uint256);
}


/**
 * @title ERC-721 ERC-721 標準的基本接口, 可選的元數據擴展
 * @dev See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md
 */
contract ERC721Metadata is ERC721Basic {
  function name() public view returns (string _name);
  function symbol() public view returns (string _symbol);
  function tokenURI(uint256 _tokenId) public view returns (string);
}

/**
 * @title ERC-721 標準的基本接口,完整實現接口
 * @dev See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md
 */
contract ERC721 is ERC721Basic, ERC721Enumerable, ERC721Metadata {
}

ERC721 合約繼承了 ERC721Basic 的基礎上,添加枚舉和元數據的擴展。安全

ERC721Enumerable枚舉擴展可使代幣更具備可訪問性:app

  • totalSupply 返回代幣總量
  • tokenOfOwnerByIndex 經過_owner全部者地址和索引值返回全部者代幣列表中的_tokenId
  • tokenByIndex 經過索引值返回tokenId

ERC721Metadata元數據擴展哦用來描述合約元信息函數

  • name 返回合約名字
  • symbol 返回代幣符號
  • tokenURI 返回_tokenId對應的資源URI

ERC721BasicToken

pragma solidity ^0.4.23;

import "./ERC721Basic.sol";
import "./ERC721Receiver.sol";
import "../../math/SafeMath.sol";
import "../../AddressUtils.sol";

/**
 * @title ERC721 標準基本實現
 * @dev see https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md
 */
contract ERC721BasicToken is ERC721Basic {
  using SafeMath for uint256;
  using AddressUtils for address;

  // Equals to `bytes4(keccak256("onERC721Received(address,uint256,bytes)"))`
  // which can be also obtained as `ERC721Receiver(0).onERC721Received.selector`
  bytes4 constant ERC721_RECEIVED = 0xf0b9e5ba;

  // token ID 到 持有人owner的映射
  mapping (uint256 => address) internal tokenOwner;

  // token ID 到受權地址address的映射
  mapping (uint256 => address) internal tokenApprovals;

  // 持有人到持有的token數量的映射
  mapping (address => uint256) internal ownedTokensCount;

  // 持有人到操做人受權的映射
  mapping (address => mapping (address => bool)) internal operatorApprovals;

  /**
   * @dev 確保msg.sender是tokenId的持有人
   * @param _tokenId uint256 ID of the token to validate its ownership belongs to msg.sender
   */
  modifier onlyOwnerOf(uint256 _tokenId) {
    require(ownerOf(_tokenId) == msg.sender);
    _;
  }

  /**
   * @dev 經過檢查msg.sender是不是代幣的持有人,被受權或者操做人來確保msg.sender能夠交易一個token
   * @param _tokenId uint256 ID of the token to validate
   */
  modifier canTransfer(uint256 _tokenId) {
    require(isApprovedOrOwner(msg.sender, _tokenId));
    _;
  }

  /**
   * @dev 獲取持有者的代幣總數
   * @param _owner address to query the balance of
   * @return uint256 representing the amount owned by the passed address
   */
  function balanceOf(address _owner) public view returns (uint256) {
    require(_owner != address(0));
    return ownedTokensCount[_owner];
  }

  /**
   * @dev 根據token ID獲取持有者
   * @param _tokenId uint256 ID of the token to query the owner of
   * @return owner address currently marked as the owner of the given token ID
   */
  function ownerOf(uint256 _tokenId) public view returns (address) {
    address owner = tokenOwner[_tokenId];
    require(owner != address(0));
    return owner;
  }

  /**
   * @dev 指定的token是否存在
   * @param _tokenId uint256 ID of the token to query the existence of
   * @return whether the token exists
   */
  function exists(uint256 _tokenId) public view returns (bool) {
    address owner = tokenOwner[_tokenId];
    return owner != address(0);
  }

  /**
   * @dev 批准另外一我的address來交易指定的代幣
   * @dev 0 address 表示沒有受權的地址
   * @dev 給定的時間內,一個token只能有一個批准的地址
   * @dev 只有token的持有者或者受權的操做人才能夠調用
   * @param _to address to be approved for the given token ID
   * @param _tokenId uint256 ID of the token to be approved
   */
  function approve(address _to, uint256 _tokenId) public {
    address owner = ownerOf(_tokenId);
    require(_to != owner);
    require(msg.sender == owner || isApprovedForAll(owner, msg.sender));

    if (getApproved(_tokenId) != address(0) || _to != address(0)) {
      tokenApprovals[_tokenId] = _to;
      emit Approval(owner, _to, _tokenId);
    }
  }

  /**
   * @dev 獲取token被受權的地址,若是沒有設置地址則爲0
   * @param _tokenId uint256 ID of the token to query the approval of
   * @return address currently approved for the given token ID
   */
  function getApproved(uint256 _tokenId) public view returns (address) {
    return tokenApprovals[_tokenId];
  }

  /**
   * @dev 設置或者取消對操做人的受權
   * @dev 一個操做人能夠表明他們轉讓發送者的全部token
   * @param _to operator address to set the approval
   * @param _approved representing the status of the approval to be set
   */
  function setApprovalForAll(address _to, bool _approved) public {
    require(_to != msg.sender);
    operatorApprovals[msg.sender][_to] = _approved;
    emit ApprovalForAll(msg.sender, _to, _approved);
  }

  /**
   * @dev 查詢是否操做人被指定的持有者受權
   * @param _owner 要查詢的受權人地址
   * @param _operator 要查詢的受權操做人地址
   * @return bool whether the given operator is approved by the given owner
   */
  function isApprovedForAll(
    address _owner,
    address _operator
  )
    public
    view
    returns (bool)
  {
    return operatorApprovals[_owner][_operator];
  }

  /**
   * @dev 將指定的token全部權轉移給另一個地址
   * @dev 不鼓勵使用這個方法,儘可能使用`safeTransferFrom` 
   * @dev 要求 msg.sender 必須爲全部者,已受權或者操做人
   * @param _from current owner of the token
   * @param _to address to receive the ownership of the given token ID
   * @param _tokenId uint256 ID of the token to be transferred
  */
  function transferFrom(
    address _from,
    address _to,
    uint256 _tokenId
  )
    public
    canTransfer(_tokenId)
  {
    require(_from != address(0));
    require(_to != address(0));

    clearApproval(_from, _tokenId);
    removeTokenFrom(_from, _tokenId);
    addTokenTo(_to, _tokenId);

    emit Transfer(_from, _to, _tokenId);
  }

  /**
   * @dev 更安全的方法,將指定的token全部權轉移給另一個地址
   * @dev 若是目標地址是一個合約,必須實現 `onERC721Received`,這個要求安全交易並返回值
`bytes4(keccak256("onERC721Received(address,uint256,bytes)"))`; 不然交易被還原
   * @dev 要求 msg.sender 必須爲全部者,已受權或者操做人
   * @param _from current owner of the token
   * @param _to address to receive the ownership of the given token ID
   * @param _tokenId uint256 ID of the token to be transferred
  */
  function safeTransferFrom(
    address _from,
    address _to,
    uint256 _tokenId
  )
    public
    canTransfer(_tokenId)
  {
    safeTransferFrom(_from, _to, _tokenId, "");
  }

   /**
   * @dev 更安全的方法,將指定的token全部權轉移給另一個地址
   * @dev 若是目標地址是一個合約,必須實現 `onERC721Received`,這個要求安全交易並返回值
`bytes4(keccak256("onERC721Received(address,uint256,bytes)"))`; 不然交易被還原
   * @dev 要求 msg.sender 必須爲全部者,已受權或者操做人
   * @param _from current owner of the token
   * @param _to address to receive the ownership of the given token ID
   * @param _tokenId uint256 ID of the token to be transferred
   * @param _data bytes data to send along with a safe transfer check
   */
  function safeTransferFrom(
    address _from,
    address _to,
    uint256 _tokenId,
    bytes _data
  )
    public
    canTransfer(_tokenId)
  {
    transferFrom(_from, _to, _tokenId);
    require(checkAndCallSafeTransfer(_from, _to, _tokenId, _data));
  }

  /**
   * @dev 返回給定的spender是否能夠交易一個給定的token
   * @param _spender address of the spender to query
   * @param _tokenId uint256 ID of the token to be transferred
   * @return bool whether the msg.sender is approved for the given token ID,
   *  is an operator of the owner, or is the owner of the token
   */
  function isApprovedOrOwner(
    address _spender,
    uint256 _tokenId
  )
    internal
    view
    returns (bool)
  {
    address owner = ownerOf(_tokenId);
    return (
      _spender == owner ||
      getApproved(_tokenId) == _spender ||
      isApprovedForAll(owner, _spender)
    );
  }

  /**
   * @dev 增發一個新token的內部方法
   * @dev 若是增發的token已經存在則撤銷
   * @param _to The address that will own the minted token
   * @param _tokenId uint256 ID of the token to be minted by the msg.sender
   */
  function _mint(address _to, uint256 _tokenId) internal {
    require(_to != address(0));
    addTokenTo(_to, _tokenId);
    emit Transfer(address(0), _to, _tokenId);
  }

  /**
   * @dev 銷燬一個token的內部方法
   * @dev 若是token不存在則撤銷
   * @param _tokenId uint256 ID of the token being burned by the msg.sender
   */
  function _burn(address _owner, uint256 _tokenId) internal {
    clearApproval(_owner, _tokenId);
    removeTokenFrom(_owner, _tokenId);
    emit Transfer(_owner, address(0), _tokenId);
  }

  /**
   * @dev 清除當前的給定token的受權,內部方法
   * @dev 若是給定地址不是token的持有者則撤銷
   * @param _owner owner of the token
   * @param _tokenId uint256 ID of the token to be transferred
   */
  function clearApproval(address _owner, uint256 _tokenId) internal {
    require(ownerOf(_tokenId) == _owner);
    if (tokenApprovals[_tokenId] != address(0)) {
      tokenApprovals[_tokenId] = address(0);
      emit Approval(_owner, address(0), _tokenId);
    }
  }

  /**
   * @dev 內部方法,將給定的token添加到給定地址列表中
   * @param _to address 指定token的新全部者
   * @param _tokenId uint256 ID of the token to be added to the tokens list of the given address
   */
  function addTokenTo(address _to, uint256 _tokenId) internal {
    require(tokenOwner[_tokenId] == address(0));
    tokenOwner[_tokenId] = _to;
    ownedTokensCount[_to] = ownedTokensCount[_to].add(1);
  }

  /**
   * @dev 內部方法,將給定的token從地址列表中移除
   * @param _from address 給定token的以前持有中地址
   * @param _tokenId uint256 ID of the token to be removed from the tokens list of the given address
   */
  function removeTokenFrom(address _from, uint256 _tokenId) internal {
    require(ownerOf(_tokenId) == _from);
    ownedTokensCount[_from] = ownedTokensCount[_from].sub(1);
    tokenOwner[_tokenId] = address(0);
  }

  /**
   * @dev 內部函數,調用目標地址上的 `onERC721Received` 
   * @dev 若是目標地址不是合同則不執行調用
   * @param _from address representing the previous owner of the given token ID
   * @param _to target address that will receive the tokens
   * @param _tokenId uint256 ID of the token to be transferred
   * @param _data bytes optional data to send along with the call
   * @return whether the call correctly returned the expected magic value
   */
  function checkAndCallSafeTransfer(
    address _from,
    address _to,
    uint256 _tokenId,
    bytes _data
  )
    internal
    returns (bool)
  {
    if (!_to.isContract()) {
      return true;
    }
    bytes4 retval = ERC721Receiver(_to).onERC721Received(
      _from, _tokenId, _data);
    return (retval == ERC721_RECEIVED);
  }
}

ERC721BasicToken 實現了ERC721Basic合約定義的接口方法,主要對token的持有人的一個添加和修改,以及受權和交易的管理,實現了基本的非同質化token的業務邏輯。具體方法實現並不難,就是對映射的公有變量的管理,可是對於權限和安全驗證值得關注,好比函數修改器還有require源碼分析

ERC721Token.sol

pragma solidity ^0.4.23;

import "./ERC721.sol";
import "./ERC721BasicToken.sol";

/**
 * @title 完整 ERC721 Token
 * 該實現包括全部ERC721標準必須的和可選的方法,此外還包括使用操做者批准全部功能
 * @dev see https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md
 */
contract ERC721Token is ERC721, ERC721BasicToken {
  // 代幣名稱
  string internal name_;

  // 代幣符號
  string internal symbol_;

  // 全部者到全部者擁有的代幣列表的映射
  mapping(address => uint256[]) internal ownedTokens;

  // 全部者代幣列表中代幣ID到索引的映射
  mapping(uint256 => uint256) internal ownedTokensIndex;

  // 保存全部代幣ID的數組,用於枚舉
  uint256[] internal allTokens;

  // allTokens數組中代幣ID到索引的映射
  mapping(uint256 => uint256) internal allTokensIndex;

  // 可選的代幣資源URIs映射
  mapping(uint256 => string) internal tokenURIs;

  /**
   * @dev Constructor function
   */
  constructor(string _name, string _symbol) public {
    name_ = _name;
    symbol_ = _symbol;
  }

  /**
   * @dev 獲取代幣名稱
   * @return string representing the token name
   */
  function name() public view returns (string) {
    return name_;
  }

  /**
   * @dev 獲取代幣符號
   * @return string representing the token symbol
   */
  function symbol() public view returns (string) {
    return symbol_;
  }

  /**
   * @dev 根據_tokenId返回對應的資源URI
   * @dev 若是token不存在異常返回空字符串
   * @param _tokenId uint256 ID of the token to query
   */
  function tokenURI(uint256 _tokenId) public view returns (string) {
    require(exists(_tokenId));
    return tokenURIs[_tokenId];
  }

  /**
   * @dev 獲取token id 經過給定的token列表中的索引
   * @param _owner address owning the tokens list to be accessed
   * @param _index uint256 representing the index to be accessed of the requested tokens list
   * @return uint256 token ID at the given index of the tokens list owned by the requested address
   */
  function tokenOfOwnerByIndex(
    address _owner,
    uint256 _index
  )
    public
    view
    returns (uint256)
  {
    require(_index < balanceOf(_owner));
    return ownedTokens[_owner][_index];
  }

  /**
   * @dev 獲取合約存儲的token總數
   * @return uint256 representing the total amount of tokens
   */
  function totalSupply() public view returns (uint256) {
    return allTokens.length;
  }

  /**
   * @dev 根據token 索引值獲取合約中token的
   * @dev 若是索引大於等於token總數則撤銷
   * @param _index uint256 representing the index to be accessed of the tokens list
   * @return uint256 token ID at the given index of the tokens list
   */
  function tokenByIndex(uint256 _index) public view returns (uint256) {
    require(_index < totalSupply());
    return allTokens[_index];
  }

  /**
   * @dev 內部方法,給存在token添加token URI
   * @dev Reverts if the token ID does not exist
   * @param _tokenId uint256 ID of the token to set its URI
   * @param _uri string URI to assign
   */
  function _setTokenURI(uint256 _tokenId, string _uri) internal {
    require(exists(_tokenId));
    tokenURIs[_tokenId] = _uri;
  }

  /**
   * @dev 內部方法,添加token ID 到給定的地址的列表中
   * @param _to address 給定token ID的新的持有者
   * @param _tokenId uint256 ID of the token to be added to the tokens list of the given address
   */
  function addTokenTo(address _to, uint256 _tokenId) internal {
    // 調用父合約的addTokenTo
    super.addTokenTo(_to, _tokenId);
    uint256 length = ownedTokens[_to].length;
    ownedTokens[_to].push(_tokenId);
    //當前的長度做爲索引
    ownedTokensIndex[_tokenId] = length;
  }

  /**
   * @dev 內部方法,從一個給定地址的列表中移除token 
   * @param _from address 給定token ID的以前的持有者address  
   * @param _tokenId uint256 ID of the token to be removed from the tokens list of the given address
   */
  function removeTokenFrom(address _from, uint256 _tokenId) internal {
    // 調用父合約的移除方法
    super.removeTokenFrom(_from, _tokenId);
    // 獲取token的索引
    uint256 tokenIndex = ownedTokensIndex[_tokenId];
    // 獲取持有人token的最後一個token索引
    uint256 lastTokenIndex = ownedTokens[_from].length.sub(1);
    // 獲取最後一個token
    uint256 lastToken = ownedTokens[_from][lastTokenIndex];
    //將最後一個token放到被刪除的索引位置,lastTokenIndex置0
    ownedTokens[_from][tokenIndex] = lastToken;
    ownedTokens[_from][lastTokenIndex] = 0; 
    // 注意這裏須要處理單元素數組,tokenIndex和lastTokenIndex都將置0.而後能夠確保將ownedTokens列表中刪除_tokenId,首先將lastToken換到第一個位置,而後刪除列表最後位置的元素   
    ownedTokens[_from].length--;
    ownedTokensIndex[_tokenId] = 0;
    ownedTokensIndex[lastToken] = tokenIndex;
  }

  /**
   * @dev 內部方法,增發一個新的token
   * @dev 若是token已經存在了就撤銷
   * @param _to address the beneficiary that will own the minted token
   * @param _tokenId uint256 ID of the token to be minted by the msg.sender
   */
  function _mint(address _to, uint256 _tokenId) internal {
    super._mint(_to, _tokenId);

    allTokensIndex[_tokenId] = allTokens.length;
    allTokens.push(_tokenId);
  }

  /**
   * @dev 內部方法,銷燬一個指定的token
   * @dev token不存在則撤銷
   * @param _owner owner of the token to burn
   * @param _tokenId uint256 ID of the token being burned by the msg.sender
   */
  function _burn(address _owner, uint256 _tokenId) internal {
    super._burn(_owner, _tokenId);

    // 清除資源URI
    if (bytes(tokenURIs[_tokenId]).length != 0) {
      delete tokenURIs[_tokenId];
    }

    // 作全部的token數組後續處理
    uint256 tokenIndex = allTokensIndex[_tokenId];
    uint256 lastTokenIndex = allTokens.length.sub(1);
    uint256 lastToken = allTokens[lastTokenIndex];
    // 能夠參考增發removeTokenFrom
    allTokens[tokenIndex] = lastToken;
    allTokens[lastTokenIndex] = 0;

    allTokens.length--;
    allTokensIndex[_tokenId] = 0;
    allTokensIndex[lastToken] = tokenIndex;
  }

}

ERC721Token實現了完整的ERC721標準,在繼承了ERC721BasicToken的基礎上增長了一些token的操做,主要在包括token的元數據,資源URI,增發銷燬,還有就是token索引的映射關係。對於具體實現咱們根據實際狀況經過繼承ERC721BasicToken或者ERC721Token來添加本身的業務邏輯。ui

OpenZeppelin ERC721源碼分析到這裏就結束了。

轉載請註明: 轉載自Ryan是菜鳥 | LNMP技術棧筆記

若是以爲本篇文章對您十分有益,何不 打賞一下

謝謝打賞

本文連接地址: OpenZeppelin ERC721源碼分析

相關文章
相關標籤/搜索