1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
//! # ORAO VRF
//!
//! Crate to interact with `orao-vrf` smart contract on Solana network.
//!
//! Provides an interface to request for a verifiable randomness (Ed25519 Signature)
//! on the Solana network.
//!
//! ## Documentation
//!
//! Please look into the following functions and structures:
//!
//! * [`RequestBuilder`] – convenient builder for randomness requests
//! * [`get_network_state`] – helper to fetch the VRF configuration
//! * [`get_randomness`] – helper to fetch the randomness request state
//! * [`randomness_account_address`] – helper to derive randomness request state address
//! * [`network_state_account_address`] – helper to derive VRF on-chain configuration address
//!
//! ## Cross Program Invocation
//!
//! For CPI please look into the `cpi` example and accouns requiremens for the [`Request`]
//! instruction.
//!
//! **Note:** requires `cpi` feature to be enabled and `sdk` feature to be disabled.
//!
//! ```ignore
//! // assuming ctx to be a context of an instruction that performs CPI
//! let vrf_program = ctx.accounts.vrf.to_account_info();
//! let request_accounts = orao_solana_vrf::cpi::accounts::Request {
//! payer: ctx.accounts.player.to_account_info(),
//! network_state: ctx.accounts.config.to_account_info(),
//! treasury: ctx.accounts.treasury.to_account_info(),
//! request: ctx.accounts.request.to_account_info(),
//! system_program: ctx.accounts.system_program.to_account_info(),
//! };
//! let cpi_ctx = CpiContext::new(vrf_program, request_accounts);
//! orao_solana_vrf::cpi::request(cpi_ctx, seed)?;
//! ```
use anchor_lang::prelude::*;
use state::{NetworkState, OraoTokenFeeConfig, Randomness};
pub use crate::error::Error;
pub mod error;
pub mod state;
mod sdk;
#[cfg(feature = "sdk")]
pub use crate::sdk::*;
declare_id!("VRFzZoJdhFWL8rkvu87LpKM3RbcVezpMEc6X5GVDr7y");
/// This is the seed used for creating request/fulfillment accounts.
pub const RANDOMNESS_ACCOUNT_SEED: &[u8] = b"orao-vrf-randomness-request";
/// This is the seed used for creating network-wide configuration PDA
/// account, that sets things like fulfillment authorities, costs, etc.
pub const CONFIG_ACCOUNT_SEED: &[u8] = b"orao-vrf-network-configuration";
// this is the number of public keys that are allowed to generate randomness
pub const MAX_FULFILLMENT_AUTHORITIES_COUNT: usize = 10;
/// Returns network state account address.
pub fn network_state_account_address() -> Pubkey {
Pubkey::find_program_address(&[CONFIG_ACCOUNT_SEED.as_ref()], &crate::id()).0
}
/// Returns randomness account address for the given seed.
pub fn randomness_account_address(seed: &[u8; 32]) -> Pubkey {
Pubkey::find_program_address(&[RANDOMNESS_ACCOUNT_SEED.as_ref(), &seed[..]], &crate::id()).0
}
/// Helper that XORes `r` into `l`.
pub fn xor_array<const N: usize>(l: &mut [u8; N], r: &[u8; N]) {
for i in 0..N {
l[i] ^= r[i];
}
}
/// Helper that checks for Byzantine quorum.
pub fn quorum(count: usize, total: usize) -> bool {
count >= (total * 2 / 3 + 1)
}
#[allow(unused_variables)]
#[program]
pub mod orao_vrf {
use super::*;
/// Performs VRF initialization (for required accounts see [`crate::InitNetwork`]).
///
/// * fee – request fee (in lamports)
/// * config_authority – VRF config update authority
/// * fulfillment_authorities – randomness fulfillment authorities
/// * token_fee_config – token fee configuration
///
/// Treasury is given via instruction accounts.
pub fn init_network(
ctx: Context<InitNetwork>,
fee: u64,
config_authority: Pubkey,
fulfillment_authorities: Vec<Pubkey>,
token_fee_config: Option<OraoTokenFeeConfig>,
) -> Result<()> {
Ok(())
}
/// Performs VRF configuration update (for required accounts see [`crate::UpdateNetwork`]).
///
/// * fee – request fee (in lamports)
/// * config_authority – VRF config update authority
/// * fulfillment_authorities – randomness fulfillment authorities
/// * token_fee_config – token fee configuration
///
/// Treasury is given via instruction accounts.
pub fn update_network(
ctx: Context<UpdateNetwork>,
fee: u64,
config_authority: Pubkey,
fulfillment_authorities: Vec<Pubkey>,
token_fee_config: Option<OraoTokenFeeConfig>,
) -> Result<()> {
Ok(())
}
/// Performs a randomness request (for required accounts see [`crate::Request`]).
///
/// * seed – unique request seed
pub fn request<'info>(
ctx: Context<'_, '_, '_, 'info, Request<'info>>,
seed: [u8; 32],
) -> Result<()> {
Ok(())
}
/// Fulfills a randomness request (for required accounts see [`crate::Fulfill`]).
pub fn fulfill(ctx: Context<Fulfill>) -> Result<()> {
Ok(())
}
}
/// Initialize network configuration.
///
/// ### Required accounts
///
/// * (signer) payer – fee payer
/// * (writable) network_state – VRF network state PDA
/// (see [`network_state_account_address`])
/// * treasury – SOL treasury account
/// * system_program - system program account
///
/// ### Remaining accounts
///
/// Following accounts are necessary if `token_fee_config` is given:
///
/// * mint_account – SPL token mint account (should match the token fee config)
/// * token_treasury_account – should match SPL token and token fee config
#[derive(Accounts)]
pub struct InitNetwork<'info> {
#[account(mut)]
payer: Signer<'info>,
#[account(
init,
payer = payer,
space = 8 + 464,
seeds = [CONFIG_ACCOUNT_SEED.as_ref()],
bump,
)]
network_state: Account<'info, NetworkState>,
/// CHECK:
treasury: AccountInfo<'info>,
system_program: Program<'info, System>,
}
/// Update network configuration.
///
/// ### Required accounts
///
/// * (signer) authority – should match configured VRF authority
/// * (writable) network_state – VRF network state PDA
/// (see [`network_state_account_address`])
/// * treasury – SOL treasury account
///
/// ### Remaining accounts
///
/// Following accounts are necessary if `token_fee_config` is given:
///
/// * mint_account – SPL token mint account (should match the token fee config)
/// * token_treasury_account – should match SPL token and token fee config
#[derive(Accounts)]
pub struct UpdateNetwork<'info> {
#[account(mut)]
authority: Signer<'info>,
#[account(
mut,
seeds = [CONFIG_ACCOUNT_SEED.as_ref()],
bump,
constraint = network_state.config.authority == authority.key(),
)]
network_state: Account<'info, NetworkState>,
/// CHECK:
treasury: AccountInfo<'info>,
}
/// Request randomness.
///
/// ### Required accounts
///
/// * (signer) payer – request and transaction fee payer
/// * (writable) network_state – VRF on-chain config PDA
/// (see [`network_state_account_address`])
/// * (writable) (from VRF on-chain config) treasury – either SOL treasury or token treasury
/// (see remaining accounts)
/// * (writable) request – randomness request PDA
/// (see [`randomness_account_address`])
/// * system_program - system program account
///
/// ### Remaining accounts
///
/// If token fee configuration exists and given treasury equals token treasury,
/// then remaining accounts are required:
///
/// * (writable) token_payer – payer token wallet
/// * token_program_account – necessary to execute the transfer
#[derive(Accounts)]
#[instruction(seed: [u8; 32])]
pub struct Request<'info> {
#[account(mut)]
payer: Signer<'info>,
#[account(
mut,
seeds = [CONFIG_ACCOUNT_SEED.as_ref()],
bump,
)]
network_state: Account<'info, NetworkState>,
/// CHECK:
#[account(
mut,
constraint = network_state.config.treasury == treasury.key() ||
network_state.config.token_fee_config.as_ref().map(|x| x.treasury) == Some(treasury.key())
@ Error::UnknownTreasuryGiven)]
treasury: AccountInfo<'info>,
#[account(
init,
payer = payer,
space = 8 + 32 + 64 + 4 + (32 + 64) * 7,
seeds = [RANDOMNESS_ACCOUNT_SEED.as_ref(), &seed],
bump,
)]
request: Account<'info, Randomness>,
system_program: Program<'info, System>,
}
/// Fulfill randomness.
///
/// ### Required accounts
///
/// * (signer) payer – transaction fee payer
/// * instruction_acc – sysvar instruction accout
/// * (writable) network_state – VRF on-chain config PDA
/// (see [`network_state_account_address`])
/// * (writable) request – randomness request PDA
/// (see [`randomness_account_address`])
#[derive(Accounts)]
pub struct Fulfill<'info> {
#[account(mut)]
payer: Signer<'info>,
/// CHECK:
instruction_acc: AccountInfo<'info>,
#[account(
mut,
seeds = [CONFIG_ACCOUNT_SEED.as_ref()],
bump,
)]
network_state: Account<'info, NetworkState>,
#[account(
mut,
seeds = [RANDOMNESS_ACCOUNT_SEED.as_ref(), &request.seed],
bump,
)]
request: Account<'info, Randomness>,
}