ERC721.sol 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "./IERC721.sol";
  4. import "./IERC721Receiver.sol";
  5. import "./extensions/IERC721Metadata.sol";
  6. import "./extensions/IERC721Enumerable.sol";
  7. import "../../utils/Address.sol";
  8. import "../../utils/Context.sol";
  9. import "../../utils/Strings.sol";
  10. import "../../utils/introspection/ERC165.sol";
  11. /**
  12. * @dev Implementation of https://eips.ethereum.org/EIPS/eip-721[ERC721] Non-Fungible Token Standard, including
  13. * the Metadata extension, but not including the Enumerable extension, which is available separately as
  14. * {ERC721Enumerable}.
  15. */
  16. contract ERC721 is Context, ERC165, IERC721, IERC721Metadata {
  17. using Address for address;
  18. using Strings for uint256;
  19. // Token name
  20. string private _name;
  21. // Token symbol
  22. string private _symbol;
  23. // Mapping from token ID to owner address
  24. mapping (uint256 => address) private _owners;
  25. // Mapping owner address to token count
  26. mapping (address => uint256) private _balances;
  27. // Mapping from token ID to approved address
  28. mapping (uint256 => address) private _tokenApprovals;
  29. // Mapping from owner to operator approvals
  30. mapping (address => mapping (address => bool)) private _operatorApprovals;
  31. /**
  32. * @dev Initializes the contract by setting a `name` and a `symbol` to the token collection.
  33. */
  34. constructor (string memory name_, string memory symbol_) {
  35. _name = name_;
  36. _symbol = symbol_;
  37. }
  38. /**
  39. * @dev See {IERC165-supportsInterface}.
  40. */
  41. function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165, IERC165) returns (bool) {
  42. return interfaceId == type(IERC721).interfaceId
  43. || interfaceId == type(IERC721Metadata).interfaceId
  44. || super.supportsInterface(interfaceId);
  45. }
  46. /**
  47. * @dev See {IERC721-balanceOf}.
  48. */
  49. function balanceOf(address owner) public view virtual override returns (uint256) {
  50. require(owner != address(0), "ERC721: balance query for the zero address");
  51. return _balances[owner];
  52. }
  53. /**
  54. * @dev See {IERC721-ownerOf}.
  55. */
  56. function ownerOf(uint256 tokenId) public view virtual override returns (address) {
  57. address owner = _owners[tokenId];
  58. require(owner != address(0), "ERC721: owner query for nonexistent token");
  59. return owner;
  60. }
  61. /**
  62. * @dev See {IERC721Metadata-name}.
  63. */
  64. function name() public view virtual override returns (string memory) {
  65. return _name;
  66. }
  67. /**
  68. * @dev See {IERC721Metadata-symbol}.
  69. */
  70. function symbol() public view virtual override returns (string memory) {
  71. return _symbol;
  72. }
  73. /**
  74. * @dev See {IERC721Metadata-tokenURI}.
  75. */
  76. function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
  77. require(_exists(tokenId), "ERC721Metadata: URI query for nonexistent token");
  78. string memory baseURI = _baseURI();
  79. return bytes(baseURI).length > 0
  80. ? string(abi.encodePacked(baseURI, tokenId.toString()))
  81. : '';
  82. }
  83. /**
  84. * @dev Base URI for computing {tokenURI}. Empty by default, can be overriden
  85. * in child contracts.
  86. */
  87. function _baseURI() internal view virtual returns (string memory) {
  88. return "";
  89. }
  90. /**
  91. * @dev See {IERC721-approve}.
  92. */
  93. function approve(address to, uint256 tokenId) public virtual override {
  94. address owner = ERC721.ownerOf(tokenId);
  95. require(to != owner, "ERC721: approval to current owner");
  96. require(_msgSender() == owner || ERC721.isApprovedForAll(owner, _msgSender()),
  97. "ERC721: approve caller is not owner nor approved for all"
  98. );
  99. _approve(to, tokenId);
  100. }
  101. /**
  102. * @dev See {IERC721-getApproved}.
  103. */
  104. function getApproved(uint256 tokenId) public view virtual override returns (address) {
  105. require(_exists(tokenId), "ERC721: approved query for nonexistent token");
  106. return _tokenApprovals[tokenId];
  107. }
  108. /**
  109. * @dev See {IERC721-setApprovalForAll}.
  110. */
  111. function setApprovalForAll(address operator, bool approved) public virtual override {
  112. require(operator != _msgSender(), "ERC721: approve to caller");
  113. _operatorApprovals[_msgSender()][operator] = approved;
  114. emit ApprovalForAll(_msgSender(), operator, approved);
  115. }
  116. /**
  117. * @dev See {IERC721-isApprovedForAll}.
  118. */
  119. function isApprovedForAll(address owner, address operator) public view virtual override returns (bool) {
  120. return _operatorApprovals[owner][operator];
  121. }
  122. /**
  123. * @dev See {IERC721-transferFrom}.
  124. */
  125. function transferFrom(address from, address to, uint256 tokenId) public virtual override {
  126. //solhint-disable-next-line max-line-length
  127. require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved");
  128. _transfer(from, to, tokenId);
  129. }
  130. /**
  131. * @dev See {IERC721-safeTransferFrom}.
  132. */
  133. function safeTransferFrom(address from, address to, uint256 tokenId) public virtual override {
  134. safeTransferFrom(from, to, tokenId, "");
  135. }
  136. /**
  137. * @dev See {IERC721-safeTransferFrom}.
  138. */
  139. function safeTransferFrom(address from, address to, uint256 tokenId, bytes memory _data) public virtual override {
  140. require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: transfer caller is not owner nor approved");
  141. _safeTransfer(from, to, tokenId, _data);
  142. }
  143. /**
  144. * @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
  145. * are aware of the ERC721 protocol to prevent tokens from being forever locked.
  146. *
  147. * `_data` is additional data, it has no specified format and it is sent in call to `to`.
  148. *
  149. * This internal function is equivalent to {safeTransferFrom}, and can be used to e.g.
  150. * implement alternative mechanisms to perform token transfer, such as signature-based.
  151. *
  152. * Requirements:
  153. *
  154. * - `from` cannot be the zero address.
  155. * - `to` cannot be the zero address.
  156. * - `tokenId` token must exist and be owned by `from`.
  157. * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
  158. *
  159. * Emits a {Transfer} event.
  160. */
  161. function _safeTransfer(address from, address to, uint256 tokenId, bytes memory _data) internal virtual {
  162. _transfer(from, to, tokenId);
  163. require(_checkOnERC721Received(from, to, tokenId, _data), "ERC721: transfer to non ERC721Receiver implementer");
  164. }
  165. /**
  166. * @dev Returns whether `tokenId` exists.
  167. *
  168. * Tokens can be managed by their owner or approved accounts via {approve} or {setApprovalForAll}.
  169. *
  170. * Tokens start existing when they are minted (`_mint`),
  171. * and stop existing when they are burned (`_burn`).
  172. */
  173. function _exists(uint256 tokenId) internal view virtual returns (bool) {
  174. return _owners[tokenId] != address(0);
  175. }
  176. /**
  177. * @dev Returns whether `spender` is allowed to manage `tokenId`.
  178. *
  179. * Requirements:
  180. *
  181. * - `tokenId` must exist.
  182. */
  183. function _isApprovedOrOwner(address spender, uint256 tokenId) internal view virtual returns (bool) {
  184. require(_exists(tokenId), "ERC721: operator query for nonexistent token");
  185. address owner = ERC721.ownerOf(tokenId);
  186. return (spender == owner || getApproved(tokenId) == spender || ERC721.isApprovedForAll(owner, spender));
  187. }
  188. /**
  189. * @dev Safely mints `tokenId` and transfers it to `to`.
  190. *
  191. * Requirements:
  192. d*
  193. * - `tokenId` must not exist.
  194. * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
  195. *
  196. * Emits a {Transfer} event.
  197. */
  198. function _safeMint(address to, uint256 tokenId) internal virtual {
  199. _safeMint(to, tokenId, "");
  200. }
  201. /**
  202. * @dev Same as {xref-ERC721-_safeMint-address-uint256-}[`_safeMint`], with an additional `data` parameter which is
  203. * forwarded in {IERC721Receiver-onERC721Received} to contract recipients.
  204. */
  205. function _safeMint(address to, uint256 tokenId, bytes memory _data) internal virtual {
  206. _mint(to, tokenId);
  207. require(_checkOnERC721Received(address(0), to, tokenId, _data), "ERC721: transfer to non ERC721Receiver implementer");
  208. }
  209. /**
  210. * @dev Mints `tokenId` and transfers it to `to`.
  211. *
  212. * WARNING: Usage of this method is discouraged, use {_safeMint} whenever possible
  213. *
  214. * Requirements:
  215. *
  216. * - `tokenId` must not exist.
  217. * - `to` cannot be the zero address.
  218. *
  219. * Emits a {Transfer} event.
  220. */
  221. function _mint(address to, uint256 tokenId) internal virtual {
  222. require(to != address(0), "ERC721: mint to the zero address");
  223. require(!_exists(tokenId), "ERC721: token already minted");
  224. _beforeTokenTransfer(address(0), to, tokenId);
  225. _balances[to] += 1;
  226. _owners[tokenId] = to;
  227. emit Transfer(address(0), to, tokenId);
  228. }
  229. /**
  230. * @dev Destroys `tokenId`.
  231. * The approval is cleared when the token is burned.
  232. *
  233. * Requirements:
  234. *
  235. * - `tokenId` must exist.
  236. *
  237. * Emits a {Transfer} event.
  238. */
  239. function _burn(uint256 tokenId) internal virtual {
  240. address owner = ERC721.ownerOf(tokenId);
  241. _beforeTokenTransfer(owner, address(0), tokenId);
  242. // Clear approvals
  243. _approve(address(0), tokenId);
  244. _balances[owner] -= 1;
  245. delete _owners[tokenId];
  246. emit Transfer(owner, address(0), tokenId);
  247. }
  248. /**
  249. * @dev Transfers `tokenId` from `from` to `to`.
  250. * As opposed to {transferFrom}, this imposes no restrictions on msg.sender.
  251. *
  252. * Requirements:
  253. *
  254. * - `to` cannot be the zero address.
  255. * - `tokenId` token must be owned by `from`.
  256. *
  257. * Emits a {Transfer} event.
  258. */
  259. function _transfer(address from, address to, uint256 tokenId) internal virtual {
  260. require(ERC721.ownerOf(tokenId) == from, "ERC721: transfer of token that is not own");
  261. require(to != address(0), "ERC721: transfer to the zero address");
  262. _beforeTokenTransfer(from, to, tokenId);
  263. // Clear approvals from the previous owner
  264. _approve(address(0), tokenId);
  265. _balances[from] -= 1;
  266. _balances[to] += 1;
  267. _owners[tokenId] = to;
  268. emit Transfer(from, to, tokenId);
  269. }
  270. /**
  271. * @dev Approve `to` to operate on `tokenId`
  272. *
  273. * Emits a {Approval} event.
  274. */
  275. function _approve(address to, uint256 tokenId) internal virtual {
  276. _tokenApprovals[tokenId] = to;
  277. emit Approval(ERC721.ownerOf(tokenId), to, tokenId);
  278. }
  279. /**
  280. * @dev Internal function to invoke {IERC721Receiver-onERC721Received} on a target address.
  281. * The call is not executed if the target address is not a contract.
  282. *
  283. * @param from address representing the previous owner of the given token ID
  284. * @param to target address that will receive the tokens
  285. * @param tokenId uint256 ID of the token to be transferred
  286. * @param _data bytes optional data to send along with the call
  287. * @return bool whether the call correctly returned the expected magic value
  288. */
  289. function _checkOnERC721Received(address from, address to, uint256 tokenId, bytes memory _data)
  290. private returns (bool)
  291. {
  292. if (to.isContract()) {
  293. try IERC721Receiver(to).onERC721Received(_msgSender(), from, tokenId, _data) returns (bytes4 retval) {
  294. return retval == IERC721Receiver(to).onERC721Received.selector;
  295. } catch (bytes memory reason) {
  296. if (reason.length == 0) {
  297. revert("ERC721: transfer to non ERC721Receiver implementer");
  298. } else {
  299. // solhint-disable-next-line no-inline-assembly
  300. assembly {
  301. revert(add(32, reason), mload(reason))
  302. }
  303. }
  304. }
  305. } else {
  306. return true;
  307. }
  308. }
  309. /**
  310. * @dev Hook that is called before any token transfer. This includes minting
  311. * and burning.
  312. *
  313. * Calling conditions:
  314. *
  315. * - When `from` and `to` are both non-zero, ``from``'s `tokenId` will be
  316. * transferred to `to`.
  317. * - When `from` is zero, `tokenId` will be minted for `to`.
  318. * - When `to` is zero, ``from``'s `tokenId` will be burned.
  319. * - `from` cannot be the zero address.
  320. * - `to` cannot be the zero address.
  321. *
  322. * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
  323. */
  324. function _beforeTokenTransfer(address from, address to, uint256 tokenId) internal virtual { }
  325. }