ERC721Consecutive.sol 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.9.0) (token/ERC721/extensions/ERC721Consecutive.sol)
  3. pragma solidity ^0.8.19;
  4. import "../ERC721.sol";
  5. import "../../../interfaces/IERC2309.sol";
  6. import "../../../utils/structs/BitMaps.sol";
  7. import "../../../utils/structs/Checkpoints.sol";
  8. /**
  9. * @dev Implementation of the ERC2309 "Consecutive Transfer Extension" as defined in
  10. * https://eips.ethereum.org/EIPS/eip-2309[EIP-2309].
  11. *
  12. * This extension allows the minting of large batches of tokens, during contract construction only. For upgradeable
  13. * contracts this implies that batch minting is only available during proxy deployment, and not in subsequent upgrades.
  14. * These batches are limited to 5000 tokens at a time by default to accommodate off-chain indexers.
  15. *
  16. * Using this extension removes the ability to mint single tokens during contract construction. This ability is
  17. * regained after construction. During construction, only batch minting is allowed.
  18. *
  19. * IMPORTANT: This extension bypasses the hooks {_beforeTokenTransfer} and {_afterTokenTransfer} for tokens minted in
  20. * batch. The hooks will be only called once per batch, so you should take `batchSize` parameter into consideration
  21. * when relying on hooks.
  22. *
  23. * IMPORTANT: When overriding {_afterTokenTransfer}, be careful about call ordering. {ownerOf} may return invalid
  24. * values during the {_afterTokenTransfer} execution if the super call is not called first. To be safe, execute the
  25. * super call before your custom logic.
  26. *
  27. * _Available since v4.8._
  28. */
  29. abstract contract ERC721Consecutive is IERC2309, ERC721 {
  30. using BitMaps for BitMaps.BitMap;
  31. using Checkpoints for Checkpoints.Trace160;
  32. Checkpoints.Trace160 private _sequentialOwnership;
  33. BitMaps.BitMap private _sequentialBurn;
  34. /**
  35. * @dev Batch mint is restricted to the constructor.
  36. * Any batch mint not emitting the {IERC721-Transfer} event outside of the constructor
  37. * is non-ERC721 compliant.
  38. */
  39. error ERC721ForbiddenBatchMint();
  40. /**
  41. * @dev Exceeds the max amount of mints per batch.
  42. */
  43. error ERC721ExceededMaxBatchMint(uint256 batchSize, uint256 maxBatch);
  44. /**
  45. * @dev Individual minting is not allowed.
  46. */
  47. error ERC721ForbiddenMint();
  48. /**
  49. * @dev Batch burn is not supported.
  50. */
  51. error ERC721ForbiddenBatchBurn();
  52. /**
  53. * @dev Maximum size of a batch of consecutive tokens. This is designed to limit stress on off-chain indexing
  54. * services that have to record one entry per token, and have protections against "unreasonably large" batches of
  55. * tokens.
  56. *
  57. * NOTE: Overriding the default value of 5000 will not cause on-chain issues, but may result in the asset not being
  58. * correctly supported by off-chain indexing services (including marketplaces).
  59. */
  60. function _maxBatchSize() internal view virtual returns (uint96) {
  61. return 5000;
  62. }
  63. /**
  64. * @dev See {ERC721-_ownerOf}. Override that checks the sequential ownership structure for tokens that have
  65. * been minted as part of a batch, and not yet transferred.
  66. */
  67. function _ownerOf(uint256 tokenId) internal view virtual override returns (address) {
  68. address owner = super._ownerOf(tokenId);
  69. // If token is owned by the core, or beyond consecutive range, return base value
  70. if (owner != address(0) || tokenId > type(uint96).max || tokenId < _firstConsecutiveId()) {
  71. return owner;
  72. }
  73. // Otherwise, check the token was not burned, and fetch ownership from the anchors
  74. // Note: no need for safe cast, we know that tokenId <= type(uint96).max
  75. return _sequentialBurn.get(tokenId) ? address(0) : address(_sequentialOwnership.lowerLookup(uint96(tokenId)));
  76. }
  77. /**
  78. * @dev Mint a batch of tokens of length `batchSize` for `to`. Returns the token id of the first token minted in the
  79. * batch; if `batchSize` is 0, returns the number of consecutive ids minted so far.
  80. *
  81. * Requirements:
  82. *
  83. * - `batchSize` must not be greater than {_maxBatchSize}.
  84. * - The function is called in the constructor of the contract (directly or indirectly).
  85. *
  86. * CAUTION: Does not emit a `Transfer` event. This is ERC721 compliant as long as it is done inside of the
  87. * constructor, which is enforced by this function.
  88. *
  89. * CAUTION: Does not invoke `onERC721Received` on the receiver.
  90. *
  91. * Emits a {IERC2309-ConsecutiveTransfer} event.
  92. */
  93. function _mintConsecutive(address to, uint96 batchSize) internal virtual returns (uint96) {
  94. uint96 next = _nextConsecutiveId();
  95. // minting a batch of size 0 is a no-op
  96. if (batchSize > 0) {
  97. if (address(this).code.length > 0) {
  98. revert ERC721ForbiddenBatchMint();
  99. }
  100. if (to == address(0)) {
  101. revert ERC721InvalidReceiver(address(0));
  102. }
  103. uint256 maxBatchSize = _maxBatchSize();
  104. if (batchSize > maxBatchSize) {
  105. revert ERC721ExceededMaxBatchMint(batchSize, maxBatchSize);
  106. }
  107. // hook before
  108. _beforeTokenTransfer(address(0), to, next, batchSize);
  109. // push an ownership checkpoint & emit event
  110. uint96 last = next + batchSize - 1;
  111. _sequentialOwnership.push(last, uint160(to));
  112. // The invariant required by this function is preserved because the new sequentialOwnership checkpoint
  113. // is attributing ownership of `batchSize` new tokens to account `to`.
  114. __unsafe_increaseBalance(to, batchSize);
  115. emit ConsecutiveTransfer(next, last, address(0), to);
  116. // hook after
  117. _afterTokenTransfer(address(0), to, next, batchSize);
  118. }
  119. return next;
  120. }
  121. /**
  122. * @dev See {ERC721-_mint}. Override version that restricts normal minting to after construction.
  123. *
  124. * WARNING: Using {ERC721Consecutive} prevents using {_mint} during construction in favor of {_mintConsecutive}.
  125. * After construction, {_mintConsecutive} is no longer available and {_mint} becomes available.
  126. */
  127. function _mint(address to, uint256 tokenId) internal virtual override {
  128. if (address(this).code.length == 0) {
  129. revert ERC721ForbiddenMint();
  130. }
  131. super._mint(to, tokenId);
  132. }
  133. /**
  134. * @dev See {ERC721-_afterTokenTransfer}. Burning of tokens that have been sequentially minted must be explicit.
  135. */
  136. function _afterTokenTransfer(
  137. address from,
  138. address to,
  139. uint256 firstTokenId,
  140. uint256 batchSize
  141. ) internal virtual override {
  142. if (
  143. to == address(0) && // if we burn
  144. firstTokenId >= _firstConsecutiveId() &&
  145. firstTokenId < _nextConsecutiveId() &&
  146. !_sequentialBurn.get(firstTokenId)
  147. ) // and the token was never marked as burnt
  148. {
  149. if (batchSize != 1) {
  150. revert ERC721ForbiddenBatchBurn();
  151. }
  152. _sequentialBurn.set(firstTokenId);
  153. }
  154. super._afterTokenTransfer(from, to, firstTokenId, batchSize);
  155. }
  156. /**
  157. * @dev Used to offset the first token id in {_nextConsecutiveId}
  158. */
  159. function _firstConsecutiveId() internal view virtual returns (uint96) {
  160. return 0;
  161. }
  162. /**
  163. * @dev Returns the next tokenId to mint using {_mintConsecutive}. It will return {_firstConsecutiveId}
  164. * if no consecutive tokenId has been minted before.
  165. */
  166. function _nextConsecutiveId() private view returns (uint96) {
  167. (bool exists, uint96 latestId, ) = _sequentialOwnership.latestCheckpoint();
  168. return exists ? latestId + 1 : _firstConsecutiveId();
  169. }
  170. }