Expand description
The Solana Randomness Service uses a Switchboard SGX enabled oracle to provide randomness to any Solana program using a callback instruction.
Program ID: RANDMo5gFnqnXJW5Z52KNmd24sAo95KAd5VbiCtq5Rh
See the crate solana-randomness-service
for the full CPI interface.
§Example Program
use anchor_lang::prelude::*;
use solana_randomness_service_lite::{SimpleRandomnessV1Request, ID as SolanaRandomnessServiceID};
#[program]
pub mod solana_randomness_consumer {
use super::*;
pub fn request_randomness(ctx: Context<RequestRandomness>) -> anchor_lang::prelude::Result<()> {
msg!("Requesting randomness...");
let request = SimpleRandomnessV1Request {
request: ctx.accounts.randomness_request.to_account_info(),
escrow: ctx.accounts.randomness_escrow.to_account_info(),
state: ctx.accounts.randomness_state.to_account_info(),
mint: ctx.accounts.randomness_mint.to_account_info(),
payer: ctx.accounts.payer.to_account_info(),
system_program: ctx.accounts.system_program.to_account_info(),
token_program: ctx.accounts.token_program.to_account_info(),
associated_token_program: ctx.accounts.associated_token_program.to_account_info(),
};
request.invoke(
ctx.accounts.randomness_service.to_account_info(),
8, // Request 8 bytes of randomness
&solana_randomness_service_lite::Callback::new(
ID,
vec![
AccountMeta::new_readonly(ctx.accounts.randomness_state.key(), true).into(),
AccountMeta::new_readonly(ctx.accounts.randomness_request.key(), false).into(),
],
[190, 217, 49, 162, 99, 26, 73, 234].to_vec(), // Our callback ixn discriminator. The oracle will append the randomness bytes to the end
),
&Some(solana_randomness_service_lite::TransactionOptions {
compute_units: Some(1_000_000),
compute_unit_price: Some(100),
}),
)?;
// Here we can emit some event to index our requests
Ok(())
}
}
#[derive(Accounts)]
pub struct RequestRandomness<'info> {
/// CHECK: manually check programID and executable status
#[account(
constraint = randomness_service.key() == SolanaRandomnessServiceID,
constraint = randomness_service.executable,
)]
pub randomness_service: AccountInfo<'info>,
/// The account that will be created on-chain to hold the randomness request.
/// Used by the off-chain oracle to pickup the request and fulfill it.
/// CHECK: todo
#[account(
mut,
signer,
owner = system_program.key(),
constraint = randomness_request.data_len() == 0 && randomness_request.lamports() == 0,
)]
pub randomness_request: AccountInfo<'info>,
/// The TokenAccount that will store the funds for the randomness request.
/// CHECK: todo
#[account(
mut,
owner = system_program.key(),
constraint = randomness_escrow.data_len() == 0 && randomness_escrow.lamports() == 0,
)]
pub randomness_escrow: AccountInfo<'info>,
/// The randomness service's state account. Responsible for storing the
/// reward escrow and the cost per random byte.
#[account(
seeds = [b"STATE"],
bump = randomness_state.bump,
seeds::program = randomness_service.key(),
)]
pub randomness_state: Box<Account<'info, solana_randomness_service::State>>,
/// The token mint to use for paying for randomness requests.
#[account(address = NativeMint::ID)]
pub randomness_mint: Account<'info, Mint>,
/// The account that will pay for the randomness request.
#[account(mut)]
pub payer: Signer<'info>,
/// The Solana System program. Used to allocate space on-chain for the randomness_request account.
pub system_program: Program<'info, System>,
/// The Solana Token program. Used to transfer funds to the randomness escrow.
pub token_program: Program<'info, Token>,
/// The Solana Associated Token program. Used to create the TokenAccount for the randomness escrow.
pub associated_token_program: Program<'info, AssociatedToken>,
}
Re-exports§
pub use types::*;
Modules§
Structs§
- Account information
- Describes a single account read or written by a program during instruction execution.
- The address of a Solana account.
Enums§
- Reasons the program may fail
Constants§
- The const program ID.
Functions§
- Returns
true
if given pubkey is the program ID. - Returns the program ID.