-
Notifications
You must be signed in to change notification settings - Fork 12.4k
ERC721 full implementation #803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
1192e68
ca163a8
71cbc51
3745025
559df81
d726c79
54a1d2e
851685c
3cef880
6f180a6
6fbe771
626742e
95a1f9a
15f9556
b332995
f4748da
fb4f728
6b98e4e
3f2ea8a
74db03b
981c6f7
73b77ae
eee5b0e
9deb637
fe6e4ff
4836279
619ae84
2e593f2
6c09d20
37929c8
3676b55
7815cc5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
This allows token implementation to be non-abstract
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -30,6 +30,9 @@ contract ERC721Token is ERC721, ERC721BasicToken { | |
| // Mapping from token id to position in the allTokens array | ||
| mapping(uint256 => uint256) internal allTokensIndex; | ||
|
|
||
| // Optional mapping for token URIs | ||
| mapping(uint256 => string) internal tokenURIs; | ||
|
|
||
| /** | ||
| * @dev Constructor function | ||
| */ | ||
|
|
@@ -63,6 +66,27 @@ contract ERC721Token is ERC721, ERC721BasicToken { | |
| return symbol_; | ||
| } | ||
|
|
||
| /** | ||
| * @dev Returns an URI for a given token ID | ||
| * @dev Throws if the token ID does not exist. May return an empty string. | ||
| * @param _tokenId uint256 ID of the token to query | ||
| */ | ||
| function tokenURI(uint256 _tokenId) public view returns (string) { | ||
| require(exists(_tokenId)); | ||
| return tokenURIs[_tokenId]; | ||
| } | ||
|
|
||
| /** | ||
| * @dev Internal function to set the token URI for a given token | ||
| * @dev Reverts if the token ID does not exist | ||
| * @param _tokenId uint256 ID of the token to set its URI | ||
| * @param _uri string URI to assign | ||
| */ | ||
| function doSetTokenURI(uint256 _tokenId, string _uri) internal { | ||
| require(exists(_tokenId)); | ||
| tokenURIs[_tokenId] = _uri; | ||
| } | ||
|
|
||
| /** | ||
| * @dev Gets the token ID at a given index of the tokens list of the requested owner | ||
| * @param _owner address owning the tokens list to be accessed | ||
|
|
@@ -152,6 +176,12 @@ contract ERC721Token is ERC721, ERC721BasicToken { | |
| function doBurn(address _owner, uint256 _tokenId) internal { | ||
| super.doBurn(_owner, _tokenId); | ||
|
|
||
| // Clear metadata (if any) | ||
| if (bytes(tokenURIs[_tokenId]).length == 0) { | ||
|
||
| delete tokenURIs[_tokenId]; | ||
| } | ||
|
|
||
| // Reorg all tokens array | ||
| uint256 tokenIndex = allTokensIndex[_tokenId]; | ||
| uint256 lastTokenIndex = allTokens.length.sub(1); | ||
| uint256 lastToken = allTokens[lastTokenIndex]; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I may be missing something, but why is this mapping needed? As per the mint function, isn't the token's identifier the same thing as its index in the allTokens array?
allTokensIndex[_tokenId] = allTokens.length;The only other place this gets read is during
burn, where you could just use the_tokenIdto infer location instead of this mapping.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a token is burnt, the one with the highest id is put in the place of the burnt one in the
allTokensarray.https://github.com/spalladino/zeppelin-solidity/blob/6fbe771c276f83e734256908877c8f9aaa2e1bfc/contracts/token/ERC721/ERC721Token.sol#L189
In this way the index ⇔ id equivalence is invalidated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but only because
mintgenerates autoincremental token IDs. I didn't want to rely on this for implementing the enumeration, as anyone should be able to override this behaviour if needed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks guys for the clarification!