#anchor #solana #pyth

pyth-min

Minimal sdk for interacting with Pyth pull oracles on Solana

3 releases

0.1.2 Nov 15, 2024
0.1.1 Jun 11, 2024
0.1.0 Jun 11, 2024

#753 in Magic Beans

Download history 12/week @ 2024-09-21 56/week @ 2024-11-09 54/week @ 2024-11-16

110 downloads per month

Apache-2.0

33KB
450 lines

An absolutely bare-minimum sdk, using zero dependencies, for consuming PythV2 prices generated by the Pyth Solana Receiver.

USAGE

Take a Pyth Price Feed Account as an UncheckedAccount. Here is example of this type of account on mainnet: https://solana.fm/address/7UVimffxr9ow1uXYxsr4LHAcV58mLzhmwaeKvJ1pjLiE

#[derive(Accounts)]
pub struct Some_Instruction<'info> {
    /// CHECK: You may also want to validate `owner = PYTH_PID`
    pub price_acc: UncheckedAccount<'info>,
}

Then simply borrow the data, minus the 8-byte Anchor discrminator:

let data = &ctx.accounts.price_acc.try_borrow_data()?[..][8..];
let price_v2 = PriceUpdateV2::get_price_update_v2_from_bytes(data);

let price = price_v2.get_price_no_older_than(
    &Clock::get()?.unix_timestamp,
    MAXIMUM_AGE, // in seconds
    None, // Pass the feed ID in bytes if you want to validate it
)?;

Or if you absolutely do not care about any verification whatsoever:

price_v2.get_price_unchecked(None)

We suggest you validate the discriminator (see DISCRIMINATOR_AS_HEX) unless you are absolutely sure this is the correct account!

No runtime deps