ERC721Consecutive.sol 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.8.0-rc.1) (token/ERC721/extensions/ERC721Consecutive.sol)
  3. pragma solidity ^0.8.0;
  4. import "../ERC721.sol";
  5. import "../../../interfaces/IERC2309.sol";
  6. import "../../../utils/Checkpoints.sol";
  7. import "../../../utils/structs/BitMaps.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. When using this extension, you should consider the {_beforeConsecutiveTokenTransfer} and
  21. * {_afterConsecutiveTokenTransfer} hooks in addition to {_beforeTokenTransfer} and {_afterTokenTransfer}.
  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 Maximum size of a batch of consecutive tokens. This is designed to limit stress on off-chain indexing
  36. * services that have to record one entry per token, and have protections against "unreasonably large" batches of
  37. * tokens.
  38. *
  39. * NOTE: Overriding the default value of 5000 will not cause on-chain issues, but may result in the asset not being
  40. * correctly supported by off-chain indexing services (including marketplaces).
  41. */
  42. function _maxBatchSize() internal view virtual returns (uint96) {
  43. return 5000;
  44. }
  45. /**
  46. * @dev See {ERC721-_ownerOf}. Override that checks the sequential ownership structure for tokens that have
  47. * been minted as part of a batch, and not yet transferred.
  48. */
  49. function _ownerOf(uint256 tokenId) internal view virtual override returns (address) {
  50. address owner = super._ownerOf(tokenId);
  51. // If token is owned by the core, or beyond consecutive range, return base value
  52. if (owner != address(0) || tokenId > type(uint96).max) {
  53. return owner;
  54. }
  55. // Otherwise, check the token was not burned, and fetch ownership from the anchors
  56. // Note: no need for safe cast, we know that tokenId <= type(uint96).max
  57. return _sequentialBurn.get(tokenId) ? address(0) : address(_sequentialOwnership.lowerLookup(uint96(tokenId)));
  58. }
  59. /**
  60. * @dev Mint a batch of tokens of length `batchSize` for `to`. Returns the token id of the first token minted in the
  61. * batch; if `batchSize` is 0, returns the number of consecutive ids minted so far.
  62. *
  63. * Requirements:
  64. *
  65. * - `batchSize` must not be greater than {_maxBatchSize}.
  66. * - The function is called in the constructor of the contract (directly or indirectly).
  67. *
  68. * CAUTION: Does not emit a `Transfer` event. This is ERC721 compliant as long as it is done outside of the
  69. * constructor, which is enforced by this function.
  70. *
  71. * CAUTION: Does not invoke `onERC721Received` on the receiver.
  72. *
  73. * Emits a {IERC2309-ConsecutiveTransfer} event.
  74. */
  75. function _mintConsecutive(address to, uint96 batchSize) internal virtual returns (uint96) {
  76. uint96 first = _totalConsecutiveSupply();
  77. // minting a batch of size 0 is a no-op
  78. if (batchSize > 0) {
  79. require(!Address.isContract(address(this)), "ERC721Consecutive: batch minting restricted to constructor");
  80. require(to != address(0), "ERC721Consecutive: mint to the zero address");
  81. require(batchSize <= _maxBatchSize(), "ERC721Consecutive: batch too large");
  82. // hook before
  83. _beforeConsecutiveTokenTransfer(address(0), to, first, batchSize);
  84. // push an ownership checkpoint & emit event
  85. uint96 last = first + batchSize - 1;
  86. _sequentialOwnership.push(last, uint160(to));
  87. emit ConsecutiveTransfer(first, last, address(0), to);
  88. // hook after
  89. _afterConsecutiveTokenTransfer(address(0), to, first, batchSize);
  90. }
  91. return first;
  92. }
  93. /**
  94. * @dev See {ERC721-_mint}. Override version that restricts normal minting to after construction.
  95. *
  96. * Warning: Using {ERC721Consecutive} prevents using {_mint} during construction in favor of {_mintConsecutive}.
  97. * After construction, {_mintConsecutive} is no longer available and {_mint} becomes available.
  98. */
  99. function _mint(address to, uint256 tokenId) internal virtual override {
  100. require(Address.isContract(address(this)), "ERC721Consecutive: can't mint during construction");
  101. super._mint(to, tokenId);
  102. }
  103. /**
  104. * @dev See {ERC721-_afterTokenTransfer}. Burning of tokens that have been sequentially minted must be explicit.
  105. */
  106. function _afterTokenTransfer(
  107. address from,
  108. address to,
  109. uint256 tokenId
  110. ) internal virtual override {
  111. if (
  112. to == address(0) && // if we burn
  113. tokenId < _totalConsecutiveSupply() && // and the tokenId was minted in a batch
  114. !_sequentialBurn.get(tokenId) // and the token was never marked as burnt
  115. ) {
  116. _sequentialBurn.set(tokenId);
  117. }
  118. super._afterTokenTransfer(from, to, tokenId);
  119. }
  120. function _totalConsecutiveSupply() private view returns (uint96) {
  121. (bool exists, uint96 latestId, ) = _sequentialOwnership.latestCheckpoint();
  122. return exists ? latestId + 1 : 0;
  123. }
  124. }