cargo   twitter  

switchboard-v2

A Rust library to interact with Switchboard V2 accounts.

This package can be used to interact and deserialize Switchboard V2 accounts.

See the documentation for more info on Switchboard.

Features

By default the crate will default to mainnet. You must explicitly enable the devnet feature to use on devnet.

Usage

Aggregator

Read Latest Result

```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))?; ```

Example(s): anchor-feed-parser, native-feed-parser

Read Aggregator History

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()?; ```

VRF Account

Read Latest Result

```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; ```

Example: anchor-vrf-parser

RequestRandomness CPI

```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, )?;

```

Example: anchor-vrf-parser

Buffer Relayer Account

Read Latest Result

```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); ```

Example: anchor-buffer-parser