当前位置:网站首页>BoredApeYachtClub 无聊猿-NFT 源码解析第二节

BoredApeYachtClub 无聊猿-NFT 源码解析第二节

2022-08-11 05:18:00 Nolan Mace

BoredApeYachtClub 无聊猿 ERC721实现 BAYC智能合约中 ERC721实现继承了以下类Context, ERC165, IERC721, IERC721Metadata, IERC721Enumerable。

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
import "./Context.sol";
import "./ERC165.sol";
import "./IERC721.sol";
import "./IERC721Metadata.sol";
import "./IERC721Enumerable.sol";
import "./SafeMath.sol";
import "./Address.sol";
import "./EnumerableSet.sol";
import "./EnumerableMap.sol";
import "./Strings.sol";
import "./IERC721Receiver.sol";

/**
 * @title ERC721 Non-Fungible Token Standard basic implementation
 * @dev see https://eips.ethereum.org/EIPS/eip-721
 */
contract ERC721 is Context, ERC165, IERC721, IERC721Metadata, IERC721Enumerable {
    using SafeMath for uint256;
    using Address for address;
    using EnumerableSet for EnumerableSet.UintSet;
    using EnumerableMap for EnumerableMap.UintToAddressMap;
    using Strings for uint256;

    // 0x150b7a02 等价于 bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))
    bytes4 private constant _ERC721_RECEIVED = 0x150b7a02;

    // 拥有者的地址映射 tokenId的set列表 uintSet 定义在Enumerable合约中
    // struct UintSet { Set _inner;}
    // struct Set {bytes32[] _values; mapping (bytes32 => uint256) _indexes;}
    mapping (address => EnumerableSet.UintSet) private _holderTokens;


    // tokenid 与拥有者地址的映射
    // struct UintToAddressMap { Map _inner; }
    // struct Map { MapEntry[] _entries;mapping (bytes32 => uint256) _indexes;}
    // struct MapEntry {bytes32 _key;bytes32 _value;}
    EnumerableMap.UintToAddressMap private _tokenOwners;

    // tokenId 与 approval地址的映射
    mapping (uint256 => address) private _tokenApprovals;

    // 拥有者 对 操作者的授权状态映射
    mapping (address => mapping (address => bool)) private _operatorApprovals;

    // Token name
    string private _name;

    // Token symbol
    string private _symbol;

    // tokenURL集合  元数据不直接存储在链上。您的合约中必须有一个指向该外部元数据的链接(tokenURI)
    // ERC721 是用来描述觉有唯一性的作品, URI 是作品的描述文件(通常称为元数据)
    mapping (uint256 => string) private _tokenURIs;

    // ERC721.sol 中的基本实现读取内存中的baseURI并即时连接生成的字符串,而不将它们存储为状态变量。
    // 如果您想将 tokenURIs 与 baseURI 分离,则使用它。否则,如果未设置 baseURI,则基本实现返回空字符串
    string private _baseURI;
    /*
     *     bytes4(keccak256('balanceOf(address)')) == 0x70a08231
     *     bytes4(keccak256('ownerOf(uint256)')) == 0x6352211e
     *     bytes4(keccak256('approve(address,uint256)')) == 0x095ea7b3
     *     bytes4(keccak256('getApproved(uint256)')) == 0x081812fc
     *     bytes4(keccak256('setApprovalForAll(address,bool)')) == 0xa22cb465
     *     bytes4(keccak256('isApprovedForAll(address,address)')) == 0xe985e9c5
     *     bytes4(keccak256('transferFrom(address,address,uint256)')) == 0x23b872dd
     *     bytes4(keccak256('safeTransferFrom(address,address,uint256)')) == 0x42842e0e
     *     bytes4(keccak256('safeTransferFrom(address,address,uint256,bytes)')) == 0xb88d4fde
     *
     *     => 0x70a08231 ^ 0x6352211e ^ 0x095ea7b3 ^ 0x081812fc ^
     *        0xa22cb465 ^ 0xe985e9c5 ^ 0x23b872dd ^ 0x42842e0e ^ 0xb88d4fde == 0x80ac58cd
     */
    bytes4 private constant _INTERFACE_ID_ERC721 = 0x80ac58cd;

    /*
     *     bytes4(keccak256('name()')) == 0x06fdde03
     *     bytes4(keccak256('symbol()')) == 0x95d89b41
     *     bytes4(keccak256('tokenURI(uint256)')) == 0xc87b56dd
     *
     *     => 0x06fdde03 ^ 0x95d89b41 ^ 0xc87b56dd == 0x5b5e139f
     */
    bytes4 private constant _INTERFACE_ID_ERC721_METADATA = 0x5b5e139f;

    /*
     *     bytes4(keccak256('totalSupply()')) == 0x18160ddd
     *     bytes4(keccak256('tokenOfOwnerByIndex(address,uint256)')) == 0x2f745c59
     *     bytes4(keccak256('tokenByIndex(uint256)')) == 0x4f6ccce7
     *
     *     => 0x18160ddd ^ 0x2f745c59 ^ 0x4f6ccce7 == 0x780e9d63
     */
    bytes4 private constant _INTERFACE_ID_ERC721_ENUMERABLE = 0x780e9d63;

    /**

     * 初始化合约  设置name 和 symbol
     */
    constructor (string memory name_, string memory symbol_) public {
        _name = name_;
        _symbol = symbol_;

        // register the supported interfaces to conform to ERC721 via ERC165
        // 注册接口 
        _registerInterface(_INTERFACE_ID_ERC721);
        _registerInterface(_INTERFACE_ID_ERC721_METADATA);
        _registerInterface(_INTERFACE_ID_ERC721_ENUMERABLE);
    }

    /**
     * 查询地址余额
     */
    function balanceOf(address owner) public view virtual override returns (uint256) {
        require(owner != address(0), "ERC721: balance query for the zero address");
        return _holderTokens[owner].length();
    }

    /**
     * @dev See {IERC721-ownerOf}.
     * 查询 token 拥有者
     */
    function ownerOf(uint256 tokenId) public view virtual override returns (address) {
        return _tokenOwners.get(tokenId, "ERC721: owner query for nonexistent token");
    }

    /**
     * @dev See {IERC721Metadata-name}.
     * 查询name
     */
    function name() public view virtual override returns (string memory) {
        return _name;
    }

    /**
     * 查询symbol
     */
    function symbol() public view virtual override returns (string memory) {
        return _symbol;
    }

    /**
     * 查询token的URL
     */
    function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
        require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");

        string memory _tokenURI = _tokenURIs[tokenId];
        string memory base = baseURI();

        // If there is no base URI, return the token URI.
        // 如果没有base URL 则返回 token url
        if (bytes(base).length == 0) {
            return _tokenURI;
        }
        // If both are set, concatenate the baseURI and tokenURI (via abi.encodePacked).
        // 如果base url 和 tokenurl 都存在 则连接两者 返回值
        if (bytes(_tokenURI).length > 0) {
            return string(abi.encodePacked(base, _tokenURI));
        }
        // If there is a baseURI but no tokenURI, concatenate the tokenID to the baseURI.
        // 如果有base URl 却没有 tokenurl 则返回 baseurl + tokenId
        return string(abi.encodePacked(base, tokenId.toString()));
    }

    /**
    * 返回base URL
    */
    function baseURI() public view virtual returns (string memory) {
        return _baseURI;
    }

    /**
     * 返回地址拥有的tokenId 
     */
    function tokenOfOwnerByIndex(address owner, uint256 index) public view virtual override returns (uint256) {
        return _holderTokens[owner].at(index);
    }

    /**
     * 返回总供应量tokenId
     */
    function totalSupply() public view virtual override returns (uint256) {
        // _tokenOwners are indexed by tokenIds, so .length() returns the number of tokenIds
        return _tokenOwners.length();
    }

    /**
     * 根据Index返回tokenId
     */
    function tokenByIndex(uint256 index) public view virtual override returns (uint256) {
        (uint256 tokenId, ) = _tokenOwners.at(index);
        return tokenId;
    }

    /**
     * 给to地址,approve证明
     */
    function approve(address to, uint256 tokenId) public virtual override {
        address owner = ERC721.ownerOf(tokenId);
        require(to != owner, "ERC721: approval to current owner");

        require(_msgSender() == owner || ERC721.isApprovedForAll(owner, _msgSender()),
            "ERC721: approve caller is not owner nor approved for all"
        );

        _approve(to, tokenId);
    }

    /**
     * 返回该token的approve证明
     */
    function getApproved(uint256 tokenId) public view virtual override returns (address) {
        require(_exists(tokenId), "ERC721: approved query for nonexistent token");

        return _tokenApprovals[tokenId];
    }

    /**
     * 设置operator的approve证明
     */
    function setApprovalForAll(address operator, bool approved) public virtual override {
        require(operator != _msgSender(), "ERC721: approve to caller");

        _operatorApprovals[_msgSender()][operator] = approved;
        emit ApprovalForAll(_msgSender(), operator, approved);
    }

    /**
     * 判断是否operator地址拥有owner地址的approve.
     */
    function isApprovedForAll(address owner, address operator) public view virtual override returns (bool) {
        return _operatorApprovals[owner][operator];
    }

    /**
     * 将from地址的token 转移到to地址.
     */
    function transferFrom(address from, address to, uint256 tokenId) public virtual override {
        //solhint-disable-next-line max-line-length
        require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved");

        _transfer(from, to, tokenId);
    }

    /**
     * 将from地址的token 转移到to地址.
     */
    function safeTransferFrom(address from, address to, uint256 tokenId) public virtual override {
        safeTransferFrom(from, to, tokenId, "");
    }

    /**
     * 将from地址的token 转移到to地址.
     */
    function safeTransferFrom(address from, address to, uint256 tokenId, bytes memory _data) public virtual override {
        // 要求该转移发起者是token拥有者或者是拥有approve
        require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved");
        _safeTransfer(from, to, tokenId, _data);
    }

    /**

     * 转移token 从from地址到 to地址
     */
    function _safeTransfer(address from, address to, uint256 tokenId, bytes memory _data) internal virtual {
        _transfer(from, to, tokenId);
        require(_checkOnERC721Received(from, to, tokenId, _data), "ERC721: transfer to non ERC721Receiver implementer");
    }

    /**
     * 是否存在该token
     */
    function _exists(uint256 tokenId) internal view virtual returns (bool) {
        return _tokenOwners.contains(tokenId);
    }

    /**
     * 是否是该token的拥有者或者有approve
     */
    function _isApprovedOrOwner(address spender, uint256 tokenId) internal view virtual returns (bool) {
        require(_exists(tokenId), "ERC721: operator query for nonexistent token");
        address owner = ERC721.ownerOf(tokenId);
        return (spender == owner || getApproved(tokenId) == spender || ERC721.isApprovedForAll(owner, spender));
    }

    /**
     * 铸造token
     */
    function _safeMint(address to, uint256 tokenId) internal virtual {
        _safeMint(to, tokenId, "");
    }

    /**
     * 铸造toekn
     */
    function _safeMint(address to, uint256 tokenId, bytes memory _data) internal virtual {
        _mint(to, tokenId);
        //检查地址,如果地址是账户,则直接转账,是合约的话则会判断,该合约是否有引用IERC721Receiver这个接口,
        //并返回onERC721Received.selector。没有的话,会进行一个回滚。
        require(_checkOnERC721Received(address(0), to, tokenId, _data), "ERC721: transfer to non ERC721Receiver implementer");
    }

    /**
     * 铸造token
     */
    function _mint(address to, uint256 tokenId) internal virtual {
        //检查 to地址是否是个空地址
        require(to != address(0), "ERC721: mint to the zero address");
        //检查该tokenId是否存在
        require(!_exists(tokenId), "ERC721: token already minted");
        //定义在铸造之前执行的可继承的函数、该合约内并没有写方法体。
        _beforeTokenTransfer(address(0), to, tokenId);
        //在holderTokens添加to-tokenId映射
        _holderTokens[to].add(tokenId);
        //在tokenOwners添加tokenId-to映射
        _tokenOwners.set(tokenId, to);
        //触发Transfer事件
        emit Transfer(address(0), to, tokenId);
    }

    /**
     * 销毁token
     */
    function _burn(uint256 tokenId) internal virtual {
        //获取tokenId拥有者地址
        address owner = ERC721.ownerOf(tokenId); // internal owner
        
        _beforeTokenTransfer(owner, address(0), tokenId);

        // Clear approvals 清除该token的所有授权
        _approve(address(0), tokenId);

        // Clear metadata (if any) 清除元数据 token元数据不存储在链上,一般在外部系统存储
        if (bytes(_tokenURIs[tokenId]).length != 0) {
            delete _tokenURIs[tokenId];
        }

        //移除holderToken的owner-tokenId映射
        _holderTokens[owner].remove(tokenId);
        //移除tokenOwner里面的 tokenId-onwer的映射
        _tokenOwners.remove(tokenId);
        //触发事件Transfer
        emit Transfer(owner, address(0), tokenId);
    }

    /**
     * 转移token
     */
    function _transfer(address from, address to, uint256 tokenId) internal virtual {
        //检查token的拥有者是否是from
        require(ERC721.ownerOf(tokenId) == from, "ERC721: transfer of token that is not own"); // internal owner
        //检查to是否为空地址
        require(to != address(0), "ERC721: transfer to the zero address");
        
        _beforeTokenTransfer(from, to, tokenId);

        // Clear approvals from the previous owner 清除该token的所有授权
        _approve(address(0), tokenId);
        //移除 from-tokenId的映射
        _holderTokens[from].remove(tokenId);
        //添加to-tokenId的映射
        _holderTokens[to].add(tokenId);
        //设置tokenId-to映射
        _tokenOwners.set(tokenId, to);

        emit Transfer(from, to, tokenId);
    }

    /**
     * 设置tokenID的元数据存储URL
     */
    function _setTokenURI(uint256 tokenId, string memory _tokenURI) internal virtual {
        require(_exists(tokenId), "ERC721Metadata: URI set of nonexistent token");
        _tokenURIs[tokenId] = _tokenURI;
    }

    /**
     * 设置BaseURl
     */
    function _setBaseURI(string memory baseURI_) internal virtual {
        _baseURI = baseURI_;
    }

    /**
     * 检查地址,如果地址是账户,则返回true,是合约的话则会判断,该合约是否有引用IERC721Receiver这个接口,
     */
    function _checkOnERC721Received(address from, address to, uint256 tokenId, bytes memory _data)
        private returns (bool)
    {
        //判断是否是合约地址,不是则返回true
        if (!to.isContract()) {
            return true;
        }
        //计算选择器以及参数的ABI编码
        bytes memory returndata = to.functionCall(abi.encodeWithSelector(
            IERC721Receiver(to).onERC721Received.selector,
            _msgSender(),
            from,
            tokenId,
            _data
        ), "ERC721: transfer to non ERC721Receiver implementer");
        // 解码returndata
        bytes4 retval = abi.decode(returndata, (bytes4));
        return (retval == _ERC721_RECEIVED);
    }

    /**
     * 将token授权给to地址
     */
    function _approve(address to, uint256 tokenId) internal virtual {
        _tokenApprovals[tokenId] = to;
        emit Approval(ERC721.ownerOf(tokenId), to, tokenId); // internal owner
    }

    function _beforeTokenTransfer(address from, address to, uint256 tokenId) internal virtual { }
}

原网站

版权声明
本文为[Nolan Mace]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_42188030/article/details/126193935