Skip to content

Instantly share code, notes, and snippets.

@laohanme
Created February 28, 2022 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laohanme/e148b6589ca99734fa0cd73d32123405 to your computer and use it in GitHub Desktop.
Save laohanme/e148b6589ca99734fa0cd73d32123405 to your computer and use it in GitHub Desktop.
dino_meta_nft.sol
// Contract based on https://docs.openzeppelin.com/contracts/3.x/erc721
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
contract DinoClub is ERC721Enumerable, Ownable {
using Strings for uint256;
bool public _isSaleActive = false;
bool public _revealed = false;
// Constants
uint256 public constant MAX_SUPPLY = 10000;
uint256 public mintPrice = 0.001 ether;
uint256 public maxBalance = 100;
uint256 public maxMint = 1;
string baseURI;
string public notRevealedUri;
string public baseExtension = ".json";
mapping(uint256 => string) private _tokenURIs;
constructor(string memory initBaseURI, string memory initNotRevealedUri)
ERC721("DinoClub", "Dino")
{
setBaseURI(initBaseURI);
setNotRevealedURI(initNotRevealedUri);
}
function mintDinoClub(uint256 tokenQuantity) public payable {
require(
totalSupply() + tokenQuantity <= MAX_SUPPLY,
"Sale would exceed max supply"
);
require(_isSaleActive, "Sale must be active to mint DinoClub");
require(
balanceOf(msg.sender) + tokenQuantity <= maxBalance,
"Sale would exceed max balance"
);
require(
tokenQuantity * mintPrice <= msg.value,
"Not enough ether sent"
);
require(tokenQuantity <= maxMint, "Can only mint 1 tokens at a time");
_mintDinoClub(tokenQuantity);
}
function _mintDinoClub(uint256 tokenQuantity) internal {
for (uint256 i = 0; i < tokenQuantity; i++) {
uint256 mintIndex = totalSupply();
if (totalSupply() < MAX_SUPPLY) {
_safeMint(msg.sender, mintIndex);
}
}
}
function tokenURI(uint256 tokenId)
public
view
virtual
override
returns (string memory)
{
require(
_exists(tokenId),
"ERC721Metadata: URI query for nonexistent token"
);
if (_revealed == false) {
return notRevealedUri;
}
string memory _tokenURI = _tokenURIs[tokenId];
string memory base = _baseURI();
// If there is no base URI, return the token URI.
if (bytes(base).length == 0) {
return _tokenURI;
}
// If both are set, concatenate the baseURI and tokenURI (via abi.encodePacked).
if (bytes(_tokenURI).length > 0) {
return string(abi.encodePacked(base, _tokenURI));
}
// If there is a baseURI but no tokenURI, concatenate the tokenID to the baseURI.
return
string(abi.encodePacked(base, tokenId.toString(), baseExtension));
}
// internal
function _baseURI() internal view virtual override returns (string memory) {
return baseURI;
}
//only owner
function flipSaleActive() public onlyOwner {
_isSaleActive = !_isSaleActive;
}
function flipReveal() public onlyOwner {
_revealed = !_revealed;
}
function setMintPrice(uint256 _mintPrice) public onlyOwner {
mintPrice = _mintPrice;
}
function setNotRevealedURI(string memory _notRevealedURI) public onlyOwner {
notRevealedUri = _notRevealedURI;
}
function setBaseURI(string memory _newBaseURI) public onlyOwner {
baseURI = _newBaseURI;
}
function setBaseExtension(string memory _newBaseExtension)
public
onlyOwner
{
baseExtension = _newBaseExtension;
}
function setMaxBalance(uint256 _maxBalance) public onlyOwner {
maxBalance = _maxBalance;
}
function setMaxMint(uint256 _maxMint) public onlyOwner {
maxMint = _maxMint;
}
function withdraw(address to) public onlyOwner {
uint256 balance = address(this).balance;
payable(to).transfer(balance);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment