当前位置:网站首页>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 .
metamask Wallet plug-in
remix Online compilation environment
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/FaucETHhttps://fauceth.komputing.org/
Rinkeby: Authenticated Faucethttps://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.https://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 .
边栏推荐
- Google可能在春节后回归中国市场。
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- Asia Pacific Financial Media | designer universe | Guangdong responds to the opinions of the national development and Reform Commission. Primary school students incarnate as small community designers
- "Designer universe" APEC design +: the list of winners of the Paris Design Award in France was recently announced. The winners of "Changsha world center Damei mansion" were awarded by the national eco
- Go learning notes (3) basic types and statements (2)
- In the era of digital economy, how to ensure security?
- How to estimate the number of threads
- opencv学习笔记八--答题卡识别
- Binary tree creation & traversal
- Secure captcha (unsafe verification code) of DVWA range
猜你喜欢
Epoll and IO multiplexing of redis
[computer skills]
It's hard to find a job when the industry is in recession
Data governance: 3 characteristics, 4 transcendence and 3 28 principles of master data
Google可能在春节后回归中国市场。
Simulation of holographic interferogram and phase reconstruction of Fourier transform based on MATLAB
链表面试题(图文详解)
Artcube information of "designer universe": Guangzhou implements the community designer system to achieve "great improvement" of urban quality | national economic and Information Center
Leetcode question brushing record | 203_ Remove linked list elements
"Friendship and righteousness" of the center for national economy and information technology: China's friendship wine - the "unparalleled loyalty and righteousness" of the solidarity group released th
随机推荐
珠海金山面试复盘
Oracle time display adjustment
【T31ZL智能视频应用处理器资料】
Luogu p1836 number page solution
649. Dota2 Senate
"Designer universe" Guangdong responds to the opinions of the national development and Reform Commission. Primary school students incarnate as small community designers | national economic and Informa
Rust language - receive command line parameter instances
Wireshark grabs packets to understand its word TCP segment
"Designer universe" APEC design +: the list of winners of the Paris Design Award in France was recently announced. The winners of "Changsha world center Damei mansion" were awarded by the national eco
Transformer principle and code elaboration
【Redis】NoSQL数据库和redis简介
Is the super browser a fingerprint browser? How to choose a good super browser?
[factorial inverse], [linear inverse], [combinatorial counting] Niu Mei's mathematical problems
[redis] Introduction to NoSQL database and redis
"Designer universe": "benefit dimension" APEC public welfare + 2022 the latest slogan and the new platform will be launched soon | Asia Pacific Financial Media
数据治理:微服务架构下的数据治理
HTTP cache, forced cache, negotiated cache
Learn Arduino with examples
软件开发的一点随记
Webrtc series-h.264 estimated bit rate calculation