187个版本

0.30.4 2024年7月13日
0.29.110 2024年7月9日
0.29.102 2024年2月29日
0.29.79 2023年12月19日
0.27.0 2023年7月10日

#1353 in 魔法豆

Download history 189/week @ 2024-04-18 273/week @ 2024-04-25 344/week @ 2024-05-02 111/week @ 2024-05-09 160/week @ 2024-05-16 135/week @ 2024-05-23 194/week @ 2024-05-30 324/week @ 2024-06-06 193/week @ 2024-06-13 237/week @ 2024-06-20 261/week @ 2024-06-27 833/week @ 2024-07-04 459/week @ 2024-07-11 306/week @ 2024-07-18 367/week @ 2024-07-25 145/week @ 2024-08-01

1,413每月下载
用于 4 个crate(3个直接使用)

MIT 许可证

7.5MB
2.5K SLoC

Rust 1.5K SLoC // 0.0% comments JavaScript 379 SLoC Shell 170 SLoC // 0.0% comments TypeScript 41 SLoC // 0.1% comments

包含 (WOFF字体,400KB) NanumBarunGothic-0f09457c7a19b7c6.ttf.woff2,(WOFF字体,135KB) FiraSans-Medium-8f9a781e4970d388.woff2,(WOFF字体,130KB) FiraSans-Regular-018c141bf0843ffd.woff2,(WOFF字体,82KB) SourceSerif4-Bold-a2c9cd1067f8b328.ttf.woff2,(WOFF字体,77KB) SourceSerif4-Regular-46f98efaafac5295.ttf.woff2,(WOFF字体,45KB) SourceCodePro-It-1cc31594bf4f1f79.ttf.woff2 及其他3个

Switchboard Logo

switchboard-solana

一个Rust库,用于与Solana上的Switchboard账户交互。

Crates.io Badge

Discord Badge

Twitter Badge

文档:switchboard-solana-rust-docs.web.app

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

Switchboard文档:docs.switchboard.xyz

安装

在您的项目目录中运行以下Cargo命令

cargo add switchboard-solana

或者将以下行添加到您的Cargo.toml文件中

[dependencies]
switchboard-solana = "0.29"

注意:次版本号对应anchor-lang依赖。此crate的0.29.*版本使用anchor-lang 0.29.0,而此crate的0.27.*版本使用anchor-lang 0.27.0。我们目前支持Anchor 29、28和27。

账户

此SDK为Oracle程序提供以下账户定义

本SDK为认证程序提供以下账户定义:

用法

功能

在允许您的程序修改其状态之前,您需要验证您的函数的enclave_signer。

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

读取最新结果

use anchor_lang::solana_program::clock;
use std::convert::TryInto;
use switchboard_solana::{AggregatorAccountData, SwitchboardDecimal, SWITCHBOARD_PROGRAM_ID};

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

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

// get result
let decimal: f64 = feed.get_result()?.try_into()?;

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

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

读取聚合器历史记录

注意:在使用之前,聚合器必须初始化历史缓冲区。

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

let history_buffer = AggregatorHistoryBuffer::new(history_account_info)?;
let current_timestamp = Clock::get()?.unix_timestamp;
let one_hour_ago: f64 = history_buffer.lower_bound(current_timestamp - 3600).unwrap().try_into()?;

VRF账户

读取VRF结果

use switchboard_solana::VrfAccountData;

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

// read the result
let result_buffer = vrf.get_result()?;
let value: &[u128] = bytemuck::cast_slice(&result_buffer[..]);
let result = value[0] % 256000 as u128;

请求随机数CPI

pub use switchboard_solana::{VrfAccountData, VrfRequestRandomness};

let switchboard_program = ctx.accounts.switchboard_program.to_account_info();

let vrf_request_randomness = VrfRequestRandomness {
    authority: ctx.accounts.state.to_account_info(),
    vrf: ctx.accounts.vrf.to_account_info(),
    oracle_queue: ctx.accounts.oracle_queue.to_account_info(),
    queue_authority: ctx.accounts.queue_authority.to_account_info(),
    data_buffer: ctx.accounts.data_buffer.to_account_info(),
    permission: ctx.accounts.permission.to_account_info(),
    escrow: ctx.accounts.escrow.clone(),
    payer_wallet: ctx.accounts.payer_wallet.clone(),
    payer_authority: ctx.accounts.payer_authority.to_account_info(),
    recent_blockhashes: ctx.accounts.recent_blockhashes.to_account_info(),
    program_state: ctx.accounts.program_state.to_account_info(),
    token_program: ctx.accounts.token_program.to_account_info(),
};

let vrf_key = ctx.accounts.vrf.key.clone();
let authority_key = ctx.accounts.authority.key.clone();

let state_seeds: &[&[&[u8]]] = &[&[
    &STATE_SEED,
    vrf_key.as_ref(),
    authority_key.as_ref(),
    &[bump],
]];
msg!("requesting randomness");
vrf_request_randomness.invoke_signed(
    switchboard_program,
    params.switchboard_state_bump,
    params.permission_bump,
    state_seeds,
)?;

依赖关系

~18-26MB
~448K SLoC