当前位置:网站首页>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/FaucETH
https://fauceth.komputing.org/
Rinkeby: Authenticated Faucet
https://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 .
边栏推荐
- Database basic commands
- Binary tree creation & traversal
- File upload of DVWA range
- Luogu p4127 [ahoi2009] similar distribution problem solution
- 上线APS系统,破除物料采购计划与生产实际脱钩的难题
- Compliance and efficiency, accelerate the digital transformation of pharmaceutical enterprises, and create a new document resource center for pharmaceutical enterprises
- It's hard to find a job when the industry is in recession
- Data governance: Data Governance under microservice architecture
- Is the super browser a fingerprint browser? How to choose a good super browser?
- Simulation of holographic interferogram and phase reconstruction of Fourier transform based on MATLAB
猜你喜欢

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

Pre knowledge reserve of TS type gymnastics to become an excellent TS gymnastics master

Asia Pacific Financial Media | art cube of "designer universe": Guangzhou community designers achieve "great improvement" in urban quality | observation of stable strategy industry fund
![[redis] Introduction to NoSQL database and redis](/img/95/229d7a08e94245f2733b8c59201cff.png)
[redis] Introduction to NoSQL database and redis

Wireshark grabs packets to understand its word TCP segment
![DataX self check error /datax/plugin/reader/_ drdsreader/plugin. Json] does not exist](/img/17/415e66d67afb055e94a966de25c2bc.png)
DataX self check error /datax/plugin/reader/_ drdsreader/plugin. Json] does not exist

A Closer Look at How Fine-tuning Changes BERT

861. Score after flipping the matrix

"Designer universe": "benefit dimension" APEC public welfare + 2022 the latest slogan and the new platform will be launched soon | Asia Pacific Financial Media

National economic information center "APEC industry +": economic data released at the night of the Spring Festival | observation of stable strategy industry fund
随机推荐
shu mei pai
PHP - Common magic method (nanny level teaching)
A Closer Look at How Fine-tuning Changes BERT
[Yugong series] February 2022 U3D full stack class 011 unity section 1 mind map
数据治理:主数据的3特征、4超越和3二八原则
edge浏览器 路径获得
opencv学习笔记九--背景建模+光流估计
octomap averageNodeColor函数说明
珠海金山面试复盘
[computer skills]
C # connect to SQLite database to read content
Data governance: 3 characteristics, 4 transcendence and 3 28 principles of master data
Rust language - receive command line parameter instances
二叉树创建 & 遍历
In the era of digital economy, how to ensure security?
软件开发的一点随记
链表面试题(图文详解)
数字经济时代,如何保障安全?
Google may return to the Chinese market after the Spring Festival.
Solution: intelligent site intelligent inspection scheme video monitoring system