Skip to content

Instantly share code, notes, and snippets.

@wrightkhlebisol
Created January 28, 2022 08:12
Show Gist options
  • Save wrightkhlebisol/d41829d40dff22b07bb4f253655711bd to your computer and use it in GitHub Desktop.
Save wrightkhlebisol/d41829d40dff22b07bb4f253655711bd to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.0+commit.c7dfd78e.js&optimize=true&runs=100000&gist=
// SPDX-License-Identifier: Unlicensed
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/utils/introspection/ERC165Checker.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
/// @title Contract to check that a given address is an NFT
/// @notice Returns integer type representing the position of the returned value from the tokenType enum
library CheckInterface {
using ERC165Checker for address;
enum tokenTypes {
NULL,
ERC1155,
ERC721
}
///
bytes4 constant Interface_ERC721 = type(IERC721).interfaceId;
bytes4 constant Interface_ERC1155 = type(IERC1155).interfaceId;
/// @notice Check token type
/// @dev Takes the token address and checks the type using the ERC165 refelection standards
/// @param _token The address of the NFT
/// @return _tokenType Specifies the type of token
function checkTokenType(address _token)
external
view
returns (uint8 _tokenType)
{
if (_token.supportsInterface(Interface_ERC1155)) {
return uint8(tokenTypes.ERC1155);
} else if (_token.supportsInterface(Interface_ERC721)) {
return uint8(tokenTypes.ERC721);
} else {
return uint8(tokenTypes.NULL);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment