ERC721Consecutive.sol 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "../ERC721.sol";
  4. import "../../../interfaces/IERC2309.sol";
  5. import "../../../utils/Checkpoints.sol";
  6. import "../../../utils/math/SafeCast.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 the contract construction. These batches are
  13. * limited to 5000 tokens at a time to accommodate off-chain indexers.
  14. *
  15. * Using this extension removes the ability to mint single tokens during the contract construction. This ability is
  16. * regained after construction. During construction, only batch minting is allowed.
  17. *
  18. * IMPORTANT: This extension bypasses the hooks {_beforeTokenTransfer} and {_afterTokenTransfer} for tokens minted in
  19. * batch. When using this extension, you should consider the {_beforeConsecutiveTokenTransfer} and
  20. * {_afterConsecutiveTokenTransfer} hooks in addition to {_beforeTokenTransfer} and {_afterTokenTransfer}.
  21. *
  22. * IMPORTANT: When overriding {_afterTokenTransfer}, be careful about call ordering. {ownerOf} may return invalid
  23. * values during the {_afterTokenTransfer} execution if the super call is not called first. To be safe, execute the
  24. * super call before your custom logic.
  25. *
  26. * _Available since v4.8._
  27. */
  28. abstract contract ERC721Consecutive is IERC2309, ERC721 {
  29. using BitMaps for BitMaps.BitMap;
  30. using Checkpoints for Checkpoints.Trace160;
  31. Checkpoints.Trace160 private _sequentialOwnership;
  32. BitMaps.BitMap private _sequentialBurn;
  33. /**
  34. * @dev See {ERC721-_ownerOf}. Override that checks the sequential ownership structure for tokens that have
  35. * been minted as part of a batch, and not yet transferred.
  36. */
  37. function _ownerOf(uint256 tokenId) internal view virtual override returns (address) {
  38. address owner = super._ownerOf(tokenId);
  39. // If token is owned by the core, or beyond consecutive range, return base value
  40. if (owner != address(0) || tokenId > type(uint96).max) {
  41. return owner;
  42. }
  43. // Otherwise, check the token was not burned, and fetch ownership from the anchors
  44. // Note: no need for safe cast, we know that tokenId <= type(uint96).max
  45. return _sequentialBurn.get(tokenId) ? address(0) : address(_sequentialOwnership.lowerLookup(uint96(tokenId)));
  46. }
  47. /**
  48. * @dev Mint a batch of tokens of length `batchSize` for `to`.
  49. *
  50. * WARNING: Consecutive mint is only available during construction. ERC721 requires that any minting done after
  51. * construction emits a `Transfer` event, which is not the case of mints performed using this function.
  52. *
  53. * WARNING: Consecutive mint is limited to batches of 5000 tokens. Further minting is possible from a contract's
  54. * point of view but would cause indexing issues for off-chain services.
  55. *
  56. * Emits a {ConsecutiveTransfer} event.
  57. */
  58. function _mintConsecutive(address to, uint96 batchSize) internal virtual returns (uint96) {
  59. uint96 first = _totalConsecutiveSupply();
  60. // minting a batch of size 0 is a no-op
  61. if (batchSize > 0) {
  62. require(!Address.isContract(address(this)), "ERC721Consecutive: batch minting restricted to constructor");
  63. require(to != address(0), "ERC721Consecutive: mint to the zero address");
  64. require(batchSize <= 5000, "ERC721Consecutive: batch too large");
  65. // hook before
  66. _beforeConsecutiveTokenTransfer(address(0), to, first, batchSize);
  67. // push an ownership checkpoint & emit event
  68. uint96 last = first + batchSize - 1;
  69. _sequentialOwnership.push(last, uint160(to));
  70. emit ConsecutiveTransfer(first, last, address(0), to);
  71. // hook after
  72. _afterConsecutiveTokenTransfer(address(0), to, first, batchSize);
  73. }
  74. return first;
  75. }
  76. /**
  77. * @dev See {ERC721-_mint}. Override version that restricts normal minting to after construction.
  78. *
  79. * Warning: Using {ERC721Consecutive} prevents using {_mint} during construction in favor of {_mintConsecutive}.
  80. * After construction, {_mintConsecutive} is no longer available and {_mint} becomes available.
  81. */
  82. function _mint(address to, uint256 tokenId) internal virtual override {
  83. require(Address.isContract(address(this)), "ERC721Consecutive: can't mint during construction");
  84. super._mint(to, tokenId);
  85. }
  86. /**
  87. * @dev See {ERC721-_afterTokenTransfer}. Burning of token that have been sequentially minted must be explicit.
  88. */
  89. function _afterTokenTransfer(
  90. address from,
  91. address to,
  92. uint256 tokenId
  93. ) internal virtual override {
  94. if (
  95. to == address(0) && // if we burn
  96. tokenId <= _totalConsecutiveSupply() && // and the tokenId was minted is a batch
  97. !_sequentialBurn.get(tokenId) // and the token was never marked as burnt
  98. ) {
  99. _sequentialBurn.set(tokenId);
  100. }
  101. super._afterTokenTransfer(from, to, tokenId);
  102. }
  103. function _totalConsecutiveSupply() private view returns (uint96) {
  104. (bool exists, uint96 latestId, ) = _sequentialOwnership.latestCheckpoint();
  105. return exists ? latestId + 1 : 0;
  106. }
  107. }