-
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
- Remove restrictions from mock mint and burn calls
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,9 +19,6 @@ contract ERC721BasicToken is ERC721Basic { | |
| // Equals to bytes4(keccak256("onERC721Received(address,uint256,bytes)")) | ||
| bytes4 ERC721_RECEIVED = 0xf0b9e5ba; | ||
|
||
|
|
||
| // Total amount of tokens | ||
| uint256 internal totalTokens; | ||
|
|
||
| // Mapping from token ID to owner | ||
| mapping (uint256 => address) internal tokenOwner; | ||
|
|
||
|
|
@@ -194,7 +191,6 @@ contract ERC721BasicToken is ERC721Basic { | |
| function doMint(address _to, uint256 _tokenId) internal { | ||
|
||
| require(_to != address(0)); | ||
| addToken(_to, _tokenId); | ||
| totalTokens = totalTokens.add(1); | ||
| Transfer(0x0, _to, _tokenId); | ||
| } | ||
|
|
||
|
|
@@ -203,11 +199,10 @@ contract ERC721BasicToken is ERC721Basic { | |
| * @dev Reverts if the token does not exist | ||
| * @param _tokenId uint256 ID of the token being burned by the msg.sender | ||
| */ | ||
| function doBurn(uint256 _tokenId) onlyOwnerOf(_tokenId) internal { | ||
| clearApproval(msg.sender, _tokenId); | ||
| removeToken(msg.sender, _tokenId); | ||
| totalTokens = totalTokens.sub(1); | ||
| Transfer(msg.sender, 0x0, _tokenId); | ||
| function doBurn(address _owner, uint256 _tokenId) internal { | ||
| clearApproval(_owner, _tokenId); | ||
| removeToken(_owner, _tokenId); | ||
| Transfer(_owner, 0x0, _tokenId); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,12 @@ contract ERC721Token is ERC721, ERC721BasicToken { | |
| // Mapping from token ID to index of the owner tokens list | ||
| mapping(uint256 => uint256) internal ownedTokensIndex; | ||
|
|
||
| // Array with all token ids, used for enumeration | ||
| uint256[] internal allTokens; | ||
|
|
||
| // Mapping from token id to position in the allTokens array | ||
| mapping(uint256 => uint256) internal allTokensIndex; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
The only other place this gets read is during
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 In this way the index ⇔ id equivalence is invalidated.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yes, but only because There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks guys for the clarification! |
||
|
|
||
| /** | ||
| * @dev Constructor function | ||
| */ | ||
|
|
@@ -73,7 +79,18 @@ contract ERC721Token is ERC721, ERC721BasicToken { | |
| * @return uint256 representing the total amount of tokens | ||
| */ | ||
| function totalSupply() public view returns (uint256) { | ||
| return totalTokens; | ||
| return allTokens.length; | ||
| } | ||
|
|
||
| /** | ||
| * @dev Gets the token ID at a given index of all the tokens in this contract | ||
| * @dev Reverts if the index is greater or equal to the total number of tokens | ||
| * @param _index uint256 representing the index to be accessed of the tokens list | ||
| * @return uint256 token ID at the given index of the tokens list | ||
| */ | ||
| function tokenByIndex(uint256 _index) public view returns (uint256) { | ||
| require(_index < totalSupply()); | ||
| return allTokens[_index]; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -113,4 +130,38 @@ contract ERC721Token is ERC721, ERC721BasicToken { | |
| ownedTokensIndex[lastToken] = tokenIndex; | ||
| } | ||
|
|
||
| /** | ||
| * @dev Internal function to mint a new token | ||
| * @dev Reverts if the given token ID already exists | ||
| * @param _to address the beneficiary that will own the minted token | ||
| * @param _tokenId uint256 ID of the token to be minted by the msg.sender | ||
| */ | ||
| function doMint(address _to, uint256 _tokenId) internal { | ||
| super.doMint(_to, _tokenId); | ||
|
|
||
| allTokensIndex[_tokenId] = allTokens.length; | ||
| allTokens.push(_tokenId); | ||
| } | ||
|
|
||
| /** | ||
| * @dev Internal function to burn a specific token | ||
| * @dev Reverts if the token does not exist | ||
| * @param _owner owner of the token to burn | ||
| * @param _tokenId uint256 ID of the token being burned by the msg.sender | ||
| */ | ||
| function doBurn(address _owner, uint256 _tokenId) internal { | ||
| super.doBurn(_owner, _tokenId); | ||
|
|
||
| uint256 tokenIndex = allTokensIndex[_tokenId]; | ||
| uint256 lastTokenIndex = allTokens.length.sub(1); | ||
| uint256 lastToken = allTokens[lastTokenIndex]; | ||
|
|
||
| allTokens[tokenIndex] = lastToken; | ||
| allTokens[lastTokenIndex] = 0; | ||
|
|
||
| allTokens.length--; | ||
| allTokensIndex[_tokenId] = 0; | ||
| allTokensIndex[lastToken] = tokenIndex; | ||
| } | ||
|
|
||
| } | ||
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'm not sure if we have a convention for this, but I think modifiers should be indented.