Solana / SVM
Switchboard Randomness for Solana and SVM applications
Prerequisites
Basic understanding of Solana development and Anchor Framework.
A working Solana development environment (Solana tool suite, Anchor).
Solana CLI connected to the devnet.
Enough SOL tokens for devnet transactions.
Tooling:
Install Switchboard On-Demand in your program.
cargo add switchboard-on-demandInstall Switchboard On-Demand in your Javascript Client file:
npm i @switchboard-xyz/on-demandStep 1: Coin Flip Program (Solana)
Define the Player State
Make sure to store the user's randomness_account in your program.
#[account] pub struct PlayerState { allowed_user: Pubkey, latest_flip_result: bool, randomness_account: Pubkey, current_guess: bool, wager: u64, bump: u8, }
Commit to a Future Slot
Commit to the game by locking the
randomness_account.Settle the Flip
Resolve randomness through the
settle_flipfunction.
Step 2: TypeScript Client
Setup and Environment Configuration:
Creating a Randomness Account: Before flipping that coin, start with a clean
randomnessaccount.
Committing to Randomness: Use randomness account to commit to the oracle.
Revealing the Vision: Once the slot is generated, invoke to generate randomness for the committed slot.
Note: here is an optional way to save the revealIx()transaction .
Settling the Flip: Finally, record the coin flip in the randomness account.
You've just integrated randomness into your Solana application.
For more details, refer to the example repository
Last updated