Last active
January 12, 2022 08:55
-
-
Save niclin/af52ec175b8fc19dcb860cc891bebae1 to your computer and use it in GitHub Desktop.
五星上將
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity >=0.7.0 <0.9.0; | |
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol"; | |
import "@openzeppelin/contracts/access/Ownable.sol"; | |
import "@openzeppelin/contracts/utils/math/SafeMath.sol"; | |
import "@openzeppelin/contracts/utils/Strings.sol"; | |
contract FiveStarGeneral is ERC721Enumerable, Ownable { | |
using SafeMath for uint256; | |
using Strings for uint256; | |
uint public constant maxGeneralPurchase = 3; | |
uint256 public MAX_GENERALS; | |
bool public saleIsActive = false; | |
string baseURI; | |
string public baseExtension = ".json"; | |
uint256 public generalPrice = 30000000000000000; //0.003 ETH | |
bool public paused = false; | |
bool public revealed = false; | |
string public notRevealedUri; | |
constructor(string memory name, string memory symbol, uint256 maxNftSupply) ERC721(name, symbol) { | |
MAX_GENERALS = maxNftSupply; | |
} | |
function mul(uint256 a, uint256 b) internal pure returns (uint256) { | |
if (a == 0) return 0; | |
uint256 c = a * b; | |
require(c / a == b, "SafeMath: multiplication overflow"); | |
return c; | |
} | |
// public | |
function mintGeneral(uint numberOfTokens) public payable { | |
require(saleIsActive, "Sale must be active to mint General"); | |
require(numberOfTokens <= maxGeneralPurchase, "Can only mint 3 tokens at a time"); | |
require(totalSupply() + (numberOfTokens) <= MAX_GENERALS, "Purchase would exceed max supply of Generals"); | |
require(generalPrice.mul(numberOfTokens) <= msg.value, "Ether value sent is not correct"); | |
for(uint i = 0; i < numberOfTokens; i++) { | |
uint mintIndex = totalSupply(); | |
if (totalSupply() < MAX_GENERALS) { | |
_safeMint(msg.sender, mintIndex); | |
} | |
} | |
} | |
function walletOfOwner(address _owner) | |
public | |
view | |
returns (uint256[] memory) | |
{ | |
uint256 ownerTokenCount = balanceOf(_owner); | |
uint256[] memory tokenIds = new uint256[](ownerTokenCount); | |
for (uint256 i; i < ownerTokenCount; i++) { | |
tokenIds[i] = tokenOfOwnerByIndex(_owner, i); | |
} | |
return tokenIds; | |
} | |
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 currentBaseURI = _baseURI(); | |
return bytes(currentBaseURI).length > 0 | |
? string(abi.encodePacked(currentBaseURI, tokenId.toString(), baseExtension)) | |
: ""; | |
} | |
//only owner | |
function flipSaleState() public onlyOwner { | |
saleIsActive = !saleIsActive; | |
} | |
function reveal() public onlyOwner { | |
revealed = true; | |
} | |
function setGeneralPrice(uint256 _newGeneralPrice) public onlyOwner { | |
generalPrice = _newGeneralPrice; | |
} | |
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 pause(bool _state) public onlyOwner { | |
paused = _state; | |
} | |
function withdraw() public payable onlyOwner { | |
// Do not remove this otherwise you will not be able to withdraw the funds. | |
// ============================================================================= | |
(bool os, ) = payable(owner()).call{value: address(this).balance}(""); | |
require(os); | |
// ============================================================================= | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment