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
Feature | Description |
---|---|
devnet | The devnet feature enables using the Switchboard Devnet Program ID instead of the Mainnet Program ID. |
Enable it in your Cargo.toml
[]
= { = "0.1.17", = ["devnet"] }
Define Your Own Devnet Feature
You can also define your own devnet feature to dynamically swap the program IDs.
[]
= []
= ["switchboard-v2/devnet"]
This allows you to build your program with a feature flag to automate devnet and mainnet builds.
# Build with Mainnet Switchboard Program ID
# Build with Devnet Switchboard Program ID
Usage
Aggregator
Read Latest Result
use clock;
use TryInto;
use ;
// check feed owner
let owner = *aggregator.owner;
if owner != SWITCHBOARD_PROGRAM_ID
// deserialize account info
let feed = ctx.accounts.aggregator.load?;
// OR
let feed = new?;
// get result
let decimal: f64 = feed.get_result?.try_into?;
// check if feed has been updated in the last 5 minutes
feed.check_staleness?;
// check if feed exceeds a confidence interval of +/i $0.80
feed.check_confidence_interval?;
Example(s): anchor-feed-parser, native-feed-parser
Read Aggregator History
Note: The Aggregator must have a history buffer initialized before using
use AggregatorHistoryBuffer;
use TryInto;
let history_buffer = new?;
let current_timestamp = get?.unix_timestamp;
let one_hour_ago: f64 = history_buffer.lower_bound.unwrap.try_into?;
VRF Account
Read Latest Result
use VrfAccountData;
// deserialize the account info
let vrf = ctx.accounts.vrf.load?;
// OR
let vrf = new?;
// read the result
let result_buffer = vrf.get_result?;
let value: & = cast_slice;
let result = value % 256000 as u128;
Example: anchor-vrf-parser
RequestRandomness CPI
pub use ;
let switchboard_program = ctx.accounts.switchboard_program.to_account_info;
let vrf_request_randomness = VrfRequestRandomness ;
let vrf_key = ctx.accounts.vrf.key.clone;
let authority_key = ctx.accounts.authority.key.clone;
let state_seeds: & = &;
msg!;
vrf_request_randomness.invoke_signed?;
Example: anchor-vrf-parser
Buffer Relayer Account
Read Latest Result
use clock;
use TryInto;
use ;
// check feed owner
let owner = *aggregator.owner;
if owner != SWITCHBOARD_PROGRAM_ID
// deserialize account info
let buffer = new?;
// get result
let buffer_result = buffer.get_result;
// check if feed has been updated in the last 5 minutes
buffer.check_staleness?;
// convert buffer to a string
let result_string = String from_utf8
.map_err?;
msg!;
Example: anchor-buffer-parser