state.rs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // This file is the pyth default implementation for using PriceAccount in your Anchor context.
  2. use anchor_lang::prelude::*;
  3. use pyth_sdk_solana::state::load_price_account;
  4. use std::ops::Deref;
  5. use std::str::FromStr;
  6. // import the error code from the error.rs file
  7. use crate::error::ErrorCode;
  8. #[derive(Clone)]
  9. pub struct PriceFeed(pyth_sdk::PriceFeed);
  10. impl anchor_lang::Owner for PriceFeed {
  11. fn owner() -> Pubkey {
  12. // The mainnet Pyth program ID
  13. let oracle_addr = "FsJ3A3u2vn5cTVofAjvy6y5kwABJAqYWpe4975bi2epH";
  14. Pubkey::from_str(oracle_addr).unwrap()
  15. }
  16. }
  17. impl anchor_lang::AccountDeserialize for PriceFeed {
  18. fn try_deserialize_unchecked(data: &mut &[u8]) -> Result<Self> {
  19. let account = load_price_account(data).map_err(|_x| error!(ErrorCode::PythError))?;
  20. let zeros: [u8; 32] = [0; 32];
  21. let dummy_key = Pubkey::new_from_array(zeros);
  22. let feed = account.to_price_feed(&dummy_key);
  23. Ok(PriceFeed(feed))
  24. }
  25. }
  26. impl anchor_lang::AccountSerialize for PriceFeed {
  27. fn try_serialize<W: std::io::Write>(&self, _writer: &mut W) -> std::result::Result<(), Error> {
  28. Err(error!(ErrorCode::TryToSerializePriceAccount))
  29. }
  30. }
  31. impl Deref for PriceFeed {
  32. type Target = pyth_sdk::PriceFeed;
  33. fn deref(&self) -> &Self::Target {
  34. &self.0
  35. }
  36. }