Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Vectorized committed Feb 9, 2022
1 parent 01e7ade commit 5ac0204
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
32 changes: 16 additions & 16 deletions contracts/ERC721A.sol
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ contract ERC721A is Context, ERC165, IERC721, IERC721Metadata, IERC721Enumerable
uint64 numberBurned;
}

uint128 internal currentIndex;
uint128 internal _currentIndex;

uint128 internal burnCounter;
uint128 internal _burnCounter;

// Token name
string private _name;
Expand Down Expand Up @@ -98,10 +98,10 @@ contract ERC721A is Context, ERC165, IERC721, IERC721Metadata, IERC721Enumerable
* @dev See {IERC721Enumerable-totalSupply}.
*/
function totalSupply() public view override returns (uint256) {
// Counter underflow is impossible as burnCounter cannot be incremented
// more than currentIndex times
// Counter underflow is impossible as _burnCounter cannot be incremented
// more than _currentIndex times
unchecked {
return currentIndex - burnCounter;
return _currentIndex - _burnCounter;
}
}

Expand All @@ -111,7 +111,7 @@ contract ERC721A is Context, ERC165, IERC721, IERC721Metadata, IERC721Enumerable
* It may also degrade with extremely large collection sizes (e.g >> 10000), test for your use case.
*/
function tokenByIndex(uint256 index) public view override returns (uint256) {
uint256 numMintedSoFar = currentIndex;
uint256 numMintedSoFar = _currentIndex;
uint256 tokenIdsIdx;

// Counter overflow is impossible as the loop breaks when
Expand All @@ -137,7 +137,7 @@ contract ERC721A is Context, ERC165, IERC721, IERC721Metadata, IERC721Enumerable
*/
function tokenOfOwnerByIndex(address owner, uint256 index) public view override returns (uint256) {
if (index >= balanceOf(owner)) revert OwnerIndexOutOfBounds();
uint256 numMintedSoFar = currentIndex;
uint256 numMintedSoFar = _currentIndex;
uint256 tokenIdsIdx;
address currOwnershipAddr;

Expand Down Expand Up @@ -201,7 +201,7 @@ contract ERC721A is Context, ERC165, IERC721, IERC721Metadata, IERC721Enumerable

// Underflow is impossible because curr must be > 0 before decrement.
unchecked {
if (curr < currentIndex) {
if (curr < _currentIndex) {
TokenOwnership memory ownership = _ownerships[curr];
if (!ownership.burned) {
if (ownership.addr != address(0)) {
Expand Down Expand Up @@ -345,7 +345,7 @@ contract ERC721A is Context, ERC165, IERC721, IERC721Metadata, IERC721Enumerable
* Tokens start existing when they are minted (`_mint`),
*/
function _exists(uint256 tokenId) internal view returns (bool) {
return tokenId < currentIndex && !_ownerships[tokenId].burned;
return tokenId < _currentIndex && !_ownerships[tokenId].burned;
}

function _safeMint(address to, uint256 quantity) internal {
Expand Down Expand Up @@ -386,15 +386,15 @@ contract ERC721A is Context, ERC165, IERC721, IERC721Metadata, IERC721Enumerable
bytes memory _data,
bool safe
) internal {
uint256 startTokenId = currentIndex;
uint256 startTokenId = _currentIndex;
if (to == address(0)) revert MintToZeroAddress();
if (quantity == 0) revert MintZeroQuantity();

_beforeTokenTransfers(address(0), to, startTokenId, quantity);

// Overflows are incredibly unrealistic.
// balance or numberMinted overflow if current value of either + quantity > 3.4e38 (2**128) - 1
// updatedIndex overflows if currentIndex + quantity > 3.4e38 (2**128) - 1
// updatedIndex overflows if _currentIndex + quantity > 3.4e38 (2**128) - 1
unchecked {
_addressData[to].balance += uint64(quantity);
_addressData[to].numberMinted += uint64(quantity);
Expand All @@ -413,7 +413,7 @@ contract ERC721A is Context, ERC165, IERC721, IERC721Metadata, IERC721Enumerable
}

if (updatedIndex > type(uint128).max) revert SafecastOverflow();
currentIndex = uint128(updatedIndex);
_currentIndex = uint128(updatedIndex);
}
_afterTokenTransfers(address(0), to, startTokenId, quantity);
}
Expand Down Expand Up @@ -464,7 +464,7 @@ contract ERC721A is Context, ERC165, IERC721, IERC721Metadata, IERC721Enumerable
if (_ownerships[nextTokenId].addr == address(0)) {
// This will suffice for checking _exists(nextTokenId),
// as a burned slot cannot contain the zero address.
if (nextTokenId < currentIndex) {
if (nextTokenId < _currentIndex) {
_ownerships[nextTokenId].addr = prevOwnership.addr;
_ownerships[nextTokenId].startTimestamp = prevOwnership.startTimestamp;
}
Expand Down Expand Up @@ -511,7 +511,7 @@ contract ERC721A is Context, ERC165, IERC721, IERC721Metadata, IERC721Enumerable
if (_ownerships[nextTokenId].addr == address(0)) {
// This will suffice for checking _exists(nextTokenId),
// as a burned slot cannot contain the zero address.
if (nextTokenId < currentIndex) {
if (nextTokenId < _currentIndex) {
_ownerships[nextTokenId].addr = prevOwnership.addr;
_ownerships[nextTokenId].startTimestamp = prevOwnership.startTimestamp;
}
Expand All @@ -521,9 +521,9 @@ contract ERC721A is Context, ERC165, IERC721, IERC721Metadata, IERC721Enumerable
emit Transfer(prevOwnership.addr, address(0), tokenId);
_afterTokenTransfers(prevOwnership.addr, address(0), tokenId, 1);

// Overflow not possible, as burnCounter cannot be exceed currentIndex times.
// Overflow not possible, as _burnCounter cannot be exceed _currentIndex times.
unchecked {
burnCounter++;
_burnCounter++;
}
}

Expand Down
8 changes: 4 additions & 4 deletions contracts/extensions/ERC721AOwnersExplicit.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@ abstract contract ERC721AOwnersExplicit is ERC721A {
*/
function _setOwnersExplicit(uint256 quantity) internal {
if (quantity == 0) revert QuantityMustBeNonZero();
if (currentIndex == 0) revert NoTokensMintedYet();
if (_currentIndex == 0) revert NoTokensMintedYet();
uint256 _nextOwnerToExplicitlySet = nextOwnerToExplicitlySet;
if (_nextOwnerToExplicitlySet >= currentIndex) revert AllOwnershipsHaveBeenSet();
if (_nextOwnerToExplicitlySet >= _currentIndex) revert AllOwnershipsHaveBeenSet();

// Index underflow is impossible.
// Counter or index overflow is incredibly unrealistic.
unchecked {
uint256 endIndex = _nextOwnerToExplicitlySet + quantity - 1;

// Set the end index to be the last token index
if (endIndex + 1 > currentIndex) {
endIndex = currentIndex - 1;
if (endIndex + 1 > _currentIndex) {
endIndex = _currentIndex - 1;
}

for (uint256 i = _nextOwnerToExplicitlySet; i <= endIndex; i++) {
Expand Down

0 comments on commit 5ac0204

Please sign in to comment.