pyth_sdk_solana/
lib.rs

1//! A Rust library for consuming price feeds from the [pyth.network](https://pyth.network/) oracle on the Solana network.
2//!
3//! Please see the [crates.io page](https://crates.io/crates/pyth-sdk-solana/) for documentation and example usage.
4
5pub use self::error::PythError;
6
7mod error;
8pub mod state;
9
10use solana_program::account_info::{
11    Account,
12    AccountInfo,
13    IntoAccountInfo,
14};
15use solana_program::pubkey::Pubkey;
16
17use state::{
18    load_price_account,
19    GenericPriceAccount,
20    SolanaPriceAccount,
21};
22
23pub use pyth_sdk::{
24    Price,
25    PriceFeed,
26    PriceIdentifier,
27    ProductIdentifier,
28};
29
30/// Maximum valid slot period before price is considered to be stale.
31pub const VALID_SLOT_PERIOD: u64 = 25;
32
33/// Loads Pyth Feed Price from Price Account Info.
34#[deprecated(note = "solana-specific, use SolanaPriceAccount::account_info_to_feed instead.")]
35pub fn load_price_feed_from_account_info(
36    price_account_info: &AccountInfo,
37) -> Result<PriceFeed, PythError> {
38    SolanaPriceAccount::account_info_to_feed(price_account_info)
39}
40
41/// Loads Pyth Price Feed from Account when using Solana Client.
42///
43/// It is a helper function which constructs Account Info when reading Account in clients.
44#[deprecated(note = "solana-specific, use SolanaPriceAccount::account_to_feed instead.")]
45pub fn load_price_feed_from_account(
46    price_key: &Pubkey,
47    price_account: &mut impl Account,
48) -> Result<PriceFeed, PythError> {
49    SolanaPriceAccount::account_to_feed(price_key, price_account)
50}
51
52impl<const N: usize, T: 'static> GenericPriceAccount<N, T>
53where
54    T: Default,
55    T: Copy,
56{
57    pub fn account_info_to_feed(price_account_info: &AccountInfo) -> Result<PriceFeed, PythError> {
58        load_price_account::<N, T>(
59            *price_account_info
60                .try_borrow_data()
61                .map_err(|_| PythError::InvalidAccountData)?,
62        )
63        .map(|acc| acc.to_price_feed(price_account_info.key))
64    }
65
66    pub fn account_to_feed(
67        price_key: &Pubkey,
68        price_account: &mut impl Account,
69    ) -> Result<PriceFeed, PythError> {
70        let price_account_info = (price_key, price_account).into_account_info();
71        Self::account_info_to_feed(&price_account_info)
72    }
73}