ERC721.sol 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. pragma solidity ^0.4.24;
  2. import "./IERC721.sol";
  3. import "./IERC721Receiver.sol";
  4. import "../../math/SafeMath.sol";
  5. import "../../utils/Address.sol";
  6. import "../../introspection/ERC165.sol";
  7. /**
  8. * @title ERC721 Non-Fungible Token Standard basic implementation
  9. * @dev see https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md
  10. */
  11. contract ERC721 is ERC165, IERC721 {
  12. using SafeMath for uint256;
  13. using Address for address;
  14. // Equals to `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`
  15. // which can be also obtained as `IERC721Receiver(0).onERC721Received.selector`
  16. bytes4 private constant _ERC721_RECEIVED = 0x150b7a02;
  17. // Mapping from token ID to owner
  18. mapping (uint256 => address) private _tokenOwner;
  19. // Mapping from token ID to approved address
  20. mapping (uint256 => address) private _tokenApprovals;
  21. // Mapping from owner to number of owned token
  22. mapping (address => uint256) private _ownedTokensCount;
  23. // Mapping from owner to operator approvals
  24. mapping (address => mapping (address => bool)) private _operatorApprovals;
  25. bytes4 private constant _InterfaceId_ERC721 = 0x80ac58cd;
  26. /*
  27. * 0x80ac58cd ===
  28. * bytes4(keccak256('balanceOf(address)')) ^
  29. * bytes4(keccak256('ownerOf(uint256)')) ^
  30. * bytes4(keccak256('approve(address,uint256)')) ^
  31. * bytes4(keccak256('getApproved(uint256)')) ^
  32. * bytes4(keccak256('setApprovalForAll(address,bool)')) ^
  33. * bytes4(keccak256('isApprovedForAll(address,address)')) ^
  34. * bytes4(keccak256('transferFrom(address,address,uint256)')) ^
  35. * bytes4(keccak256('safeTransferFrom(address,address,uint256)')) ^
  36. * bytes4(keccak256('safeTransferFrom(address,address,uint256,bytes)'))
  37. */
  38. constructor () public {
  39. // register the supported interfaces to conform to ERC721 via ERC165
  40. _registerInterface(_InterfaceId_ERC721);
  41. }
  42. /**
  43. * @dev Gets the balance of the specified address
  44. * @param owner address to query the balance of
  45. * @return uint256 representing the amount owned by the passed address
  46. */
  47. function balanceOf(address owner) public view returns (uint256) {
  48. require(owner != address(0));
  49. return _ownedTokensCount[owner];
  50. }
  51. /**
  52. * @dev Gets the owner of the specified token ID
  53. * @param tokenId uint256 ID of the token to query the owner of
  54. * @return owner address currently marked as the owner of the given token ID
  55. */
  56. function ownerOf(uint256 tokenId) public view returns (address) {
  57. address owner = _tokenOwner[tokenId];
  58. require(owner != address(0));
  59. return owner;
  60. }
  61. /**
  62. * @dev Approves another address to transfer the given token ID
  63. * The zero address indicates there is no approved address.
  64. * There can only be one approved address per token at a given time.
  65. * Can only be called by the token owner or an approved operator.
  66. * @param to address to be approved for the given token ID
  67. * @param tokenId uint256 ID of the token to be approved
  68. */
  69. function approve(address to, uint256 tokenId) public {
  70. address owner = ownerOf(tokenId);
  71. require(to != owner);
  72. require(msg.sender == owner || isApprovedForAll(owner, msg.sender));
  73. _tokenApprovals[tokenId] = to;
  74. emit Approval(owner, to, tokenId);
  75. }
  76. /**
  77. * @dev Gets the approved address for a token ID, or zero if no address set
  78. * Reverts if the token ID does not exist.
  79. * @param tokenId uint256 ID of the token to query the approval of
  80. * @return address currently approved for the given token ID
  81. */
  82. function getApproved(uint256 tokenId) public view returns (address) {
  83. require(_exists(tokenId));
  84. return _tokenApprovals[tokenId];
  85. }
  86. /**
  87. * @dev Sets or unsets the approval of a given operator
  88. * An operator is allowed to transfer all tokens of the sender on their behalf
  89. * @param to operator address to set the approval
  90. * @param approved representing the status of the approval to be set
  91. */
  92. function setApprovalForAll(address to, bool approved) public {
  93. require(to != msg.sender);
  94. _operatorApprovals[msg.sender][to] = approved;
  95. emit ApprovalForAll(msg.sender, to, approved);
  96. }
  97. /**
  98. * @dev Tells whether an operator is approved by a given owner
  99. * @param owner owner address which you want to query the approval of
  100. * @param operator operator address which you want to query the approval of
  101. * @return bool whether the given operator is approved by the given owner
  102. */
  103. function isApprovedForAll(address owner, address operator) public view returns (bool) {
  104. return _operatorApprovals[owner][operator];
  105. }
  106. /**
  107. * @dev Transfers the ownership of a given token ID to another address
  108. * Usage of this method is discouraged, use `safeTransferFrom` whenever possible
  109. * Requires the msg sender to be the owner, approved, or operator
  110. * @param from current owner of the token
  111. * @param to address to receive the ownership of the given token ID
  112. * @param tokenId uint256 ID of the token to be transferred
  113. */
  114. function transferFrom(address from, address to, uint256 tokenId) public {
  115. require(_isApprovedOrOwner(msg.sender, tokenId));
  116. _transferFrom(from, to, tokenId);
  117. }
  118. /**
  119. * @dev Safely transfers the ownership of a given token ID to another address
  120. * If the target address is a contract, it must implement `onERC721Received`,
  121. * which is called upon a safe transfer, and return the magic value
  122. * `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise,
  123. * the transfer is reverted.
  124. *
  125. * Requires the msg sender to be the owner, approved, or operator
  126. * @param from current owner of the token
  127. * @param to address to receive the ownership of the given token ID
  128. * @param tokenId uint256 ID of the token to be transferred
  129. */
  130. function safeTransferFrom(address from, address to, uint256 tokenId) public {
  131. // solium-disable-next-line arg-overflow
  132. safeTransferFrom(from, to, tokenId, "");
  133. }
  134. /**
  135. * @dev Safely transfers the ownership of a given token ID to another address
  136. * If the target address is a contract, it must implement `onERC721Received`,
  137. * which is called upon a safe transfer, and return the magic value
  138. * `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise,
  139. * the transfer is reverted.
  140. * Requires the msg sender to be the owner, approved, or operator
  141. * @param from current owner of the token
  142. * @param to address to receive the ownership of the given token ID
  143. * @param tokenId uint256 ID of the token to be transferred
  144. * @param _data bytes data to send along with a safe transfer check
  145. */
  146. function safeTransferFrom(address from, address to, uint256 tokenId, bytes _data) public {
  147. transferFrom(from, to, tokenId);
  148. // solium-disable-next-line arg-overflow
  149. require(_checkOnERC721Received(from, to, tokenId, _data));
  150. }
  151. /**
  152. * @dev Returns whether the specified token exists
  153. * @param tokenId uint256 ID of the token to query the existence of
  154. * @return whether the token exists
  155. */
  156. function _exists(uint256 tokenId) internal view returns (bool) {
  157. address owner = _tokenOwner[tokenId];
  158. return owner != address(0);
  159. }
  160. /**
  161. * @dev Returns whether the given spender can transfer a given token ID
  162. * @param spender address of the spender to query
  163. * @param tokenId uint256 ID of the token to be transferred
  164. * @return bool whether the msg.sender is approved for the given token ID,
  165. * is an operator of the owner, or is the owner of the token
  166. */
  167. function _isApprovedOrOwner(address spender, uint256 tokenId) internal view returns (bool) {
  168. address owner = ownerOf(tokenId);
  169. // Disable solium check because of
  170. // https://github.com/duaraghav8/Solium/issues/175
  171. // solium-disable-next-line operator-whitespace
  172. return (spender == owner || getApproved(tokenId) == spender || isApprovedForAll(owner, spender));
  173. }
  174. /**
  175. * @dev Internal function to mint a new token
  176. * Reverts if the given token ID already exists
  177. * @param to The address that will own the minted token
  178. * @param tokenId uint256 ID of the token to be minted
  179. */
  180. function _mint(address to, uint256 tokenId) internal {
  181. require(to != address(0));
  182. _addTokenTo(to, tokenId);
  183. emit Transfer(address(0), to, tokenId);
  184. }
  185. /**
  186. * @dev Internal function to burn a specific token
  187. * Reverts if the token does not exist
  188. * Deprecated, use _burn(uint256) instead.
  189. * @param owner owner of the token to burn
  190. * @param tokenId uint256 ID of the token being burned
  191. */
  192. function _burn(address owner, uint256 tokenId) internal {
  193. _clearApproval(tokenId);
  194. _removeTokenFrom(owner, tokenId);
  195. emit Transfer(owner, address(0), tokenId);
  196. }
  197. /**
  198. * @dev Internal function to burn a specific token
  199. * Reverts if the token does not exist
  200. * @param tokenId uint256 ID of the token being burned
  201. */
  202. function _burn(uint256 tokenId) internal {
  203. _burn(ownerOf(tokenId), tokenId);
  204. }
  205. /**
  206. * @dev Internal function to add a token ID to the list of a given address
  207. * Note that this function is left internal to make ERC721Enumerable possible, but is not
  208. * intended to be called by custom derived contracts: in particular, it emits no Transfer event.
  209. * @param to address representing the new owner of the given token ID
  210. * @param tokenId uint256 ID of the token to be added to the tokens list of the given address
  211. */
  212. function _addTokenTo(address to, uint256 tokenId) internal {
  213. require(_tokenOwner[tokenId] == address(0));
  214. _tokenOwner[tokenId] = to;
  215. _ownedTokensCount[to] = _ownedTokensCount[to].add(1);
  216. }
  217. /**
  218. * @dev Internal function to remove a token ID from the list of a given address
  219. * Note that this function is left internal to make ERC721Enumerable possible, but is not
  220. * intended to be called by custom derived contracts: in particular, it emits no Transfer event,
  221. * and doesn't clear approvals.
  222. * @param from address representing the previous owner of the given token ID
  223. * @param tokenId uint256 ID of the token to be removed from the tokens list of the given address
  224. */
  225. function _removeTokenFrom(address from, uint256 tokenId) internal {
  226. require(ownerOf(tokenId) == from);
  227. _ownedTokensCount[from] = _ownedTokensCount[from].sub(1);
  228. _tokenOwner[tokenId] = address(0);
  229. }
  230. /**
  231. * @dev Internal function to transfer ownership of a given token ID to another address.
  232. * As opposed to transferFrom, this imposes no restrictions on msg.sender.
  233. * @param from current owner of the token
  234. * @param to address to receive the ownership of the given token ID
  235. * @param tokenId uint256 ID of the token to be transferred
  236. */
  237. function _transferFrom(address from, address to, uint256 tokenId) internal {
  238. require(ownerOf(tokenId) == from);
  239. require(to != address(0));
  240. _clearApproval(tokenId);
  241. _ownedTokensCount[from] = _ownedTokensCount[from].sub(1);
  242. _ownedTokensCount[to] = _ownedTokensCount[to].add(1);
  243. _tokenOwner[tokenId] = to;
  244. emit Transfer(from, to, tokenId);
  245. }
  246. /**
  247. * @dev Internal function to invoke `onERC721Received` on a target address
  248. * The call is not executed if the target address is not a contract
  249. * @param from address representing the previous owner of the given token ID
  250. * @param to target address that will receive the tokens
  251. * @param tokenId uint256 ID of the token to be transferred
  252. * @param _data bytes optional data to send along with the call
  253. * @return whether the call correctly returned the expected magic value
  254. */
  255. function _checkOnERC721Received(address from, address to, uint256 tokenId, bytes _data) internal returns (bool) {
  256. if (!to.isContract()) {
  257. return true;
  258. }
  259. bytes4 retval = IERC721Receiver(to).onERC721Received(msg.sender, from, tokenId, _data);
  260. return (retval == _ERC721_RECEIVED);
  261. }
  262. /**
  263. * @dev Private function to clear current approval of a given token ID
  264. * @param tokenId uint256 ID of the token to be transferred
  265. */
  266. function _clearApproval(uint256 tokenId) private {
  267. if (_tokenApprovals[tokenId] != address(0)) {
  268. _tokenApprovals[tokenId] = address(0);
  269. }
  270. }
  271. }