contract NFT_WEB3_EXPLORER is ERC721, ERC721Enumerable, Ownable { string private _baseURIextended; //我们设置最多可以mint1000个 uint256 public constant MAX_SUPPLY = 5000; //每个mint的价格是0EHT uint256 public constant PRICE_PER_TOKEN = 0 ether; // 设置nft的名字和 symbol constructor() ERC721("quantclass_nft", "QUANTCLASS_NFT") { }
function _beforeTokenTransfer(address from, address to, uint256 tokenId) internal override(ERC721, ERC721Enumerable) { super._beforeTokenTransfer(from, to, tokenId); }
function supportsInterface(bytes4 interfaceId) public view virtual override(ERC721, ERC721Enumerable)returns(bool) { return super.supportsInterface(interfaceId); }
function mint(uint numberOfTokens) public payable { uint256 ts = totalSupply(); require(ts + numberOfTokens <= MAX_SUPPLY, "Purchase would exceed max tokens"); require(PRICE_PER_TOKEN * numberOfTokens <= msg.value, "Ether value sent is not correct");
for (uint256 i = 0; i < numberOfTokens; i++) { _safeMint(msg.sender, ts + i); } }
function withdraw() public onlyOwner { uint balance = address(this).balance; payable(msg.sender).transfer(balance); } }
// We require the Hardhat Runtime Environment explicitly here. This is optional // but useful for running the script in a standalone fashion through `node <script>`. // // When running the script with `npx hardhat run <script>` you'll find the Hardhat // Runtime Environment's members available in the global scope. const hre = require("hardhat");
asyncfunctionmain() { // Hardhat always runs the compile task when running scripts with its command // line interface. // // If this script is run directly using `node` you may want to call compile // manually to make sure everything is compiled // await hre.run('compile');
// We get the contract to deploy constQuantclassNFT = await hre.ethers.getContractFactory("QUANTCLASS_NFT"); const quantclassNFT = awaitQuantclassNFT.deploy();
// We recommend this pattern to be able to use async/await everywhere // and properly handle errors. main() .then(() => process.exit(0)) .catch((error) => { console.error(error); process.exit(1); });
配置hardhat
安装env包
1
npm install dotenv
在根目录创建 .env 文件
1 2 3
PRIVATE_KEY = wallet_private_key
API = https://eth-rinkeby.alchemyapi.io/v2/YOUR_API_KEY
// This is a sample Hardhat task. To learn how to create your own go to // https://hardhat.org/guides/create-task.html require('dotenv').config(); const { API, PRIVATE_KEY } = process.env;
// You need to export an object to set up your config // Go to https://hardhat.org/config/ to learn more