ERC721.sol 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. pragma solidity ^0.5.2;
  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://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md
  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. bytes4 private constant _INTERFACE_ID_ERC721 = 0x80ac58cd;
  28. /*
  29. * 0x80ac58cd ===
  30. * bytes4(keccak256('balanceOf(address)')) ^
  31. * bytes4(keccak256('ownerOf(uint256)')) ^
  32. * bytes4(keccak256('approve(address,uint256)')) ^
  33. * bytes4(keccak256('getApproved(uint256)')) ^
  34. * bytes4(keccak256('setApprovalForAll(address,bool)')) ^
  35. * bytes4(keccak256('isApprovedForAll(address,address)')) ^
  36. * bytes4(keccak256('transferFrom(address,address,uint256)')) ^
  37. * bytes4(keccak256('safeTransferFrom(address,address,uint256)')) ^
  38. * bytes4(keccak256('safeTransferFrom(address,address,uint256,bytes)'))
  39. */
  40. constructor () public {
  41. // register the supported interfaces to conform to ERC721 via ERC165
  42. _registerInterface(_INTERFACE_ID_ERC721);
  43. }
  44. /**
  45. * @dev Gets the balance of the specified address
  46. * @param owner address to query the balance of
  47. * @return uint256 representing the amount owned by the passed address
  48. */
  49. function balanceOf(address owner) public view returns (uint256) {
  50. require(owner != address(0));
  51. return _ownedTokensCount[owner].current();
  52. }
  53. /**
  54. * @dev Gets the owner of the specified token ID
  55. * @param tokenId uint256 ID of the token to query the owner of
  56. * @return address currently marked as the owner of the given token ID
  57. */
  58. function ownerOf(uint256 tokenId) public view returns (address) {
  59. address owner = _tokenOwner[tokenId];
  60. require(owner != address(0));
  61. return owner;
  62. }
  63. /**
  64. * @dev Approves another address to transfer the given token ID
  65. * The zero address indicates there is no approved address.
  66. * There can only be one approved address per token at a given time.
  67. * Can only be called by the token owner or an approved operator.
  68. * @param to address to be approved for the given token ID
  69. * @param tokenId uint256 ID of the token to be approved
  70. */
  71. function approve(address to, uint256 tokenId) public {
  72. address owner = ownerOf(tokenId);
  73. require(to != owner);
  74. require(msg.sender == owner || isApprovedForAll(owner, msg.sender));
  75. _tokenApprovals[tokenId] = to;
  76. emit Approval(owner, to, tokenId);
  77. }
  78. /**
  79. * @dev Gets the approved address for a token ID, or zero if no address set
  80. * Reverts if the token ID does not exist.
  81. * @param tokenId uint256 ID of the token to query the approval of
  82. * @return address currently approved for the given token ID
  83. */
  84. function getApproved(uint256 tokenId) public view returns (address) {
  85. require(_exists(tokenId));
  86. return _tokenApprovals[tokenId];
  87. }
  88. /**
  89. * @dev Sets or unsets the approval of a given operator
  90. * An operator is allowed to transfer all tokens of the sender on their behalf
  91. * @param to operator address to set the approval
  92. * @param approved representing the status of the approval to be set
  93. */
  94. function setApprovalForAll(address to, bool approved) public {
  95. require(to != msg.sender);
  96. _operatorApprovals[msg.sender][to] = approved;
  97. emit ApprovalForAll(msg.sender, to, approved);
  98. }
  99. /**
  100. * @dev Tells whether an operator is approved by a given owner
  101. * @param owner owner address which you want to query the approval of
  102. * @param operator operator address which you want to query the approval of
  103. * @return bool whether the given operator is approved by the given owner
  104. */
  105. function isApprovedForAll(address owner, address operator) public view returns (bool) {
  106. return _operatorApprovals[owner][operator];
  107. }
  108. /**
  109. * @dev Transfers the ownership of a given token ID to another address
  110. * Usage of this method is discouraged, use `safeTransferFrom` whenever possible
  111. * Requires the msg.sender to be the owner, approved, or operator
  112. * @param from current owner of the token
  113. * @param to address to receive the ownership of the given token ID
  114. * @param tokenId uint256 ID of the token to be transferred
  115. */
  116. function transferFrom(address from, address to, uint256 tokenId) public {
  117. require(_isApprovedOrOwner(msg.sender, tokenId));
  118. _transferFrom(from, to, tokenId);
  119. }
  120. /**
  121. * @dev Safely transfers the ownership of a given token ID to another address
  122. * If the target address is a contract, it must implement `onERC721Received`,
  123. * which is called upon a safe transfer, and return the magic value
  124. * `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise,
  125. * the transfer is reverted.
  126. * Requires the msg.sender to be the owner, approved, or operator
  127. * @param from current owner of the token
  128. * @param to address to receive the ownership of the given token ID
  129. * @param tokenId uint256 ID of the token to be transferred
  130. */
  131. function safeTransferFrom(address from, address to, uint256 tokenId) public {
  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 memory _data) public {
  147. transferFrom(from, to, tokenId);
  148. require(_checkOnERC721Received(from, to, tokenId, _data));
  149. }
  150. /**
  151. * @dev Returns whether the specified token exists
  152. * @param tokenId uint256 ID of the token to query the existence of
  153. * @return bool whether the token exists
  154. */
  155. function _exists(uint256 tokenId) internal view returns (bool) {
  156. address owner = _tokenOwner[tokenId];
  157. return owner != address(0);
  158. }
  159. /**
  160. * @dev Returns whether the given spender can transfer a given token ID
  161. * @param spender address of the spender to query
  162. * @param tokenId uint256 ID of the token to be transferred
  163. * @return bool whether the msg.sender is approved for the given token ID,
  164. * is an operator of the owner, or is the owner of the token
  165. */
  166. function _isApprovedOrOwner(address spender, uint256 tokenId) internal view returns (bool) {
  167. address owner = ownerOf(tokenId);
  168. return (spender == owner || getApproved(tokenId) == spender || isApprovedForAll(owner, spender));
  169. }
  170. /**
  171. * @dev Internal function to mint a new token
  172. * Reverts if the given token ID already exists
  173. * @param to The address that will own the minted token
  174. * @param tokenId uint256 ID of the token to be minted
  175. */
  176. function _mint(address to, uint256 tokenId) internal {
  177. require(to != address(0));
  178. require(!_exists(tokenId));
  179. _tokenOwner[tokenId] = to;
  180. _ownedTokensCount[to].increment();
  181. emit Transfer(address(0), to, tokenId);
  182. }
  183. /**
  184. * @dev Internal function to burn a specific token
  185. * Reverts if the token does not exist
  186. * Deprecated, use _burn(uint256) instead.
  187. * @param owner owner of the token to burn
  188. * @param tokenId uint256 ID of the token being burned
  189. */
  190. function _burn(address owner, uint256 tokenId) internal {
  191. require(ownerOf(tokenId) == owner);
  192. _clearApproval(tokenId);
  193. _ownedTokensCount[owner].decrement();
  194. _tokenOwner[tokenId] = address(0);
  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 transfer ownership of a given token ID to another address.
  207. * As opposed to transferFrom, this imposes no restrictions on msg.sender.
  208. * @param from current owner of the token
  209. * @param to address to receive the ownership of the given token ID
  210. * @param tokenId uint256 ID of the token to be transferred
  211. */
  212. function _transferFrom(address from, address to, uint256 tokenId) internal {
  213. require(ownerOf(tokenId) == from);
  214. require(to != address(0));
  215. _clearApproval(tokenId);
  216. _ownedTokensCount[from].decrement();
  217. _ownedTokensCount[to].increment();
  218. _tokenOwner[tokenId] = to;
  219. emit Transfer(from, to, tokenId);
  220. }
  221. /**
  222. * @dev Internal function to invoke `onERC721Received` on a target address
  223. * The call is not executed if the target address is not a contract
  224. * @param from address representing the previous owner of the given token ID
  225. * @param to target address that will receive the tokens
  226. * @param tokenId uint256 ID of the token to be transferred
  227. * @param _data bytes optional data to send along with the call
  228. * @return bool whether the call correctly returned the expected magic value
  229. */
  230. function _checkOnERC721Received(address from, address to, uint256 tokenId, bytes memory _data)
  231. internal returns (bool)
  232. {
  233. if (!to.isContract()) {
  234. return true;
  235. }
  236. bytes4 retval = IERC721Receiver(to).onERC721Received(msg.sender, from, tokenId, _data);
  237. return (retval == _ERC721_RECEIVED);
  238. }
  239. /**
  240. * @dev Private function to clear current approval of a given token ID
  241. * @param tokenId uint256 ID of the token to be transferred
  242. */
  243. function _clearApproval(uint256 tokenId) private {
  244. if (_tokenApprovals[tokenId] != address(0)) {
  245. _tokenApprovals[tokenId] = address(0);
  246. }
  247. }
  248. }