light_token/
utils.rs

1//! Utility functions and default account configurations.
2
3use light_sdk_types::LIGHT_TOKEN_PROGRAM_ID;
4use light_token_interface::{
5    instructions::transfer2::MultiInputTokenDataWithContext, state::Token,
6};
7use solana_account_info::AccountInfo;
8use solana_instruction::AccountMeta;
9use solana_pubkey::Pubkey;
10
11use crate::{error::TokenSdkError, AnchorDeserialize, AnchorSerialize};
12
13pub fn get_token_account_balance(token_account_info: &AccountInfo) -> Result<u64, TokenSdkError> {
14    let data = token_account_info
15        .try_borrow_data()
16        .map_err(|_| TokenSdkError::AccountBorrowFailed)?;
17    Token::amount_from_slice(&data).map_err(|_| TokenSdkError::InvalidAccountData)
18}
19
20/// Check if an account owner is a Light token program.
21///
22/// Returns `Ok(true)` if owner is `LIGHT_TOKEN_PROGRAM_ID`.
23/// Returns `Ok(false)` if owner is SPL Token or Token-2022.
24/// Returns `Err` if owner is unrecognized.
25pub fn is_light_token_owner(owner: &Pubkey) -> Result<bool, TokenSdkError> {
26    let light_token_program_id = Pubkey::from(LIGHT_TOKEN_PROGRAM_ID);
27
28    if owner == &light_token_program_id {
29        return Ok(true);
30    }
31
32    let spl_token = Pubkey::from(light_token_types::SPL_TOKEN_PROGRAM_ID);
33    let spl_token_2022 = Pubkey::from(light_token_types::SPL_TOKEN_2022_PROGRAM_ID);
34
35    if owner == &spl_token_2022 || owner == &spl_token {
36        return Ok(false);
37    }
38
39    Err(TokenSdkError::CannotDetermineAccountType)
40}
41
42/// Check if an account is a Light token account (by checking its owner).
43///
44/// Returns `Ok(true)` if owner is `LIGHT_TOKEN_PROGRAM_ID`.
45/// Returns `Ok(false)` if owner is SPL Token or Token-2022.
46/// Returns `Err` if owner is unrecognized.
47pub fn is_token_account(account_info: &AccountInfo) -> Result<bool, TokenSdkError> {
48    is_light_token_owner(account_info.owner)
49}
50
51pub const CLOSE_TOKEN_ACCOUNT_DISCRIMINATOR: u8 = 9;
52
53#[derive(Debug, Clone, AnchorSerialize, AnchorDeserialize)]
54pub struct PackedCompressedTokenDataWithContext {
55    pub mint: u8,
56    pub source_or_recipient_token_account: u8,
57    pub multi_input_token_data_with_context: MultiInputTokenDataWithContext,
58}
59
60pub fn account_meta_from_account_info(account_info: &AccountInfo) -> AccountMeta {
61    AccountMeta {
62        pubkey: *account_info.key,
63        is_signer: account_info.is_signer,
64        is_writable: account_info.is_writable,
65    }
66}
67
68#[derive(Debug, Clone, AnchorSerialize, AnchorDeserialize)]
69pub struct AccountInfoToCompress<'info> {
70    pub account_info: AccountInfo<'info>,
71    pub signer_seeds: Vec<Vec<u8>>,
72}
73use light_sdk::constants::REGISTERED_PROGRAM_PDA;
74use light_token_types::{
75    ACCOUNT_COMPRESSION_AUTHORITY_PDA, ACCOUNT_COMPRESSION_PROGRAM_ID, CPI_AUTHORITY_PDA,
76    LIGHT_SYSTEM_PROGRAM_ID, NOOP_PROGRAM_ID, PROGRAM_ID as LIGHT_COMPRESSED_TOKEN_PROGRAM_ID,
77};
78
79/// Standard pubkeys for compressed token instructions
80#[derive(Debug, Copy, Clone)]
81pub struct TokenDefaultAccounts {
82    pub light_system_program: Pubkey,
83    pub registered_program_pda: Pubkey,
84    pub noop_program: Pubkey,
85    pub account_compression_authority: Pubkey,
86    pub account_compression_program: Pubkey,
87    pub self_program: Pubkey,
88    pub cpi_authority_pda: Pubkey,
89    pub system_program: Pubkey,
90    pub compressed_token_program: Pubkey,
91}
92
93impl Default for TokenDefaultAccounts {
94    fn default() -> Self {
95        Self {
96            light_system_program: Pubkey::from(LIGHT_SYSTEM_PROGRAM_ID),
97            registered_program_pda: Pubkey::from(REGISTERED_PROGRAM_PDA),
98            noop_program: Pubkey::from(NOOP_PROGRAM_ID),
99            account_compression_authority: Pubkey::from(ACCOUNT_COMPRESSION_AUTHORITY_PDA),
100            account_compression_program: Pubkey::from(ACCOUNT_COMPRESSION_PROGRAM_ID),
101            self_program: Pubkey::from(LIGHT_COMPRESSED_TOKEN_PROGRAM_ID),
102            cpi_authority_pda: Pubkey::from(CPI_AUTHORITY_PDA),
103            system_program: Pubkey::default(),
104            compressed_token_program: Pubkey::from(LIGHT_TOKEN_PROGRAM_ID),
105        }
106    }
107}