2 releases
0.1.1 | Jun 11, 2024 |
---|---|
0.1.0 | Jun 11, 2024 |
#4 in #pyth
29KB
342 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!