ERC721BasicToken.sol 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. pragma solidity ^0.4.21;
  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 existence 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. emit 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 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. emit 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. emit 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(
  141. address _from,
  142. address _to,
  143. uint256 _tokenId
  144. )
  145. public
  146. canTransfer(_tokenId)
  147. {
  148. // solium-disable-next-line arg-overflow
  149. safeTransferFrom(_from, _to, _tokenId, "");
  150. }
  151. /**
  152. * @dev Safely transfers the ownership of a given token ID to another address
  153. * @dev If the target address is a contract, it must implement `onERC721Received`,
  154. * which is called upon a safe transfer, and return the magic value
  155. * `bytes4(keccak256("onERC721Received(address,uint256,bytes)"))`; otherwise,
  156. * the transfer is reverted.
  157. * @dev Requires the msg sender to be the owner, approved, or operator
  158. * @param _from current owner of the token
  159. * @param _to address to receive the ownership of the given token ID
  160. * @param _tokenId uint256 ID of the token to be transferred
  161. * @param _data bytes data to send along with a safe transfer check
  162. */
  163. function safeTransferFrom(
  164. address _from,
  165. address _to,
  166. uint256 _tokenId,
  167. bytes _data
  168. )
  169. public
  170. canTransfer(_tokenId)
  171. {
  172. transferFrom(_from, _to, _tokenId);
  173. // solium-disable-next-line arg-overflow
  174. require(checkAndCallSafeTransfer(_from, _to, _tokenId, _data));
  175. }
  176. /**
  177. * @dev Returns whether the given spender can transfer a given token ID
  178. * @param _spender address of the spender to query
  179. * @param _tokenId uint256 ID of the token to be transferred
  180. * @return bool whether the msg.sender is approved for the given token ID,
  181. * is an operator of the owner, or is the owner of the token
  182. */
  183. function isApprovedOrOwner(address _spender, uint256 _tokenId) internal view returns (bool) {
  184. address owner = ownerOf(_tokenId);
  185. return _spender == owner || getApproved(_tokenId) == _spender || isApprovedForAll(owner, _spender);
  186. }
  187. /**
  188. * @dev Internal function to mint a new token
  189. * @dev Reverts if the given token ID already exists
  190. * @param _to The address that will own the minted token
  191. * @param _tokenId uint256 ID of the token to be minted by the msg.sender
  192. */
  193. function _mint(address _to, uint256 _tokenId) internal {
  194. require(_to != address(0));
  195. addTokenTo(_to, _tokenId);
  196. emit Transfer(address(0), _to, _tokenId);
  197. }
  198. /**
  199. * @dev Internal function to burn a specific token
  200. * @dev Reverts if the token does not exist
  201. * @param _tokenId uint256 ID of the token being burned by the msg.sender
  202. */
  203. function _burn(address _owner, uint256 _tokenId) internal {
  204. clearApproval(_owner, _tokenId);
  205. removeTokenFrom(_owner, _tokenId);
  206. emit Transfer(_owner, address(0), _tokenId);
  207. }
  208. /**
  209. * @dev Internal function to clear current approval of a given token ID
  210. * @dev Reverts if the given address is not indeed the owner of the token
  211. * @param _owner owner of the token
  212. * @param _tokenId uint256 ID of the token to be transferred
  213. */
  214. function clearApproval(address _owner, uint256 _tokenId) internal {
  215. require(ownerOf(_tokenId) == _owner);
  216. if (tokenApprovals[_tokenId] != address(0)) {
  217. tokenApprovals[_tokenId] = address(0);
  218. emit Approval(_owner, address(0), _tokenId);
  219. }
  220. }
  221. /**
  222. * @dev Internal function to add a token ID to the list of a given address
  223. * @param _to address representing the new owner of the given token ID
  224. * @param _tokenId uint256 ID of the token to be added to the tokens list of the given address
  225. */
  226. function addTokenTo(address _to, uint256 _tokenId) internal {
  227. require(tokenOwner[_tokenId] == address(0));
  228. tokenOwner[_tokenId] = _to;
  229. ownedTokensCount[_to] = ownedTokensCount[_to].add(1);
  230. }
  231. /**
  232. * @dev Internal function to remove a token ID from the list of a given address
  233. * @param _from address representing the previous owner of the given token ID
  234. * @param _tokenId uint256 ID of the token to be removed from the tokens list of the given address
  235. */
  236. function removeTokenFrom(address _from, uint256 _tokenId) internal {
  237. require(ownerOf(_tokenId) == _from);
  238. ownedTokensCount[_from] = ownedTokensCount[_from].sub(1);
  239. tokenOwner[_tokenId] = address(0);
  240. }
  241. /**
  242. * @dev Internal function to invoke `onERC721Received` on a target address
  243. * @dev The call is not executed if the target address is not a contract
  244. * @param _from address representing the previous owner of the given token ID
  245. * @param _to target address that will receive the tokens
  246. * @param _tokenId uint256 ID of the token to be transferred
  247. * @param _data bytes optional data to send along with the call
  248. * @return whether the call correctly returned the expected magic value
  249. */
  250. function checkAndCallSafeTransfer(
  251. address _from,
  252. address _to,
  253. uint256 _tokenId,
  254. bytes _data
  255. )
  256. internal
  257. returns (bool)
  258. {
  259. if (!_to.isContract()) {
  260. return true;
  261. }
  262. bytes4 retval = ERC721Receiver(_to).onERC721Received(_from, _tokenId, _data);
  263. return (retval == ERC721_RECEIVED);
  264. }
  265. }