This repo contains a step-by-step walkthrough to integrate Switchboard's VRF in a brand new anchor program.
You can checkout each step of the walkthrough with the command, wherethe tag is the step # in the table of contents below.
git checkout tags/0 -b main
Table of Contents
- #0 Anchor Init
- #1 Add Dependencies
- #2 Initial Program Scaffolding
- #3 Setup Switchboard Environment
- #4 Add init_client Instruction
- #5 Add request_randomness Instruction
- #6 Add consume_randomness Instruction
- #7 anchor-client-gen
- #8 Deployment Instructions
- #9 More Info
Switchboard's Verifiable Randomness Function (VRF) allows a user to request an oracle to produce a randomness output on-chain. Once the oracle has responded, the VRF proof must be verified before using it. The VRF proof takes 276 instructions (~40 transactions) to fully verify on-chain. Once the proof is verified, the Switchboard program will execute the callback defined by the VRF Account during account creation.
Our client program will be very basic. It will initialize a state account to hold our randomness result. It will then request randomness from our Switchboard oracle. Once verified, the Switchboard program will call the consume_randomness instruction which will update our client's state.
So the full flow will look like this,
- derive our client program derived address,
vrfClientKey
- create a Switchboard VRF Account with
vrfClientKey
as the authority. This will allow the client program to sign and request randomness. - invoke
init_client
for ourvrfClientKey
with our VRF Account - invoke
request_randomness
, which will assign an oracle to our VRF request - oracle watches the chain then responds to our VRF request with the proof calculated using its secret key
- oracle executes the 276 instructions to verify the VRF proof
- when VRF proof is verified, the Switchboard program will invoke our client
program's
consume_randomness
instruction - Done!