在Conflux上 使用Hardhat创建和部署CRC721 NFT
This tutorial will guide you through the steps to create, write, and deploy an CRC721 NFT smart contract on Conflux CoreSpace using Hardhat. Additionally, it will show you how to view your contract on ConfluxScan.
如果您不熟悉 Hardhat,请参考 Hardhat 文档。
前提条件
Before you start, make sure you have the following:
- Node.js installed on your computer.
- A text editor, such as Visual Studio Code.
- A basic understanding of smart contract development and the Solidity programming language.
第1步:设置您的环境
First, you need to set up your development environment:
mkdir conflux-nft-crc721
cd conflux-nft-crc721
npm init -y
npm install --save-dev hardhat
Now, run the following command to create a Hardhat project:
npx hardhat
When prompted, choose the default project setup by pressing Enter for all questions. This will create a basic Hardhat project setup with all the necessary configurations. After setting up, your project directory should include the Hardhat configuration files.
步骤2: 安装相关程序
Install OpenZeppelin contracts, which provide a secure, audited implementation of ERC721 tokens, And Install Hardhat-Conflux-Plugin, which is built on top of js-conflux-sdk, making its usage for deployment and interaction very similar to that of js-conflux-sdk:
npm install @openzeppelin/contracts hardhat-conflux js-conflux-sdk
For additional information on the Hardhat-Conflux-Plugin, please refer to the Hardhat Conflux Plugin Tutorials. To learn more about the JS-Conflux-SDK, please visit the JS-Conflux-SDK Documentation.
第3步:编写智能合约
Create a new file named ConfluxCRC721NFT.sol
in the contracts
folder and add the following Solidity code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract ConfluxCRC721NFT is ERC721URIStorage, Ownable {
uint256 public nextTokenId;
constructor(
string memory name_,
string memory symbol_
) ERC721(name_, symbol_) Ownable(msg.sender) {}
function mint(address to, string memory uri) public onlyOwner {
_safeMint(to, nextTokenId);
_setTokenURI(nextTokenId, uri);
nextTokenId++;
}
}
This contract uses OpenZeppelin's ERC721URIStorage and Ownable contracts to create an NFT that only the owner can mint.
第4步:配置Hardhat
Edit the hardhat.config.js
to add the Conflux network configuration:
require("@nomicfoundation/hardhat-toolbox");
require("hardhat-conflux");
const PRIVATE_KEY = "YOUR_PRIVATE_KEY_HERE";
/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
solidity: "0.8.24",
defaultNetwork: "cfxTestnet",
networks: {
cfx: {
url: "https://main.confluxrpc.com",
accounts: [PRIVATE_KEY],
chainId: 1029,
},
cfxTestnet: {
url: "https://test.confluxrpc.com",
accounts: [PRIVATE_KEY],
chainId: 1,
},
},
};
将YOUR_PRIVATE_KEY_HERE
替换为您的私钥。
第5步:部署合约
Create a deployment script in the scripts
folder, named deploy.js
:
const hre = require("hardhat");
async function main() {
const signers = await hre.conflux.getSigners();
const defaultAccount = signers[0];
const ConfluxCRC721NFT = await hre.conflux.getContractFactory(
"MerkleTreeNFT"
);
const receipt = await ConfluxCRC721NFT.constructor("Confi", "Confi")
.sendTransaction({
from: defaultAccount.address,
})
.executed();
console.log(
`Contract deployment ${
receipt.outcomeStatus === 0 ? "succeeded" : "failed"
}`
);
console.log("MerkleTreeNFT deployed to:", receipt.contractCreated);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
通过运行以下命令部署合约:
npx hardhat run scripts/deploy.js --network cfxTestnet
Step 6: Minting an NFT
To mint an NFT, you can use a script that interacts directly with the mint
function in your smart contract. This script will mint an NFT to a specified address.
Create another script in the scripts
folder and name it mintNFT.js
:
This script sets the URI of a specific token, where YOUR_CONTRACT_ADDRESS
is the address of your deployed NFT contract and NFT_RECEIVER_ADDRESS
is the address of the NFT you want mint to.
const hre = require("hardhat");
async function main() {
const signers = await hre.conflux.getSigners();
const defaultAccount = signers[0];
const contractAddress = "YOUR_CONTRACT_ADDRESS";
const recipientAddress = "NFT_RECEIVER_ADDRESS"; // Address to receive the NFT
const tokenURI =
"https://raw.githubusercontent.com/conflux-fans/dual-space-nft-metadata/main/2023040104"; // Replace the example tokenURI with the actual metadata URI for the NFT
const ConfluxCRC721NFT = await hre.conflux.getContractAt(
"ConfluxCRC721NFT",
contractAddress
);
const receipt = await ConfluxCRC721NFT.mint(recipientAddress, tokenURI)
.sendTransaction({
from: defaultAccount.address,
})
.executed();
console.log(
`Minted NFT to ${recipientAddress}: Transaction Hash: ${receipt.transactionHash}`
);
}
main().catch((error) => {
console.error(error);
process.exit(1);
});
Replace YOUR_CONTRACT_ADDRESS
with your contract's address and RECIPIENT_WALLET_ADDRESS
with the address of the wallet that should receive the NFT.
These steps and scripts allow you to manage the lifecycle of your NFT, from minting to setting metadata, directly through Hardhat scripts. Adjust the tokenURI
in both scripts to match your NFT's metadata location.
You will see the following message after successful deployment
第7步:在ConfluxScan上查看您的合约
After deploying your contract, go to ConfluxScan, enter your contract address in the search bar, and you can view your contract's details, transactions, and more.
If you wish to verify your contract using Hardhat, please refer to this article.
第8步:赞助您的合约(可选)
Conflux实现了赞助机制,来补贴智能合约的使用。 If you want to get your contract sponsored, please refer to the following articles:
结论
This guide provided a step-by-step process for creating, deploying, and managing an CRC721 NFT on Conflux CoreSpace using Hardhat. By following these steps, you can launch your own NFT project on Conflux coreSpace.