|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// ERC721A Contracts v3.3.0 |
| 3 | +// Creator: Chiru Labs |
| 4 | + |
| 5 | +pragma solidity ^0.8.4; |
| 6 | + |
| 7 | +import "./IERC721AQueryable.sol"; |
| 8 | +import "../ERC721AVirtualApprove.sol"; |
| 9 | + |
| 10 | +/** |
| 11 | + * @title ERC721A Queryable |
| 12 | + * @dev ERC721A subclass with convenience query functions. |
| 13 | + */ |
| 14 | +abstract contract ERC721AQueryable is ERC721A, IERC721AQueryable { |
| 15 | + /** |
| 16 | + * @dev Returns the `TokenOwnership` struct at `tokenId` without reverting. |
| 17 | + * |
| 18 | + * If the `tokenId` is out of bounds: |
| 19 | + * - `addr` = `address(0)` |
| 20 | + * - `startTimestamp` = `0` |
| 21 | + * - `burned` = `false` |
| 22 | + * |
| 23 | + * If the `tokenId` is burned: |
| 24 | + * - `addr` = `<Address of owner before token was burned>` |
| 25 | + * - `startTimestamp` = `<Timestamp when token was burned>` |
| 26 | + * - `burned = `true` |
| 27 | + * |
| 28 | + * Otherwise: |
| 29 | + * - `addr` = `<Address of owner>` |
| 30 | + * - `startTimestamp` = `<Timestamp of start of ownership>` |
| 31 | + * - `burned = `false` |
| 32 | + */ |
| 33 | + function explicitOwnershipOf(uint256 tokenId) public view override returns (TokenOwnership memory) { |
| 34 | + TokenOwnership memory ownership; |
| 35 | + if (tokenId < _startTokenId() || tokenId >= _currentIndex) { |
| 36 | + return ownership; |
| 37 | + } |
| 38 | + ownership = _ownerships[tokenId]; |
| 39 | + if (ownership.burned) { |
| 40 | + return ownership; |
| 41 | + } |
| 42 | + return _ownershipOf(tokenId); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @dev Returns an array of `TokenOwnership` structs at `tokenIds` in order. |
| 47 | + * See {ERC721AQueryable-explicitOwnershipOf} |
| 48 | + */ |
| 49 | + function explicitOwnershipsOf(uint256[] memory tokenIds) external view override returns (TokenOwnership[] memory) { |
| 50 | + unchecked { |
| 51 | + uint256 tokenIdsLength = tokenIds.length; |
| 52 | + TokenOwnership[] memory ownerships = new TokenOwnership[](tokenIdsLength); |
| 53 | + for (uint256 i; i != tokenIdsLength; ++i) { |
| 54 | + ownerships[i] = explicitOwnershipOf(tokenIds[i]); |
| 55 | + } |
| 56 | + return ownerships; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @dev Returns an array of token IDs owned by `owner`, |
| 62 | + * in the range [`start`, `stop`) |
| 63 | + * (i.e. `start <= tokenId < stop`). |
| 64 | + * |
| 65 | + * This function allows for tokens to be queried if the collection |
| 66 | + * grows too big for a single call of {ERC721AQueryable-tokensOfOwner}. |
| 67 | + * |
| 68 | + * Requirements: |
| 69 | + * |
| 70 | + * - `start` < `stop` |
| 71 | + */ |
| 72 | + /* solhint-disable*/ |
| 73 | + function tokensOfOwnerIn( |
| 74 | + address owner, |
| 75 | + uint256 start, |
| 76 | + uint256 stop |
| 77 | + ) external view override returns (uint256[] memory) { |
| 78 | + unchecked { |
| 79 | + if (start >= stop) revert InvalidQueryRange(); |
| 80 | + uint256 tokenIdsIdx; |
| 81 | + uint256 stopLimit = _currentIndex; |
| 82 | + // Set `start = max(start, _startTokenId())`. |
| 83 | + if (start < _startTokenId()) { |
| 84 | + start = _startTokenId(); |
| 85 | + } |
| 86 | + // Set `stop = min(stop, _currentIndex)`. |
| 87 | + if (stop > stopLimit) { |
| 88 | + stop = stopLimit; |
| 89 | + } |
| 90 | + uint256 tokenIdsMaxLength = balanceOf(owner); |
| 91 | + // Set `tokenIdsMaxLength = min(balanceOf(owner), stop - start)`, |
| 92 | + // to cater for cases where `balanceOf(owner)` is too big. |
| 93 | + if (start < stop) { |
| 94 | + uint256 rangeLength = stop - start; |
| 95 | + if (rangeLength < tokenIdsMaxLength) { |
| 96 | + tokenIdsMaxLength = rangeLength; |
| 97 | + } |
| 98 | + } else { |
| 99 | + tokenIdsMaxLength = 0; |
| 100 | + } |
| 101 | + uint256[] memory tokenIds = new uint256[](tokenIdsMaxLength); |
| 102 | + if (tokenIdsMaxLength == 0) { |
| 103 | + return tokenIds; |
| 104 | + } |
| 105 | + // We need to call `explicitOwnershipOf(start)`, |
| 106 | + // because the slot at `start` may not be initialized. |
| 107 | + TokenOwnership memory ownership = explicitOwnershipOf(start); |
| 108 | + address currOwnershipAddr; |
| 109 | + // If the starting slot exists (i.e. not burned), initialize `currOwnershipAddr`. |
| 110 | + // `ownership.address` will not be zero, as `start` is clamped to the valid token ID range. |
| 111 | + if (!ownership.burned) { |
| 112 | + currOwnershipAddr = ownership.addr; |
| 113 | + } |
| 114 | + for (uint256 i = start; i != stop && tokenIdsIdx != tokenIdsMaxLength; ++i) { |
| 115 | + ownership = _ownerships[i]; |
| 116 | + if (ownership.burned) { |
| 117 | + continue; |
| 118 | + } |
| 119 | + if (ownership.addr != address(0)) { |
| 120 | + currOwnershipAddr = ownership.addr; |
| 121 | + } |
| 122 | + if (currOwnershipAddr == owner) { |
| 123 | + tokenIds[tokenIdsIdx++] = i; |
| 124 | + } |
| 125 | + } |
| 126 | + // Downsize the array to fit. |
| 127 | + assembly { |
| 128 | + mstore(tokenIds, tokenIdsIdx) |
| 129 | + } |
| 130 | + return tokenIds; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /* solhint-enable */ |
| 135 | + |
| 136 | + /** |
| 137 | + * @dev Returns an array of token IDs owned by `owner`. |
| 138 | + * |
| 139 | + * This function scans the ownership mapping and is O(totalSupply) in complexity. |
| 140 | + * It is meant to be called off-chain. |
| 141 | + * |
| 142 | + * See {ERC721AQueryable-tokensOfOwnerIn} for splitting the scan into |
| 143 | + * multiple smaller scans if the collection is large enough to cause |
| 144 | + * an out-of-gas error (10K pfp collections should be fine). |
| 145 | + */ |
| 146 | + function tokensOfOwner(address owner) external view override returns (uint256[] memory) { |
| 147 | + unchecked { |
| 148 | + uint256 tokenIdsIdx; |
| 149 | + address currOwnershipAddr; |
| 150 | + uint256 tokenIdsLength = balanceOf(owner); |
| 151 | + uint256[] memory tokenIds = new uint256[](tokenIdsLength); |
| 152 | + TokenOwnership memory ownership; |
| 153 | + for (uint256 i = _startTokenId(); tokenIdsIdx != tokenIdsLength; ++i) { |
| 154 | + ownership = _ownerships[i]; |
| 155 | + if (ownership.burned) { |
| 156 | + continue; |
| 157 | + } |
| 158 | + if (ownership.addr != address(0)) { |
| 159 | + currOwnershipAddr = ownership.addr; |
| 160 | + } |
| 161 | + if (currOwnershipAddr == owner) { |
| 162 | + tokenIds[tokenIdsIdx++] = i; |
| 163 | + } |
| 164 | + } |
| 165 | + return tokenIds; |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments