// File: contracts/NFPeaceV2.sol



// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;

import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
import "@openzeppelin/contracts/token/ERC1155/IERC1155Receiver.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol";
import "@openzeppelin/contracts/utils/introspection/ERC165.sol";

import "./NFPeace.sol";

contract NFPeaceV2 is
    ERC165,
    IERC721Receiver,
    IERC1155Receiver,
    ReentrancyGuard
{
    /// @notice The previous NFPeace (V1) contract. NFPeaceV2 fixes a potential exploit.
    /// @dev The V1 NFPeace contract
    NFPeace public constant NFPEACE_V1 = NFPeace(0x0000000011E48D382b4F627437A2bBAc3b10F90e);

    /// @notice Unchain (https://unchain.fund) is a charity project created by blockchain activists. Its goal is to break the chain of war which the Russian Federation started against Ukraine.
    /// @dev The wallet address of the humanitarian relief fund unchain.fund
    address payable public constant CHARITY_ADDRESS = payable(0x10E1439455BD2624878b243819E31CfEE9eb721C);

    /// @notice Minimum auction duration after a bid in seconds (15 minutes).
    /// @dev Minimum auction runtime in seconds after new bids
    uint32 public constant BIDDING_GRACE_PERIOD = 15 minutes;

    /// @notice Each bid has to increase by at least 10%
    /// @dev The minimum percentage increase per bid
    uint32 public constant BID_PERCENTAGE_INCREASE = 10;

    /// @dev The minimum value of an auction
    uint64 public constant DEFAULT_STARTING_PRICE = 0.05 ether;

    /// @dev The initial auction ID of the V2 NFPeace contract
    uint64 public initialAuctionId = 0;

    /// @dev The next auction ID
    uint64 public nextAuctionId = 0;

    struct Auction {
        address tokenContract;
        uint256 tokenId;
        address latestBidder;
        uint128 latestBid;
        uint64 startingPrice; // max ~18.44 Ether
        uint32 endTimestamp; // latest date is Sun Feb 07 2106 06:28:15 GMT (in 84 years)
        uint16 tokenERCStandard;
        uint8 tokenAmount;
        bool settled;
    }

    /// @dev Each auction is identified by an ID
    mapping(uint256 => Auction) private _auctions;

    /// @dev When the automatic refunds of previous bids fail, they are stored in here
    mapping(address => uint256) private _balances;

    /// @dev Emitted when an NFT is sent to the contract.
    event AuctionInitialised(uint64 indexed auctionId);

    /// @dev Emitted when a new bid is entered.
    event Bid(uint64 indexed auctionId, uint256 indexed bid, address indexed from);

    /// @dev Emitted when a new bid is entered within the BIDDING_GRACE_PERIOD.
    event AuctionExtended(uint64 indexed auctionId, uint256 indexed endTimestamp);

    /// @dev Emitted when an auction is settled, the NFT is sent to the winner and the funds sent to the charity.
    event AuctionSettled(uint64 indexed auctionId);

    /// @dev Set the initial tokenID.
    constructor () {
        initialAuctionId = NFPEACE_V1.nextAuctionId();
        nextAuctionId = initialAuctionId;
    }

    /// @dev Get an Auction by its ID
    function getAuction (uint64 auctionId)
        public view
        returns (
            address tokenContract,
            uint256 tokenId,
            address latestBidder,
            uint128 latestBid,
            uint64 startingPrice,
            uint32 endTimestamp,
            uint16 tokenERCStandard,
            uint8 tokenAmount,
            bool settled
    ) {
        // Proxy V1 auctions through to the V1 contract.
        if (auctionId < initialAuctionId) {
            return NFPEACE_V1.getAuction(auctionId);
        }

        Auction memory auction = _auctions[auctionId];

        return (
            auction.tokenContract,
            auction.tokenId,
            auction.latestBidder,
            auction.latestBid,
            auction.startingPrice,
            auction.endTimestamp,
            auction.tokenERCStandard,
            auction.tokenAmount,
            auction.settled
        );
    }

    /// @dev The minimum value of the next bid for an auction.
    function currentBidPrice (uint64 auctionId)
        external view
        returns (uint128)
    {
        return _currentBidPrice(_auctions[auctionId]);
    }

    /// @dev Enter a new bid
    /// @param auctionId The Auction ID to bid on
    function bid (uint64 auctionId)
        external payable
        nonReentrant
    {
        Auction storage auction = _auctions[auctionId];
        uint256 bidValue = msg.value;
        address bidder = msg.sender;

        require(bidValue >= _currentBidPrice(auction), "Minimum bid value not met.");
        require(block.timestamp <= auction.endTimestamp, "Auction is not active.");

        // Pay back previous bidder
        if (_hasBid(auction)) {
            if (!payable(auction.latestBidder).send(auction.latestBid)) {
                _balances[auction.latestBidder] += auction.latestBid;
            }
        }

        _maybeExtendTime(auctionId, auction);

        // Store the bid
        auction.latestBid = uint128(bidValue);
        auction.latestBidder = bidder;

        emit Bid(auctionId, bidValue, bidder);
    }

    /// @dev Settles an auction
    /// @param auctionId The Auction ID to claim.
    function settle (uint64 auctionId) external {
        Auction storage auction = _auctions[auctionId];
        require(!auction.settled, "Auction already settled.");
        require(auction.endTimestamp > 0, "Auction does not exist.");
        require(block.timestamp > auction.endTimestamp, "Auction not complete.");

        if (_hasBid(auction)) {
            (bool success,) = CHARITY_ADDRESS.call{ value: auction.latestBid }("");
            require(success, "Failed to forward funds");
        }

        if (auction.tokenERCStandard == 721) {
            IERC721(auction.tokenContract).safeTransferFrom(address(this), auction.latestBidder, auction.tokenId, "");
        } else if (auction.tokenERCStandard == 1155) {
            IERC1155(auction.tokenContract).safeTransferFrom(address(this), auction.latestBidder, auction.tokenId, auction.tokenAmount, "");
        }

        // End the auction
        auction.settled = true;
        emit AuctionSettled(auctionId);
    }

    /// @dev Get the balance for an address.
    function getBalance (address _address) external view returns (uint256) {
        return _balances[_address];
    }

    /// @dev Withdraw user balance in case automatic refund in bid failed.
    function withdraw () external {
        uint256 amount = _balances[msg.sender];
        require(amount > 0, "No balance to withdraw.");
        // Set balance to zero because it could be called again in receive before send returns
        _balances[msg.sender] = 0;
        if (!payable(msg.sender).send(amount)) {
            _balances[msg.sender] = amount;
        }
    }

    /// @dev Hook for `saveTransferFrom` of ERC721 tokens to this contract
    /// @param from The address which previously owned the token
    /// @param tokenId The ID of the token being transferred
    /// @param data The custom starting price (set by a user)
    function onERC721Received(
        address,
        address from,
        uint256 tokenId,
        bytes calldata data
    ) public override returns (bytes4) {
        _initializeAuction(tokenId, 721, from, _getStartingPrice(data), 1);

        return IERC721Receiver.onERC721Received.selector;
    }

    /// @dev Hook for `saveTransferFrom` of ERC1155 tokens to this contract
    /// @param from The address which previously owned the token
    /// @param id The ID of the token being transferred
    /// @param value The amount of tokens being transferred
    /// @param data The custom starting price (set by a user)
    function onERC1155Received(
        address,
        address from,
        uint256 id,
        uint256 value,
        bytes calldata data
    ) public override returns (bytes4) {
        require(value < 256, "Too many tokens");

        _initializeAuction(id, 1155, from, _getStartingPrice(data), uint8(value));

        return IERC1155Receiver.onERC1155Received.selector;
    }

    /// @dev Hook for `safeBatchTransferFrom` of ERC1155 tokens to this contract
    /// @param operator The address which initiated the transfer
    /// @param from The address which previously owned the token
    /// @param ids An array containing ids of each token being transferred (order and length must match values array)
    /// @param values An array containing amounts of each token being transferred (order and length must match ids array)
    /// @param data The custom starting price (set by a user)
    function onERC1155BatchReceived(
        address operator,
        address from,
        uint256[] calldata ids,
        uint256[] calldata values,
        bytes calldata data
    ) external override returns (bytes4) {
        for (uint256 index = 0; index < ids.length; index++) {
            onERC1155Received(operator, from, ids[index], values[index], data);
        }

        return IERC1155Receiver.onERC1155BatchReceived.selector;
    }

    /// @dev Get the starting price based on default or user input. Warns users about out of range price.
    function _getStartingPrice (bytes calldata data) internal pure returns (uint64) {
        uint64 price = toUint64(data);

        return price > DEFAULT_STARTING_PRICE ? price : DEFAULT_STARTING_PRICE;
    }

    /// @dev Initializes an auction
    function _initializeAuction (
        uint256 tokenId,
        uint16 tokenERCStandard,
        address tokenOwner,
        uint64 startingPrice,
        uint8 tokenAmount
    ) internal
    {
        _auctions[nextAuctionId] = Auction(
            msg.sender,                          // the NFT token contract
            tokenId,                             // the NFT token ID
            tokenOwner,                          // the token owner can claim for free if no one bids
            0,                                   // no bid has been placed (latestBid = 0)
            startingPrice,                       // minimum bid (can be customised per auction)
            uint32(block.timestamp + 24 hours),  // auction ends 24 hours from now
            tokenERCStandard,                    // ERC721 or ERC1155
            tokenAmount,                         // the number of tokens with that ID (for ERC1155)
            false                                // the auction is not settled
        );

        emit AuctionInitialised(nextAuctionId);

        nextAuctionId++;
    }

    /// @dev Extends the end time of an auction if we are within the grace period.
    function _maybeExtendTime (uint64 auctionId, Auction storage auction) internal {
        uint64 gracePeriodStart = auction.endTimestamp - BIDDING_GRACE_PERIOD;
        uint64 _now = uint64(block.timestamp);
        if (_now > gracePeriodStart) {
            auction.endTimestamp = uint32(_now + BIDDING_GRACE_PERIOD);

            emit AuctionExtended(auctionId, auction.endTimestamp);
        }
    }

    /// @dev Whether an auction has an existing bid
    function _hasBid (Auction memory auction) internal pure returns (bool) {
        return auction.latestBid > 0;
    }

    /// @dev Calculates the minimum price for the next bid
    function _currentBidPrice (Auction memory auction) internal pure returns (uint128) {
        if (! _hasBid(auction)) {
            return auction.startingPrice;
        }

        uint128 percentageIncreasePrice = auction.latestBid * (100 + BID_PERCENTAGE_INCREASE) / 100;
        return percentageIncreasePrice - auction.latestBid < auction.startingPrice
            ? auction.latestBid + auction.startingPrice
            : percentageIncreasePrice;
    }

    /// @dev Transform a bytes string into a uint64. If no string provided, return 0
    function toUint64(bytes memory _bytes) internal pure returns (uint64) {
        if (_bytes.length == 0) return 0;

        require(_bytes.length <= 8, "toUint64_outOfBounds");
        uint64 tempUint;

        assembly {
            tempUint := mload(add(add(_bytes, 0x8), 0))
        }

        return tempUint;
    }
}

// File: @openzeppelin/contracts/security/ReentrancyGuard.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (security/ReentrancyGuard.sol)

pragma solidity ^0.8.0;

/**
 * @dev Contract module that helps prevent reentrant calls to a function.
 *
 * Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
 * available, which can be applied to functions to make sure there are no nested
 * (reentrant) calls to them.
 *
 * Note that because there is a single `nonReentrant` guard, functions marked as
 * `nonReentrant` may not call one another. This can be worked around by making
 * those functions `private`, and then adding `external` `nonReentrant` entry
 * points to them.
 *
 * TIP: If you would like to learn more about reentrancy and alternative ways
 * to protect against it, check out our blog post
 * https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
 */
abstract contract ReentrancyGuard {
    // Booleans are more expensive than uint256 or any type that takes up a full
    // word because each write operation emits an extra SLOAD to first read the
    // slot's contents, replace the bits taken up by the boolean, and then write
    // back. This is the compiler's defense against contract upgrades and
    // pointer aliasing, and it cannot be disabled.

    // The values being non-zero value makes deployment a bit more expensive,
    // but in exchange the refund on every call to nonReentrant will be lower in
    // amount. Since refunds are capped to a percentage of the total
    // transaction's gas, it is best to keep them low in cases like this one, to
    // increase the likelihood of the full refund coming into effect.
    uint256 private constant _NOT_ENTERED = 1;
    uint256 private constant _ENTERED = 2;

    uint256 private _status;

    constructor() {
        _status = _NOT_ENTERED;
    }

    /**
     * @dev Prevents a contract from calling itself, directly or indirectly.
     * Calling a `nonReentrant` function from another `nonReentrant`
     * function is not supported. It is possible to prevent this from happening
     * by making the `nonReentrant` function external, and making it call a
     * `private` function that does the actual work.
     */
    modifier nonReentrant() {
        // On the first call to nonReentrant, _notEntered will be true
        require(_status != _ENTERED, "ReentrancyGuard: reentrant call");

        // Any calls to nonReentrant after this point will fail
        _status = _ENTERED;

        _;

        // By storing the original value once again, a refund is triggered (see
        // https://eips.ethereum.org/EIPS/eip-2200)
        _status = _NOT_ENTERED;
    }
}

// File: @openzeppelin/contracts/token/ERC1155/IERC1155.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC1155/IERC1155.sol)

pragma solidity ^0.8.0;

import "../../utils/introspection/IERC165.sol";

/**
 * @dev Required interface of an ERC1155 compliant contract, as defined in the
 * https://eips.ethereum.org/EIPS/eip-1155[EIP].
 *
 * _Available since v3.1._
 */
interface IERC1155 is IERC165 {
    /**
     * @dev Emitted when `value` tokens of token type `id` are transferred from `from` to `to` by `operator`.
     */
    event TransferSingle(address indexed operator, address indexed from, address indexed to, uint256 id, uint256 value);

    /**
     * @dev Equivalent to multiple {TransferSingle} events, where `operator`, `from` and `to` are the same for all
     * transfers.
     */
    event TransferBatch(
        address indexed operator,
        address indexed from,
        address indexed to,
        uint256[] ids,
        uint256[] values
    );

    /**
     * @dev Emitted when `account` grants or revokes permission to `operator` to transfer their tokens, according to
     * `approved`.
     */
    event ApprovalForAll(address indexed account, address indexed operator, bool approved);

    /**
     * @dev Emitted when the URI for token type `id` changes to `value`, if it is a non-programmatic URI.
     *
     * If an {URI} event was emitted for `id`, the standard
     * https://eips.ethereum.org/EIPS/eip-1155#metadata-extensions[guarantees] that `value` will equal the value
     * returned by {IERC1155MetadataURI-uri}.
     */
    event URI(string value, uint256 indexed id);

    /**
     * @dev Returns the amount of tokens of token type `id` owned by `account`.
     *
     * Requirements:
     *
     * - `account` cannot be the zero address.
     */
    function balanceOf(address account, uint256 id) external view returns (uint256);

    /**
     * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {balanceOf}.
     *
     * Requirements:
     *
     * - `accounts` and `ids` must have the same length.
     */
    function balanceOfBatch(address[] calldata accounts, uint256[] calldata ids)
        external
        view
        returns (uint256[] memory);

    /**
     * @dev Grants or revokes permission to `operator` to transfer the caller's tokens, according to `approved`,
     *
     * Emits an {ApprovalForAll} event.
     *
     * Requirements:
     *
     * - `operator` cannot be the caller.
     */
    function setApprovalForAll(address operator, bool approved) external;

    /**
     * @dev Returns true if `operator` is approved to transfer ``account``'s tokens.
     *
     * See {setApprovalForAll}.
     */
    function isApprovedForAll(address account, address operator) external view returns (bool);

    /**
     * @dev Transfers `amount` tokens of token type `id` from `from` to `to`.
     *
     * Emits a {TransferSingle} event.
     *
     * Requirements:
     *
     * - `to` cannot be the zero address.
     * - If the caller is not `from`, it must be have been approved to spend ``from``'s tokens via {setApprovalForAll}.
     * - `from` must have a balance of tokens of type `id` of at least `amount`.
     * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the
     * acceptance magic value.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 id,
        uint256 amount,
        bytes calldata data
    ) external;

    /**
     * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {safeTransferFrom}.
     *
     * Emits a {TransferBatch} event.
     *
     * Requirements:
     *
     * - `ids` and `amounts` must have the same length.
     * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the
     * acceptance magic value.
     */
    function safeBatchTransferFrom(
        address from,
        address to,
        uint256[] calldata ids,
        uint256[] calldata amounts,
        bytes calldata data
    ) external;
}

// File: @openzeppelin/contracts/token/ERC1155/IERC1155Receiver.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.5.0) (token/ERC1155/IERC1155Receiver.sol)

pragma solidity ^0.8.0;

import "../../utils/introspection/IERC165.sol";

/**
 * @dev _Available since v3.1._
 */
interface IERC1155Receiver is IERC165 {
    /**
     * @dev Handles the receipt of a single ERC1155 token type. This function is
     * called at the end of a `safeTransferFrom` after the balance has been updated.
     *
     * NOTE: To accept the transfer, this must return
     * `bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"))`
     * (i.e. 0xf23a6e61, or its own function selector).
     *
     * @param operator The address which initiated the transfer (i.e. msg.sender)
     * @param from The address which previously owned the token
     * @param id The ID of the token being transferred
     * @param value The amount of tokens being transferred
     * @param data Additional data with no specified format
     * @return `bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"))` if transfer is allowed
     */
    function onERC1155Received(
        address operator,
        address from,
        uint256 id,
        uint256 value,
        bytes calldata data
    ) external returns (bytes4);

    /**
     * @dev Handles the receipt of a multiple ERC1155 token types. This function
     * is called at the end of a `safeBatchTransferFrom` after the balances have
     * been updated.
     *
     * NOTE: To accept the transfer(s), this must return
     * `bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)"))`
     * (i.e. 0xbc197c81, or its own function selector).
     *
     * @param operator The address which initiated the batch transfer (i.e. msg.sender)
     * @param from The address which previously owned the token
     * @param ids An array containing ids of each token being transferred (order and length must match values array)
     * @param values An array containing amounts of each token being transferred (order and length must match ids array)
     * @param data Additional data with no specified format
     * @return `bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)"))` if transfer is allowed
     */
    function onERC1155BatchReceived(
        address operator,
        address from,
        uint256[] calldata ids,
        uint256[] calldata values,
        bytes calldata data
    ) external returns (bytes4);
}

// File: @openzeppelin/contracts/token/ERC721/IERC721.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC721/IERC721.sol)

pragma solidity ^0.8.0;

import "../../utils/introspection/IERC165.sol";

/**
 * @dev Required interface of an ERC721 compliant contract.
 */
interface IERC721 is IERC165 {
    /**
     * @dev Emitted when `tokenId` token is transferred from `from` to `to`.
     */
    event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);

    /**
     * @dev Emitted when `owner` enables `approved` to manage the `tokenId` token.
     */
    event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);

    /**
     * @dev Emitted when `owner` enables or disables (`approved`) `operator` to manage all of its assets.
     */
    event ApprovalForAll(address indexed owner, address indexed operator, bool approved);

    /**
     * @dev Returns the number of tokens in ``owner``'s account.
     */
    function balanceOf(address owner) external view returns (uint256 balance);

    /**
     * @dev Returns the owner of the `tokenId` token.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function ownerOf(uint256 tokenId) external view returns (address owner);

    /**
     * @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
     * are aware of the ERC721 protocol to prevent tokens from being forever locked.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must exist and be owned by `from`.
     * - If the caller is not `from`, it must be have been allowed to move this token by either {approve} or {setApprovalForAll}.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId
    ) external;

    /**
     * @dev Transfers `tokenId` token from `from` to `to`.
     *
     * WARNING: Usage of this method is discouraged, use {safeTransferFrom} whenever possible.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must be owned by `from`.
     * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(
        address from,
        address to,
        uint256 tokenId
    ) external;

    /**
     * @dev Gives permission to `to` to transfer `tokenId` token to another account.
     * The approval is cleared when the token is transferred.
     *
     * Only a single account can be approved at a time, so approving the zero address clears previous approvals.
     *
     * Requirements:
     *
     * - The caller must own the token or be an approved operator.
     * - `tokenId` must exist.
     *
     * Emits an {Approval} event.
     */
    function approve(address to, uint256 tokenId) external;

    /**
     * @dev Returns the account approved for `tokenId` token.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function getApproved(uint256 tokenId) external view returns (address operator);

    /**
     * @dev Approve or remove `operator` as an operator for the caller.
     * Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller.
     *
     * Requirements:
     *
     * - The `operator` cannot be the caller.
     *
     * Emits an {ApprovalForAll} event.
     */
    function setApprovalForAll(address operator, bool _approved) external;

    /**
     * @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
     *
     * See {setApprovalForAll}
     */
    function isApprovedForAll(address owner, address operator) external view returns (bool);

    /**
     * @dev Safely transfers `tokenId` token from `from` to `to`.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must exist and be owned by `from`.
     * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId,
        bytes calldata data
    ) external;
}

// File: @openzeppelin/contracts/token/ERC721/IERC721Receiver.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC721/IERC721Receiver.sol)

pragma solidity ^0.8.0;

/**
 * @title ERC721 token receiver interface
 * @dev Interface for any contract that wants to support safeTransfers
 * from ERC721 asset contracts.
 */
interface IERC721Receiver {
    /**
     * @dev Whenever an {IERC721} `tokenId` token is transferred to this contract via {IERC721-safeTransferFrom}
     * by `operator` from `from`, this function is called.
     *
     * It must return its Solidity selector to confirm the token transfer.
     * If any other value is returned or the interface is not implemented by the recipient, the transfer will be reverted.
     *
     * The selector can be obtained in Solidity with `IERC721.onERC721Received.selector`.
     */
    function onERC721Received(
        address operator,
        address from,
        uint256 tokenId,
        bytes calldata data
    ) external returns (bytes4);
}

// File: @openzeppelin/contracts/utils/introspection/ERC165.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/introspection/ERC165.sol)

pragma solidity ^0.8.0;

import "./IERC165.sol";

/**
 * @dev Implementation of the {IERC165} interface.
 *
 * Contracts that want to implement ERC165 should inherit from this contract and override {supportsInterface} to check
 * for the additional interface id that will be supported. For example:
 *
 * ```solidity
 * function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
 *     return interfaceId == type(MyInterface).interfaceId || super.supportsInterface(interfaceId);
 * }
 * ```
 *
 * Alternatively, {ERC165Storage} provides an easier to use but more expensive implementation.
 */
abstract contract ERC165 is IERC165 {
    /**
     * @dev See {IERC165-supportsInterface}.
     */
    function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
        return interfaceId == type(IERC165).interfaceId;
    }
}

// File: contracts/NFPeace.sol



// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;

import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
import "@openzeppelin/contracts/token/ERC1155/IERC1155Receiver.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol";
import "@openzeppelin/contracts/utils/introspection/ERC165.sol";

contract NFPeace is
    ERC165,
    IERC721Receiver,
    IERC1155Receiver,
    ReentrancyGuard
{
    /// @notice Unchain (https://unchain.fund) is a charity project created by blockchain activists. Its goal is to break the chain of war which the Russian Federation started against Ukraine.
    /// @dev The wallet address of the humanitarian relief fund unchain.fund
    address payable public constant CHARITY_ADDRESS = payable(0x10E1439455BD2624878b243819E31CfEE9eb721C);

    /// @notice Minimum auction duration after a bid in seconds (15 minutes).
    /// @dev Minimum auction runtime in seconds after new bids
    uint32 public constant BIDDING_GRACE_PERIOD = 15 minutes;

    /// @notice Each bid has to increase by at least 10%
    /// @dev The minimum percentage increase per bid
    uint32 public constant BID_PERCENTAGE_INCREASE = 10;

    /// @dev The minimum value of an auction
    uint64 public constant DEFAULT_STARTING_PRICE = 0.05 ether;

    /// @dev The next auction ID
    uint64 public nextAuctionId = 0;

    struct Auction {
        address tokenContract;
        uint256 tokenId;
        address latestBidder;
        uint128 latestBid;
        uint64 startingPrice; // max ~18.44 Ether
        uint32 endTimestamp; // latest date is Sun Feb 07 2106 06:28:15 GMT (in 84 years)
        uint16 tokenERCStandard;
        uint8 tokenAmount;
        bool settled;
    }

    /// @dev Each auction is identified by an ID
    mapping(uint256 => Auction) private _auctions;

    /// @dev Emitted when an NFT is sent to the contract.
    event AuctionInitialised(uint64 indexed auctionId);

    /// @dev Emitted when a new bid is entered.
    event Bid(uint64 indexed auctionId, uint256 indexed bid, address indexed from);

    /// @dev Emitted when a new bid is entered within the BIDDING_GRACE_PERIOD.
    event AuctionExtended(uint64 indexed auctionId, uint256 indexed endTimestamp);

    /// @dev Emitted when an auction is settled, the NFT is sent to the winner and the funds sent to the charity.
    event AuctionSettled(uint64 indexed auctionId);

    /// @dev Get an Auction by its ID
    function getAuction (uint64 auctionId)
        public view
        returns (
            address tokenContract,
            uint256 tokenId,
            address latestBidder,
            uint128 latestBid,
            uint64 startingPrice,
            uint32 endTimestamp,
            uint16 tokenERCStandard,
            uint8 tokenAmount,
            bool settled
    ) {
        Auction memory auction = _auctions[auctionId];

        return (
            auction.tokenContract,
            auction.tokenId,
            auction.latestBidder,
            auction.latestBid,
            auction.startingPrice,
            auction.endTimestamp,
            auction.tokenERCStandard,
            auction.tokenAmount,
            auction.settled
        );
    }

    /// @dev The minimum value of the next bid for an auction.
    function currentBidPrice (uint64 auctionId)
        external view
        returns (uint128)
    {
        return _currentBidPrice(_auctions[auctionId]);
    }

    /// @dev Enter a new bid
    /// @param auctionId The Auction ID to bid on
    function bid (uint64 auctionId)
        external payable
        nonReentrant
    {
        Auction storage auction = _auctions[auctionId];
        uint256 bidValue = msg.value;
        address bidder = msg.sender;

        require(bidValue >= _currentBidPrice(auction), "Minimum bid value not met.");
        require(block.timestamp <= auction.endTimestamp, "Auction is not active.");

        // Pay back previous bidder
        if (_hasBid(auction)) {
            payable(auction.latestBidder).transfer(auction.latestBid);
        }

        _maybeExtendTime(auctionId, auction);

        // Store the bid
        auction.latestBid = uint128(bidValue);
        auction.latestBidder = bidder;

        emit Bid(auctionId, bidValue, bidder);
    }

    /// @dev Settles an auction
    /// @param auctionId The Auction ID to claim.
    function settle (uint64 auctionId) external {
        Auction storage auction = _auctions[auctionId];
        require(!auction.settled, "Auction already settled.");
        require(auction.endTimestamp > 0, "Auction does not exist.");
        require(block.timestamp > auction.endTimestamp, "Auction not complete.");

        if (_hasBid(auction)) {
            (bool success,) = CHARITY_ADDRESS.call{ value: auction.latestBid }("");
            require(success, "Failed to forward funds");
        }

        if (auction.tokenERCStandard == 721) {
            IERC721(auction.tokenContract).safeTransferFrom(address(this), auction.latestBidder, auction.tokenId, "");
        } else if (auction.tokenERCStandard == 1155) {
            IERC1155(auction.tokenContract).safeTransferFrom(address(this), auction.latestBidder, auction.tokenId, auction.tokenAmount, "");
        }

        // End the auction
        auction.settled = true;
        emit AuctionSettled(auctionId);
    }

    /// @dev Hook for `saveTransferFrom` of ERC721 tokens to this contract
    /// @param from The address which previously owned the token
    /// @param tokenId The ID of the token being transferred
    /// @param data The custom starting price (set by a user)
    function onERC721Received(
        address,
        address from,
        uint256 tokenId,
        bytes calldata data
    ) public override returns (bytes4) {
        _initializeAuction(tokenId, 721, from, _getStartingPrice(data), 1);

        return IERC721Receiver.onERC721Received.selector;
    }

    /// @dev Hook for `saveTransferFrom` of ERC1155 tokens to this contract
    /// @param from The address which previously owned the token
    /// @param id The ID of the token being transferred
    /// @param value The amount of tokens being transferred
    /// @param data The custom starting price (set by a user)
    function onERC1155Received(
        address,
        address from,
        uint256 id,
        uint256 value,
        bytes calldata data
    ) public override returns (bytes4) {
        require(value < 256, "Too many tokens");

        _initializeAuction(id, 1155, from, _getStartingPrice(data), uint8(value));

        return IERC1155Receiver.onERC1155Received.selector;
    }

    /// @dev Hook for `safeBatchTransferFrom` of ERC1155 tokens to this contract
    /// @param operator The address which initiated the transfer
    /// @param from The address which previously owned the token
    /// @param ids An array containing ids of each token being transferred (order and length must match values array)
    /// @param values An array containing amounts of each token being transferred (order and length must match ids array)
    /// @param data The custom starting price (set by a user)
    function onERC1155BatchReceived(
        address operator,
        address from,
        uint256[] calldata ids,
        uint256[] calldata values,
        bytes calldata data
    ) external override returns (bytes4) {
        for (uint256 index = 0; index < ids.length; index++) {
            onERC1155Received(operator, from, ids[index], values[index], data);
        }

        return IERC1155Receiver.onERC1155BatchReceived.selector;
    }

    /// @dev Get the starting price based on default or user input. Warns users about out of range price.
    function _getStartingPrice (bytes calldata data) internal pure returns (uint64) {
        uint64 price = toUint64(data);

        return price > DEFAULT_STARTING_PRICE ? price : DEFAULT_STARTING_PRICE;
    }

    /// @dev Initializes an auction
    function _initializeAuction (
        uint256 tokenId,
        uint16 tokenERCStandard,
        address tokenOwner,
        uint64 startingPrice,
        uint8 tokenAmount
    ) internal
    {
        _auctions[nextAuctionId] = Auction(
            msg.sender,                          // the NFT token contract
            tokenId,                             // the NFT token ID
            tokenOwner,                          // the token owner can claim for free if no one bids
            0,                                   // no bid has been placed (latestBid = 0)
            startingPrice,                       // minimum bid (can be customised per auction)
            uint32(block.timestamp + 24 hours),  // auction ends 24 hours from now
            tokenERCStandard,                    // ERC721 or ERC1155
            tokenAmount,                         // the number of tokens with that ID (for ERC1155)
            false                                // the auction is not settled
        );

        emit AuctionInitialised(nextAuctionId);

        nextAuctionId++;
    }

    /// @dev Extends the end time of an auction if we are within the grace period.
    function _maybeExtendTime (uint64 auctionId, Auction storage auction) internal {
        uint64 gracePeriodStart = auction.endTimestamp - BIDDING_GRACE_PERIOD;
        uint64 _now = uint64(block.timestamp);
        if (_now > gracePeriodStart) {
            auction.endTimestamp = uint32(_now + BIDDING_GRACE_PERIOD);

            emit AuctionExtended(auctionId, auction.endTimestamp);
        }
    }

    /// @dev Whether an auction has an existing bid
    function _hasBid (Auction memory auction) internal pure returns (bool) {
        return auction.latestBid > 0;
    }

    /// @dev Calculates the minimum price for the next bid
    function _currentBidPrice (Auction memory auction) internal pure returns (uint128) {
        if (! _hasBid(auction)) {
            return auction.startingPrice;
        }

        uint128 percentageIncreasePrice = auction.latestBid * (100 + BID_PERCENTAGE_INCREASE) / 100;
        return percentageIncreasePrice - auction.latestBid < auction.startingPrice
            ? auction.latestBid + auction.startingPrice
            : percentageIncreasePrice;
    }

    /// @dev Transform a bytes string into a uint64. If no string provided, return 0
    function toUint64(bytes memory _bytes) internal pure returns (uint64) {
        if (_bytes.length == 0) return 0;

        require(_bytes.length <= 8, "toUint64_outOfBounds");
        uint64 tempUint;

        assembly {
            tempUint := mload(add(add(_bytes, 0x8), 0))
        }

        return tempUint;
    }
}

// File: @openzeppelin/contracts/utils/introspection/IERC165.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/introspection/IERC165.sol)

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC165 standard, as defined in the
 * https://eips.ethereum.org/EIPS/eip-165[EIP].
 *
 * Implementers can declare support of contract interfaces, which can then be
 * queried by others ({ERC165Checker}).
 *
 * For an implementation, see {ERC165}.
 */
interface IERC165 {
    /**
     * @dev Returns true if this contract implements the interface defined by
     * `interfaceId`. See the corresponding
     * https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]
     * to learn more about how these ids are created.
     *
     * This function call must use less than 30 000 gas.
     */
    function supportsInterface(bytes4 interfaceId) external view returns (bool);
}