Skip to content

Commit d3634eb

Browse files
committed
Remove unused internal functions, update mocks
1 parent 59603a7 commit d3634eb

File tree

4 files changed

+73
-175
lines changed

4 files changed

+73
-175
lines changed

contracts/mocks/ERC721BasicTokenMock.sol

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,25 @@ import "../token/ERC721/ERC721BasicToken.sol";
99
*/
1010
contract ERC721BasicTokenMock is ERC721BasicToken {
1111
function mint(address _to, uint256 _tokenId) public {
12-
super._mint(_to, _tokenId);
12+
require(_to != address(0));
13+
require(!exists(_tokenId));
14+
15+
tokenOwner[_tokenId] = _to;
16+
ownedTokensCount[_to] = ownedTokensCount[_to].add(1);
17+
emit Transfer(address(0), _to, _tokenId);
1318
}
1419

1520
function burn(uint256 _tokenId) public {
16-
super._burn(ownerOf(_tokenId), _tokenId);
21+
address _owner = ownerOf(_tokenId);
22+
require(_owner != address(0));
23+
24+
if (tokenApprovals[_tokenId] != address(0)) {
25+
tokenApprovals[_tokenId] = address(0);
26+
emit Approval(_owner, address(0), _tokenId);
27+
}
28+
29+
tokenOwner[_tokenId] = address(0);
30+
ownedTokensCount[_owner] = ownedTokensCount[_owner].sub(1);
31+
emit Transfer(_owner, address(0), _tokenId);
1732
}
1833
}

contracts/mocks/ERC721TokenMock.sol

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,58 @@ contract ERC721TokenMock is ERC721Token {
1414
{ }
1515

1616
function mint(address _to, uint256 _tokenId) public {
17-
super._mint(_to, _tokenId);
17+
require(_to != address(0));
18+
require(!exists(_tokenId));
19+
20+
tokenOwner[_tokenId] = _to;
21+
ownedTokensCount[_to] = ownedTokensCount[_to].add(1);
22+
23+
ownedTokensIndex[_tokenId] = ownedTokens[_to].length;
24+
ownedTokens[_to].push(_tokenId);
25+
26+
allTokensIndex[_tokenId] = allTokens.length;
27+
allTokens.push(_tokenId);
28+
29+
emit Transfer(address(0), _to, _tokenId);
1830
}
1931

2032
function burn(uint256 _tokenId) public {
21-
super._burn(ownerOf(_tokenId), _tokenId);
33+
address _owner = ownerOf(_tokenId);
34+
require(_owner != address(0));
35+
36+
if (tokenApprovals[_tokenId] != address(0)) {
37+
tokenApprovals[_tokenId] = address(0);
38+
emit Approval(_owner, address(0), _tokenId);
39+
}
40+
41+
tokenOwner[_tokenId] = address(0);
42+
ownedTokensCount[_owner] = ownedTokensCount[_owner].sub(1);
43+
44+
uint256 removeOwnedTokenIndex = ownedTokensIndex[_tokenId];
45+
uint256 lastOwnedTokenIndex = ownedTokens[_owner].length.sub(1);
46+
uint256 lastOwnedToken = ownedTokens[_owner][lastOwnedTokenIndex];
47+
48+
ownedTokens[_owner][removeOwnedTokenIndex] = lastOwnedToken;
49+
ownedTokens[_owner][lastOwnedTokenIndex] = 0;
50+
ownedTokens[_owner].length--;
51+
ownedTokensIndex[_tokenId] = 0;
52+
ownedTokensIndex[lastOwnedToken] = removeOwnedTokenIndex;
53+
54+
if (bytes(tokenURIs[_tokenId]).length != 0) {
55+
delete tokenURIs[_tokenId];
56+
}
57+
58+
uint256 removeAllTokenIndex = allTokensIndex[_tokenId];
59+
uint256 lastAllTokenIndex = allTokens.length.sub(1);
60+
uint256 lastAllToken = allTokens[lastAllTokenIndex];
61+
62+
allTokens[removeAllTokenIndex] = lastAllToken;
63+
allTokens[lastAllTokenIndex] = 0;
64+
allTokens.length--;
65+
allTokensIndex[_tokenId] = 0;
66+
allTokensIndex[lastAllToken] = removeAllTokenIndex;
67+
68+
emit Transfer(_owner, address(0), _tokenId);
2269
}
2370

2471
function setTokenURI(uint256 _tokenId, string _uri) public {

contracts/token/ERC721/ERC721BasicToken.sol

Lines changed: 3 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ contract ERC721BasicToken is SupportsInterfaceWithLookup, ERC721Basic {
167167

168168
address spender = msg.sender;
169169
require(
170+
// Disable solium check because of
171+
// https://github.com/duaraghav8/Solium/issues/175
172+
// solium-disable-next-line operator-whitespace
170173
spender == owner ||
171174
isApprovedForAll(owner, spender) ||
172175
getApproved(_tokenId) == spender
@@ -232,91 +235,6 @@ contract ERC721BasicToken is SupportsInterfaceWithLookup, ERC721Basic {
232235
require(checkAndCallSafeTransfer(_from, _to, _tokenId, _data));
233236
}
234237

235-
/**
236-
* @dev Returns whether the given spender can transfer a given token ID
237-
* @param _spender address of the spender to query
238-
* @param _tokenId uint256 ID of the token to be transferred
239-
* @return bool whether the msg.sender is approved for the given token ID,
240-
* is an operator of the owner, or is the owner of the token
241-
*/
242-
function isApprovedOrOwner(
243-
address _spender,
244-
uint256 _tokenId
245-
)
246-
internal
247-
view
248-
returns (bool)
249-
{
250-
address owner = ownerOf(_tokenId);
251-
// Disable solium check because of
252-
// https://github.com/duaraghav8/Solium/issues/175
253-
// solium-disable-next-line operator-whitespace
254-
return (
255-
_spender == owner ||
256-
getApproved(_tokenId) == _spender ||
257-
isApprovedForAll(owner, _spender)
258-
);
259-
}
260-
261-
/**
262-
* @dev Internal function to mint a new token
263-
* Reverts if the given token ID already exists
264-
* @param _to The address that will own the minted token
265-
* @param _tokenId uint256 ID of the token to be minted by the msg.sender
266-
*/
267-
function _mint(address _to, uint256 _tokenId) internal {
268-
require(_to != address(0));
269-
addTokenTo(_to, _tokenId);
270-
emit Transfer(address(0), _to, _tokenId);
271-
}
272-
273-
/**
274-
* @dev Internal function to burn a specific token
275-
* Reverts if the token does not exist
276-
* @param _tokenId uint256 ID of the token being burned by the msg.sender
277-
*/
278-
function _burn(address _owner, uint256 _tokenId) internal {
279-
clearApproval(_owner, _tokenId);
280-
removeTokenFrom(_owner, _tokenId);
281-
emit Transfer(_owner, address(0), _tokenId);
282-
}
283-
284-
/**
285-
* @dev Internal function to clear current approval of a given token ID
286-
* Reverts if the given address is not indeed the owner of the token
287-
* @param _owner owner of the token
288-
* @param _tokenId uint256 ID of the token to be transferred
289-
*/
290-
function clearApproval(address _owner, uint256 _tokenId) internal {
291-
require(ownerOf(_tokenId) == _owner);
292-
if (tokenApprovals[_tokenId] != address(0)) {
293-
tokenApprovals[_tokenId] = address(0);
294-
emit Approval(_owner, address(0), _tokenId);
295-
}
296-
}
297-
298-
/**
299-
* @dev Internal function to add a token ID to the list of a given address
300-
* @param _to address representing the new owner of the given token ID
301-
* @param _tokenId uint256 ID of the token to be added to the tokens list of the given address
302-
*/
303-
function addTokenTo(address _to, uint256 _tokenId) internal {
304-
require(tokenOwner[_tokenId] == address(0));
305-
tokenOwner[_tokenId] = _to;
306-
ownedTokensCount[_to] = ownedTokensCount[_to].add(1);
307-
}
308-
309-
/**
310-
* @dev Internal function to remove a token ID from the list of a given address
311-
* @param _from address representing the previous owner of the given token ID
312-
* @param _tokenId uint256 ID of the token to be removed from the tokens list of the given address
313-
*/
314-
function removeTokenFrom(address _from, uint256 _tokenId) internal {
315-
require(ownerOf(_tokenId) == _from);
316-
ownedTokensCount[_from] = ownedTokensCount[_from].sub(1);
317-
tokenOwner[_tokenId] = address(0);
318-
}
319-
320238
/**
321239
* @dev Internal function to invoke `onERC721Received` on a target address
322240
* The call is not executed if the target address is not a contract

contracts/token/ERC721/ERC721Token.sol

Lines changed: 4 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -123,23 +123,17 @@ contract ERC721Token is SupportsInterfaceWithLookup, ERC721BasicToken, ERC721 {
123123
{
124124
super.transferFrom(_from, _to, _tokenId);
125125

126-
uint256 tokenIndex = ownedTokensIndex[_tokenId];
126+
uint256 removeTokenIndex = ownedTokensIndex[_tokenId];
127127
uint256 lastTokenIndex = ownedTokens[_from].length.sub(1);
128128
uint256 lastToken = ownedTokens[_from][lastTokenIndex];
129129

130-
ownedTokens[_from][tokenIndex] = lastToken;
130+
ownedTokens[_from][removeTokenIndex] = lastToken;
131131
ownedTokens[_from][lastTokenIndex] = 0;
132-
// Note that this will handle single-element arrays. In that case, both tokenIndex and lastTokenIndex are going to
133-
// be zero. Then we can make sure that we will remove _tokenId from the ownedTokens list since we are first swapping
134-
// the lastToken to the first position, and then dropping the element placed in the last position of the list
135-
136132
ownedTokens[_from].length--;
137-
ownedTokensIndex[_tokenId] = 0;
138-
ownedTokensIndex[lastToken] = tokenIndex;
133+
ownedTokensIndex[lastToken] = removeTokenIndex;
139134

140-
uint256 length = ownedTokens[_to].length;
141135
ownedTokens[_to].push(_tokenId);
142-
ownedTokensIndex[_tokenId] = length;
136+
ownedTokensIndex[_tokenId] = ownedTokens[_to].length - 1;
143137
}
144138
/**
145139
* @dev Gets the total amount of tokens stored by the contract
@@ -170,80 +164,4 @@ contract ERC721Token is SupportsInterfaceWithLookup, ERC721BasicToken, ERC721 {
170164
require(exists(_tokenId));
171165
tokenURIs[_tokenId] = _uri;
172166
}
173-
174-
/**
175-
* @dev Internal function to add a token ID to the list of a given address
176-
* @param _to address representing the new owner of the given token ID
177-
* @param _tokenId uint256 ID of the token to be added to the tokens list of the given address
178-
*/
179-
function addTokenTo(address _to, uint256 _tokenId) internal {
180-
super.addTokenTo(_to, _tokenId);
181-
uint256 length = ownedTokens[_to].length;
182-
ownedTokens[_to].push(_tokenId);
183-
ownedTokensIndex[_tokenId] = length;
184-
}
185-
186-
/**
187-
* @dev Internal function to remove a token ID from the list of a given address
188-
* @param _from address representing the previous owner of the given token ID
189-
* @param _tokenId uint256 ID of the token to be removed from the tokens list of the given address
190-
*/
191-
function removeTokenFrom(address _from, uint256 _tokenId) internal {
192-
super.removeTokenFrom(_from, _tokenId);
193-
194-
uint256 tokenIndex = ownedTokensIndex[_tokenId];
195-
uint256 lastTokenIndex = ownedTokens[_from].length.sub(1);
196-
uint256 lastToken = ownedTokens[_from][lastTokenIndex];
197-
198-
ownedTokens[_from][tokenIndex] = lastToken;
199-
ownedTokens[_from][lastTokenIndex] = 0;
200-
// Note that this will handle single-element arrays. In that case, both tokenIndex and lastTokenIndex are going to
201-
// be zero. Then we can make sure that we will remove _tokenId from the ownedTokens list since we are first swapping
202-
// the lastToken to the first position, and then dropping the element placed in the last position of the list
203-
204-
ownedTokens[_from].length--;
205-
ownedTokensIndex[_tokenId] = 0;
206-
ownedTokensIndex[lastToken] = tokenIndex;
207-
}
208-
209-
/**
210-
* @dev Internal function to mint a new token
211-
* Reverts if the given token ID already exists
212-
* @param _to address the beneficiary that will own the minted token
213-
* @param _tokenId uint256 ID of the token to be minted by the msg.sender
214-
*/
215-
function _mint(address _to, uint256 _tokenId) internal {
216-
super._mint(_to, _tokenId);
217-
218-
allTokensIndex[_tokenId] = allTokens.length;
219-
allTokens.push(_tokenId);
220-
}
221-
222-
/**
223-
* @dev Internal function to burn a specific token
224-
* Reverts if the token does not exist
225-
* @param _owner owner of the token to burn
226-
* @param _tokenId uint256 ID of the token being burned by the msg.sender
227-
*/
228-
function _burn(address _owner, uint256 _tokenId) internal {
229-
super._burn(_owner, _tokenId);
230-
231-
// Clear metadata (if any)
232-
if (bytes(tokenURIs[_tokenId]).length != 0) {
233-
delete tokenURIs[_tokenId];
234-
}
235-
236-
// Reorg all tokens array
237-
uint256 tokenIndex = allTokensIndex[_tokenId];
238-
uint256 lastTokenIndex = allTokens.length.sub(1);
239-
uint256 lastToken = allTokens[lastTokenIndex];
240-
241-
allTokens[tokenIndex] = lastToken;
242-
allTokens[lastTokenIndex] = 0;
243-
244-
allTokens.length--;
245-
allTokensIndex[_tokenId] = 0;
246-
allTokensIndex[lastToken] = tokenIndex;
247-
}
248-
249167
}

0 commit comments

Comments
 (0)