ERC721.sol 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. pragma solidity ^0.5.0;
  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 _INTERFACE_ID_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(_INTERFACE_ID_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. safeTransferFrom(from, to, tokenId, "");
  132. }
  133. /**
  134. * @dev Safely transfers the ownership of a given token ID to another address
  135. * If the target address is a contract, it must implement `onERC721Received`,
  136. * which is called upon a safe transfer, and return the magic value
  137. * `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise,
  138. * the transfer is reverted.
  139. * Requires the msg sender to be the owner, approved, or operator
  140. * @param from current owner of the token
  141. * @param to address to receive the ownership of the given token ID
  142. * @param tokenId uint256 ID of the token to be transferred
  143. * @param _data bytes data to send along with a safe transfer check
  144. */
  145. function safeTransferFrom(address from, address to, uint256 tokenId, bytes memory _data) public {
  146. transferFrom(from, to, tokenId);
  147. require(_checkOnERC721Received(from, to, tokenId, _data));
  148. }
  149. /**
  150. * @dev Returns whether the specified token exists
  151. * @param tokenId uint256 ID of the token to query the existence of
  152. * @return whether the token exists
  153. */
  154. function _exists(uint256 tokenId) internal view returns (bool) {
  155. address owner = _tokenOwner[tokenId];
  156. return owner != address(0);
  157. }
  158. /**
  159. * @dev Returns whether the given spender can transfer a given token ID
  160. * @param spender address of the spender to query
  161. * @param tokenId uint256 ID of the token to be transferred
  162. * @return bool whether the msg.sender is approved for the given token ID,
  163. * is an operator of the owner, or is the owner of the token
  164. */
  165. function _isApprovedOrOwner(address spender, uint256 tokenId) internal view returns (bool) {
  166. address owner = ownerOf(tokenId);
  167. return (spender == owner || getApproved(tokenId) == spender || isApprovedForAll(owner, spender));
  168. }
  169. /**
  170. * @dev Internal function to mint a new token
  171. * Reverts if the given token ID already exists
  172. * @param to The address that will own the minted token
  173. * @param tokenId uint256 ID of the token to be minted
  174. */
  175. function _mint(address to, uint256 tokenId) internal {
  176. require(to != address(0));
  177. require(!_exists(tokenId));
  178. _tokenOwner[tokenId] = to;
  179. _ownedTokensCount[to] = _ownedTokensCount[to].add(1);
  180. emit Transfer(address(0), to, tokenId);
  181. }
  182. /**
  183. * @dev Internal function to burn a specific token
  184. * Reverts if the token does not exist
  185. * Deprecated, use _burn(uint256) instead.
  186. * @param owner owner of the token to burn
  187. * @param tokenId uint256 ID of the token being burned
  188. */
  189. function _burn(address owner, uint256 tokenId) internal {
  190. require(ownerOf(tokenId) == owner);
  191. _clearApproval(tokenId);
  192. _ownedTokensCount[owner] = _ownedTokensCount[owner].sub(1);
  193. _tokenOwner[tokenId] = address(0);
  194. emit Transfer(owner, address(0), tokenId);
  195. }
  196. /**
  197. * @dev Internal function to burn a specific token
  198. * Reverts if the token does not exist
  199. * @param tokenId uint256 ID of the token being burned
  200. */
  201. function _burn(uint256 tokenId) internal {
  202. _burn(ownerOf(tokenId), tokenId);
  203. }
  204. /**
  205. * @dev Internal function to transfer ownership of a given token ID to another address.
  206. * As opposed to transferFrom, this imposes no restrictions on msg.sender.
  207. * @param from current owner of the token
  208. * @param to address to receive the ownership of the given token ID
  209. * @param tokenId uint256 ID of the token to be transferred
  210. */
  211. function _transferFrom(address from, address to, uint256 tokenId) internal {
  212. require(ownerOf(tokenId) == from);
  213. require(to != address(0));
  214. _clearApproval(tokenId);
  215. _ownedTokensCount[from] = _ownedTokensCount[from].sub(1);
  216. _ownedTokensCount[to] = _ownedTokensCount[to].add(1);
  217. _tokenOwner[tokenId] = to;
  218. emit Transfer(from, to, tokenId);
  219. }
  220. /**
  221. * @dev Internal function to invoke `onERC721Received` on a target address
  222. * The call is not executed if the target address is not a contract
  223. * @param from address representing the previous owner of the given token ID
  224. * @param to target address that will receive the tokens
  225. * @param tokenId uint256 ID of the token to be transferred
  226. * @param _data bytes optional data to send along with the call
  227. * @return whether the call correctly returned the expected magic value
  228. */
  229. function _checkOnERC721Received(address from, address to, uint256 tokenId, bytes memory _data)
  230. internal returns (bool)
  231. {
  232. if (!to.isContract()) {
  233. return true;
  234. }
  235. bytes4 retval = IERC721Receiver(to).onERC721Received(msg.sender, from, tokenId, _data);
  236. return (retval == _ERC721_RECEIVED);
  237. }
  238. /**
  239. * @dev Private function to clear current approval of a given token ID
  240. * @param tokenId uint256 ID of the token to be transferred
  241. */
  242. function _clearApproval(uint256 tokenId) private {
  243. if (_tokenApprovals[tokenId] != address(0)) {
  244. _tokenApprovals[tokenId] = address(0);
  245. }
  246. }
  247. }