@@ -67,37 +67,6 @@ contract ERC721Enumerable is ERC165, ERC721, IERC721Enumerable {
67
67
return _allTokens[index];
68
68
}
69
69
70
- /**
71
- * @dev Internal function to add a token ID to the list of a given address
72
- * This function is internal due to language limitations, see the note in ERC721.sol.
73
- * It is not intended to be called by custom derived contracts: in particular, it emits no Transfer event.
74
- * @param to address representing the new owner of the given token ID
75
- * @param tokenId uint256 ID of the token to be added to the tokens list of the given address
76
- */
77
- function _addTokenTo (address to , uint256 tokenId ) internal {
78
- super ._addTokenTo (to, tokenId);
79
-
80
- _addTokenToOwnerEnumeration (to, tokenId);
81
- }
82
-
83
- /**
84
- * @dev Internal function to remove a token ID from the list of a given address
85
- * This function is internal due to language limitations, see the note in ERC721.sol.
86
- * It is not intended to be called by custom derived contracts: in particular, it emits no Transfer event,
87
- * and doesn't clear approvals.
88
- * @param from address representing the previous owner of the given token ID
89
- * @param tokenId uint256 ID of the token to be removed from the tokens list of the given address
90
- */
91
- function _removeTokenFrom (address from , uint256 tokenId ) internal {
92
- super ._removeTokenFrom (from, tokenId);
93
-
94
- _removeTokenFromOwnerEnumeration (from, tokenId);
95
-
96
- // Since the token is being destroyed, we also clear its index
97
- // TODO(nventuro): 0 is still a valid index, so arguably this isnt really helpful, remove?
98
- _ownedTokensIndex[tokenId] = 0 ;
99
- }
100
-
101
70
/**
102
71
* @dev Internal function to transfer ownership of a given token ID to another address.
103
72
* As opposed to transferFrom, this imposes no restrictions on msg.sender.
@@ -122,8 +91,9 @@ contract ERC721Enumerable is ERC165, ERC721, IERC721Enumerable {
122
91
function _mint (address to , uint256 tokenId ) internal {
123
92
super ._mint (to, tokenId);
124
93
125
- _allTokensIndex[tokenId] = _allTokens.length ;
126
- _allTokens.push (tokenId);
94
+ _addTokenToOwnerEnumeration (to, tokenId);
95
+
96
+ _addTokenToAllTokensEnumeration (tokenId);
127
97
}
128
98
129
99
/**
@@ -136,17 +106,11 @@ contract ERC721Enumerable is ERC165, ERC721, IERC721Enumerable {
136
106
function _burn (address owner , uint256 tokenId ) internal {
137
107
super ._burn (owner, tokenId);
138
108
139
- // Reorg all tokens array
140
- uint256 tokenIndex = _allTokensIndex[tokenId];
141
- uint256 lastTokenIndex = _allTokens.length .sub (1 );
142
- uint256 lastToken = _allTokens[lastTokenIndex];
143
-
144
- _allTokens[tokenIndex] = lastToken;
145
- _allTokens[lastTokenIndex] = 0 ;
109
+ _removeTokenFromOwnerEnumeration (owner, tokenId);
110
+ // Since tokenId will be deleted, we can clear its slot in _ownedTokensIndex to trigger a gas refund
111
+ _ownedTokensIndex[tokenId] = 0 ;
146
112
147
- _allTokens.length -- ;
148
- _allTokensIndex[tokenId] = 0 ;
149
- _allTokensIndex[lastToken] = tokenIndex;
113
+ _removeTokenFromAllTokensEnumeration (tokenId);
150
114
}
151
115
152
116
/**
@@ -164,9 +128,17 @@ contract ERC721Enumerable is ERC165, ERC721, IERC721Enumerable {
164
128
* @param tokenId uint256 ID of the token to be added to the tokens list of the given address
165
129
*/
166
130
function _addTokenToOwnerEnumeration (address to , uint256 tokenId ) private {
167
- uint256 newOwnedTokensLength = _ownedTokens[to].push (tokenId);
168
- // No need to use SafeMath since the length after a push cannot be zero
169
- _ownedTokensIndex[tokenId] = newOwnedTokensLength - 1 ;
131
+ _ownedTokensIndex[tokenId] = _ownedTokens[to].length ;
132
+ _ownedTokens[to].push (tokenId);
133
+ }
134
+
135
+ /**
136
+ * @dev Private function to add a token to this extension's token tracking data structures.
137
+ * @param tokenId uint256 ID of the token to be added to the tokens list
138
+ */
139
+ function _addTokenToAllTokensEnumeration (uint256 tokenId ) private {
140
+ _allTokensIndex[tokenId] = _allTokens.length ;
141
+ _allTokens.push (tokenId);
170
142
}
171
143
172
144
/**
@@ -182,21 +154,45 @@ contract ERC721Enumerable is ERC165, ERC721, IERC721Enumerable {
182
154
// then delete the last slot (swap and pop).
183
155
184
156
uint256 lastTokenIndex = _ownedTokens[from].length .sub (1 );
185
- uint256 lastTokenId = _ownedTokens[from][lastTokenIndex];
186
-
187
157
uint256 tokenIndex = _ownedTokensIndex[tokenId];
188
158
189
- _ownedTokens[from][tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token
190
- _ownedTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index
159
+ // When the token to delete is the last token, the swap operation is unnecessary
160
+ if (tokenIndex != lastTokenIndex) {
161
+ uint256 lastTokenId = _ownedTokens[from][lastTokenIndex];
191
162
192
- // Note that this will handle single-element arrays. In that case, both tokenIndex and lastTokenIndex are going
193
- // to be zero. The swap operation will therefore have no effect, but the token _will_ be deleted during the
194
- // 'pop' operation.
163
+ _ownedTokens[from][tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token
164
+ _ownedTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index
165
+ }
195
166
196
167
// This also deletes the contents at the last position of the array
197
168
_ownedTokens[from].length -- ;
198
169
199
170
// Note that _ownedTokensIndex[tokenId] hasn't been cleared: it still points to the old slot (now occcupied by
200
- // lasTokenId).
171
+ // lasTokenId, or just over the end of the array if the token was the last one).
172
+ }
173
+
174
+ /**
175
+ * @dev Private function to remove a token from this extension's token tracking data structures.
176
+ * This has O(1) time complexity, but alters the order of the _allTokens array.
177
+ * @param tokenId uint256 ID of the token to be removed from the tokens list
178
+ */
179
+ function _removeTokenFromAllTokensEnumeration (uint256 tokenId ) private {
180
+ // To prevent a gap in the tokens array, we store the last token in the index of the token to delete, and
181
+ // then delete the last slot (swap and pop).
182
+
183
+ uint256 lastTokenIndex = _allTokens.length .sub (1 );
184
+ uint256 tokenIndex = _allTokensIndex[tokenId];
185
+
186
+ // When the token to delete is the last token, the swap operation is unnecessary. However, since this occurs so
187
+ // rarely (when the last minted token is burnt) that we still do the swap here to avoid the gas cost of adding
188
+ // an 'if' statement (like in _removeTokenFromOwnerEnumeration)
189
+ uint256 lastTokenId = _allTokens[lastTokenIndex];
190
+
191
+ _allTokens[tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token
192
+ _allTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index
193
+
194
+ // This also deletes the contents at the last position of the array
195
+ _allTokens.length -- ;
196
+ _allTokensIndex[tokenId] = 0 ;
201
197
}
202
198
}
0 commit comments