6/12/2023 - Deprecated in favor of the switchboard-solana
crate.
Run the following Cargo command in your project directory:
bash
cargo add switchboard-v2
Or add the following line to your Cargo.toml:
toml
[dependencies]
switchboard-v2 = "0.2.0"
Directory
Read an aggregator result on-chain
```rust use anchorlang::solanaprogram::clock; use std::convert::TryInto; use switchboardv2::{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))?; ```
Note: The Aggregator must have a history buffer initialized before using
```rust use switchboard_v2::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()?; ```
```rust use switchboard_v2::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; ```
```rust pub use switchboard_v2::{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, )?; ```
```rust use anchorlang::solanaprogram::clock; use std::convert::TryInto; use switchboardv2::{BufferRelayerAccountData, SWITCHBOARDPROGRAM_ID};
// check feed owner let owner = *aggregator.owner; if owner != SWITCHBOARDPROGRAMID { return Err(error!(ErrorCode::InvalidSwitchboardAccount)); }
// deserialize account info let buffer = BufferRelayerAccountData::new(feedaccountinfo)?;
// get result let bufferresult = buffer.getresult();
// check if feed has been updated in the last 5 minutes buffer.checkstaleness(clock::Clock::get().unwrap().unixtimestamp, 300)?;
// convert buffer to a string let resultstring = String::fromutf8(buffer.result) .maperr(|| error!(ErrorCode::StringConversionFailed))?; msg!("Buffer string {:?}!", result_string); ```