ERC721.sol 13 KB

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