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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
//! # ORAO VRF
//!
//! Crate to interact with `orao-vrf` smart contract on Solana network.
//!
//! Provides an interface to request 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 account requirements 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)?;
//! ```
#![cfg_attr(docsrs, feature(doc_cfg))]
#![allow(deprecated)]
use anchor_lang::prelude::*;
use state::{NetworkState, OraoTokenFeeConfig, Randomness, RandomnessV2};
pub use crate::error::Error;
pub mod error;
pub mod events;
pub mod state;
mod sdk;
#[cfg(feature = "sdk")]
pub use crate::sdk::*;
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
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 to create 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 for the given VRF address.
///
/// * `vrf_id` — use [`crate::id`] to get the proper address
pub fn network_state_account_address(vrf_id: &Pubkey) -> Pubkey {
Pubkey::find_program_address(&[CONFIG_ACCOUNT_SEED], vrf_id).0
}
/// Returns randomness account address for the given seed and VRF address.
///
/// * `vrf_id` — use [`crate::id`] to get the proper address
pub fn randomness_account_address(vrf_id: &Pubkey, seed: &[u8; 32]) -> Pubkey {
Pubkey::find_program_address(&[RANDOMNESS_ACCOUNT_SEED, &seed[..]], vrf_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 const fn quorum(count: usize, total: usize) -> bool {
count >= majority(total)
}
/// Helper that returns the majority for the given total value.
pub const fn majority(total: usize) -> usize {
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(())
}
/// (**deprecated: see [`crate::Request`]**) 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(())
}
/// (**deprecated: see [`crate::FulfillV2`]**) Fulfills a randomness request
/// (for required accounts see [`crate::Fulfill`]).
pub fn fulfill(ctx: Context<Fulfill>) -> Result<()> {
Ok(())
}
/// Performs a randomness request (for required accounts see [`crate::RequestV2`]).
///
/// * seed – unique request seed
pub fn request_v2<'info>(
ctx: Context<'_, '_, '_, 'info, RequestV2<'info>>,
seed: [u8; 32],
) -> Result<()> {
Ok(())
}
/// Fulfills a randomness request (for required accounts see [`crate::FulfillV2`]).
pub fn fulfill_v2(ctx: Context<FulfillV2>) -> 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)]
pub payer: Signer<'info>,
#[account(
init,
payer = payer,
space = 8 + 464,
seeds = [CONFIG_ACCOUNT_SEED],
bump,
)]
pub network_state: Account<'info, NetworkState>,
/// CHECK:
pub treasury: AccountInfo<'info>,
pub 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)]
pub authority: Signer<'info>,
#[account(
mut,
seeds = [CONFIG_ACCOUNT_SEED],
bump,
constraint = network_state.config.authority == authority.key(),
)]
pub network_state: Account<'info, NetworkState>,
/// CHECK:
pub treasury: AccountInfo<'info>,
}
/// (**deprecated: see [`crate::RequestV2`]**) 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
///
/// ### Space calculation for account creation (request)
///
/// * 8 (anchor tag) + 32 (seed) + 64 (combined randomness) + 4 (response array length) + (32 (fulfiller pubkey) + 64 (fulfiller randomness)) * 7 (maximum number of responses)
///
#[derive(Accounts)]
#[instruction(seed: [u8; 32])]
#[deprecated(since = "0.4.0", note = "See RequestV2")]
pub struct Request<'info> {
#[account(mut)]
pub payer: Signer<'info>,
#[account(
mut,
seeds = [CONFIG_ACCOUNT_SEED],
bump,
)]
pub 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)]
pub treasury: AccountInfo<'info>,
#[account(
init,
payer = payer,
space = 8 + Randomness::SIZE,
seeds = [RANDOMNESS_ACCOUNT_SEED, &seed],
bump,
)]
pub request: Account<'info, Randomness>,
pub system_program: Program<'info, System>,
}
/// (**deprecated: see [`crate::FulfillV2`]**) Fulfill randomness.
///
/// ### Required accounts
///
/// * (signer) payer – transaction fee payer
/// * instruction_acc – sysvar instruction account
/// * (writable) network_state – VRF on-chain config PDA
/// (see [`network_state_account_address`])
/// * (writable) request – randomness request PDA
/// (see [`randomness_account_address`])
#[derive(Accounts)]
#[deprecated(since = "0.4.0", note = "See FulfillV2")]
pub struct Fulfill<'info> {
#[account(mut)]
pub payer: Signer<'info>,
/// CHECK:
pub instruction_acc: AccountInfo<'info>,
#[account(
mut,
seeds = [CONFIG_ACCOUNT_SEED],
bump,
)]
pub network_state: Account<'info, NetworkState>,
#[account(
mut,
seeds = [RANDOMNESS_ACCOUNT_SEED, &request.seed],
bump,
)]
pub request: Account<'info, Randomness>,
}
/// Request randomness (v2).
///
/// ### 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
///
/// ### Space calculation for account creation (request)
///
/// * 8 (anchor tag) + 32 (client) + 32 (seed) + 4 (response array length) + (32 (fulfiller pubkey) + 64 (fulfiller randomness)) * 7 (maximum number of responses)
///
#[derive(Accounts)]
#[instruction(seed: [u8; 32])]
pub struct RequestV2<'info> {
#[account(mut)]
pub payer: Signer<'info>,
#[account(
mut,
seeds = [CONFIG_ACCOUNT_SEED],
bump,
)]
pub 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)]
pub treasury: AccountInfo<'info>,
#[account(
init,
payer = payer,
space = 8 + RandomnessV2::PENDING_SIZE,
seeds = [RANDOMNESS_ACCOUNT_SEED, &seed],
bump,
)]
pub request: Account<'info, RandomnessV2>,
pub system_program: Program<'info, System>,
}
/// Fulfill randomness (v2).
///
/// ### Required accounts
///
/// * (signer) payer – transaction fee payer
/// * instruction_acc – sysvar instruction account
/// * (writable) network_state – VRF on-chain config PDA
/// (see [`network_state_account_address`])
/// * (writable) request – randomness request PDA
/// (see [`randomness_account_address`])
/// * (writable) client - client account (see [`Randomness::client`])
#[derive(Accounts)]
pub struct FulfillV2<'info> {
#[account(mut)]
pub payer: Signer<'info>,
/// CHECK:
pub instruction_acc: AccountInfo<'info>,
#[account(
mut,
seeds = [CONFIG_ACCOUNT_SEED],
bump,
)]
pub network_state: Account<'info, NetworkState>,
#[account(
mut,
seeds = [RANDOMNESS_ACCOUNT_SEED, request.seed()],
bump,
)]
pub request: Account<'info, RandomnessV2>,
#[account(mut, constraint = *request.client() == client.key())]
pub client: AccountInfo<'info>,
pub system_program: Program<'info, System>,
}