ERC1155Utils.sol 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.1.0-rc.0) (token/ERC1155/utils/ERC1155Utils.sol)
  3. pragma solidity ^0.8.20;
  4. import {IERC1155Receiver} from "../IERC1155Receiver.sol";
  5. import {IERC1155Errors} from "../../../interfaces/draft-IERC6093.sol";
  6. /**
  7. * @dev Library that provide common ERC-1155 utility functions.
  8. *
  9. * See https://eips.ethereum.org/EIPS/eip-1155[ERC-1155].
  10. */
  11. library ERC1155Utils {
  12. /**
  13. * @dev Performs an acceptance check for the provided `operator` by calling {IERC1155-onERC1155Received}
  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 is doesn't contain code (i.e. an EOA).
  17. * Otherwise, the recipient must implement {IERC1155Receiver-onERC1155Received} and return the acceptance magic value to accept
  18. * the transfer.
  19. */
  20. function checkOnERC1155Received(
  21. address operator,
  22. address from,
  23. address to,
  24. uint256 id,
  25. uint256 value,
  26. bytes memory data
  27. ) internal {
  28. if (to.code.length > 0) {
  29. try IERC1155Receiver(to).onERC1155Received(operator, from, id, value, data) returns (bytes4 response) {
  30. if (response != IERC1155Receiver.onERC1155Received.selector) {
  31. // Tokens rejected
  32. revert IERC1155Errors.ERC1155InvalidReceiver(to);
  33. }
  34. } catch (bytes memory reason) {
  35. if (reason.length == 0) {
  36. // non-IERC1155Receiver implementer
  37. revert IERC1155Errors.ERC1155InvalidReceiver(to);
  38. } else {
  39. /// @solidity memory-safe-assembly
  40. assembly {
  41. revert(add(32, reason), mload(reason))
  42. }
  43. }
  44. }
  45. }
  46. }
  47. /**
  48. * @dev Performs a batch acceptance check for the provided `operator` by calling {IERC1155-onERC1155BatchReceived}
  49. * on the `to` address. The `operator` is generally the address that initiated the token transfer (i.e. `msg.sender`).
  50. *
  51. * The acceptance call is not executed and treated as a no-op if the target address is doesn't contain code (i.e. an EOA).
  52. * Otherwise, the recipient must implement {IERC1155Receiver-onERC1155Received} and return the acceptance magic value to accept
  53. * the transfer.
  54. */
  55. function checkOnERC1155BatchReceived(
  56. address operator,
  57. address from,
  58. address to,
  59. uint256[] memory ids,
  60. uint256[] memory values,
  61. bytes memory data
  62. ) internal {
  63. if (to.code.length > 0) {
  64. try IERC1155Receiver(to).onERC1155BatchReceived(operator, from, ids, values, data) returns (
  65. bytes4 response
  66. ) {
  67. if (response != IERC1155Receiver.onERC1155BatchReceived.selector) {
  68. // Tokens rejected
  69. revert IERC1155Errors.ERC1155InvalidReceiver(to);
  70. }
  71. } catch (bytes memory reason) {
  72. if (reason.length == 0) {
  73. // non-IERC1155Receiver implementer
  74. revert IERC1155Errors.ERC1155InvalidReceiver(to);
  75. } else {
  76. /// @solidity memory-safe-assembly
  77. assembly {
  78. revert(add(32, reason), mload(reason))
  79. }
  80. }
  81. }
  82. }
  83. }
  84. }