ERC721Upgradeable.sol 15 KB

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