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-demand

Install Switchboard On-Demand in your Javascript Client file:

npm i @switchboard-xyz/on-demand

Step 1: Coin Flip Program (Solana)

  1. 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_flip function.

Step 2: TypeScript Client

  1. Setup and Environment Configuration:

  1. Creating a Randomness Account: Before flipping that coin, start with a clean randomness account.

  1. Committing to Randomness: Use randomness account to commit to the oracle.

  1. 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 .

  1. 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