![Switchboard Logo](https://github.com/switchboard-xyz/core-sdk/raw/main/website/static/img/icons/switchboard/avatar.png) # switchboard-solana > A Rust library to interact with Switchboard accounts on Solana. [![Crates.io Badge](https://img.shields.io/crates/v/switchboard-solana?label=switchboard-solana&logo=rust)](https://crates.io/crates/switchboard-solana) [![Discord Badge](https://img.shields.io/discord/841525135311634443?color=blueviolet&logo=discord&logoColor=white)](https://discord.gg/switchboardxyz) [![Twitter Badge](https://img.shields.io/twitter/follow/switchboardxyz?label=Follow+Switchboard)](https://twitter.com/switchboardxyz)

Typedocs: docs.rs/switchboard-solana

Solana SDK: github.com/switchboard-xyz/solana-sdk

Switchboard Documentation: docs.switchboard.xyz

Install

Run the following Cargo command in your project directory:

bash cargo add switchboard-solana

Or add the following line to your Cargo.toml:

toml [dependencies] switchboard-solana = "0.28"

NOTE: The minor version corresponds to the anchor-lang dependency. Version 0.28.* of this crate uses anchor-lang 0.28.0 while version 0.27.* of this crate uses anchor-lang 0.27.0.

Accounts

This SDK provides the following account definitions for the Oracle Program:

This SDK provides the following account definitions for the Attestation Program:

Usage

Functions

You will need to validate your function's enclave_signer before allowing your program to modify its state.

```rust use switchboard_solana::FunctionAccountData;

[derive(Accounts)]

pub struct SaveDataInstruction<'info> { // ... your required accounts to modify your program's state

// We use this to derive and verify the functions enclave state
#[account(
    constraint =
        function.load()?.validate(
          &enclave_signer.to_account_info()
        )?
)]
pub function: AccountLoader<'info, FunctionAccountData>,
pub enclave_signer: Signer<'info>,

} ```

Aggregator

Read Latest Result

```rust use anchorlang::solanaprogram::clock; use std::convert::TryInto; use switchboardsolana::{AggregatorAccountData, SwitchboardDecimal, SWITCHBOARDPROGRAM_ID};

// check feed owner let owner = *aggregator.owner; if owner != SWITCHBOARDPROGRAMID { return Err(error!(ErrorCode::InvalidSwitchboardAccount)); }

// deserialize account info let feed = ctx.accounts.aggregator.load()?; // OR let feed = AggregatorAccountData::new(feedaccountinfo)?;

// get result let decimal: f64 = feed.getresult()?.tryinto()?;

// check if feed has been updated in the last 5 minutes feed.checkstaleness(clock::Clock::get().unwrap().unixtimestamp, 300)?;

// check if feed exceeds a confidence interval of +/i $0.80 feed.checkconfidenceinterval(SwitchboardDecimal::from_f64(0.80))?; ```

Read Aggregator History

Note: The Aggregator must have a history buffer initialized before using

```rust use switchboard_solana::AggregatorHistoryBuffer; use std::convert::TryInto;

let historybuffer = AggregatorHistoryBuffer::new(historyaccountinfo)?; let currenttimestamp = Clock::get()?.unixtimestamp; let onehourago: f64 = historybuffer.lowerbound(currenttimestamp - 3600).unwrap().try_into()?; ```

VRF Account

Read VRF Result

```rust use switchboard_solana::VrfAccountData;

// deserialize the account info let vrf = ctx.accounts.vrf.load()?; // OR let vrf = VrfAccountData::new(vrfaccountinfo)?;

// read the result let resultbuffer = vrf.getresult()?; let value: &[u128] = bytemuck::castslice(&resultbuffer[..]); let result = value[0] % 256000 as u128; ```

RequestRandomness CPI

```rust pub use switchboard_solana::{VrfAccountData, VrfRequestRandomness};

let switchboardprogram = ctx.accounts.switchboardprogram.toaccountinfo();

let vrfrequestrandomness = VrfRequestRandomness { authority: ctx.accounts.state.toaccountinfo(), vrf: ctx.accounts.vrf.toaccountinfo(), oraclequeue: ctx.accounts.oraclequeue.toaccountinfo(), queueauthority: ctx.accounts.queueauthority.toaccountinfo(), databuffer: ctx.accounts.databuffer.toaccountinfo(), permission: ctx.accounts.permission.toaccountinfo(), escrow: ctx.accounts.escrow.clone(), payerwallet: ctx.accounts.payerwallet.clone(), payerauthority: ctx.accounts.payerauthority.toaccountinfo(), recentblockhashes: ctx.accounts.recentblockhashes.toaccountinfo(), programstate: ctx.accounts.programstate.toaccountinfo(), tokenprogram: ctx.accounts.tokenprogram.toaccountinfo(), };

let vrfkey = ctx.accounts.vrf.key.clone(); let authoritykey = ctx.accounts.authority.key.clone();

let stateseeds: &[&[&[u8]]] = &[&[ &STATESEED, vrfkey.asref(), authoritykey.asref(), &[bump], ]]; msg!("requesting randomness"); vrfrequestrandomness.invokesigned( switchboardprogram, params.switchboardstatebump, params.permissionbump, stateseeds, )?;

```