web3_utils/
pyth.rs

1use solana_program::{
2    account_info::AccountInfo,
3    clock::Clock,
4    msg,
5    program_error::ProgramError,
6    pubkey::{Pubkey},
7    pubkey,
8    sysvar::Sysvar,
9};
10use pyth_solana_receiver_sdk::{
11    self,
12    price_update::Price,
13};
14use std::convert::TryInto;
15
16use crate::{check::{check_account_key}, price_update::OriginSolanaPriceUpdateV2};
17
18pub const SOL_MINT: Pubkey = pubkey!("So11111111111111111111111111111111111111112");
19
20pub const PYTH_SOL_USD_FEED: Pubkey = pubkey!("7UVimffxr9ow1uXYxsr4LHAcV58mLzhmwaeKvJ1pjLiE");
21
22pub const PRICE_FEED_DISCRIMATOR: [u8; 8] = [34, 241, 35, 99, 157, 126, 244, 205];
23
24pub const PYTH_PRICE_FEED: [u8; 32] = [
25    239, 13, 139, 111, 218, 44, 235, 164, 29, 161, 93, 64, 149, 209, 218, 57, 42, 13,
26    47, 142, 208, 198, 199, 188, 15, 76, 250, 200, 194, 128, 181, 109,
27];
28
29pub fn parse_price(data: &[u8]) -> Result<OriginSolanaPriceUpdateV2, ProgramError> {
30    // now the pyth accounts are anchor account
31    let suffix = &data[..8];
32    if suffix != PRICE_FEED_DISCRIMATOR {
33        msg!("discrimator err");
34        return Err(ProgramError::InvalidArgument);
35    }
36    let update = OriginSolanaPriceUpdateV2::new(data)?;
37
38    Ok(update)
39}
40 
41pub fn get_oracle_price_fp32(
42    account: &AccountInfo,
43    clock: &Clock,
44    maximum_age: u64,
45) -> Result<u64, ProgramError> {
46    check_account_key(account, &PYTH_SOL_USD_FEED)?;
47    msg!("pyth account ok");
48
49    let data = &account.data.borrow();
50    let update = parse_price(data)?;
51    msg!("get the update ok");
52
53    let Price { price, exponent, .. } = update.0
54        .get_price_no_older_than(clock, maximum_age, &PYTH_PRICE_FEED)
55        .map_err(|_| ProgramError::InvalidArgument)?;
56    msg!("get the price ok");
57
58    let price = if exponent > 0 {
59        ((price as u128) << 32) * 10u128.pow(exponent as u32)
60    } else {
61        ((price as u128) << 32) / 10u128.pow((-exponent) as u32)
62    };
63
64    let corrected_price = (price * 10u128.pow(6)) / 10u128.pow(9);
65
66    let final_price: u64 = corrected_price
67        .try_into()
68        .map_err(|_| ProgramError::InvalidArgument)?;
69    msg!("get the correct price ok");
70
71    msg!("Pyth SOL/USD FP32 price: {:?}", final_price);
72
73    Ok(final_price)
74}
75
76
77pub fn get_domain_price_sol(
78    domain_price_usd: u64,
79    sol_pyth_feed_account: &AccountInfo,
80) -> Result<u64, ProgramError> {
81
82    let clock = Clock::get()
83        .map_err(|_| ProgramError::InvalidArgument)?;
84    msg!("get clock ok");
85
86    #[cfg(feature="devnet")]
87    let query_deviation = 6000;
88    #[cfg(not(feature="devnet"))]
89    let query_deviation = 60;
90
91    msg!("now the deviation: {:?}", query_deviation);
92
93    let sol_price = get_oracle_price_fp32(
94        &sol_pyth_feed_account, &clock, query_deviation)
95        .map_err(|_| ProgramError::InvalidArgument)?;
96
97    Ok(domain_price_usd * sol_price)
98}