diff --git a/contracts/token/ERC721/extensions/ERC721Enumerable.sol b/contracts/token/ERC721/extensions/ERC721Enumerable.sol index 012e0ffc337..1bd4375290d 100644 --- a/contracts/token/ERC721/extensions/ERC721Enumerable.sol +++ b/contracts/token/ERC721/extensions/ERC721Enumerable.sol @@ -122,17 +122,19 @@ abstract contract ERC721Enumerable is ERC721, IERC721Enumerable { uint256 lastTokenIndex = balanceOf(from); uint256 tokenIndex = _ownedTokensIndex[tokenId]; + mapping(uint256 index => uint256) storage _ownedTokensByOwner = _ownedTokens[from]; + // When the token to delete is the last token, the swap operation is unnecessary if (tokenIndex != lastTokenIndex) { - uint256 lastTokenId = _ownedTokens[from][lastTokenIndex]; + uint256 lastTokenId = _ownedTokensByOwner[lastTokenIndex]; - _ownedTokens[from][tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token + _ownedTokensByOwner[tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token _ownedTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index } // This also deletes the contents at the last position of the array delete _ownedTokensIndex[tokenId]; - delete _ownedTokens[from][lastTokenIndex]; + delete _ownedTokensByOwner[lastTokenIndex]; } /**