English / 中文
VRF is a secure verifiable random number on the chain. It is used to generate secure random numbers. For details, see the ChainLink VRF official documentation.
This sample code demonstrates how to use ChainLink for NFT blind box design.
-
Configure the private key The private key put in .env in the format "PRIVATE_KEY= XXXX ", from which the code automatically reads.
-
Get Link test coins Every time you go to ChainLink to request VRF random number, you need to consume Link coins. Therefore, you need to apply for Link test coins before the test. For the Goerli test network, go to Request testnet LINK , Then "Netwrok" selects "Ethereum Sepolia" , click "Connect wallet" connect with MeatMask", click "Send Request" for geting 25 Test Link
- Install Dependencies
npm install
Node Version:v20.11.0
- Create ChainLink SubscriptionID
Login ChainLink VRF Test network , Click on "Create Subscription" to Create a SubscriptionID and you can see the created SubscriptionID under "My Subscriptions"
- Save SubscriptionID
Save the SubscriptionID created in the previous step to .env
## .env
SubscriptionId=ddddd
- Configuring Environment Variables .env file into the private key and infura node ID
## .env
PRIVATE_KEY=xxxxxxxxxxxxxxxx
INFURA_ID=yyyyyyyy
- Compile contract
npx hardhat compile
- Deployment test contract
npx hardhat run scripts/deploy.js --network sepolia
For different test networks (non-sepolia), please refer to Chain Link: supported-networks, in ./contracts/RandomNumberVRF.sol Modify the values of vrfCoordinator, link, keyHash
- Add Consumer
Click to enter the already created Subscription, click "Add Consumer" to fill in the contract address returned by deploy in the previous step. After filling in, you need to wait for 10 seconds. At this time, refresh the page and you can see the configured consumer address.
- Get random number
npx hardhat run scripts/random-number-vrf.js --network sepolia
After the requestRandomWords request is sent, it takes a certain amount of time for the ChainLink callback fulfillRandomWords to generate random numbers, which prevents the Main program from ending. Therefore, it is necessary to set up a loop to detect whether the RequestFulfilled event is generated.
- Get randomword again
npx hardhat run scripts/transaction.js --network sepolia
RandomWords After the random number is generated, it is passed in through the contract. The requestID saved in ./scripts/deployment.json is used to obtain the random number obtained before. You can customize random number usage scenarios. This example is only used as a calling reference.
After the random number is generated, it can be viewed in Ehterscan
- Sacking the NFT721 contract
npx hardhat run scripts/deployDungeonsAndDragonsCharacter.js --netwrok sepolia
- Mint NFT
npx hardhat run scripts/blindCharacter.js --netwrok sepolia
Github sample code: https://github.com/PatrickAlphaC/dungeons-and-dragons-nft
Chainlink Reporting Overview of Off-Chain: https://learnblockchain.cn/article/2186
How to obtain random numbers in NFT(ERC721): https://learnblockchain.cn/article/1776
Develop a DeFi project in 10 minutes using Chainlink predictor: https://learnblockchain.cn/article/1056
chainlink goerli faucet: https://faucets.chain.link/goerli?_ga=2.35440098.2104755910.1637393798-1377742816.1635817935
ChainLink VRF official document: https://docs.chain.link/docs/get-a-random-number/