ERC721.sol 15 KB

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