ERC721.sol 13 KB

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