pub struct PriceUpdateV2 {
pub write_authority: PubkeyBytes,
pub verification_level: VerificationLevel,
pub price_message: PriceFeedMessage,
pub posted_slot: u64,
}
Fields§
§verification_level: VerificationLevel
§price_message: PriceFeedMessage
§posted_slot: u64
Implementations§
Source§impl PriceUpdateV2
impl PriceUpdateV2
pub const LEN: usize = 134usize
Sourcepub fn get_price_update_v2_from_bytes(v: &[u8]) -> PriceUpdateV2
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
impl PriceUpdateV2
Sourcepub fn get_price_unchecked(
&self,
feed_id: Option<&FeedId>,
) -> Result<Price, GetPriceError>
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.
Sourcepub 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>
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(())
}
Sourcepub fn get_price_no_older_than(
&self,
unix_timestamp: i64,
maximum_age: u64,
feed_id: Option<&FeedId>,
) -> Result<Price, GetPriceError>
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
impl Clone for PriceUpdateV2
Source§fn clone(&self) -> PriceUpdateV2
fn clone(&self) -> PriceUpdateV2
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more