ERC1155Utils.sol 3.4 KB

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