当前位置:网站首页>NFT smart contract release, blind box, public offering technology practice -- contract

NFT smart contract release, blind box, public offering technology practice -- contract

2022-07-06 07:57:00 NFT practitioner

This article mainly introduces you from the perspective of Technology nft Mainstream gameplay - issue , Blind box , Public offering and other process steps , This is also the way most projects in the market play .

This article nft Development is mainly divided into two parts : 1. The contract part 2. Jigsaw puzzle part .

This time Nft I'm also going to explain the Development Series in two articles , Today we will mainly talk about the contract .

   The contract part mainly includes the following functions :
  • Establish and eth Test the smart contract of network interaction

  • Nft To be able to be mint

  • Nft Be able to set the total amount

  • Nft Set the maximum holding of each address

  • Nft Be able to limit single mint The amount of

  • Nft Be able to set the switch to go public

    The puzzle part mainly includes the following functions :

  • How to quickly make a variety of puzzles and meta Information

  • How to upload ipfs Interstellar network system ( Test net )

    These are the technical points that need to be explained in this article .

Before development , You need to prepare the following tools and environments .

  1. metamask Wallet plug-in

    The crypto wallet for Defi, Web3 Dapps and NFTs | MetaMask

  2. remix Online compilation environment

    Remix - Ethereum IDE

Specific wallet installation and remix Use , No more explanation here .

The contract part

First, paste the source code of this contract directly in remix To compile . Source code is as follows :

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
   
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
   
contract NftMeta is ERC721Enumerable, Ownable {
    using Strings for uint256;
   
    //  Whether to permit nft Sell - switch 
    bool public _isSaleActive = false;
    //  Initialize the blind box , Wait until a certain time to open the box randomly , become true
    bool public _revealed = false;
   
    // nft Total quantity 
    uint256 public constant MAX_SUPPLY = 10;
    //  Casting Nft The price of 
    uint256 public mintPrice = 0.3 ether;
    //  There can only be one purse cast nft Number 
    uint256 public maxBalance = 1;
    //  once mint Of nft The number of 
    uint256 public maxMint = 1;
   
    //  After the blind box switch is turned on , Need to display the picture of unpacking base Address 
    string baseURI;
    //  Blind box pictures meta,json Address , It will be mentioned later 
    string public notRevealedUri;
    //  Extension type of default address 
    string public baseExtension = ".json";
   
    mapping(uint256 => string) private _tokenURIs;
   
    //  Constructors 
    constructor(string memory initBaseURI, string memory initNotRevealedUri)
        ERC721("Nft Meta", "NM") //  Realized ERC721 The parent constructor of , Is an implementation of subclass inheritance 
    {
        setBaseURI(initBaseURI);
        setNotRevealedURI(initNotRevealedUri);
    } 
    //  Cast at an external address nft Function call for 
    function mintNftMeta(uint256 tokenQuantity) public payable {
        //  Verify the total supply + Quantity per casting <= nft Total quantity 
        require(
            totalSupply() + tokenQuantity <= MAX_SUPPLY,
            "Sale would exceed max supply"
        );
        //  Verify whether the opening status is enabled 
        require(_isSaleActive, "Sale must be active to mint NicMetas");
        //  Verify... In the address of the cast wallet nft The number of  +  The quantity of this casting  <=  The biggest possession of this wallet nft The number of 
        require( 
            balanceOf(msg.sender) + tokenQuantity <= maxBalance,
            "Sale would exceed max balance"
        );
        //  Check the quantity of this casting * The price of casting  <=  Attached to this message eth The number of 
        require(
            tokenQuantity * mintPrice <= msg.value,
            "Not enough ether sent"
        );
        //  Check the quantity of this casting  <=  The maximum quantity cast this time 
        require(tokenQuantity <= maxMint, "Can only mint 1 tokens at a time");
        //  The above verification conditions are met , Conduct nft The casting of 
        _mintNftMeta(tokenQuantity);
    }
   
    //  Casting 
    function _mintNftMeta(uint256 tokenQuantity) internal {
        for (uint256 i = 0; i < tokenQuantity; i++) {
            // mintIndex It's casting nft The serial number of , According to the total supply from 0 Start accumulating 
            uint256 mintIndex = totalSupply();
            if (totalSupply() < MAX_SUPPLY) {
                //  call erc721 Safe casting method of 
                _safeMint(msg.sender, mintIndex);
            }
        }
    }
   
    //  Return each nft Address of the Uri, It contains nft The whole message , Including the name , describe , Properties, etc 
    function tokenURI(uint256 tokenId)
        public
        view
        virtual
        override
        returns (string memory)
    {
        require(
            _exists(tokenId),
            "ERC721Metadata: URI query for nonexistent token"
        );
   
        //  The blind box has not been opened , Then the default is a black background picture or other pictures 
        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);
    }
}

The above is the contract source code .

  Yes erc721 The extension part and permission part of , Inherit at the same time , rewrite erc721 Part of the way , As we'll see .

These state variables are mainly used for Nft Set some properties of , Including the total amount , Each address has a maximum of mint The quantity of .

The above is the realization of the casting method , Check the conditions before casting , And then call erc721 Of _safemint Cast in a safe way .

Deploy the contract code , There are several points to note

At the same time, switch the test network to Rinkeby Test net , At the same time, it needs to be passed in advance faucet Receive the test currency . Receiving address :

Faucets | ChainlinkGet testnet LINK for an account on one of the supported blockchain testnets so you can create and test your own oracle and Chainlinked smart contract.https://faucets.chain.link/FaucETHicon-default.png?t=M5H6https://fauceth.komputing.org/

Rinkeby: Authenticated Fauceticon-default.png?t=M5H6https://faucet.rinkeby.io/ After deployment , There will be

In us mint Before , It is necessary to turn on the casting switch ( In the red frame ), Click to change the status variable to true.

The red function is the casting method , Fill in quantity 1, At the same time, because we set the minimum in the state variable of the contract Mint The price of is 0.3ether, So we need to be in msg.value With ether Quantity to complete mint.

Because it is not allowed to enter decimals in the red box , therefore , adopt ether The website performs fee unit conversion

Ethereum Unit Converter | Ether to Gwei, Wei, Finney, Szabo, Shannon etc.icon-default.png?t=M5H6https://eth-converter.com/

 

  take 0.3ether Input , To get Gwei The number of , Assign this quantity to the red box above mint. After success , Will be in ipfs The test network sees a card numbered 0 Pictures of the .ipfs Test network address :

thus , We nft The contract part under development has been completed . 

原网站

版权声明
本文为[NFT practitioner]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060753171243.html