ERC721BasicToken.sol 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. pragma solidity ^0.4.23;
  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 private 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(
  111. address _owner,
  112. address _operator
  113. )
  114. public
  115. view
  116. returns (bool)
  117. {
  118. return operatorApprovals[_owner][_operator];
  119. }
  120. /**
  121. * @dev Transfers the ownership of a given token ID to another address
  122. * @dev Usage of this method is discouraged, use `safeTransferFrom` whenever possible
  123. * @dev Requires the msg sender to be the owner, approved, or operator
  124. * @param _from current owner of the token
  125. * @param _to address to receive the ownership of the given token ID
  126. * @param _tokenId uint256 ID of the token to be transferred
  127. */
  128. function transferFrom(
  129. address _from,
  130. address _to,
  131. uint256 _tokenId
  132. )
  133. public
  134. canTransfer(_tokenId)
  135. {
  136. require(_from != address(0));
  137. require(_to != address(0));
  138. clearApproval(_from, _tokenId);
  139. removeTokenFrom(_from, _tokenId);
  140. addTokenTo(_to, _tokenId);
  141. emit Transfer(_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. */
  154. function safeTransferFrom(
  155. address _from,
  156. address _to,
  157. uint256 _tokenId
  158. )
  159. public
  160. canTransfer(_tokenId)
  161. {
  162. // solium-disable-next-line arg-overflow
  163. safeTransferFrom(_from, _to, _tokenId, "");
  164. }
  165. /**
  166. * @dev Safely transfers the ownership of a given token ID to another address
  167. * @dev If the target address is a contract, it must implement `onERC721Received`,
  168. * which is called upon a safe transfer, and return the magic value
  169. * `bytes4(keccak256("onERC721Received(address,uint256,bytes)"))`; otherwise,
  170. * the transfer is reverted.
  171. * @dev Requires the msg sender to be the owner, approved, or operator
  172. * @param _from current owner of the token
  173. * @param _to address to receive the ownership of the given token ID
  174. * @param _tokenId uint256 ID of the token to be transferred
  175. * @param _data bytes data to send along with a safe transfer check
  176. */
  177. function safeTransferFrom(
  178. address _from,
  179. address _to,
  180. uint256 _tokenId,
  181. bytes _data
  182. )
  183. public
  184. canTransfer(_tokenId)
  185. {
  186. transferFrom(_from, _to, _tokenId);
  187. // solium-disable-next-line arg-overflow
  188. require(checkAndCallSafeTransfer(_from, _to, _tokenId, _data));
  189. }
  190. /**
  191. * @dev Returns whether the given spender can transfer a given token ID
  192. * @param _spender address of the spender to query
  193. * @param _tokenId uint256 ID of the token to be transferred
  194. * @return bool whether the msg.sender is approved for the given token ID,
  195. * is an operator of the owner, or is the owner of the token
  196. */
  197. function isApprovedOrOwner(
  198. address _spender,
  199. uint256 _tokenId
  200. )
  201. internal
  202. view
  203. returns (bool)
  204. {
  205. address owner = ownerOf(_tokenId);
  206. // Disable solium check because of
  207. // https://github.com/duaraghav8/Solium/issues/175
  208. // solium-disable-next-line operator-whitespace
  209. return (
  210. _spender == owner ||
  211. getApproved(_tokenId) == _spender ||
  212. isApprovedForAll(owner, _spender)
  213. );
  214. }
  215. /**
  216. * @dev Internal function to mint a new token
  217. * @dev Reverts if the given token ID already exists
  218. * @param _to The address that will own the minted token
  219. * @param _tokenId uint256 ID of the token to be minted by the msg.sender
  220. */
  221. function _mint(address _to, uint256 _tokenId) internal {
  222. require(_to != address(0));
  223. addTokenTo(_to, _tokenId);
  224. emit Transfer(address(0), _to, _tokenId);
  225. }
  226. /**
  227. * @dev Internal function to burn a specific token
  228. * @dev Reverts if the token does not exist
  229. * @param _tokenId uint256 ID of the token being burned by the msg.sender
  230. */
  231. function _burn(address _owner, uint256 _tokenId) internal {
  232. clearApproval(_owner, _tokenId);
  233. removeTokenFrom(_owner, _tokenId);
  234. emit Transfer(_owner, address(0), _tokenId);
  235. }
  236. /**
  237. * @dev Internal function to clear current approval of a given token ID
  238. * @dev Reverts if the given address is not indeed the owner of the token
  239. * @param _owner owner of the token
  240. * @param _tokenId uint256 ID of the token to be transferred
  241. */
  242. function clearApproval(address _owner, uint256 _tokenId) internal {
  243. require(ownerOf(_tokenId) == _owner);
  244. if (tokenApprovals[_tokenId] != address(0)) {
  245. tokenApprovals[_tokenId] = address(0);
  246. emit Approval(_owner, address(0), _tokenId);
  247. }
  248. }
  249. /**
  250. * @dev Internal function to add a token ID to the list of a given address
  251. * @param _to address representing the new owner of the given token ID
  252. * @param _tokenId uint256 ID of the token to be added to the tokens list of the given address
  253. */
  254. function addTokenTo(address _to, uint256 _tokenId) internal {
  255. require(tokenOwner[_tokenId] == address(0));
  256. tokenOwner[_tokenId] = _to;
  257. ownedTokensCount[_to] = ownedTokensCount[_to].add(1);
  258. }
  259. /**
  260. * @dev Internal function to remove a token ID from the list of a given address
  261. * @param _from address representing the previous owner of the given token ID
  262. * @param _tokenId uint256 ID of the token to be removed from the tokens list of the given address
  263. */
  264. function removeTokenFrom(address _from, uint256 _tokenId) internal {
  265. require(ownerOf(_tokenId) == _from);
  266. ownedTokensCount[_from] = ownedTokensCount[_from].sub(1);
  267. tokenOwner[_tokenId] = address(0);
  268. }
  269. /**
  270. * @dev Internal function to invoke `onERC721Received` on a target address
  271. * @dev The call is not executed if the target address is not a contract
  272. * @param _from address representing the previous owner of the given token ID
  273. * @param _to target address that will receive the tokens
  274. * @param _tokenId uint256 ID of the token to be transferred
  275. * @param _data bytes optional data to send along with the call
  276. * @return whether the call correctly returned the expected magic value
  277. */
  278. function checkAndCallSafeTransfer(
  279. address _from,
  280. address _to,
  281. uint256 _tokenId,
  282. bytes _data
  283. )
  284. internal
  285. returns (bool)
  286. {
  287. if (!_to.isContract()) {
  288. return true;
  289. }
  290. bytes4 retval = ERC721Receiver(_to).onERC721Received(
  291. _from, _tokenId, _data);
  292. return (retval == ERC721_RECEIVED);
  293. }
  294. }