Skip to content

Instantly share code, notes, and snippets.

@CodingFu
Created October 6, 2022 19:42
Show Gist options
  • Save CodingFu/a29992b3d8ecb5716ef10bce3f65f7ca to your computer and use it in GitHub Desktop.
Save CodingFu/a29992b3d8ecb5716ef10bce3f65f7ca to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
contract XNFT is ERC721URIStorage, Ownable {
using Strings for uint256;
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
// STORAGE OF NFT LEVELS
mapping(uint256 => uint8) public tokenIdToLevels;
uint8 MAX_LEVEL = 5;
string[6] LEVEL_NAMES = [
"Limbo",
"Adept",
"Expert",
"Master",
"Supreme",
"Xtoker"
];
uint256[6] MAX_SUPPLIES = [3000, 1000, 500, 200, 100, 50];
uint256[6] MINT_LEVEL_PRICES_WEI = [
1 * 10**18,
2 * 10**18,
3 * 10**18,
4 * 10**18,
5 * 10**18,
6 * 10**18
];
// MINT
function mint(uint8 _level) public {
require(_level <= MAX_LEVEL, "wrong level");
uint256 mintPriceWei = MINT_LEVEL_PRICES_WEI[_level];
require(msg.value >= mintPriceWei, "not enough ether to mint");
uint256 newTokenId = _tokenIds.current();
_mint(msg.sender, newTokenId);
// saving the level of the token just minted
tokenIdToLevels[newTokenId] = _level;
}
// ADMIN
function setMintPrice(uint8 _nftLevel, uint256 _newPriceWei)
public
onlyOwner
{
require(_nftLevel <= MAX_LEVEL, "invalid level");
MINT_LEVEL_PRICES_WEI[_nftLevel] = _newPriceWei;
}
function setMaxSupply(uint8 _nftLevel, uint256 _newMaxSupply)
public
onlyOwner
{
require(_nftLevel <= MAX_LEVEL, "invalid level");
require(
_newMaxSupply > MAX_SUPPLIES[_nftLevel],
"max supply should be more than the current max supply"
);
MAX_SUPPLIES[_nftLevel] = _newMaxSupply;
}
// CONSTRUCTOR
constructor() ERC721("XNFT", "XPASS") {}
// VIEWS
function getLevel(uint256 _tokenId) public view returns (uint8) {
uint8 nftLevel = tokenIdToLevels[_tokenId];
return nftLevel;
}
function getLevelName(uint8 _level) public view returns (string memory) {
return LEVEL_NAMES[_level];
}
function getMaxSupply() public view returns (uint256) {
return
MAX_SUPPLIES[0] +
MAX_SUPPLIES[1] +
MAX_SUPPLIES[2] +
MAX_SUPPLIES[3] +
MAX_SUPPLIES[4] +
MAX_SUPPLIES[5];
}
// TODO: I think we can use this function to get metadata for the pass
function ___getTokenURI(uint256 _tokenId)
public
view
returns (string memory)
{
uint8 level = getLevel(_tokenId);
string memory levelName = getLevelName(level);
bytes memory metaData = abi.encodePacked(
"{",
'"name": "XPASS ',
levelName,
" #",
_tokenId.toString(),
'",',
'"description": "Xtok Pass"',
'"NFT LEVEL":',
level.toString(),
",",
"}"
);
return string(metaData);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment