carbon-token-2022-decoder 1.0.0

Carbon Token 2022 Decoder
Documentation
//! This code was AUTOGENERATED using the Codama library.
use {
    crate::types::{AccountState, Extension},
    solana_pubkey::Pubkey,
    spl_token_2022::extension::BaseStateWithExtensions,
};

#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, borsh::BorshSerialize, borsh::BorshDeserialize, PartialEq)]
pub struct Token {
    /// The mint associated with this account.
    pub mint: Pubkey,
    /// The owner of this account.
    pub owner: Pubkey,
    /// The amount of tokens this account holds.
    pub amount: u64,
    /// If `delegate` is `Some` then `delegated_amount` represents
    /// the amount authorized by the delegate.
    pub delegate: Option<Pubkey>,
    /// The account's state.
    pub state: AccountState,
    /// If is_native.is_some, this is a native token, and the value logs the
    /// rent-exempt reserve. An Account is required to be rent-exempt, so
    /// the value is used by the Processor to ensure that wrapped SOL
    /// accounts do not drop below this threshold.
    pub is_native: Option<u64>,
    /// The amount delegated.
    pub delegated_amount: u64,
    /// Optional authority to close the account.
    pub close_authority: Option<Pubkey>,
    /// The extensions activated on the token account.
    pub extensions: Option<Vec<Extension>>,
}

impl From<spl_token_2022::extension::StateWithExtensions<'_, spl_token_2022::state::Account>>
    for Token
{
    fn from(
        value: spl_token_2022::extension::StateWithExtensions<'_, spl_token_2022::state::Account>,
    ) -> Self {
        let extensions = value.get_extension_types().ok().map(|extensions| {
            extensions
                .iter()
                .filter_map(|extension_type| {
                    crate::types::Extension::from_account_and_type(&value, extension_type)
                })
                .collect::<Vec<_>>()
        });

        Token {
            mint: value.base.mint,
            owner: value.base.owner,
            amount: value.base.amount,
            delegate: value.base.delegate.into(),
            state: match value.base.state {
                spl_token_2022::state::AccountState::Uninitialized => {
                    crate::types::AccountState::Uninitialized
                }
                spl_token_2022::state::AccountState::Initialized => {
                    crate::types::AccountState::Initialized
                }
                spl_token_2022::state::AccountState::Frozen => crate::types::AccountState::Frozen,
            },
            is_native: value.base.is_native.into(),
            delegated_amount: value.base.delegated_amount,
            close_authority: value.base.close_authority.into(),
            extensions,
        }
    }
}