#on-demand #switchboard #api-client #oracles #ipfs #interact-with-switchboard

switchboard-on-demand-client

Switchboard On Demand Client: Interact with the Switchboard On Demand and Crossbar API

40 releases

0.4.2 Dec 1, 2025
0.4.1 Sep 13, 2025
0.4.1-alpha0.0.0 Oct 9, 2025
0.4.0-alpha-0.0.5 May 21, 2025
0.1.5 Jul 15, 2024

#4 in #oracles

Download history 39/week @ 2025-12-18 17/week @ 2025-12-25 45/week @ 2026-01-01 44/week @ 2026-01-08 8/week @ 2026-01-15 52/week @ 2026-01-22 36/week @ 2026-01-29 71/week @ 2026-02-05 76/week @ 2026-02-12 46/week @ 2026-02-19 43/week @ 2026-02-26 264/week @ 2026-03-05 109/week @ 2026-03-12 86/week @ 2026-03-19 115/week @ 2026-03-26 211/week @ 2026-04-02

738 downloads per month
Used in 4 crates (3 directly)

MIT license

115KB
2.5K SLoC

Switchboard-On-Demand-Client

This crate is designed to interact with Switchboard on-demand, the Crossbar service, and queue gateways.

Crossbar

A middleman service to fetch oracle jobs from IPFS and to return feed price simulations. This is useful for updating a price constantly instead of sending requests directly to oracles.

Gateways

The frontend to interact with Switchboard oracles.

Example

#[tokio::main]
async fn main() {
    let client = RpcClient::new("https://api.devnet.solana.com".to_string());
    let queue_key = Pubkey::from_str("FfD96yeXs4cxZshoPPSKhSPgVQxLAJUT3gefgh84m1Di").unwrap();
    let feed = Pubkey::from_str("7Zi7LkGGARDKhUEFPBUQDsVZ9L965LPEv2rBRdmSXCWh").unwrap();
    let kp = read_keypair_file("authority.json").unwrap();

    let queue = QueueAccountData::load(&client, &queue_key).await.unwrap();
    let gw = &queue.fetch_gateways(&client).await.unwrap()[0];
    let crossbar = CrossbarClient::default();
    let feed_data = PullFeed::load_data(&client, &feed).await.unwrap();
    let feed_hash = feed_data.feed_hash();

    let simulation = crossbar.simulate_feeds(&[&feed_hash]).await.unwrap();
    println!("simulation: {:#?}", simulation);

    let ctx = SbContext::new();
    let (ix, responses, num_success, luts) = PullFeed::fetch_update_ix(
        ctx.clone(),
        &client,
        FetchUpdateParams {
            feed,
            payer: kp.pubkey(),
            gateway: gw.clone(),
            crossbar: Some(crossbar),
            ..Default::default()
        },
    )
    .await
    .unwrap();

    let blockhash = client.get_latest_blockhash().await.unwrap();
    let msg = Message::try_compile(
        &kp.pubkey(),
        &[
            ComputeBudgetInstruction::set_compute_unit_limit(1_400_000),
            ComputeBudgetInstruction::set_compute_unit_price(35_000),
            ix.clone()
        ],
        &luts,
        blockhash)
    .unwrap();

    let versioned_tx = VersionedTransaction::try_new(V0(msg), &[&kp]).unwrap();
    let result: Response<RpcSimulateTransactionResult> = client
        .simulate_transaction(&versioned_tx)
        .await
        .unwrap();
    println!("ix: {:#?}", result);
}

Updating many feeds at once

async fn main() {
    let ctx = SbContext::new();
    let client = RpcClient::new("===".to_string());
    let queue_key = Pubkey::from_str("A43DyUGA7s8eXPxqEjJY6EBu1KKbNgfxF8h17VAHn13w").unwrap();
    let mut feeds = Vec::new();
    feeds.push(Pubkey::from_str("FwzcymbxHJ7CArSmAwnguzyEBakLq2h3TZjHsz1r51rr").unwrap());
    feeds.push(Pubkey::from_str("ceuXtwYhAd3hs9xFfcrZZq6byY1s9b1NfsPckVkVyuC").unwrap());
    feeds.push(Pubkey::from_str("HhNHzSJjQuhni5GwaVkhqBQrMz1EZAW5Nt32RKnZAL2g").unwrap());
    let kp = read_keypair_file("authority.json").unwrap();

    let queue = QueueAccountData::load(&client, &queue_key).await.unwrap();
    let gw = &queue.fetch_gateways(&client).await.unwrap()[0];
    let slothash = SlotHashSysvar::get_latest_slothash(&client).await.unwrap();
    let (ixs, luts) = PullFeed::fetch_update_consensus_ix(
        sb_context,
        &client,
        FetchUpdateManyParams {
            feeds: feeds,
            payer: kp.pubkey(),
            gateway: gw.clone(),
            crossbar: Some(crossbar),
            num_signatures: Some(1),
            ..Default::default()
        },
    )
    .await
    .unwrap();

    let blockhash = client.get_latest_blockhash().await.unwrap();
    let msg = Message::try_compile(
        &kp.pubkey(),
        &[
            ixs[0].clone(), // secp256k1 instruction
            ixs[1].clone(), // fetch update intsruction
            ComputeBudgetInstruction::set_compute_unit_limit(1_400_000),
            ComputeBudgetInstruction::set_compute_unit_price(69_000),
            
        ],
        &luts,
        blockhash,
    )
    .unwrap();

    let versioned_tx = VersionedTransaction::try_new(V0(msg), &[&kp]).unwrap();
    let result = client.simulate_transaction(&versioned_tx).await.unwrap();
    println!(
        "Simulation logs: {:#?}",
        result.value.logs.unwrap_or(vec![])
    );

}

Dependencies

~62–90MB
~1.5M SLoC