nft合约

// Author: hanpeng  (fergus_hinn:[email protected]) 
// Contract based on https://docs.openzeppelin.com/contracts/5.x/erc721
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
// import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
import "@openzeppelin/contracts/utils/Counters.sol";

contract FuGu is ERC721Enumerable,ReentrancyGuard{
    using Strings for uint256;
    address Owner;
    using Counters for Counters.Counter;
    Counters.Counter private _tokenIdCounter;

    bool public _isSaleActive = false;
    bool public _revealed = false;

    // Constants
    uint256 public constant MAX_SUPPLY = 10000;
    uint256 public mintPrice = 0.0001 ether;
    uint256 public maxBalance = 10;
    uint256 public maxMint = 1;

    string baseURI;
    string public notRevealedUri;
    string public baseURISuffix = ".json";

    mapping(uint256 => string) private _tokenURIs;

    constructor() ERC721("FuGu", "FuGu") {
          Owner = msg.sender;
         _tokenIdCounter.increment(); // Making sure we start at token ID 1
    
    }

//     constructor(address ownerAddr) ERC721("FuGu", "FuGu") Ownable(ownerAddr) {
//     _tokenIdCounter.increment();  // Making sure we start at token ID 1
// }
//     constructor() ERC721("FuGu", "FuGu")  {
//     _tokenIdCounter.increment();  // Making sure we start at token ID 1
// }
    function mint(uint256 tokenNum) public payable nonReentrant{
        require(
            totalSupply() + tokenNum <= MAX_SUPPLY,
            "Sale would exceed max supply"
        );
        require(_isSaleActive, "Sale must be active to mint FANs");
        require(
            balanceOf(msg.sender) + tokenNum <= maxBalance,
            "Sale would exceed max balance"
        );
        require(
            tokenNum * mintPrice <= msg.value,
            "Not enough ether sent"
        );
        require(tokenNum <= maxMint, "Can only mint 1 tokens at a time");

        _mintFuGu(tokenNum);
    }

    function _mintFuGu(uint256 tokenQuantity) internal {
        // for (uint256 i = 0; i < tokenQuantity; i++) {
        //     uint256 mintIndex = totalSupply();
        //     if (totalSupply() < MAX_SUPPLY) {
        //         _safeMint(msg.sender, mintIndex);
        //     }
        // }
        uint256 tokenId = _tokenIdCounter.current();
        _safeMint(msg.sender, tokenId);
        // string memory _uri = string.concat(tokenId,baseURISuffix);
    //      string memory IndexString = string.toString(tokenId);
    //     string memory Prefix = "https://gate3.s3.ap-northeast-1.amazonaws.com/gate3winner/";
    //     string memory Suffix = ".json";
    //    string memory tokenURI = string.concat(Prefix,IndexString,Suffix);
        // _setTokenURI(tokenId, _setURI(tokenId));
        _tokenIdCounter.increment();
    }
    //  function _setURI(uint256 _tokenId) internal pure returns(string memory uri){
    //     uri = string(abi.encodePacked(_tokenId.toString(),".json"));
    // }
    // function tokenURI(uint256 tokenId)
    //     public
    //     view
    //     override(ERC721, ERC721URIStorage)
    //     returns (string memory)
    // {
    //     return super.tokenURI(tokenId);
    // }
    // function _getTokenURI(uint256 tokenIndex) internal pure returns(string memory) {
    //     string memory IndexString = string.toString(tokenIndex);
    //     string memory Prefix = "https://gate3.s3.ap-northeast-1.amazonaws.com/gate3winner/";
    //     string memory Suffix = ".json";
    //    string memory tokenURI = string.concat(Prefix,IndexString,Suffix);
    //    return tokenURI;
    // }
    // function tokenURI(uint256 tokenId) public view override(ERC721, ERC721URIStorage) returns (string memory) {
    //     return super.tokenURI(tokenId);
    // }

    /**
     * @dev See {IERC721Metadata-tokenURI}.
     */
    // function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
    //     _requireOwned(tokenId);

    //     string memory baseURI = _baseURI();
    //     return bytes(baseURI).length > 0 ? string.concat(baseURI, tokenId.toString()) : "";
    // }
    // function tokenURI(uint256 tokenId) public view override(ERC721, ERC721URIStorage) returns (string memory) {
    //     return super.tokenURI(tokenId);
    // }
    // 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(), baseURISuffix));
    // }

    // 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 setBaseURISuffix (string memory _newBaseURISuffix)
        public
        onlyOwner
    {
        baseURISuffix = _newBaseURISuffix;
    }

    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);
    }
    //     function supportsInterface(bytes4 interfaceId) public view override(ERC721, ERC721Enumerable) returns (bool) {
    //     return super.supportsInterface(interfaceId);
    // }
    modifier onlyOwner() {
      require(msg.sender == Owner);
       _;
     }
}

你可能感兴趣的:(区块链)