Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
reduce lookup and rename function for clarity
  • Loading branch information
kfishnchips committed Mar 10, 2023
commit 1c7570d30b522da478289e17459114859644b008
14 changes: 5 additions & 9 deletions contracts/token/ERC721/extensions/ERC721Consecutive.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,7 @@ abstract contract ERC721Consecutive is IERC2309, ERC721 {
address owner = super._ownerOf(tokenId);

// If token is owned by the core, or beyond consecutive range, return base value
if (
owner != address(0) ||
tokenId > (uint256(type(uint96).max) + _firstConsecutiveId()) ||
tokenId < _firstConsecutiveId()
) {
if (owner != address(0) || tokenId > type(uint96).max || tokenId < _firstConsecutiveId()) {
return owner;
}

Expand All @@ -86,7 +82,7 @@ abstract contract ERC721Consecutive is IERC2309, ERC721 {
* Emits a {IERC2309-ConsecutiveTransfer} event.
*/
function _mintConsecutive(address to, uint96 batchSize) internal virtual returns (uint96) {
uint96 first = _totalConsecutiveSupply();
uint96 first = _nextConsecutiveId();

// minting a batch of size 0 is a no-op
if (batchSize > 0) {
Expand Down Expand Up @@ -137,7 +133,7 @@ abstract contract ERC721Consecutive is IERC2309, ERC721 {
if (
to == address(0) && // if we burn
firstTokenId >= _firstConsecutiveId() &&
firstTokenId - _firstConsecutiveId() < _totalConsecutiveSupply() && // and the tokenId is in the batch range
firstTokenId - _firstConsecutiveId() < _nextConsecutiveId() && // and the tokenId is in the batch range
!_sequentialBurn.get(firstTokenId)
) // and the token was never marked as burnt
{
Expand All @@ -148,13 +144,13 @@ abstract contract ERC721Consecutive is IERC2309, ERC721 {
}

/**
* @dev Used to offset the first token id in {_totalConsecutiveSupply}
* @dev Used to offset the first token id in {_nextConsecutiveId}
*/
function _firstConsecutiveId() internal view virtual returns (uint96) {
return 0;
}

function _totalConsecutiveSupply() private view returns (uint96) {
function _nextConsecutiveId() private view returns (uint96) {
(bool exists, uint96 latestId, ) = _sequentialOwnership.latestCheckpoint();
return exists ? latestId + 1 : _firstConsecutiveId();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think 0 to _firstConsecutiveId() should be accounted for the _totalConsecutiveSupply, it affects line 135, which will be excluding the after hook for real burned tokenIds

For example:

  1. Someone replaces _firstConsecutiveId() with a 5
  2. They consecutively mint tokenIds from 5 to 9 (5 mints)
  3. After that, someone else mints the token 2 via normal minting
  4. Then, they burn it, and the token will make it to the _sequentialBurn on the ownerOf(...) method, which is not what we want

I think this is what we should aim for:

Suggested change
return exists ? latestId + 1 : _firstConsecutiveId();
return exists ? latestId + 1 - _firstConsecutiveId() : 0;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make more sense for someone to consecutively mint token 10 instead of 2? I was thinking that there could be no minting below the first consecutive id.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would requiring that the tokenId be greater than the last batch minted one in the _mint function be a viable option? It might be too inefficient...

    function _mint(address to, uint256 tokenId) internal virtual override {
        require(Address.isContract(address(this)), "ERC721Consecutive: can't mint during construction");
        (, uint96 latestId, ) = _sequentialOwnership.latestCheckpoint();
        require(tokenId > latestId, "ERC721Consecutive: token id must be greater than latest id");
        super._mint(to, tokenId);
    }

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is something we don't want, because it adds 2 (cold) sload to the mint operation, which we want to keep as cheap as possible.

Also, if we do that, it would be impossible to _mint a token that was first minted as part of a batch then burnt.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that mint already check if the tokenId has an owner, and revert if that is the case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, I'm still wrapping my head around the internals of this but I think I got it.

}
Expand Down