Skip to main content

security_token_client/generated/accounts/
rate.rs

1//! This code was AUTOGENERATED using the codama library.
2//! Please DO NOT EDIT THIS FILE, instead use visitors
3//! to add features, then rerun codama to update it.
4//!
5//! <https://github.com/codama-idl/codama>
6//!
7
8use crate::generated::types::Rounding;
9use borsh::BorshDeserialize;
10use borsh::BorshSerialize;
11
12#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
13#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
14pub struct Rate {
15    pub discriminator: u8,
16    pub rounding: Rounding,
17    pub numerator: u8,
18    pub denominator: u8,
19    pub bump: u8,
20}
21
22impl Rate {
23    pub const LEN: usize = 4;
24
25    #[inline(always)]
26    pub fn from_bytes(data: &[u8]) -> Result<Self, std::io::Error> {
27        let mut data = data;
28        Self::deserialize(&mut data)
29    }
30}
31
32impl<'a> TryFrom<&solana_account_info::AccountInfo<'a>> for Rate {
33    type Error = std::io::Error;
34
35    fn try_from(account_info: &solana_account_info::AccountInfo<'a>) -> Result<Self, Self::Error> {
36        let mut data: &[u8] = &(*account_info.data).borrow();
37        Self::deserialize(&mut data)
38    }
39}
40
41#[cfg(feature = "fetch")]
42pub fn fetch_rate(
43    rpc: &solana_client::rpc_client::RpcClient,
44    address: &solana_pubkey::Pubkey,
45) -> Result<crate::shared::DecodedAccount<Rate>, std::io::Error> {
46    let accounts = fetch_all_rate(rpc, &[*address])?;
47    Ok(accounts[0].clone())
48}
49
50#[cfg(feature = "fetch")]
51pub fn fetch_all_rate(
52    rpc: &solana_client::rpc_client::RpcClient,
53    addresses: &[solana_pubkey::Pubkey],
54) -> Result<Vec<crate::shared::DecodedAccount<Rate>>, std::io::Error> {
55    let accounts = rpc
56        .get_multiple_accounts(addresses)
57        .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;
58    let mut decoded_accounts: Vec<crate::shared::DecodedAccount<Rate>> = Vec::new();
59    for i in 0..addresses.len() {
60        let address = addresses[i];
61        let account = accounts[i].as_ref().ok_or(std::io::Error::new(
62            std::io::ErrorKind::Other,
63            format!("Account not found: {}", address),
64        ))?;
65        let data = Rate::from_bytes(&account.data)?;
66        decoded_accounts.push(crate::shared::DecodedAccount {
67            address,
68            account: account.clone(),
69            data,
70        });
71    }
72    Ok(decoded_accounts)
73}
74
75#[cfg(feature = "fetch")]
76pub fn fetch_maybe_rate(
77    rpc: &solana_client::rpc_client::RpcClient,
78    address: &solana_pubkey::Pubkey,
79) -> Result<crate::shared::MaybeAccount<Rate>, std::io::Error> {
80    let accounts = fetch_all_maybe_rate(rpc, &[*address])?;
81    Ok(accounts[0].clone())
82}
83
84#[cfg(feature = "fetch")]
85pub fn fetch_all_maybe_rate(
86    rpc: &solana_client::rpc_client::RpcClient,
87    addresses: &[solana_pubkey::Pubkey],
88) -> Result<Vec<crate::shared::MaybeAccount<Rate>>, std::io::Error> {
89    let accounts = rpc
90        .get_multiple_accounts(addresses)
91        .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;
92    let mut decoded_accounts: Vec<crate::shared::MaybeAccount<Rate>> = Vec::new();
93    for i in 0..addresses.len() {
94        let address = addresses[i];
95        if let Some(account) = accounts[i].as_ref() {
96            let data = Rate::from_bytes(&account.data)?;
97            decoded_accounts.push(crate::shared::MaybeAccount::Exists(
98                crate::shared::DecodedAccount {
99                    address,
100                    account: account.clone(),
101                    data,
102                },
103            ));
104        } else {
105            decoded_accounts.push(crate::shared::MaybeAccount::NotFound(address));
106        }
107    }
108    Ok(decoded_accounts)
109}
110
111#[cfg(feature = "anchor")]
112impl anchor_lang::AccountDeserialize for Rate {
113    fn try_deserialize_unchecked(buf: &mut &[u8]) -> anchor_lang::Result<Self> {
114        Ok(Self::deserialize(buf)?)
115    }
116}
117
118#[cfg(feature = "anchor")]
119impl anchor_lang::AccountSerialize for Rate {}
120
121#[cfg(feature = "anchor")]
122impl anchor_lang::Owner for Rate {
123    fn owner() -> Pubkey {
124        crate::SECURITY_TOKEN_PROGRAM_ID
125    }
126}
127
128#[cfg(feature = "anchor-idl-build")]
129impl anchor_lang::IdlBuild for Rate {}
130
131#[cfg(feature = "anchor-idl-build")]
132impl anchor_lang::Discriminator for Rate {
133    const DISCRIMINATOR: &[u8] = &[0; 8];
134}