PriceUpdateV2

Struct PriceUpdateV2 

Source
pub struct PriceUpdateV2 {
    pub write_authority: PubkeyBytes,
    pub verification_level: VerificationLevel,
    pub price_message: PriceFeedMessage,
    pub posted_slot: u64,
}

Fields§

§write_authority: PubkeyBytes§verification_level: VerificationLevel§price_message: PriceFeedMessage§posted_slot: u64

Implementations§

Source§

impl PriceUpdateV2

Source

pub const LEN: usize = 134usize

Source

pub fn get_price_update_v2_from_bytes(v: &[u8]) -> PriceUpdateV2

Interpret a PriceUpdateV2 from a byte slice

If you have fetched a “Price Feed Account” on chain, you probably want to get the data with

let data = &ctx.accounts.price.try_borrow_data()?[..];

Skip the first 8 bytes (Anchor discriminator)

let message_bytes = &data[8..];

Source§

impl PriceUpdateV2

Source

pub fn get_price_unchecked( &self, feed_id: Option<&FeedId>, ) -> Result<Price, GetPriceError>

Get a Price from a PriceUpdateV2 account for a given FeedId.

§Warning

This function does not check :

  • How recent the price is
  • Whether the price update has been verified

It is therefore unsafe to use this function without any extra checks, as it allows for the possibility of using unverified or outdated price updates.

Source

pub fn get_price_no_older_than_with_custom_verification_level( &self, unix_timestamp: i64, maximum_age: u64, feed_id: Option<&FeedId>, verification_level: VerificationLevel, ) -> Result<Price, GetPriceError>

Get a Price from a PriceUpdateV2 account for a given FeedId no older than maximum_age with customizable verification level.

§Warning

Lowering the verification level from Full to Partial increases the risk of using a malicious price update. Please read the documentation for VerificationLevel for more information.

§Example
use pyth_solana_receiver_sdk::price_update::{get_feed_id_from_hex, VerificationLevel, PriceUpdateV2};
use anchor_lang::prelude::*;

const MAXIMUM_AGE : u64 = 30;
const FEED_ID: &str = "0xef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d"; // SOL/USD

#[derive(Accounts)]
#[instruction(amount_in_usd : u64)]
pub struct ReadPriceAccount<'info> {
    pub price_update: Account<'info, PriceUpdateV2>,
}

pub fn read_price_account(ctx : Context<ReadPriceAccount>) -> Result<()> {
    let price_update = &mut ctx.accounts.price_update;
    let price = price_update.get_price_no_older_than_with_custom_verification_level(&Clock::get()?.unix_timestamp, MAXIMUM_AGE, &get_feed_id_from_hex(FEED_ID)?, VerificationLevel::Partial{num_signatures: 5})?;
    Ok(())
}
Source

pub fn get_price_no_older_than( &self, unix_timestamp: i64, maximum_age: u64, feed_id: Option<&FeedId>, ) -> Result<Price, GetPriceError>

Get a Price from a PriceUpdateV2 account for a given FeedId no older than maximum_age with Full verification.

§Example
use pyth_solana_receiver_sdk::price_update::{get_feed_id_from_hex, PriceUpdateV2};
use anchor_lang::prelude::*;

const MAXIMUM_AGE : u64 = 30;
const FEED_ID: &str = "0xef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d"; // SOL/USD

#[derive(Accounts)]
#[instruction(amount_in_usd : u64)]
pub struct ReadPriceAccount<'info> {
    pub price_update: Account<'info, PriceUpdateV2>,
}

pub fn read_price_account(ctx : Context<ReadPriceAccount>) -> Result<()> {
    let price_update = &mut ctx.accounts.price_update;
    let price = price_update.get_price_no_older_than(&Clock::get()?.unix_timestamp, MAXIMUM_AGE, &get_feed_id_from_hex(FEED_ID)?)?;
    Ok(())
}

Trait Implementations§

Source§

impl Clone for PriceUpdateV2

Source§

fn clone(&self) -> PriceUpdateV2

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PriceUpdateV2

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for PriceUpdateV2

Source§

fn eq(&self, other: &PriceUpdateV2) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Copy for PriceUpdateV2

Source§

impl StructuralPartialEq for PriceUpdateV2

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.