123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- pragma solidity ^0.4.24;
- import "./IERC721Enumerable.sol";
- import "./ERC721.sol";
- import "../../introspection/ERC165.sol";
- /**
- * @title ERC-721 Non-Fungible Token with optional enumeration extension logic
- * @dev See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md
- */
- contract ERC721Enumerable is ERC165, ERC721, IERC721Enumerable {
- // Mapping from owner to list of owned token IDs
- mapping(address => uint256[]) private _ownedTokens;
- // Mapping from token ID to index of the owner tokens list
- mapping(uint256 => uint256) private _ownedTokensIndex;
- // Array with all token ids, used for enumeration
- uint256[] private _allTokens;
- // Mapping from token id to position in the allTokens array
- mapping(uint256 => uint256) private _allTokensIndex;
- bytes4 private constant _InterfaceId_ERC721Enumerable = 0x780e9d63;
- /**
- * 0x780e9d63 ===
- * bytes4(keccak256('totalSupply()')) ^
- * bytes4(keccak256('tokenOfOwnerByIndex(address,uint256)')) ^
- * bytes4(keccak256('tokenByIndex(uint256)'))
- */
- /**
- * @dev Constructor function
- */
- constructor () public {
- // register the supported interface to conform to ERC721 via ERC165
- _registerInterface(_InterfaceId_ERC721Enumerable);
- }
- /**
- * @dev Gets the token ID at a given index of the tokens list of the requested owner
- * @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 Gets the total amount of tokens stored by the contract
- * @return uint256 representing the total amount of tokens
- */
- function totalSupply() public view returns (uint256) {
- return _allTokens.length;
- }
- /**
- * @dev Gets the token ID at a given index of all the tokens in this contract
- * Reverts if the index is greater or equal to the total number of tokens
- * @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 Internal function to add a token ID to the list of a given address
- * This function is internal due to language limitations, see the note in ERC721.sol.
- * It is not intended to be called by custom derived contracts: in particular, it emits no Transfer event.
- * @param to address representing the new owner of the given 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 {
- super._addTokenTo(to, tokenId);
- _addTokenToOwnerEnumeration(to, tokenId);
- }
- /**
- * @dev Internal function to remove a token ID from the list of a given address
- * This function is internal due to language limitations, see the note in ERC721.sol.
- * It is not intended to be called by custom derived contracts: in particular, it emits no Transfer event,
- * and doesn't clear approvals.
- * @param from address representing the previous owner of the given token ID
- * @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);
- _removeTokenFromOwnerEnumeration(from, tokenId);
- // Since the token is being destroyed, we also clear its index
- // TODO(nventuro): 0 is still a valid index, so arguably this isnt really helpful, remove?
- _ownedTokensIndex[tokenId] = 0;
- }
- /**
- * @dev Internal function to transfer ownership of a given token ID to another address.
- * As opposed to transferFrom, this imposes no restrictions on 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) internal {
- super._transferFrom(from, to, tokenId);
- _removeTokenFromOwnerEnumeration(from, tokenId);
- _addTokenToOwnerEnumeration(to, tokenId);
- }
- /**
- * @dev Internal function to mint a new token
- * Reverts if the given token ID already exists
- * @param to address the beneficiary that will own the minted token
- * @param tokenId uint256 ID of the token to be minted
- */
- function _mint(address to, uint256 tokenId) internal {
- super._mint(to, tokenId);
- _allTokensIndex[tokenId] = _allTokens.length;
- _allTokens.push(tokenId);
- }
- /**
- * @dev Internal function to burn a specific token
- * Reverts if the token does not exist
- * Deprecated, use _burn(uint256) instead
- * @param owner owner of the token to burn
- * @param tokenId uint256 ID of the token being burned
- */
- function _burn(address owner, uint256 tokenId) internal {
- super._burn(owner, tokenId);
- // Reorg all tokens array
- uint256 tokenIndex = _allTokensIndex[tokenId];
- uint256 lastTokenIndex = _allTokens.length.sub(1);
- uint256 lastToken = _allTokens[lastTokenIndex];
- _allTokens[tokenIndex] = lastToken;
- _allTokens[lastTokenIndex] = 0;
- _allTokens.length--;
- _allTokensIndex[tokenId] = 0;
- _allTokensIndex[lastToken] = tokenIndex;
- }
- /**
- * @dev Gets the list of token IDs of the requested owner
- * @param owner address owning the tokens
- * @return uint256[] List of token IDs owned by the requested address
- */
- function _tokensOfOwner(address owner) internal view returns (uint256[] storage) {
- return _ownedTokens[owner];
- }
- /**
- * @dev Private function to add a token to this extension's ownership-tracking data structures.
- * @param to address representing the new owner of the given token ID
- * @param tokenId uint256 ID of the token to be added to the tokens list of the given address
- */
- function _addTokenToOwnerEnumeration(address to, uint256 tokenId) private {
- uint256 newOwnedTokensLength = _ownedTokens[to].push(tokenId);
- // No need to use SafeMath since the length after a push cannot be zero
- _ownedTokensIndex[tokenId] = newOwnedTokensLength - 1;
- }
- /**
- * @dev Private function to remove a token from this extension's ownership-tracking data structures. Note that
- * while the token is not assigned a new owner, the _ownedTokensIndex mapping is _not_ updated: this allows for
- * gas optimizations e.g. when performing a transfer operation (avoiding double writes).
- * This has O(1) time complexity, but alters the order of the _ownedTokens array.
- * @param from address representing the previous owner of the given token ID
- * @param tokenId uint256 ID of the token to be removed from the tokens list of the given address
- */
- function _removeTokenFromOwnerEnumeration(address from, uint256 tokenId) private {
- // To prevent a gap in from's tokens array, we store the last token in the index of the token to delete, and
- // then delete the last slot (swap and pop).
- uint256 lastTokenIndex = _ownedTokens[from].length.sub(1);
- uint256 lastTokenId = _ownedTokens[from][lastTokenIndex];
- uint256 tokenIndex = _ownedTokensIndex[tokenId];
- _ownedTokens[from][tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token
- _ownedTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index
- // Note that this will handle single-element arrays. In that case, both tokenIndex and lastTokenIndex are going
- // to be zero. The swap operation will therefore have no effect, but the token _will_ be deleted during the
- // 'pop' operation.
- // This also deletes the contents at the last position of the array
- _ownedTokens[from].length--;
- // Note that _ownedTokensIndex[tokenId] hasn't been cleared: it still points to the old slot (now occcupied by
- // lasTokenId).
- }
- }
|