Skip to content

Instantly share code, notes, and snippets.

@indiejoseph
Created March 21, 2022 14:23
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 indiejoseph/3bcdc138553f3bb1753d1c37fc29b22d to your computer and use it in GitHub Desktop.
Save indiejoseph/3bcdc138553f3bb1753d1c37fc29b22d to your computer and use it in GitHub Desktop.
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "hardhat/console.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/utils/Context.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Burnable.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Pausable.sol";
contract WEARKey is ERC721, ERC721Burnable, ERC721Pausable, Ownable {
using Counters for Counters.Counter;
string public _baseTokenURI;
Counters.Counter private _tokenIdTracker;
bool public sale = false;
uint256 public constant MAX_ITEMS = 1000;
uint256 public constant MAX_MINT = 15;
uint256 public basePrice = 0.35 ether;
uint256 public preSalePrice = 0.15 ether;
//mapping for whilelist
mapping(address => bool) public whiteListedAddresses;
modifier onlySaleOpenOrWhitelisted() {
bool saleIsOpen = sale != false;
bool isWhitelisted = whiteListedAddresses[msg.sender] == true;
require(totalSupply() <= MAX_ITEMS, "Sold out");
require(saleIsOpen || isWhitelisted, "Public sale is not started yet");
if (_msgSender() != owner()) {
require(!paused(), "Pausable: paused");
}
_;
}
constructor(string memory baseTokenURI_) ERC721("WEARKey", "WEARKEY") {
_baseTokenURI = baseTokenURI_;
addAddressToWhiteList(msg.sender);
}
function totalMint() public view returns (uint256) {
return totalSupply();
}
function mintBatch(address _to, uint256 _count) public payable onlySaleOpenOrWhitelisted {
uint256 total = totalSupply();
require(_count <= MAX_MINT, "Exceeds number");
require(total + _count <= MAX_ITEMS, "Max limit");
require(msg.value >= price(_count), "Value below price");
for (uint256 i = 0; i < _count; i++) {
_tokenIdTracker.increment();
_mint(_to, _tokenIdTracker.current());
}
}
function mint(address _to) public payable onlySaleOpenOrWhitelisted {
uint256 mintIndex = totalSupply() + 1;
require(mintIndex <= MAX_ITEMS, "Max limit");
require(msg.value >= price(1), "Value below price");
_tokenIdTracker.increment();
_mint(_to, _tokenIdTracker.current());
}
function price(uint256 _count) public view returns (uint256) {
if (sale == false) {
// pre-sale price
return preSalePrice * _count;
}
return basePrice * _count;
}
function setBaseURI(string memory baseURI) public onlyOwner {
_baseTokenURI = baseURI;
}
// https://docs.opensea.io/docs/1-structuring-your-smart-contract#creature-erc721-contract
function baseTokenURI() public view returns (string memory) {
return _baseTokenURI;
}
// internal
function _baseURI() internal view virtual override returns (string memory) {
return _baseTokenURI;
}
function pause(bool val) public onlyOwner {
if (val == true) {
_pause();
return;
}
_unpause();
}
function toggleSale() public onlyOwner {
sale = !sale;
}
function addAddressToWhiteList(address _whiteListedAddresses) public onlyOwner {
require(
whiteListedAddresses[_whiteListedAddresses] == false,
"this address is already white listed"
);
whiteListedAddresses[_whiteListedAddresses] = true;
}
function totalSupply() public view returns (uint256) {
return _tokenIdTracker.current();
}
// widthdraw fund from the contract
function withdraw() public onlyOwner {
require(address(this).balance > 0, "Balance is 0");
payable(owner()).transfer(address(this).balance);
}
function _beforeTokenTransfer(
address from,
address to,
uint256 tokenId
) internal virtual override(ERC721, ERC721Pausable) {
super._beforeTokenTransfer(from, to, tokenId);
}
function supportsInterface(bytes4 interfaceId)
public
view
virtual
override(ERC721)
returns (bool)
{
return super.supportsInterface(interfaceId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment