Signed-off-by: Hadrien Croubois <hadrien.croubois@gmail.com> Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com> Co-authored-by: Francisco Giordano <fg@frang.io> Co-authored-by: Joseph Delong <joseph@delong.me> Co-authored-by: Arr00 <13561405+arr00@users.noreply.github.com> Co-authored-by: Renan Souza <renan.rodrigues.souza1@gmail.com> Co-authored-by: Ernesto García <ernestognw@gmail.com> Co-authored-by: Voronor <129545215+voronor@users.noreply.github.com> Co-authored-by: StackOverflowExcept1on <109800286+StackOverflowExcept1on@users.noreply.github.com> Co-authored-by: Michalis Kargakis <kargakis@protonmail.com> Co-authored-by: Bilog WEB3 <155262265+Bilogweb3@users.noreply.github.com> Co-authored-by: Fallengirl <155266340+Fallengirl@users.noreply.github.com> Co-authored-by: XxAlex74xX <30472093+XxAlex74xX@users.noreply.github.com> Co-authored-by: PixelPilot <161360836+PixelPil0t1@users.noreply.github.com> Co-authored-by: kilavvy <140459108+kilavvy@users.noreply.github.com> Co-authored-by: Devkuni <155117116+detrina@users.noreply.github.com> Co-authored-by: Danbo <140512416+dannbbb1@users.noreply.github.com> Co-authored-by: Ann Wagner <chant_77_swirly@icloud.com> Co-authored-by: comfsrt <155266597+comfsrt@users.noreply.github.com> Co-authored-by: Bob <158583129+bouchmann@users.noreply.github.com> Co-authored-by: JohnBonny <158583902+JohnBonny@users.noreply.github.com> Co-authored-by: moonman <155266991+moooonman@users.noreply.github.com> Co-authored-by: kazak <alright-epsilon8h@icloud.com> Co-authored-by: Wei <ybxerlvqtx@rambler.ru> Co-authored-by: Maxim Evtush <154841002+maximevtush@users.noreply.github.com> Co-authored-by: Vitalyr <158586577+Vitaliyr888@users.noreply.github.com> Co-authored-by: pendrue <158588659+pendrue@users.noreply.github.com> Co-authored-by: Tronica <wudmytrotest404@gmail.com> Co-authored-by: emmmm <155267286+eeemmmmmm@users.noreply.github.com> Co-authored-by: bigbear <155267841+aso20455@users.noreply.github.com> Co-authored-by: Tomás Andróil <tomasandroil@gmail.com> Co-authored-by: GooseMatrix <155266802+GooseMatrix@users.noreply.github.com> Co-authored-by: jasmy <3776356370@qq.com> Co-authored-by: SITADRITA1 <mrlime2018@gmail.com> Co-authored-by: Ocenka <testoviydiman1@gmail.com>
89 lines
3.5 KiB
Solidity
89 lines
3.5 KiB
Solidity
// SPDX-License-Identifier: MIT
|
|
// OpenZeppelin Contracts (last updated v5.3.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))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|