当前位置:网站首页>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 .
边栏推荐
- MySQL view tablespace and create table statements
- 861. Score after flipping the matrix
- MES, APS and ERP are essential to realize fine production
- Key value judgment in the cycle of TS type gymnastics, as keyword use
- 数字经济时代,如何保障安全?
- Asia Pacific Financial Media | "APEC industry +" Western Silicon Valley invests 2trillion yuan in Chengdu Chongqing economic circle to catch up with Shanghai | stable strategy industry fund observatio
- Pre knowledge reserve of TS type gymnastics to become an excellent TS gymnastics master
- The State Economic Information Center "APEC industry +" Western Silicon Valley will invest 2trillion yuan in Chengdu Chongqing economic circle, which will surpass the observation of Shanghai | stable
- Redis list detailed explanation of character types yyds dry goods inventory
- . Net 6 learning notes: what is NET Core
猜你喜欢
[1. Delphi foundation] 1 Introduction to Delphi Programming
opencv学习笔记八--答题卡识别
Data governance: 3 characteristics, 4 transcendence and 3 28 principles of master data
07- [istio] istio destinationrule (purpose rule)
08- [istio] istio gateway, virtual service and the relationship between them
数据治理:主数据的3特征、4超越和3二八原则
Parameter self-tuning of relay feedback PID controller
Simulation of holographic interferogram and phase reconstruction of Fourier transform based on MATLAB
ROS learning (IX): referencing custom message types in header files
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
随机推荐
National economic information center "APEC industry +": economic data released at the night of the Spring Festival | observation of stable strategy industry fund
Artcube information of "designer universe": Guangzhou implements the community designer system to achieve "great improvement" of urban quality | national economic and Information Center
Description of octomap averagenodecolor function
Webrtc series-h.264 estimated bit rate calculation
[Yugong series] February 2022 U3D full stack class 011 unity section 1 mind map
Qualitative risk analysis of Oracle project management system
数据治理:误区梳理篇
Comparison of usage scenarios and implementations of extensions, equal, and like in TS type Gymnastics
The difference between TS Gymnastics (cross operation) and interface inheritance
数据治理:微服务架构下的数据治理
24. Query table data (basic)
datax自检报错 /datax/plugin/reader/._drdsreader/plugin.json]不存在
Interview Reply of Zhuhai Jinshan
C # connect to SQLite database to read content
"Designer universe": "benefit dimension" APEC public welfare + 2022 the latest slogan and the new platform will be launched soon | Asia Pacific Financial Media
实现精细化生产, MES、APS、ERP必不可少
edge浏览器 路径获得
Data governance: data quality
Entity class design for calculating age based on birthday
数据治理:主数据的3特征、4超越和3二八原则