ERC721BasicToken.sol 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. pragma solidity ^0.4.18;
  2. import "./ERC721Basic.sol";
  3. import "./ERC721Receiver.sol";
  4. import "../../math/SafeMath.sol";
  5. import "../../AddressUtils.sol";
  6. /**
  7. * @title ERC721 Non-Fungible Token Standard basic implementation
  8. * @dev see https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md
  9. */
  10. contract ERC721BasicToken is ERC721Basic {
  11. using SafeMath for uint256;
  12. using AddressUtils for address;
  13. // Equals to `bytes4(keccak256("onERC721Received(address,uint256,bytes)"))`
  14. // which can be also obtained as `ERC721Receiver(0).onERC721Received.selector`
  15. bytes4 constant ERC721_RECEIVED = 0xf0b9e5ba;
  16. // Mapping from token ID to owner
  17. mapping (uint256 => address) internal tokenOwner;
  18. // Mapping from token ID to approved address
  19. mapping (uint256 => address) internal tokenApprovals;
  20. // Mapping from owner to number of owned token
  21. mapping (address => uint256) internal ownedTokensCount;
  22. // Mapping from owner to operator approvals
  23. mapping (address => mapping (address => bool)) internal operatorApprovals;
  24. /**
  25. * @dev Guarantees msg.sender is owner of the given token
  26. * @param _tokenId uint256 ID of the token to validate its ownership belongs to msg.sender
  27. */
  28. modifier onlyOwnerOf(uint256 _tokenId) {
  29. require(ownerOf(_tokenId) == msg.sender);
  30. _;
  31. }
  32. /**
  33. * @dev Checks msg.sender can transfer a token, by being owner, approved, or operator
  34. * @param _tokenId uint256 ID of the token to validate
  35. */
  36. modifier canTransfer(uint256 _tokenId) {
  37. require(isApprovedOrOwner(msg.sender, _tokenId));
  38. _;
  39. }
  40. /**
  41. * @dev Gets the balance of the specified address
  42. * @param _owner address to query the balance of
  43. * @return uint256 representing the amount owned by the passed address
  44. */
  45. function balanceOf(address _owner) public view returns (uint256) {
  46. require(_owner != address(0));
  47. return ownedTokensCount[_owner];
  48. }
  49. /**
  50. * @dev Gets the owner of the specified token ID
  51. * @param _tokenId uint256 ID of the token to query the owner of
  52. * @return owner address currently marked as the owner of the given token ID
  53. */
  54. function ownerOf(uint256 _tokenId) public view returns (address) {
  55. address owner = tokenOwner[_tokenId];
  56. require(owner != address(0));
  57. return owner;
  58. }
  59. /**
  60. * @dev Returns whether the specified token exists
  61. * @param _tokenId uint256 ID of the token to query the existance of
  62. * @return whether the token exists
  63. */
  64. function exists(uint256 _tokenId) public view returns (bool) {
  65. address owner = tokenOwner[_tokenId];
  66. return owner != address(0);
  67. }
  68. /**
  69. * @dev Approves another address to transfer the given token ID
  70. * @dev The zero address indicates there is no approved address.
  71. * @dev There can only be one approved address per token at a given time.
  72. * @dev Can only be called by the token owner or an approved operator.
  73. * @param _to address to be approved for the given token ID
  74. * @param _tokenId uint256 ID of the token to be approved
  75. */
  76. function approve(address _to, uint256 _tokenId) public {
  77. address owner = ownerOf(_tokenId);
  78. require(_to != owner);
  79. require(msg.sender == owner || isApprovedForAll(owner, msg.sender));
  80. if (getApproved(_tokenId) != address(0) || _to != address(0)) {
  81. tokenApprovals[_tokenId] = _to;
  82. Approval(owner, _to, _tokenId);
  83. }
  84. }
  85. /**
  86. * @dev Gets the approved address for a token ID, or zero if no address set
  87. * @param _tokenId uint256 ID of the token to query the approval of
  88. * @return address currently approved for a the given token ID
  89. */
  90. function getApproved(uint256 _tokenId) public view returns (address) {
  91. return tokenApprovals[_tokenId];
  92. }
  93. /**
  94. * @dev Sets or unsets the approval of a given operator
  95. * @dev An operator is allowed to transfer all tokens of the sender on their behalf
  96. * @param _to operator address to set the approval
  97. * @param _approved representing the status of the approval to be set
  98. */
  99. function setApprovalForAll(address _to, bool _approved) public {
  100. require(_to != msg.sender);
  101. operatorApprovals[msg.sender][_to] = _approved;
  102. ApprovalForAll(msg.sender, _to, _approved);
  103. }
  104. /**
  105. * @dev Tells whether an operator is approved by a given owner
  106. * @param _owner owner address which you want to query the approval of
  107. * @param _operator operator address which you want to query the approval of
  108. * @return bool whether the given operator is approved by the given owner
  109. */
  110. function isApprovedForAll(address _owner, address _operator) public view returns (bool) {
  111. return operatorApprovals[_owner][_operator];
  112. }
  113. /**
  114. * @dev Transfers the ownership of a given token ID to another address
  115. * @dev Usage of this method is discouraged, use `safeTransferFrom` whenever possible
  116. * @dev Requires the msg sender to be the owner, approved, or operator
  117. * @param _from current owner of the token
  118. * @param _to address to receive the ownership of the given token ID
  119. * @param _tokenId uint256 ID of the token to be transferred
  120. */
  121. function transferFrom(address _from, address _to, uint256 _tokenId) public canTransfer(_tokenId) {
  122. require(_from != address(0));
  123. require(_to != address(0));
  124. clearApproval(_from, _tokenId);
  125. removeTokenFrom(_from, _tokenId);
  126. addTokenTo(_to, _tokenId);
  127. Transfer(_from, _to, _tokenId);
  128. }
  129. /**
  130. * @dev Safely transfers the ownership of a given token ID to another address
  131. * @dev If the target address is a contract, it must implement `onERC721Received`,
  132. * which is called upon a safe transfer, and return the magic value
  133. * `bytes4(keccak256("onERC721Received(address,uint256,bytes)"))`; otherwise,
  134. * the transfer is reverted.
  135. * @dev Requires the msg sender to be the owner, approved, or operator
  136. * @param _from current owner of the token
  137. * @param _to address to receive the ownership of the given token ID
  138. * @param _tokenId uint256 ID of the token to be transferred
  139. */
  140. function safeTransferFrom(address _from, address _to, uint256 _tokenId) public canTransfer(_tokenId) {
  141. safeTransferFrom(_from, _to, _tokenId, "");
  142. }
  143. /**
  144. * @dev Safely transfers the ownership of a given token ID to another address
  145. * @dev If the target address is a contract, it must implement `onERC721Received`,
  146. * which is called upon a safe transfer, and return the magic value
  147. * `bytes4(keccak256("onERC721Received(address,uint256,bytes)"))`; otherwise,
  148. * the transfer is reverted.
  149. * @dev Requires the msg sender to be the owner, approved, or operator
  150. * @param _from current owner of the token
  151. * @param _to address to receive the ownership of the given token ID
  152. * @param _tokenId uint256 ID of the token to be transferred
  153. * @param _data bytes data to send along with a safe transfer check
  154. */
  155. function safeTransferFrom(address _from, address _to, uint256 _tokenId, bytes _data) public canTransfer(_tokenId) {
  156. transferFrom(_from, _to, _tokenId);
  157. require(checkAndCallSafeTransfer(_from, _to, _tokenId, _data));
  158. }
  159. /**
  160. * @dev Returns whether the given spender can transfer a given token ID
  161. * @param _spender address of the spender to query
  162. * @param _tokenId uint256 ID of the token to be transferred
  163. * @return bool whether the msg.sender is approved for the given token ID,
  164. * is an operator of the owner, or is the owner of the token
  165. */
  166. function isApprovedOrOwner(address _spender, uint256 _tokenId) internal view returns (bool) {
  167. address owner = ownerOf(_tokenId);
  168. return _spender == owner || getApproved(_tokenId) == _spender || isApprovedForAll(owner, _spender);
  169. }
  170. /**
  171. * @dev Internal function to mint a new token
  172. * @dev Reverts if the given token ID already exists
  173. * @param _to The address that will own the minted token
  174. * @param _tokenId uint256 ID of the token to be minted by the msg.sender
  175. */
  176. function _mint(address _to, uint256 _tokenId) internal {
  177. require(_to != address(0));
  178. addTokenTo(_to, _tokenId);
  179. Transfer(address(0), _to, _tokenId);
  180. }
  181. /**
  182. * @dev Internal function to burn a specific token
  183. * @dev Reverts if the token does not exist
  184. * @param _tokenId uint256 ID of the token being burned by the msg.sender
  185. */
  186. function _burn(address _owner, uint256 _tokenId) internal {
  187. clearApproval(_owner, _tokenId);
  188. removeTokenFrom(_owner, _tokenId);
  189. Transfer(_owner, address(0), _tokenId);
  190. }
  191. /**
  192. * @dev Internal function to clear current approval of a given token ID
  193. * @dev Reverts if the given address is not indeed the owner of the token
  194. * @param _owner owner of the token
  195. * @param _tokenId uint256 ID of the token to be transferred
  196. */
  197. function clearApproval(address _owner, uint256 _tokenId) internal {
  198. require(ownerOf(_tokenId) == _owner);
  199. if (tokenApprovals[_tokenId] != address(0)) {
  200. tokenApprovals[_tokenId] = address(0);
  201. Approval(_owner, address(0), _tokenId);
  202. }
  203. }
  204. /**
  205. * @dev Internal function to add a token ID to the list of a given address
  206. * @param _to address representing the new owner of the given token ID
  207. * @param _tokenId uint256 ID of the token to be added to the tokens list of the given address
  208. */
  209. function addTokenTo(address _to, uint256 _tokenId) internal {
  210. require(tokenOwner[_tokenId] == address(0));
  211. tokenOwner[_tokenId] = _to;
  212. ownedTokensCount[_to] = ownedTokensCount[_to].add(1);
  213. }
  214. /**
  215. * @dev Internal function to remove a token ID from the list of a given address
  216. * @param _from address representing the previous owner of the given token ID
  217. * @param _tokenId uint256 ID of the token to be removed from the tokens list of the given address
  218. */
  219. function removeTokenFrom(address _from, uint256 _tokenId) internal {
  220. require(ownerOf(_tokenId) == _from);
  221. ownedTokensCount[_from] = ownedTokensCount[_from].sub(1);
  222. tokenOwner[_tokenId] = address(0);
  223. }
  224. /**
  225. * @dev Internal function to invoke `onERC721Received` on a target address
  226. * @dev The call is not executed if the target address is not a contract
  227. * @param _from address representing the previous owner of the given token ID
  228. * @param _to target address that will receive the tokens
  229. * @param _tokenId uint256 ID of the token to be transferred
  230. * @param _data bytes optional data to send along with the call
  231. * @return whether the call correctly returned the expected magic value
  232. */
  233. function checkAndCallSafeTransfer(address _from, address _to, uint256 _tokenId, bytes _data) internal returns (bool) {
  234. if (!_to.isContract()) {
  235. return true;
  236. }
  237. bytes4 retval = ERC721Receiver(_to).onERC721Received(_from, _tokenId, _data);
  238. return (retval == ERC721_RECEIVED);
  239. }
  240. }