S Price: $0.509656 (+3.64%)

Contract Diff Checker

Contract Name:
PaintSwapLibrary

Contract Source Code:

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (token/ERC1155/IERC1155.sol)

pragma solidity ^0.8.20;

import {IERC165} from "../../utils/introspection/IERC165.sol";

/**
 * @dev Required interface of an ERC-1155 compliant contract, as defined in the
 * https://eips.ethereum.org/EIPS/eip-1155[ERC].
 */
interface IERC1155 is IERC165 {
    /**
     * @dev Emitted when `value` amount of tokens of 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 value of tokens of token type `id` owned by `account`.
     */
    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 zero address.
     */
    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 a `value` amount of tokens of type `id` from `from` to `to`.
     *
     * WARNING: This function can potentially allow a reentrancy attack when transferring tokens
     * to an untrusted contract, when invoking {onERC1155Received} on the receiver.
     * Ensure to follow the checks-effects-interactions pattern and consider employing
     * reentrancy guards when interacting with untrusted contracts.
     *
     * Emits a {TransferSingle} event.
     *
     * Requirements:
     *
     * - `to` cannot be the zero address.
     * - If the caller is not `from`, it must have been approved to spend ``from``'s tokens via {setApprovalForAll}.
     * - `from` must have a balance of tokens of type `id` of at least `value` 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 value, bytes calldata data) external;

    /**
     * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {safeTransferFrom}.
     *
     * WARNING: This function can potentially allow a reentrancy attack when transferring tokens
     * to an untrusted contract, when invoking {onERC1155BatchReceived} on the receiver.
     * Ensure to follow the checks-effects-interactions pattern and consider employing
     * reentrancy guards when interacting with untrusted contracts.
     *
     * Emits either a {TransferSingle} or a {TransferBatch} event, depending on the length of the array arguments.
     *
     * Requirements:
     *
     * - `ids` and `values` 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 values,
        bytes calldata data
    ) external;
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (token/ERC20/IERC20.sol)

pragma solidity ^0.8.20;

/**
 * @dev Interface of the ERC-20 standard as defined in the ERC.
 */
interface IERC20 {
    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);

    /**
     * @dev Returns the value of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the value of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves a `value` amount of tokens from the caller's account to `to`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address to, uint256 value) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets a `value` amount of tokens as the allowance of `spender` over the
     * caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 value) external returns (bool);

    /**
     * @dev Moves a `value` amount of tokens from `from` to `to` using the
     * allowance mechanism. `value` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(address from, address to, uint256 value) external returns (bool);
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (token/ERC721/IERC721.sol)

pragma solidity ^0.8.20;

import {IERC165} from "../../utils/introspection/IERC165.sol";

/**
 * @dev Required interface of an ERC-721 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`.
     *
     * 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;

    /**
     * @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
     * are aware of the ERC-721 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 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: Note that the caller is responsible to confirm that the recipient is capable of receiving ERC-721
     * or else they may be permanently lost. Usage of {safeTransferFrom} prevents loss, though the caller must
     * understand this adds an external call which potentially creates a reentrancy vulnerability.
     *
     * 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 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 address zero.
     *
     * Emits an {ApprovalForAll} event.
     */
    function setApprovalForAll(address operator, bool approved) external;

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

    /**
     * @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);
}

// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/introspection/IERC165.sol)

pragma solidity ^0.8.20;

/**
 * @dev Interface of the ERC-165 standard, as defined in the
 * https://eips.ethereum.org/EIPS/eip-165[ERC].
 *
 * 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[ERC 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);
}

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

struct NFT {
  address nft;
  uint256 tokenId;
}

enum PaymentFailureMode {
  ALLOW_FAILURE,
  NOT_ALLOWED
}

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

import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";

interface IERC2981 is IERC165 {
  /// @notice Called with the sale price to determine how much royalty
  //          is owed and to whom.
  /// @param tokenId - the NFT asset queried for royalty information
  /// @param salePrice - the sale price of the NFT asset specified by _tokenId
  /// @return receiver - address of who should be sent the royalty payment
  /// @return royaltyAmount - the royalty payment amount for _salePrice
  function royaltyInfo(
    uint256 tokenId,
    uint256 salePrice
  ) external view returns (address receiver, uint256 royaltyAmount);
}

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

import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";

interface IWrappedToken is IERC20 {
  function deposit() external payable;

  function withdraw(uint256 amount) external;
}

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

import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import {IERC1155} from "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {IWrappedToken} from "./interfaces/IWrappedToken.sol";
import {IERC165, IERC2981} from "./interfaces/IERC2981.sol";
import {PaymentFailureMode} from "./globals.sol";

library PaintSwapLibrary {
  error InvalidNFT();
  error InvalidERCType();
  error InvalidAmountForERC721();
  error InvalidPrice();
  error SellingNotEnabled();
  error InsufficientBalance();
  error NotOwner();
  error NotApproved(address operator);
  error InsufficientValue();
  error CoinTransferFailed(address to);

  // Pre-condition is that the NFTS are either ERC721 or ERC1155
  function safeNFTTransferFrom(address nft, uint256 tokenId, uint256 amount, address from, address to) external {
    if (isERC1155(nft)) {
      IERC1155(nft).safeTransferFrom(from, to, tokenId, amount, "");
    } else {
      IERC721(nft).safeTransferFrom(from, to, tokenId);
    }
  }

  function checkBalance(address nft, uint256 tokenId, uint256 amount, address owner) external view {
    // Check that owner has these nfts and approved us
    if (isERC1155(nft)) {
      require(IERC1155(nft).balanceOf(owner, tokenId) >= amount, InsufficientBalance());
    } else {
      require(IERC721(nft).ownerOf(tokenId) == owner, NotOwner());
    }
  }

  function checkBalanceAndApproval(
    address nft,
    uint256 tokenId,
    uint256 amount,
    address owner,
    address operator
  ) external view {
    // Check that owner has these nfts and approved us
    if (isERC1155(nft)) {
      require(IERC1155(nft).balanceOf(owner, tokenId) >= amount, InsufficientBalance());
      require(IERC1155(nft).isApprovedForAll(owner, operator), NotApproved(operator));
    } else {
      require(IERC721(nft).ownerOf(tokenId) == owner, NotOwner());
      require(
        IERC721(nft).isApprovedForAll(owner, operator) || IERC721(nft).getApproved(tokenId) == operator,
        NotApproved(operator)
      );
    }
  }

  function safeTransferFromUs(
    address token,
    address receiver,
    uint256 amount,
    address wNative,
    PaymentFailureMode _paymentFailureMode
  ) external returns (bool success) {
    if (tokenIsNativeFtm(token)) {
      // Do an FTM transfer
      uint256 balance = address(this).balance;
      uint256 amountToSend = balance >= amount ? amount : balance;
      (success, ) = receiver.call{value: amountToSend}("");
      if (_paymentFailureMode == PaymentFailureMode.ALLOW_FAILURE && !success) {
        // Send them wrapped native instead
        IWrappedToken(wNative).deposit{value: amountToSend}();
        IERC20(wNative).transfer(receiver, amountToSend);
        success = true;
      }
    } else {
      uint256 balance = IERC20(token).balanceOf(address(this));
      uint256 amountToSend = balance >= amount ? amount : balance;
      IERC20(token).transfer(receiver, amountToSend);
      success = true;
    }
  }

  function checkAddListing(
    address nft,
    uint96 price,
    uint256 amount,
    bool isSellingEnabled,
    uint96 maxNative
  ) external view {
    require(nft != address(0), InvalidNFT());
    uint256 ercType = getNFTERCType(nft);
    require(ercType == 1155 || ercType == 721, InvalidERCType());
    require((ercType == 1155 && amount != 0) || amount == 1, InvalidAmountForERC721());
    require(price != 0 && price < maxNative, InvalidPrice());
    require(isSellingEnabled, SellingNotEnabled());
  }

  function isFinished(uint256 endTime) public view returns (bool) {
    return endTime <= block.timestamp;
  }

  function nextMinimumBid(uint96 price, uint96 highestBid, uint96 percentIncrease) public pure returns (uint96) {
    if (highestBid == 0) {
      return price;
    } else {
      return _nextMinimumBid(highestBid, percentIncrease);
    }
  }

  function _nextMinimumBid(uint96 highestBid, uint96 percentIncrease) private pure returns (uint96) {
    uint96 newMin = (highestBid + (highestBid * percentIncrease) / 10000);
    if (newMin == highestBid) {
      return highestBid + 1; // Increase by 1
    }
    return newMin;
  }

  // The zero address is used to show that the token is native fantom
  function tokenIsNativeFtm(address token) public pure returns (bool) {
    return token == address(0);
  }

  // An NFT contract could be malicious and prevent a sale finishing. Try send NFT to our backup address,
  // if possible, otherwise it just gets lost forever.
  function tryRoyaltyInfo(
    address nft,
    uint256 tokenId,
    uint256 salePrice
  ) external view returns (address receiver, uint256 royaltyAmount) {
    try IERC2981(nft).royaltyInfo(tokenId, salePrice) returns (address _receiver, uint256 _royaltyAmount) {
      return (_receiver, _royaltyAmount);
    } catch {}

    return (address(0), 0);
  }

  function isValidNFTContract(address nft) public view returns (bool) {
    return isERC1155(nft) || isERC721(nft);
  }

  function getNFTERCType(address nft) public view returns (uint256) {
    if (isERC1155(nft)) {
      return 1155;
    } else if (isERC721(nft)) {
      return 721;
    } else {
      return 0;
    }
  }

  function isERC721(address nft) public view returns (bool) {
    try IERC165(nft).supportsInterface(type(IERC721).interfaceId) returns (bool supportsERC721) {
      return supportsERC721;
    } catch {}

    return false;
  }

  function isERC1155(address nft) public view returns (bool) {
    try IERC165(nft).supportsInterface(type(IERC1155).interfaceId) returns (bool supportsERC1155) {
      return supportsERC1155;
    } catch {}

    return false;
  }

  function wrapNative(address wNative) external {
    IWrappedToken(wNative).deposit{value: msg.value}();
    IERC20(wNative).transfer(msg.sender, msg.value);
  }

  function addListingTransferFee(uint96 listingFee, address devFeeAddress) external {
    if (listingFee != 0) {
      // If paying with ftm
      require(msg.value >= listingFee, InsufficientValue());
      (bool sent, ) = devFeeAddress.call{value: listingFee}("");
      require(sent, CoinTransferFailed(devFeeAddress));

      // Refund any excess FTM
      if (msg.value > listingFee) {
        (bool sent1, ) = msg.sender.call{value: msg.value - listingFee}("");
        require(sent1, CoinTransferFailed(msg.sender));
      }
    }
  }
}

Please enter a contract address above to load the contract details and source code.

Context size (optional):