12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- // SPDX-License-Identifier: MIT
- // OpenZeppelin Contracts (last updated v5.3.0-rc.0) (token/ERC1155/utils/ERC1155Utils.sol)
- pragma solidity ^0.8.20;
- import {IERC1155Receiver} from "../IERC1155Receiver.sol";
- import {IERC1155Errors} from "../../../interfaces/draft-IERC6093.sol";
- /**
- * @dev Library that provide common ERC-1155 utility functions.
- *
- * See https://eips.ethereum.org/EIPS/eip-1155[ERC-1155].
- *
- * _Available since v5.1._
- */
- library ERC1155Utils {
- /**
- * @dev Performs an acceptance check for the provided `operator` by calling {IERC1155Receiver-onERC1155Received}
- * on the `to` address. The `operator` is generally the address that initiated the token transfer (i.e. `msg.sender`).
- *
- * The acceptance call is not executed and treated as a no-op if the target address doesn't contain code (i.e. an EOA).
- * Otherwise, the recipient must implement {IERC1155Receiver-onERC1155Received} and return the acceptance magic value to accept
- * the transfer.
- */
- function checkOnERC1155Received(
- address operator,
- address from,
- address to,
- uint256 id,
- uint256 value,
- bytes memory data
- ) internal {
- if (to.code.length > 0) {
- try IERC1155Receiver(to).onERC1155Received(operator, from, id, value, data) returns (bytes4 response) {
- if (response != IERC1155Receiver.onERC1155Received.selector) {
- // Tokens rejected
- revert IERC1155Errors.ERC1155InvalidReceiver(to);
- }
- } catch (bytes memory reason) {
- if (reason.length == 0) {
- // non-IERC1155Receiver implementer
- revert IERC1155Errors.ERC1155InvalidReceiver(to);
- } else {
- assembly ("memory-safe") {
- revert(add(32, reason), mload(reason))
- }
- }
- }
- }
- }
- /**
- * @dev Performs a batch acceptance check for the provided `operator` by calling {IERC1155Receiver-onERC1155BatchReceived}
- * on the `to` address. The `operator` is generally the address that initiated the token transfer (i.e. `msg.sender`).
- *
- * The acceptance call is not executed and treated as a no-op if the target address doesn't contain code (i.e. an EOA).
- * Otherwise, the recipient must implement {IERC1155Receiver-onERC1155Received} and return the acceptance magic value to accept
- * the transfer.
- */
- function checkOnERC1155BatchReceived(
- address operator,
- address from,
- address to,
- uint256[] memory ids,
- uint256[] memory values,
- bytes memory data
- ) internal {
- if (to.code.length > 0) {
- try IERC1155Receiver(to).onERC1155BatchReceived(operator, from, ids, values, data) returns (
- bytes4 response
- ) {
- if (response != IERC1155Receiver.onERC1155BatchReceived.selector) {
- // Tokens rejected
- revert IERC1155Errors.ERC1155InvalidReceiver(to);
- }
- } catch (bytes memory reason) {
- if (reason.length == 0) {
- // non-IERC1155Receiver implementer
- revert IERC1155Errors.ERC1155InvalidReceiver(to);
- } else {
- assembly ("memory-safe") {
- revert(add(32, reason), mload(reason))
- }
- }
- }
- }
- }
- }
|