ERC721.sol 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. pragma solidity ^0.5.0;
  2. import "../../GSN/Context.sol";
  3. import "./IERC721.sol";
  4. import "./IERC721Receiver.sol";
  5. import "../../math/SafeMath.sol";
  6. import "../../utils/Address.sol";
  7. import "../../drafts/Counters.sol";
  8. import "../../introspection/ERC165.sol";
  9. /**
  10. * @title ERC721 Non-Fungible Token Standard basic implementation
  11. * @dev see https://eips.ethereum.org/EIPS/eip-721
  12. */
  13. contract ERC721 is Context, ERC165, IERC721 {
  14. using SafeMath for uint256;
  15. using Address for address;
  16. using Counters for Counters.Counter;
  17. // Equals to `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`
  18. // which can be also obtained as `IERC721Receiver(0).onERC721Received.selector`
  19. bytes4 private constant _ERC721_RECEIVED = 0x150b7a02;
  20. // Mapping from token ID to owner
  21. mapping (uint256 => address) private _tokenOwner;
  22. // Mapping from token ID to approved address
  23. mapping (uint256 => address) private _tokenApprovals;
  24. // Mapping from owner to number of owned token
  25. mapping (address => Counters.Counter) private _ownedTokensCount;
  26. // Mapping from owner to operator approvals
  27. mapping (address => mapping (address => bool)) private _operatorApprovals;
  28. /*
  29. * bytes4(keccak256('balanceOf(address)')) == 0x70a08231
  30. * bytes4(keccak256('ownerOf(uint256)')) == 0x6352211e
  31. * bytes4(keccak256('approve(address,uint256)')) == 0x095ea7b3
  32. * bytes4(keccak256('getApproved(uint256)')) == 0x081812fc
  33. * bytes4(keccak256('setApprovalForAll(address,bool)')) == 0xa22cb465
  34. * bytes4(keccak256('isApprovedForAll(address,address)')) == 0xe985e9c5
  35. * bytes4(keccak256('transferFrom(address,address,uint256)')) == 0x23b872dd
  36. * bytes4(keccak256('safeTransferFrom(address,address,uint256)')) == 0x42842e0e
  37. * bytes4(keccak256('safeTransferFrom(address,address,uint256,bytes)')) == 0xb88d4fde
  38. *
  39. * => 0x70a08231 ^ 0x6352211e ^ 0x095ea7b3 ^ 0x081812fc ^
  40. * 0xa22cb465 ^ 0xe985e9c ^ 0x23b872dd ^ 0x42842e0e ^ 0xb88d4fde == 0x80ac58cd
  41. */
  42. bytes4 private constant _INTERFACE_ID_ERC721 = 0x80ac58cd;
  43. constructor () public {
  44. // register the supported interfaces to conform to ERC721 via ERC165
  45. _registerInterface(_INTERFACE_ID_ERC721);
  46. }
  47. /**
  48. * @dev Gets the balance of the specified address.
  49. * @param owner address to query the balance of
  50. * @return uint256 representing the amount owned by the passed address
  51. */
  52. function balanceOf(address owner) public view returns (uint256) {
  53. require(owner != address(0), "ERC721: balance query for the zero address");
  54. return _ownedTokensCount[owner].current();
  55. }
  56. /**
  57. * @dev Gets the owner of the specified token ID.
  58. * @param tokenId uint256 ID of the token to query the owner of
  59. * @return address currently marked as the owner of the given token ID
  60. */
  61. function ownerOf(uint256 tokenId) public view returns (address) {
  62. address owner = _tokenOwner[tokenId];
  63. require(owner != address(0), "ERC721: owner query for nonexistent token");
  64. return owner;
  65. }
  66. /**
  67. * @dev Approves another address to transfer the given token ID
  68. * The zero address indicates there is no approved address.
  69. * There can only be one approved address per token at a given time.
  70. * Can only be called by the token owner or an approved operator.
  71. * @param to address to be approved for the given token ID
  72. * @param tokenId uint256 ID of the token to be approved
  73. */
  74. function approve(address to, uint256 tokenId) public {
  75. address owner = ownerOf(tokenId);
  76. require(to != owner, "ERC721: approval to current owner");
  77. require(_msgSender() == owner || isApprovedForAll(owner, _msgSender()),
  78. "ERC721: approve caller is not owner nor approved for all"
  79. );
  80. _tokenApprovals[tokenId] = to;
  81. emit Approval(owner, to, tokenId);
  82. }
  83. /**
  84. * @dev Gets the approved address for a token ID, or zero if no address set
  85. * Reverts if the token ID does not exist.
  86. * @param tokenId uint256 ID of the token to query the approval of
  87. * @return address currently approved for the given token ID
  88. */
  89. function getApproved(uint256 tokenId) public view returns (address) {
  90. require(_exists(tokenId), "ERC721: approved query for nonexistent token");
  91. return _tokenApprovals[tokenId];
  92. }
  93. /**
  94. * @dev Sets or unsets the approval of a given operator
  95. * 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 != _msgSender(), "ERC721: approve to caller");
  101. _operatorApprovals[_msgSender()][to] = approved;
  102. emit ApprovalForAll(_msgSender(), 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. * Usage of this method is discouraged, use {safeTransferFrom} whenever possible.
  116. * 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 {
  122. //solhint-disable-next-line max-line-length
  123. require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved");
  124. _transferFrom(from, to, tokenId);
  125. }
  126. /**
  127. * @dev Safely transfers the ownership of a given token ID to another address
  128. * If the target address is a contract, it must implement {IERC721Receiver-onERC721Received},
  129. * which is called upon a safe transfer, and return the magic value
  130. * `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise,
  131. * the transfer is reverted.
  132. * Requires the msg.sender to be the owner, approved, or operator
  133. * @param from current owner of the token
  134. * @param to address to receive the ownership of the given token ID
  135. * @param tokenId uint256 ID of the token to be transferred
  136. */
  137. function safeTransferFrom(address from, address to, uint256 tokenId) public {
  138. safeTransferFrom(from, to, tokenId, "");
  139. }
  140. /**
  141. * @dev Safely transfers the ownership of a given token ID to another address
  142. * If the target address is a contract, it must implement {IERC721Receiver-onERC721Received},
  143. * which is called upon a safe transfer, and return the magic value
  144. * `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise,
  145. * the transfer is reverted.
  146. * Requires the _msgSender() to be the owner, approved, or operator
  147. * @param from current owner of the token
  148. * @param to address to receive the ownership of the given token ID
  149. * @param tokenId uint256 ID of the token to be transferred
  150. * @param _data bytes data to send along with a safe transfer check
  151. */
  152. function safeTransferFrom(address from, address to, uint256 tokenId, bytes memory _data) public {
  153. require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved");
  154. _safeTransferFrom(from, to, tokenId, _data);
  155. }
  156. /**
  157. * @dev Safely transfers the ownership of a given token ID to another address
  158. * If the target address is a contract, it must implement `onERC721Received`,
  159. * which is called upon a safe transfer, and return the magic value
  160. * `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise,
  161. * the transfer is reverted.
  162. * Requires the msg.sender to be the owner, approved, or operator
  163. * @param from current owner of the token
  164. * @param to address to receive the ownership of the given token ID
  165. * @param tokenId uint256 ID of the token to be transferred
  166. * @param _data bytes data to send along with a safe transfer check
  167. */
  168. function _safeTransferFrom(address from, address to, uint256 tokenId, bytes memory _data) internal {
  169. _transferFrom(from, to, tokenId);
  170. require(_checkOnERC721Received(from, to, tokenId, _data), "ERC721: transfer to non ERC721Receiver implementer");
  171. }
  172. /**
  173. * @dev Returns whether the specified token exists.
  174. * @param tokenId uint256 ID of the token to query the existence of
  175. * @return bool whether the token exists
  176. */
  177. function _exists(uint256 tokenId) internal view returns (bool) {
  178. address owner = _tokenOwner[tokenId];
  179. return owner != address(0);
  180. }
  181. /**
  182. * @dev Returns whether the given spender can transfer a given token ID.
  183. * @param spender address of the spender to query
  184. * @param tokenId uint256 ID of the token to be transferred
  185. * @return bool whether the msg.sender is approved for the given token ID,
  186. * is an operator of the owner, or is the owner of the token
  187. */
  188. function _isApprovedOrOwner(address spender, uint256 tokenId) internal view returns (bool) {
  189. require(_exists(tokenId), "ERC721: operator query for nonexistent token");
  190. address owner = ownerOf(tokenId);
  191. return (spender == owner || getApproved(tokenId) == spender || isApprovedForAll(owner, spender));
  192. }
  193. /**
  194. * @dev Internal function to safely mint a new token.
  195. * Reverts if the given token ID already exists.
  196. * If the target address is a contract, it must implement `onERC721Received`,
  197. * which is called upon a safe transfer, and return the magic value
  198. * `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise,
  199. * the transfer is reverted.
  200. * @param to The address that will own the minted token
  201. * @param tokenId uint256 ID of the token to be minted
  202. */
  203. function _safeMint(address to, uint256 tokenId) internal {
  204. _safeMint(to, tokenId, "");
  205. }
  206. /**
  207. * @dev Internal function to safely mint a new token.
  208. * Reverts if the given token ID already exists.
  209. * If the target address is a contract, it must implement `onERC721Received`,
  210. * which is called upon a safe transfer, and return the magic value
  211. * `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise,
  212. * the transfer is reverted.
  213. * @param to The address that will own the minted token
  214. * @param tokenId uint256 ID of the token to be minted
  215. * @param _data bytes data to send along with a safe transfer check
  216. */
  217. function _safeMint(address to, uint256 tokenId, bytes memory _data) internal {
  218. _mint(to, tokenId);
  219. require(_checkOnERC721Received(address(0), to, tokenId, _data), "ERC721: transfer to non ERC721Receiver implementer");
  220. }
  221. /**
  222. * @dev Internal function to mint a new token.
  223. * Reverts if the given token ID already exists.
  224. * @param to The address that will own the minted token
  225. * @param tokenId uint256 ID of the token to be minted
  226. */
  227. function _mint(address to, uint256 tokenId) internal {
  228. require(to != address(0), "ERC721: mint to the zero address");
  229. require(!_exists(tokenId), "ERC721: token already minted");
  230. _tokenOwner[tokenId] = to;
  231. _ownedTokensCount[to].increment();
  232. emit Transfer(address(0), to, tokenId);
  233. }
  234. /**
  235. * @dev Internal function to burn a specific token.
  236. * Reverts if the token does not exist.
  237. * Deprecated, use {_burn} instead.
  238. * @param owner owner of the token to burn
  239. * @param tokenId uint256 ID of the token being burned
  240. */
  241. function _burn(address owner, uint256 tokenId) internal {
  242. require(ownerOf(tokenId) == owner, "ERC721: burn of token that is not own");
  243. _clearApproval(tokenId);
  244. _ownedTokensCount[owner].decrement();
  245. _tokenOwner[tokenId] = address(0);
  246. emit Transfer(owner, address(0), tokenId);
  247. }
  248. /**
  249. * @dev Internal function to burn a specific token.
  250. * Reverts if the token does not exist.
  251. * @param tokenId uint256 ID of the token being burned
  252. */
  253. function _burn(uint256 tokenId) internal {
  254. _burn(ownerOf(tokenId), tokenId);
  255. }
  256. /**
  257. * @dev Internal function to transfer ownership of a given token ID to another address.
  258. * As opposed to {transferFrom}, this imposes no restrictions on msg.sender.
  259. * @param from current owner of the token
  260. * @param to address to receive the ownership of the given token ID
  261. * @param tokenId uint256 ID of the token to be transferred
  262. */
  263. function _transferFrom(address from, address to, uint256 tokenId) internal {
  264. require(ownerOf(tokenId) == from, "ERC721: transfer of token that is not own");
  265. require(to != address(0), "ERC721: transfer to the zero address");
  266. _clearApproval(tokenId);
  267. _ownedTokensCount[from].decrement();
  268. _ownedTokensCount[to].increment();
  269. _tokenOwner[tokenId] = to;
  270. emit Transfer(from, to, tokenId);
  271. }
  272. /**
  273. * @dev Internal function to invoke {IERC721Receiver-onERC721Received} on a target address.
  274. * The call is not executed if the target address is not a contract.
  275. *
  276. * This is an internal detail of the `ERC721` contract and its use is deprecated.
  277. * @param from address representing the previous owner of the given token ID
  278. * @param to target address that will receive the tokens
  279. * @param tokenId uint256 ID of the token to be transferred
  280. * @param _data bytes optional data to send along with the call
  281. * @return bool whether the call correctly returned the expected magic value
  282. */
  283. function _checkOnERC721Received(address from, address to, uint256 tokenId, bytes memory _data)
  284. internal returns (bool)
  285. {
  286. if (!to.isContract()) {
  287. return true;
  288. }
  289. // solhint-disable-next-line avoid-low-level-calls
  290. (bool success, bytes memory returndata) = to.call(abi.encodeWithSelector(
  291. IERC721Receiver(to).onERC721Received.selector,
  292. _msgSender(),
  293. from,
  294. tokenId,
  295. _data
  296. ));
  297. if (!success) {
  298. if (returndata.length > 0) {
  299. // solhint-disable-next-line no-inline-assembly
  300. assembly {
  301. let returndata_size := mload(returndata)
  302. revert(add(32, returndata), returndata_size)
  303. }
  304. } else {
  305. revert("ERC721: transfer to non ERC721Receiver implementer");
  306. }
  307. } else {
  308. bytes4 retval = abi.decode(returndata, (bytes4));
  309. return (retval == _ERC721_RECEIVED);
  310. }
  311. }
  312. /**
  313. * @dev Private function to clear current approval of a given token ID.
  314. * @param tokenId uint256 ID of the token to be transferred
  315. */
  316. function _clearApproval(uint256 tokenId) private {
  317. if (_tokenApprovals[tokenId] != address(0)) {
  318. _tokenApprovals[tokenId] = address(0);
  319. }
  320. }
  321. }