gmsol_decode/value/
account.rs

1use std::marker::PhantomData;
2
3use solana_sdk::pubkey::Pubkey;
4
5use crate::{decoder::AccountAccessDecoder, AccountAccess, Decode, DecodeError, Decoder, Visitor};
6
7use super::OwnedData;
8
9/// A decoded account.
10#[derive(Debug, Clone, Copy)]
11pub struct Account<T> {
12    pubkey: Pubkey,
13    lamports: u64,
14    slot: u64,
15    data: OwnedData<T>,
16}
17
18impl<T> Account<T> {
19    /// Get the owner of the account.
20    pub fn owner(&self) -> &Pubkey {
21        self.data.owner()
22    }
23
24    /// Get the address of the account.
25    pub fn pubkey(&self) -> &Pubkey {
26        &self.pubkey
27    }
28
29    /// Get lamports of the account.
30    pub fn lamports(&self) -> u64 {
31        self.lamports
32    }
33
34    /// Get the data of the account.
35    pub fn data(&self) -> &T {
36        self.data.data()
37    }
38
39    /// Convert into the inner data.
40    pub fn into_data(self) -> T {
41        self.data.into_data()
42    }
43
44    /// Get the slot at which the account was updated.
45    pub fn slot(&self) -> u64 {
46        self.slot
47    }
48}
49
50impl<T> Decode for Account<T>
51where
52    T: Decode,
53{
54    fn decode<D: Decoder>(decoder: D) -> Result<Self, DecodeError> {
55        struct AccountVisitor<T>(PhantomData<T>);
56
57        impl<T: Decode> Visitor for AccountVisitor<T> {
58            type Value = Account<T>;
59
60            fn visit_account(
61                self,
62                account: impl AccountAccess,
63            ) -> Result<Self::Value, DecodeError> {
64                Ok(Account {
65                    pubkey: account.pubkey()?,
66                    lamports: account.lamports()?,
67                    slot: account.slot()?,
68                    data: OwnedData::<T>::decode(AccountAccessDecoder::new(account))?,
69                })
70            }
71        }
72
73        decoder.decode_account(AccountVisitor(PhantomData))
74    }
75}