-
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
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,15 @@ | ||
| pragma solidity ^0.4.18; | ||
|
|
||
| /** | ||
| * Utility library of inline functions on addresses | ||
| */ | ||
| library AddressUtils { | ||
|
|
||
| /** | ||
| * Returns whether there is code in the target address | ||
| * @param addr address address to check | ||
| * @return whether there is code in the target address | ||
| */ | ||
| function isContract(address addr) internal view returns (bool) { | ||
| uint size; | ||
|
||
| assembly { size := extcodesize(addr) } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,17 +2,23 @@ pragma solidity ^0.4.18; | |
|
|
||
| import "./ERC721Basic.sol"; | ||
|
|
||
| /// @title ERC-721 Non-Fungible Token Standard, optional enumeration extension | ||
|
||
| /// @dev See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md | ||
| contract ERC721Enumerable is ERC721Basic { | ||
| function totalSupply() public view returns (uint256); | ||
| function tokenOfOwnerByIndex(address _owner, uint256 _index) public view returns (uint256 _tokenId); | ||
| // function tokenByIndex(uint256 _index) public view returns (uint256); | ||
| } | ||
|
|
||
| /// @title ERC-721 Non-Fungible Token Standard, optional metadata extension | ||
| /// @dev See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md | ||
| contract ERC721Metadata is ERC721Basic { | ||
| function name() public view returns (string _name); | ||
| function symbol() public view returns (string _symbol); | ||
| function tokenURI(uint256 _tokenId) public view returns (string); | ||
| } | ||
|
|
||
| /// @title ERC-721 Non-Fungible Token Standard, full implementation interface | ||
| /// @dev See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md | ||
| contract ERC721 is ERC721Basic, ERC721Enumerable, ERC721Metadata { | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,13 +6,14 @@ import "../../math/SafeMath.sol"; | |
| import "../../AddressUtils.sol"; | ||
|
|
||
| /** | ||
| * @title ERC721BasicToken | ||
| * Generic implementation for the required functionality of the ERC721 standard | ||
| * @title ERC721 Non-Fungible Token Standard basic implementation | ||
| * @dev see https://github.com/ethereum/eips/issues/721 | ||
| */ | ||
| contract ERC721BasicToken is ERC721Basic { | ||
| using SafeMath for uint256; | ||
| using AddressUtils for address; | ||
|
|
||
| // Gas allowed in calls to onERC721Received on safeTransfers | ||
| uint256 SAFE_TRANSFER_GAS_STIPEND = 50000; | ||
|
|
||
| // Equals to bytes4(keccak256("onERC721Received(address,uint256,bytes)")) | ||
|
|
@@ -42,6 +43,10 @@ contract ERC721BasicToken is ERC721Basic { | |
| _; | ||
| } | ||
|
|
||
| /** | ||
| * @dev Checks msg.sender can transfer a token, by being owner, approved, or operator | ||
| * @param _tokenId uint256 ID of the token to validate | ||
| */ | ||
| modifier canTransfer(uint256 _tokenId) { | ||
|
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. should this be
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. I kind of like the name because it clearly distinguishes from
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. Actually it's only being used to checking transfers. Where else do you see this being used @shrugs? We could change it to
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. perhaps a superclass will want to allow an owner to change the metadata of the token on-chain? (or is that not allowed by the spec?). Regardless, you're right; the concept of ownership here is pretty much just "can I transfer this token" which seems fine. I'm not particularly attached to |
||
| require(isApprovedOrOwner(msg.sender, _tokenId)); | ||
| _; | ||
|
|
@@ -67,13 +72,20 @@ contract ERC721BasicToken is ERC721Basic { | |
| return owner; | ||
| } | ||
|
|
||
| /** | ||
| * @dev Returns whether the specified token exists | ||
| * @param _tokenId uint256 ID of the token to query the existance of | ||
| * @return whether the token exists | ||
| */ | ||
| function exists(uint256 _tokenId) public view returns (bool) { | ||
| address owner = tokenOwner[_tokenId]; | ||
| return owner != 0; | ||
|
||
| } | ||
|
|
||
| /** | ||
| * @dev Approves another address to claim for the ownership of the given token ID | ||
| * @dev Approves another address to transfer the given token ID | ||
| * @dev The zero address indicates there is no approved address. | ||
| * @dev There can only be one approved address per token at a given time. | ||
| * @param _to address to be approved for the given token ID | ||
| * @param _tokenId uint256 ID of the token to be approved | ||
| */ | ||
|
|
@@ -87,17 +99,18 @@ contract ERC721BasicToken is ERC721Basic { | |
| } | ||
|
|
||
| /** | ||
| * @dev Gets the approved address to take ownership of a given token ID | ||
| * @dev Gets the approved address for a token ID, or zero if no address set | ||
| * @param _tokenId uint256 ID of the token to query the approval of | ||
| * @return address currently approved to take ownership of the given token ID | ||
| * @return address currently approved for a the given token ID | ||
| */ | ||
| function getApproved(uint256 _tokenId) public view returns (address) { | ||
| return tokenApprovals[_tokenId]; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * @dev Sets the approval of a given operator | ||
| * @dev Sets or unsets the approval of a given operator | ||
| * @dev An operator is allowed to transfer all tokens of the sender on their behalf | ||
| * @param _to operator address to set the approval | ||
| * @param _approved representing the status of the approval to be set | ||
| */ | ||
|
|
@@ -119,6 +132,8 @@ contract ERC721BasicToken is ERC721Basic { | |
|
|
||
| /** | ||
| * @dev Transfers the ownership of a given token ID to another address | ||
| * @dev Usage of this method is discouraged, use `safeTransferFrom` whenever possible | ||
| * @dev Requires the msg sender to be the owner, approved, or operator | ||
| * @param _from current owner of the token | ||
| * @param _to address to receive the ownership of the given token ID | ||
| * @param _tokenId uint256 ID of the token to be transferred | ||
|
|
@@ -128,7 +143,12 @@ contract ERC721BasicToken is ERC721Basic { | |
| } | ||
|
|
||
| /** | ||
| * @dev Transfers the ownership of a given token ID to another address | ||
| * @dev Safely transfers the ownership of a given token ID to another address | ||
| * @dev If the target address is a contract, it must implement `onERC721Received`, | ||
| * which is called upon a safe transfer, and return the magic value | ||
| * `bytes4(keccak256("onERC721Received(address,uint256,bytes)"))`; otherwise, | ||
| * the transfer is reverted. | ||
| * @dev Requires the msg sender to be the owner, approved, or operator | ||
| * @param _from current owner of the token | ||
| * @param _to address to receive the ownership of the given token ID | ||
| * @param _tokenId uint256 ID of the token to be transferred | ||
|
|
@@ -138,7 +158,12 @@ contract ERC721BasicToken is ERC721Basic { | |
| } | ||
|
|
||
| /** | ||
| * @dev Transfers the ownership of a given token ID to another address | ||
| * @dev Safely transfers the ownership of a given token ID to another address | ||
| * @dev If the target address is a contract, it must implement `onERC721Received`, | ||
| * which is called upon a safe transfer, and return the magic value | ||
| * `bytes4(keccak256("onERC721Received(address,uint256,bytes)"))`; otherwise, | ||
| * the transfer is reverted. | ||
| * @dev Requires the msg sender to be the owner, approved, or operator | ||
| * @param _from current owner of the token | ||
| * @param _to address to receive the ownership of the given token ID | ||
| * @param _tokenId uint256 ID of the token to be transferred | ||
|
|
@@ -149,7 +174,7 @@ contract ERC721BasicToken is ERC721Basic { | |
| } | ||
|
|
||
| /** | ||
| * @dev Tells whether the given spender can transfer a given token ID | ||
| * @dev Returns whether the given spender can transfer a given token ID | ||
| * @param _spender address of the spender to query | ||
| * @param _tokenId uint256 ID of the token to be transferred | ||
| * @return bool whether the msg.sender is approved for the given token ID, | ||
|
|
@@ -161,7 +186,8 @@ contract ERC721BasicToken is ERC721Basic { | |
| } | ||
|
|
||
| /** | ||
| * @dev Mint token function | ||
| * @dev Internal function to mint a new token | ||
| * @dev Reverts if the given token ID already exists | ||
| * @param _to The address that will own the minted token | ||
| * @param _tokenId uint256 ID of the token to be minted by the msg.sender | ||
| */ | ||
|
|
@@ -173,7 +199,8 @@ contract ERC721BasicToken is ERC721Basic { | |
| } | ||
|
|
||
| /** | ||
| * @dev Burns a specific token | ||
| * @dev Internal function to burn a specific token | ||
| * @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 { | ||
|
|
@@ -207,6 +234,8 @@ contract ERC721BasicToken is ERC721Basic { | |
|
|
||
| /** | ||
| * @dev Internal function to clear current approval of a given token ID | ||
| * @dev Reverts if the given address is not indeed the owner of the token | ||
| * @param _owner owner of the token | ||
| * @param _tokenId uint256 ID of the token to be transferred | ||
| */ | ||
| function clearApproval(address _owner, uint256 _tokenId) internal { | ||
|
|
@@ -239,6 +268,15 @@ contract ERC721BasicToken is ERC721Basic { | |
| tokenOwner[_tokenId] = 0; | ||
| } | ||
|
|
||
| /** | ||
| * @dev Internal function to invoke `onERC721Received` on a target address | ||
| * @dev The call is not executed if the target address is not a contract | ||
| * @dev Returns whether the call correctly returned the expected magic value | ||
|
||
| * @param _from address representing the previous owner of the given token ID | ||
| * @param _to target address that will receive the tokens | ||
| * @param _tokenId uint256 ID of the token to be transferred | ||
| * @param _data bytes optional data to send along with the call | ||
| */ | ||
| function checkAndCallSafeTransfer(address _from, address _to, uint256 _tokenId, bytes _data) internal returns (bool) { | ||
| return !_to.isContract() || | ||
|
||
| (ERC721Receiver(_to).onERC721Received.gas(SAFE_TRANSFER_GAS_STIPEND)(_from, _tokenId, _data) == ERC721_RECEIVED); | ||
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,7 @@ import "./ERC721BasicToken.sol"; | |
| * @title Full ERC721 Token | ||
| * This implementation includes all the required and some optional functionality of the ERC721 standard | ||
| * Moreover, it includes approve all functionality using operatable terminology | ||
|
||
| * @dev see https://github.com/ethereum/eips/issues/721 and https://github.com/ethereum/EIPs/pull/841 | ||
| * @dev see https://github.com/ethereum/eips/issues/721 | ||
| */ | ||
| contract ERC721Token is ERC721, ERC721BasicToken { | ||
| // Token name | ||
|
|
||
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.
A contract address doesn't have code until its constructor finishes executing; during execution of the constructor,
isContractwill not correctly detect that it is (or will be) a contract.How this affects ERC721 is not clear. Since the contract still doesn't have code, it would also be unable to execute
onERC721Received, so it makes no difference whetherisContractreturns true or false. OTOH, this situation is likely a programmer error, and it will fail silently. Sadly I don't think there's a solution other than changing the interface to make it explicit that anonERC721Receivedcall is expected.Given the odd semantics, I'm not sure we should provide
isContractas a standalone helper. I'm inclined to say no.If we do, we should place a disclaimer stating that it will return false if the contract constructor hasn't yet finished running.
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.
Adding a disclaimer!