ERC721Utils.sol 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.1.0-rc.0) (token/ERC721/utils/ERC721Utils.sol)
  3. pragma solidity ^0.8.20;
  4. import {IERC721Receiver} from "../IERC721Receiver.sol";
  5. import {IERC721Errors} from "../../../interfaces/draft-IERC6093.sol";
  6. /**
  7. * @dev Library that provide common ERC-721 utility functions.
  8. *
  9. * See https://eips.ethereum.org/EIPS/eip-721[ERC-721].
  10. */
  11. library ERC721Utils {
  12. /**
  13. * @dev Performs an acceptance check for the provided `operator` by calling {IERC721-onERC721Received}
  14. * on the `to` address. The `operator` is generally the address that initiated the token transfer (i.e. `msg.sender`).
  15. *
  16. * The acceptance call is not executed and treated as a no-op if the target address doesn't contain code (i.e. an EOA).
  17. * Otherwise, the recipient must implement {IERC721Receiver-onERC721Received} and return the acceptance magic value to accept
  18. * the transfer.
  19. */
  20. function checkOnERC721Received(
  21. address operator,
  22. address from,
  23. address to,
  24. uint256 tokenId,
  25. bytes memory data
  26. ) internal {
  27. if (to.code.length > 0) {
  28. try IERC721Receiver(to).onERC721Received(operator, from, tokenId, data) returns (bytes4 retval) {
  29. if (retval != IERC721Receiver.onERC721Received.selector) {
  30. // Token rejected
  31. revert IERC721Errors.ERC721InvalidReceiver(to);
  32. }
  33. } catch (bytes memory reason) {
  34. if (reason.length == 0) {
  35. // non-IERC721Receiver implementer
  36. revert IERC721Errors.ERC721InvalidReceiver(to);
  37. } else {
  38. /// @solidity memory-safe-assembly
  39. assembly {
  40. revert(add(32, reason), mload(reason))
  41. }
  42. }
  43. }
  44. }
  45. }
  46. }