phoenix_onchain_mm/
oracle.rs

1use anchor_lang::prelude::*;
2use pyth_sdk_solana::state::load_price_account;
3use std::ops::Deref;
4use std::str::FromStr;
5
6use crate::errors::StrategyError;
7
8#[account]
9#[derive(Debug, Copy)]
10pub struct OracleConfig {
11    pub oracle_base_account: Pubkey,
12    pub oracle_quote_account: Pubkey,
13}
14
15impl OracleConfig {
16    pub const LEN: usize = 64;
17}
18
19#[derive(Clone)]
20pub struct PriceFeed(pyth_sdk::PriceFeed);
21
22impl anchor_lang::Owner for PriceFeed {
23    fn owner() -> Pubkey {
24        // Make sure the owner is the pyth oracle account on solana devnet
25        let oracle_addr = "gSbePebfvPy7tRqimPoVecS2UsBvYv46ynrzWocc92s";
26        Pubkey::from_str(oracle_addr).unwrap()
27    }
28}
29
30impl anchor_lang::AccountDeserialize for PriceFeed {
31    fn try_deserialize_unchecked(data: &mut &[u8]) -> Result<Self> {
32        let account = load_price_account(data).map_err(|_x| error!(StrategyError::PythError))?;
33
34        // Use a dummy key since the key field will be removed from the SDK
35        let zeros: [u8; 32] = [0; 32];
36        let dummy_key = Pubkey::new_from_array(zeros);
37        let feed = account.to_price_feed(&dummy_key);
38        Ok(PriceFeed(feed))
39    }
40}
41
42impl anchor_lang::AccountSerialize for PriceFeed {
43    fn try_serialize<W: std::io::Write>(&self, _writer: &mut W) -> std::result::Result<(), Error> {
44        Err(error!(StrategyError::TryToSerializePriceAccount))
45    }
46}
47
48impl Deref for PriceFeed {
49    type Target = pyth_sdk::PriceFeed;
50
51    fn deref(&self) -> &Self::Target {
52        &self.0
53    }
54}