Skip to main content

Step-By-Step Guide

Any Solana program can request and consume verifiable randomness onchain within seconds using the MagicBlock VRF SDK. By the end of this guide, you’ll have a working example that rolls a dice using verifiable randomness.
1

Write your program

Write your Solana program as you normally.
2

Add request and consume randomness instructions.

Add CPI hooks that request and consume randomness via callback from a verified oracle.
3

Deploy your program on Solana

Deploy your Solana program using Anchor CLI.
4

Execute transactions for onchain randomness.

Send transactions to generate and consume randomness onchain.

Roll Dice Example

Roll Dice GIF The following software packages may be required, other versions may also be compatible:
SoftwareVersionInstallation Guide
Solana2.1.21Install Solana
Rust1.82.0Install Rust
Anchor0.31.1Install Anchor

Quick Access

Find the full basic randomness example for Anchor framework implementation:

Code Snippets

  • 1. Write program
  • 2. Request & Consume Randomnness
  • 3. Deploy
  • 4. Test
A simple roll dice program where player initialize state account to store, request and consume randomness:
pub const PLAYER: &[u8] = b"playerd";

#[program]
pub mod random_dice {
    use super::*;

    pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
        msg!(
            "Initializing player account: {:?}",
            ctx.accounts.player.key()
        );
        Ok(())
    }

    // ... Additional instructions will be added here
}

/// Context for initializing player
#[derive(Accounts)]
pub struct Initialize<'info> {
    #[account(mut)]
    pub payer: Signer<'info>,
    #[account(init_if_needed, payer = payer, space = 8 + 1, seeds = [PLAYER, payer.key().to_bytes().as_slice()], bump)]
    pub player: Account<'info, Player>,
    pub system_program: Program<'info, System>,
}

/// Player struct
#[account]
pub struct Player {
    pub last_result: u8,
}
⬆️ Back to Top

Solana Explorer

Get insights about your transactions and accounts on Solana:

Solana RPC Providers

Send transactions and requests through existing RPC providers:

Solana Validator Dashboard

Find real-time updates on Solana’s validator infrastructure:

Server Status Subscriptions

Subscribe to Solana’s and MagicBlock’s server status:

MagicBlock Products