ERC721.sol 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. require(to != address(0));
  117. _clearApproval(from, tokenId);
  118. _removeTokenFrom(from, tokenId);
  119. _addTokenTo(to, tokenId);
  120. emit Transfer(from, to, tokenId);
  121. }
  122. /**
  123. * @dev Safely transfers the ownership of a given token ID to another address
  124. * If the target address is a contract, it must implement `onERC721Received`,
  125. * which is called upon a safe transfer, and return the magic value
  126. * `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise,
  127. * the transfer is reverted.
  128. *
  129. * Requires the msg sender to be the owner, approved, or operator
  130. * @param from current owner of the token
  131. * @param to address to receive the ownership of the given token ID
  132. * @param tokenId uint256 ID of the token to be transferred
  133. */
  134. function safeTransferFrom(address from, address to, uint256 tokenId) public {
  135. // solium-disable-next-line arg-overflow
  136. safeTransferFrom(from, to, tokenId, "");
  137. }
  138. /**
  139. * @dev Safely transfers the ownership of a given token ID to another address
  140. * If the target address is a contract, it must implement `onERC721Received`,
  141. * which is called upon a safe transfer, and return the magic value
  142. * `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise,
  143. * the transfer is reverted.
  144. * Requires the msg sender to be the owner, approved, or operator
  145. * @param from current owner of the token
  146. * @param to address to receive the ownership of the given token ID
  147. * @param tokenId uint256 ID of the token to be transferred
  148. * @param _data bytes data to send along with a safe transfer check
  149. */
  150. function safeTransferFrom(address from, address to, uint256 tokenId, bytes _data) public {
  151. transferFrom(from, to, tokenId);
  152. // solium-disable-next-line arg-overflow
  153. require(_checkOnERC721Received(from, to, tokenId, _data));
  154. }
  155. /**
  156. * @dev Returns whether the specified token exists
  157. * @param tokenId uint256 ID of the token to query the existence of
  158. * @return whether the token exists
  159. */
  160. function _exists(uint256 tokenId) internal view returns (bool) {
  161. address owner = _tokenOwner[tokenId];
  162. return owner != address(0);
  163. }
  164. /**
  165. * @dev Returns whether the given spender can transfer a given token ID
  166. * @param spender address of the spender to query
  167. * @param tokenId uint256 ID of the token to be transferred
  168. * @return bool whether the msg.sender is approved for the given token ID,
  169. * is an operator of the owner, or is the owner of the token
  170. */
  171. function _isApprovedOrOwner(address spender, uint256 tokenId) internal view returns (bool) {
  172. address owner = ownerOf(tokenId);
  173. // Disable solium check because of
  174. // https://github.com/duaraghav8/Solium/issues/175
  175. // solium-disable-next-line operator-whitespace
  176. return (spender == owner || getApproved(tokenId) == spender || isApprovedForAll(owner, spender));
  177. }
  178. /**
  179. * @dev Internal function to mint a new token
  180. * Reverts if the given token ID already exists
  181. * @param to The address that will own the minted token
  182. * @param tokenId uint256 ID of the token to be minted by the msg.sender
  183. */
  184. function _mint(address to, uint256 tokenId) internal {
  185. require(to != address(0));
  186. _addTokenTo(to, tokenId);
  187. emit Transfer(address(0), to, tokenId);
  188. }
  189. /**
  190. * @dev Internal function to burn a specific token
  191. * Reverts if the token does not exist
  192. * @param tokenId uint256 ID of the token being burned by the msg.sender
  193. */
  194. function _burn(address owner, uint256 tokenId) internal {
  195. _clearApproval(owner, tokenId);
  196. _removeTokenFrom(owner, tokenId);
  197. emit Transfer(owner, address(0), tokenId);
  198. }
  199. /**
  200. * @dev Internal function to add a token ID to the list of a given address
  201. * Note that this function is left internal to make ERC721Enumerable possible, but is not
  202. * intended to be called by custom derived contracts: in particular, it emits no Transfer event.
  203. * @param to address representing the new owner of the given token ID
  204. * @param tokenId uint256 ID of the token to be added to the tokens list of the given address
  205. */
  206. function _addTokenTo(address to, uint256 tokenId) internal {
  207. require(_tokenOwner[tokenId] == address(0));
  208. _tokenOwner[tokenId] = to;
  209. _ownedTokensCount[to] = _ownedTokensCount[to].add(1);
  210. }
  211. /**
  212. * @dev Internal function to remove a token ID from the list of a given address
  213. * Note that this function is left internal to make ERC721Enumerable possible, but is not
  214. * intended to be called by custom derived contracts: in particular, it emits no Transfer event,
  215. * and doesn't clear approvals.
  216. * @param from address representing the previous owner of the given token ID
  217. * @param tokenId uint256 ID of the token to be removed from the tokens list of the given address
  218. */
  219. function _removeTokenFrom(address from, uint256 tokenId) internal {
  220. require(ownerOf(tokenId) == from);
  221. _ownedTokensCount[from] = _ownedTokensCount[from].sub(1);
  222. _tokenOwner[tokenId] = address(0);
  223. }
  224. /**
  225. * @dev Internal function to invoke `onERC721Received` on a target address
  226. * The call is not executed if the target address is not a contract
  227. * @param from address representing the previous owner of the given token ID
  228. * @param to target address that will receive the tokens
  229. * @param tokenId uint256 ID of the token to be transferred
  230. * @param _data bytes optional data to send along with the call
  231. * @return whether the call correctly returned the expected magic value
  232. */
  233. function _checkOnERC721Received(address from, address to, uint256 tokenId, bytes _data) internal returns (bool) {
  234. if (!to.isContract()) {
  235. return true;
  236. }
  237. bytes4 retval = IERC721Receiver(to).onERC721Received(msg.sender, from, tokenId, _data);
  238. return (retval == _ERC721_RECEIVED);
  239. }
  240. /**
  241. * @dev Private function to clear current approval of a given token ID
  242. * Reverts if the given address is not indeed the owner of the token
  243. * @param owner owner of the token
  244. * @param tokenId uint256 ID of the token to be transferred
  245. */
  246. function _clearApproval(address owner, uint256 tokenId) private {
  247. require(ownerOf(tokenId) == owner);
  248. if (_tokenApprovals[tokenId] != address(0)) {
  249. _tokenApprovals[tokenId] = address(0);
  250. }
  251. }
  252. }