Pick a depth. Each prompt opens in your AI pre-loaded with the lesson. Click a row to preview the prompt.
Chainlink (the dominant Ethereum oracle) pushes prices on-chain on a fixed cadence — every X seconds or every Y% deviation. Pyth (the dominant Solana oracle) lets the consumer pull the price into the same transaction via a price update. Pull is cheaper and fresher but requires the consumer to include the price update in their tx. This architectural choice shapes how Solana DeFi handles price-sensitive operations.
Pull a Pyth price in a swap.
<10 bps.use pyth_solana_receiver_sdk::price_update::{PriceUpdateV2, get_feed_id_from_hex};
#[derive(Accounts)]
pub struct Swap<'info> {
pub user: Signer<'info>,
pub price_update: Account<'info, PriceUpdateV2>,
// ... other accounts
}
pub fn swap(ctx: Context<Swap>, amount_in: u64) -> Result<()> {
let feed_id = get_feed_id_from_hex(
"0xef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d", // SOL/USD
)?;
let price = ctx.accounts.price_update.get_price_no_older_than(
&Clock::get()?,
30, // max staleness in seconds
&feed_id,
)?;
// price.price is i64 (scaled); price.expo is i32 (negative exponent)
// price.conf is u64 (95% confidence interval)
require!(price.conf < (price.price as u64) / 200, ErrCode::WidePythConfidence);
// Use price for swap logic...
Ok(())
}
// Note: the price_update account is freshly written each tx by the caller's
// off-chain ingestion. Stale prices are caller-bug, not protocol bug.