orca_sol/accounts/
oracle.rs1use crate::types::{AdaptiveFeeConstants, AdaptiveFeeVariables};
9use borsh::BorshDeserialize;
10use borsh::BorshSerialize;
11use solana_pubkey::Pubkey;
12
13#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
14#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
15pub struct Oracle {
16 pub discriminator: [u8; 8],
17 #[cfg_attr(
18 feature = "serde",
19 serde(with = "serde_with::As::<serde_with::DisplayFromStr>")
20 )]
21 pub whirlpool: Pubkey,
22 pub trade_enable_timestamp: u64,
23 pub adaptive_fee_constants: AdaptiveFeeConstants,
24 pub adaptive_fee_variables: AdaptiveFeeVariables,
25 #[cfg_attr(feature = "serde", serde(with = "serde_with::As::<serde_with::Bytes>"))]
26 pub reserved: [u8; 128],
27}
28
29impl Oracle {
30 pub const LEN: usize = 254;
31
32 #[inline(always)]
33 pub fn from_bytes(data: &[u8]) -> Result<Self, std::io::Error> {
34 let mut data = data;
35 Self::deserialize(&mut data)
36 }
37}
38
39impl<'a> TryFrom<&solana_account_info::AccountInfo<'a>> for Oracle {
40 type Error = std::io::Error;
41
42 fn try_from(account_info: &solana_account_info::AccountInfo<'a>) -> Result<Self, Self::Error> {
43 let mut data: &[u8] = &(*account_info.data).borrow();
44 Self::deserialize(&mut data)
45 }
46}
47
48#[cfg(feature = "fetch")]
49pub fn fetch_oracle(
50 rpc: &solana_client::rpc_client::RpcClient,
51 address: &solana_pubkey::Pubkey,
52) -> Result<crate::shared::DecodedAccount<Oracle>, std::io::Error> {
53 let accounts = fetch_all_oracle(rpc, &[*address])?;
54 Ok(accounts[0].clone())
55}
56
57#[cfg(feature = "fetch")]
58pub fn fetch_all_oracle(
59 rpc: &solana_client::rpc_client::RpcClient,
60 addresses: &[solana_pubkey::Pubkey],
61) -> Result<Vec<crate::shared::DecodedAccount<Oracle>>, std::io::Error> {
62 let accounts = rpc
63 .get_multiple_accounts(addresses)
64 .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;
65 let mut decoded_accounts: Vec<crate::shared::DecodedAccount<Oracle>> = Vec::new();
66 for i in 0..addresses.len() {
67 let address = addresses[i];
68 let account = accounts[i].as_ref().ok_or(std::io::Error::new(
69 std::io::ErrorKind::Other,
70 format!("Account not found: {}", address),
71 ))?;
72 let data = Oracle::from_bytes(&account.data)?;
73 decoded_accounts.push(crate::shared::DecodedAccount {
74 address,
75 account: account.clone(),
76 data,
77 });
78 }
79 Ok(decoded_accounts)
80}
81
82#[cfg(feature = "fetch")]
83pub fn fetch_maybe_oracle(
84 rpc: &solana_client::rpc_client::RpcClient,
85 address: &solana_pubkey::Pubkey,
86) -> Result<crate::shared::MaybeAccount<Oracle>, std::io::Error> {
87 let accounts = fetch_all_maybe_oracle(rpc, &[*address])?;
88 Ok(accounts[0].clone())
89}
90
91#[cfg(feature = "fetch")]
92pub fn fetch_all_maybe_oracle(
93 rpc: &solana_client::rpc_client::RpcClient,
94 addresses: &[solana_pubkey::Pubkey],
95) -> Result<Vec<crate::shared::MaybeAccount<Oracle>>, std::io::Error> {
96 let accounts = rpc
97 .get_multiple_accounts(addresses)
98 .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;
99 let mut decoded_accounts: Vec<crate::shared::MaybeAccount<Oracle>> = Vec::new();
100 for i in 0..addresses.len() {
101 let address = addresses[i];
102 if let Some(account) = accounts[i].as_ref() {
103 let data = Oracle::from_bytes(&account.data)?;
104 decoded_accounts.push(crate::shared::MaybeAccount::Exists(
105 crate::shared::DecodedAccount {
106 address,
107 account: account.clone(),
108 data,
109 },
110 ));
111 } else {
112 decoded_accounts.push(crate::shared::MaybeAccount::NotFound(address));
113 }
114 }
115 Ok(decoded_accounts)
116}
117
118#[cfg(feature = "anchor")]
119impl anchor_lang::AccountDeserialize for Oracle {
120 fn try_deserialize_unchecked(buf: &mut &[u8]) -> anchor_lang::Result<Self> {
121 Ok(Self::deserialize(buf)?)
122 }
123}
124
125#[cfg(feature = "anchor")]
126impl anchor_lang::AccountSerialize for Oracle {}
127
128#[cfg(feature = "anchor")]
129impl anchor_lang::Owner for Oracle {
130 fn owner() -> Pubkey {
131 crate::WHIRLPOOL_ID
132 }
133}
134
135#[cfg(feature = "anchor-idl-build")]
136impl anchor_lang::IdlBuild for Oracle {}
137
138#[cfg(feature = "anchor-idl-build")]
139impl anchor_lang::Discriminator for Oracle {
140 const DISCRIMINATOR: [u8; 8] = [0; 8];
141}