This repository demonstrates a simple setup for using Chainlink VRF 2.5 for local development.
The project is a "Flip a Coin" application that provides a local development experience similar to a Node.js backend.
The frontend is a Next.js app with a button to flip a coin.
The backend is a smart contract deployed on Hardhat that generates a random number and returns "Tails" if it's even or "Heads" if it's odd.
This README explains how to set up and use the project. If you want to dive into the details, I wrote a blog post about it: https://thothbaboon.github.io/blog/chainlink-vrf-hardhat-local-setup/.
Install the dependencies:
npm installRun the tests - everything should be green ✅:
npx hardhat testStart a local network:
npx hardhat nodeDeploy the contracts:
npx hardhat run scripts/deployLocal.ts --network localhostThe backend is ready! 🚀
Install the dependencies:
npm installStart the app:
npm run devOpen your browser and go to http://localhost:3000
Open your browser's developer console to see the logs.
Click the Flip button.
You should see a request ID in the console. The first time, the ID should be 1, and it increments with each flip.
When using the mock VRF, the random number request must be fulfilled manually.
Let's choose an odd number: 3 and run the fulfill request script:
NUMBER=3 REQUEST_ID=1 npx hardhat run scripts/fulfillVrfRequest.ts --network localhostIt displays Tails.
Flip again, but now let's fulfill request 2 with an even number: 4:
NUMBER=4 REQUEST_ID=2 npx hardhat run scripts/fulfillVrfRequest.ts --network localhostIt displays Heads!
After making changes to the FlipCoin contract:
- Compile the contracts to generate new artifacts
- Copy the ABI to /app using the script:
npx hardhat compile
node scripts/copyAbi.jsTo generate the typing for VRFCoordinatorV2_5Mock, the raw ABI first needs to be wrapped:
npx hardhat run scripts/wrapAbi.ts
npx typechain --target ethers-v6 --out-dir types abi/VRFCoordinatorV2_5Mock.jsonUsed by the unit tests and to set up the local environment. It deploys the VRF Coordinator, the FlipCoin contract, and manages the subscription.