This project demonstrates how to get a verifiable random number using Chainlink VRF as a tamper-proof random number generator (RNG). This random number can be used in any application which rely on unpredictable outcomes:
- Blockchain games and NFTs
- Random assignment of duties and resources (e.g. randomly assigning judges to cases)
- Choosing a representative sample for consensus mechanisms
- Install dependencies using
yarn
- Create a
.env
in the project directory and write the following environment variables:
MNEMONIC = REPLACE_WITH_YOUR_MNEMONIC
RINKEBY_RPC = https://rinkeby.infura.io/v3/YOUR_INFURA_PROJECT_ID
- Deploy the smart contract
contracts/MyRNG.sol
using the script inscripts/deployMyRNG.ts
. Make sure you have some Rinkeby test ETH in your account you are using to deploy the contract.
npx hardhat run scripts/deployMyRNG.ts --network rinkeby
- Request a random number from Chainlink VRF using the script given in
scripts/getNewRandomNumber.ts
.
npx hardhat run scripts/getNewRandomNumber.ts --network rinkeby
- Check for the random value by running the script in
scripts/getCurrentNumber.ts
. It may take some time for the new random value to appear. So, you may need to run this script multiple times until the new random value appears.
npx hardhat run scripts/getCurrentNumber.ts --network rinkeby