token_ERC721_ERC721.sol.orig.patch 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. --- token/ERC721/ERC721.sol.orig 1969-12-31 18:00:00
  2. +++ token/ERC721/ERC721.sol.orig 2023-08-13 09:56:16
  3. @@ -0,0 +1,465 @@
  4. +// SPDX-License-Identifier: MIT
  5. +// OpenZeppelin Contracts (last updated v4.9.0) (token/ERC721/ERC721.sol)
  6. +
  7. +pragma solidity ^0.8.20;
  8. +
  9. +import {IERC721} from "./IERC721.sol";
  10. +import {IERC721Receiver} from "./IERC721Receiver.sol";
  11. +import {IERC721Metadata} from "./extensions/IERC721Metadata.sol";
  12. +import {Context} from "../../utils/Context.sol";
  13. +import {Strings} from "../../utils/Strings.sol";
  14. +import {IERC165, ERC165} from "../../utils/introspection/ERC165.sol";
  15. +import {IERC721Errors} from "../../interfaces/draft-IERC6093.sol";
  16. +
  17. +/**
  18. + * @dev Implementation of https://eips.ethereum.org/EIPS/eip-721[ERC721] Non-Fungible Token Standard, including
  19. + * the Metadata extension, but not including the Enumerable extension, which is available separately as
  20. + * {ERC721Enumerable}.
  21. + */
  22. +abstract contract ERC721 is Context, ERC165, IERC721, IERC721Metadata, IERC721Errors {
  23. + using Strings for uint256;
  24. +
  25. + // Token name
  26. + string private _name;
  27. +
  28. + // Token symbol
  29. + string private _symbol;
  30. +
  31. + mapping(uint256 tokenId => address) private _owners;
  32. +
  33. + mapping(address owner => uint256) private _balances;
  34. +
  35. + mapping(uint256 tokenId => address) private _tokenApprovals;
  36. +
  37. + mapping(address owner => mapping(address operator => bool)) private _operatorApprovals;
  38. +
  39. + /**
  40. + * @dev Initializes the contract by setting a `name` and a `symbol` to the token collection.
  41. + */
  42. + constructor(string memory name_, string memory symbol_) {
  43. + _name = name_;
  44. + _symbol = symbol_;
  45. + }
  46. +
  47. + /**
  48. + * @dev See {IERC165-supportsInterface}.
  49. + */
  50. + function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165, IERC165) returns (bool) {
  51. + return
  52. + interfaceId == type(IERC721).interfaceId ||
  53. + interfaceId == type(IERC721Metadata).interfaceId ||
  54. + super.supportsInterface(interfaceId);
  55. + }
  56. +
  57. + /**
  58. + * @dev See {IERC721-balanceOf}.
  59. + */
  60. + function balanceOf(address owner) public view virtual returns (uint256) {
  61. + if (owner == address(0)) {
  62. + revert ERC721InvalidOwner(address(0));
  63. + }
  64. + return _balances[owner];
  65. + }
  66. +
  67. + /**
  68. + * @dev See {IERC721-ownerOf}.
  69. + */
  70. + function ownerOf(uint256 tokenId) public view virtual returns (address) {
  71. + address owner = _ownerOf(tokenId);
  72. + if (owner == address(0)) {
  73. + revert ERC721NonexistentToken(tokenId);
  74. + }
  75. + return owner;
  76. + }
  77. +
  78. + /**
  79. + * @dev See {IERC721Metadata-name}.
  80. + */
  81. + function name() public view virtual returns (string memory) {
  82. + return _name;
  83. + }
  84. +
  85. + /**
  86. + * @dev See {IERC721Metadata-symbol}.
  87. + */
  88. + function symbol() public view virtual returns (string memory) {
  89. + return _symbol;
  90. + }
  91. +
  92. + /**
  93. + * @dev See {IERC721Metadata-tokenURI}.
  94. + */
  95. + function tokenURI(uint256 tokenId) public view virtual returns (string memory) {
  96. + _requireMinted(tokenId);
  97. +
  98. + string memory baseURI = _baseURI();
  99. + return bytes(baseURI).length > 0 ? string.concat(baseURI, tokenId.toString()) : "";
  100. + }
  101. +
  102. + /**
  103. + * @dev Base URI for computing {tokenURI}. If set, the resulting URI for each
  104. + * token will be the concatenation of the `baseURI` and the `tokenId`. Empty
  105. + * by default, can be overridden in child contracts.
  106. + */
  107. + function _baseURI() internal view virtual returns (string memory) {
  108. + return "";
  109. + }
  110. +
  111. + /**
  112. + * @dev See {IERC721-approve}.
  113. + */
  114. + function approve(address to, uint256 tokenId) public virtual {
  115. + _approve(to, tokenId, _msgSender());
  116. + }
  117. +
  118. + /**
  119. + * @dev See {IERC721-getApproved}.
  120. + */
  121. + function getApproved(uint256 tokenId) public view virtual returns (address) {
  122. + _requireMinted(tokenId);
  123. +
  124. + return _getApproved(tokenId);
  125. + }
  126. +
  127. + /**
  128. + * @dev See {IERC721-setApprovalForAll}.
  129. + */
  130. + function setApprovalForAll(address operator, bool approved) public virtual {
  131. + _setApprovalForAll(_msgSender(), operator, approved);
  132. + }
  133. +
  134. + /**
  135. + * @dev See {IERC721-isApprovedForAll}.
  136. + */
  137. + function isApprovedForAll(address owner, address operator) public view virtual returns (bool) {
  138. + return _operatorApprovals[owner][operator];
  139. + }
  140. +
  141. + /**
  142. + * @dev See {IERC721-transferFrom}.
  143. + */
  144. + function transferFrom(address from, address to, uint256 tokenId) public virtual {
  145. + if (to == address(0)) {
  146. + revert ERC721InvalidReceiver(address(0));
  147. + }
  148. + // Setting an "auth" arguments enables the `_isAuthorized` check which verifies that the token exists
  149. + // (from != 0). Therefore, it is not needed to verify that the return value is not 0 here.
  150. + address previousOwner = _update(to, tokenId, _msgSender());
  151. + if (previousOwner != from) {
  152. + revert ERC721IncorrectOwner(from, tokenId, previousOwner);
  153. + }
  154. + }
  155. +
  156. + /**
  157. + * @dev See {IERC721-safeTransferFrom}.
  158. + */
  159. + function safeTransferFrom(address from, address to, uint256 tokenId) public {
  160. + safeTransferFrom(from, to, tokenId, "");
  161. + }
  162. +
  163. + /**
  164. + * @dev See {IERC721-safeTransferFrom}.
  165. + */
  166. + function safeTransferFrom(address from, address to, uint256 tokenId, bytes memory data) public virtual {
  167. + transferFrom(from, to, tokenId);
  168. + _checkOnERC721Received(from, to, tokenId, data);
  169. + }
  170. +
  171. + /**
  172. + * @dev Returns the owner of the `tokenId`. Does NOT revert if token doesn't exist
  173. + *
  174. + * IMPORTANT: Any overrides to this function that add ownership of tokens not tracked by the
  175. + * core ERC721 logic MUST be matched with the use of {_increaseBalance} to keep balances
  176. + * consistent with ownership. The invariant to preserve is that for any address `a` the value returned by
  177. + * `balanceOf(a)` must be equal to the number of tokens such that `_ownerOf(tokenId)` is `a`.
  178. + */
  179. + function _ownerOf(uint256 tokenId) internal view virtual returns (address) {
  180. + return _owners[tokenId];
  181. + }
  182. +
  183. + /**
  184. + * @dev Returns the approved address for `tokenId`. Returns 0 if `tokenId` is not minted.
  185. + */
  186. + function _getApproved(uint256 tokenId) internal view virtual returns (address) {
  187. + return _tokenApprovals[tokenId];
  188. + }
  189. +
  190. + /**
  191. + * @dev Returns whether `spender` is allowed to manage `owner`'s tokens, or `tokenId` in
  192. + * particular (ignoring whether it is owned by `owner`).
  193. + *
  194. + * WARNING: This function assumes that `owner` is the actual owner of `tokenId` and does not
  195. + * verify this assumption.
  196. + */
  197. + function _isAuthorized(address owner, address spender, uint256 tokenId) internal view virtual returns (bool) {
  198. + return
  199. + spender != address(0) &&
  200. + (owner == spender || isApprovedForAll(owner, spender) || _getApproved(tokenId) == spender);
  201. + }
  202. +
  203. + /**
  204. + * @dev Checks if `spender` can operate on `tokenId`, assuming the provided `owner` is the actual owner.
  205. + * Reverts if `spender` has not approval for all assets of the provided `owner` nor the actual owner approved the `spender` for the specific `tokenId`.
  206. + *
  207. + * WARNING: This function relies on {_isAuthorized}, so it doesn't check whether `owner` is the
  208. + * actual owner of `tokenId`.
  209. + */
  210. + function _checkAuthorized(address owner, address spender, uint256 tokenId) internal view virtual {
  211. + if (!_isAuthorized(owner, spender, tokenId)) {
  212. + if (owner == address(0)) {
  213. + revert ERC721NonexistentToken(tokenId);
  214. + } else {
  215. + revert ERC721InsufficientApproval(spender, tokenId);
  216. + }
  217. + }
  218. + }
  219. +
  220. + /**
  221. + * @dev Unsafe write access to the balances, used by extensions that "mint" tokens using an {ownerOf} override.
  222. + *
  223. + * NOTE: the value is limited to type(uint128).max. This protect against _balance overflow. It is unrealistic that
  224. + * a uint256 would ever overflow from increments when these increments are bounded to uint128 values.
  225. + *
  226. + * WARNING: Increasing an account's balance using this function tends to be paired with an override of the
  227. + * {_ownerOf} function to resolve the ownership of the corresponding tokens so that balances and ownership
  228. + * remain consistent with one another.
  229. + */
  230. + function _increaseBalance(address account, uint128 value) internal virtual {
  231. + unchecked {
  232. + _balances[account] += value;
  233. + }
  234. + }
  235. +
  236. + /**
  237. + * @dev Transfers `tokenId` from its current owner to `to`, or alternatively mints (or burns) if the current owner
  238. + * (or `to`) is the zero address. Returns the owner of the `tokenId` before the update.
  239. + *
  240. + * The `auth` argument is optional. If the value passed is non 0, then this function will check that
  241. + * `auth` is either the owner of the token, or approved to operate on the token (by the owner).
  242. + *
  243. + * Emits a {Transfer} event.
  244. + *
  245. + * NOTE: If overriding this function in a way that tracks balances, see also {_increaseBalance}.
  246. + */
  247. + function _update(address to, uint256 tokenId, address auth) internal virtual returns (address) {
  248. + address from = _ownerOf(tokenId);
  249. +
  250. + // Perform (optional) operator check
  251. + if (auth != address(0)) {
  252. + _checkAuthorized(from, auth, tokenId);
  253. + }
  254. +
  255. + // Execute the update
  256. + if (from != address(0)) {
  257. + delete _tokenApprovals[tokenId];
  258. + unchecked {
  259. + _balances[from] -= 1;
  260. + }
  261. + }
  262. +
  263. + if (to != address(0)) {
  264. + unchecked {
  265. + _balances[to] += 1;
  266. + }
  267. + }
  268. +
  269. + _owners[tokenId] = to;
  270. +
  271. + emit Transfer(from, to, tokenId);
  272. +
  273. + return from;
  274. + }
  275. +
  276. + /**
  277. + * @dev Mints `tokenId` and transfers it to `to`.
  278. + *
  279. + * WARNING: Usage of this method is discouraged, use {_safeMint} whenever possible
  280. + *
  281. + * Requirements:
  282. + *
  283. + * - `tokenId` must not exist.
  284. + * - `to` cannot be the zero address.
  285. + *
  286. + * Emits a {Transfer} event.
  287. + */
  288. + function _mint(address to, uint256 tokenId) internal {
  289. + if (to == address(0)) {
  290. + revert ERC721InvalidReceiver(address(0));
  291. + }
  292. + address previousOwner = _update(to, tokenId, address(0));
  293. + if (previousOwner != address(0)) {
  294. + revert ERC721InvalidSender(address(0));
  295. + }
  296. + }
  297. +
  298. + /**
  299. + * @dev Mints `tokenId`, transfers it to `to` and checks for `to` acceptance.
  300. + *
  301. + * Requirements:
  302. + *
  303. + * - `tokenId` must not exist.
  304. + * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
  305. + *
  306. + * Emits a {Transfer} event.
  307. + */
  308. + function _safeMint(address to, uint256 tokenId) internal {
  309. + _safeMint(to, tokenId, "");
  310. + }
  311. +
  312. + /**
  313. + * @dev Same as {xref-ERC721-_safeMint-address-uint256-}[`_safeMint`], with an additional `data` parameter which is
  314. + * forwarded in {IERC721Receiver-onERC721Received} to contract recipients.
  315. + */
  316. + function _safeMint(address to, uint256 tokenId, bytes memory data) internal virtual {
  317. + _mint(to, tokenId);
  318. + _checkOnERC721Received(address(0), to, tokenId, data);
  319. + }
  320. +
  321. + /**
  322. + * @dev Destroys `tokenId`.
  323. + * The approval is cleared when the token is burned.
  324. + * This is an internal function that does not check if the sender is authorized to operate on the token.
  325. + *
  326. + * Requirements:
  327. + *
  328. + * - `tokenId` must exist.
  329. + *
  330. + * Emits a {Transfer} event.
  331. + */
  332. + function _burn(uint256 tokenId) internal {
  333. + address previousOwner = _update(address(0), tokenId, address(0));
  334. + if (previousOwner == address(0)) {
  335. + revert ERC721NonexistentToken(tokenId);
  336. + }
  337. + }
  338. +
  339. + /**
  340. + * @dev Transfers `tokenId` from `from` to `to`.
  341. + * As opposed to {transferFrom}, this imposes no restrictions on msg.sender.
  342. + *
  343. + * Requirements:
  344. + *
  345. + * - `to` cannot be the zero address.
  346. + * - `tokenId` token must be owned by `from`.
  347. + *
  348. + * Emits a {Transfer} event.
  349. + */
  350. + function _transfer(address from, address to, uint256 tokenId) internal {
  351. + if (to == address(0)) {
  352. + revert ERC721InvalidReceiver(address(0));
  353. + }
  354. + address previousOwner = _update(to, tokenId, address(0));
  355. + if (previousOwner == address(0)) {
  356. + revert ERC721NonexistentToken(tokenId);
  357. + } else if (previousOwner != from) {
  358. + revert ERC721IncorrectOwner(from, tokenId, previousOwner);
  359. + }
  360. + }
  361. +
  362. + /**
  363. + * @dev Safely transfers `tokenId` token from `from` to `to`, checking that contract recipients
  364. + * are aware of the ERC721 standard to prevent tokens from being forever locked.
  365. + *
  366. + * `data` is additional data, it has no specified format and it is sent in call to `to`.
  367. + *
  368. + * This internal function is like {safeTransferFrom} in the sense that it invokes
  369. + * {IERC721Receiver-onERC721Received} on the receiver, and can be used to e.g.
  370. + * implement alternative mechanisms to perform token transfer, such as signature-based.
  371. + *
  372. + * Requirements:
  373. + *
  374. + * - `tokenId` token must exist and be owned by `from`.
  375. + * - `to` cannot be the zero address.
  376. + * - `from` cannot be the zero address.
  377. + * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
  378. + *
  379. + * Emits a {Transfer} event.
  380. + */
  381. + function _safeTransfer(address from, address to, uint256 tokenId) internal {
  382. + _safeTransfer(from, to, tokenId, "");
  383. + }
  384. +
  385. + /**
  386. + * @dev Same as {xref-ERC721-_safeTransfer-address-address-uint256-}[`_safeTransfer`], with an additional `data` parameter which is
  387. + * forwarded in {IERC721Receiver-onERC721Received} to contract recipients.
  388. + */
  389. + function _safeTransfer(address from, address to, uint256 tokenId, bytes memory data) internal virtual {
  390. + _transfer(from, to, tokenId);
  391. + _checkOnERC721Received(from, to, tokenId, data);
  392. + }
  393. +
  394. + /**
  395. + * @dev Approve `to` to operate on `tokenId`
  396. + *
  397. + * The `auth` argument is optional. If the value passed is non 0, then this function will check that `auth` is
  398. + * either the owner of the token, or approved to operate on all tokens held by this owner.
  399. + *
  400. + * Emits an {Approval} event.
  401. + */
  402. + function _approve(address to, uint256 tokenId, address auth) internal virtual returns (address) {
  403. + address owner = ownerOf(tokenId);
  404. +
  405. + // We do not use _isAuthorized because single-token approvals should not be able to call approve
  406. + if (auth != address(0) && owner != auth && !isApprovedForAll(owner, auth)) {
  407. + revert ERC721InvalidApprover(auth);
  408. + }
  409. +
  410. + _tokenApprovals[tokenId] = to;
  411. + emit Approval(owner, to, tokenId);
  412. +
  413. + return owner;
  414. + }
  415. +
  416. + /**
  417. + * @dev Approve `operator` to operate on all of `owner` tokens
  418. + *
  419. + * Requirements:
  420. + * - operator can't be the address zero.
  421. + *
  422. + * Emits an {ApprovalForAll} event.
  423. + */
  424. + function _setApprovalForAll(address owner, address operator, bool approved) internal virtual {
  425. + if (operator == address(0)) {
  426. + revert ERC721InvalidOperator(operator);
  427. + }
  428. + _operatorApprovals[owner][operator] = approved;
  429. + emit ApprovalForAll(owner, operator, approved);
  430. + }
  431. +
  432. + /**
  433. + * @dev Reverts if the `tokenId` has not been minted yet.
  434. + */
  435. + function _requireMinted(uint256 tokenId) internal view virtual {
  436. + if (_ownerOf(tokenId) == address(0)) {
  437. + revert ERC721NonexistentToken(tokenId);
  438. + }
  439. + }
  440. +
  441. + /**
  442. + * @dev Private function to invoke {IERC721Receiver-onERC721Received} on a target address. This will revert if the
  443. + * recipient doesn't accept the token transfer. The call is not executed if the target address is not a contract.
  444. + *
  445. + * @param from address representing the previous owner of the given token ID
  446. + * @param to target address that will receive the tokens
  447. + * @param tokenId uint256 ID of the token to be transferred
  448. + * @param data bytes optional data to send along with the call
  449. + */
  450. + function _checkOnERC721Received(address from, address to, uint256 tokenId, bytes memory data) private {
  451. + if (to.code.length > 0) {
  452. + try IERC721Receiver(to).onERC721Received(_msgSender(), from, tokenId, data) returns (bytes4 retval) {
  453. + if (retval != IERC721Receiver.onERC721Received.selector) {
  454. + revert ERC721InvalidReceiver(to);
  455. + }
  456. + } catch (bytes memory reason) {
  457. + if (reason.length == 0) {
  458. + revert ERC721InvalidReceiver(to);
  459. + } else {
  460. + /// @solidity memory-safe-assembly
  461. + assembly {
  462. + revert(add(32, reason), mload(reason))
  463. + }
  464. + }
  465. + }
  466. + }
  467. + }
  468. +}