Skip to main content

light_compressed_token_sdk/
utils.rs

1//! Utility functions and default account configurations.
2
3use light_sdk::constants::REGISTERED_PROGRAM_PDA;
4use light_sdk_types::LIGHT_TOKEN_PROGRAM_ID;
5use light_token_interface::{
6    instructions::transfer2::MultiInputTokenDataWithContext, state::Token,
7};
8use light_token_types::{
9    ACCOUNT_COMPRESSION_AUTHORITY_PDA, ACCOUNT_COMPRESSION_PROGRAM_ID, CPI_AUTHORITY_PDA,
10    LIGHT_SYSTEM_PROGRAM_ID, NOOP_PROGRAM_ID, PROGRAM_ID as LIGHT_COMPRESSED_TOKEN_PROGRAM_ID,
11};
12use solana_account_info::AccountInfo;
13use solana_instruction::AccountMeta;
14use solana_pubkey::Pubkey;
15
16use crate::{error::TokenSdkError, AnchorDeserialize, AnchorSerialize};
17
18pub fn get_token_account_balance(token_account_info: &AccountInfo) -> Result<u64, TokenSdkError> {
19    let data = token_account_info
20        .try_borrow_data()
21        .map_err(|_| TokenSdkError::AccountBorrowFailed)?;
22    Token::amount_from_slice(&data).map_err(|_| TokenSdkError::InvalidAccountData)
23}
24
25/// Check if an account owner is a Light token program.
26///
27/// Returns `Ok(true)` if owner is `LIGHT_TOKEN_PROGRAM_ID`.
28/// Returns `Ok(false)` if owner is SPL Token or Token-2022.
29/// Returns `Err` if owner is unrecognized.
30pub fn is_light_token_owner(owner: &Pubkey) -> Result<bool, TokenSdkError> {
31    let light_token_program_id = Pubkey::from(LIGHT_TOKEN_PROGRAM_ID);
32
33    if owner == &light_token_program_id {
34        return Ok(true);
35    }
36
37    let spl_token = Pubkey::from(light_token_types::SPL_TOKEN_PROGRAM_ID);
38    let spl_token_2022 = Pubkey::from(light_token_types::SPL_TOKEN_2022_PROGRAM_ID);
39
40    if owner == &spl_token_2022 || owner == &spl_token {
41        return Ok(false);
42    }
43
44    Err(TokenSdkError::CannotDetermineAccountType)
45}
46
47/// Check if an account is a Light token account (by checking its owner).
48///
49/// Returns `Ok(true)` if owner is `LIGHT_TOKEN_PROGRAM_ID`.
50/// Returns `Ok(false)` if owner is SPL Token or Token-2022.
51/// Returns `Err` if owner is unrecognized.
52pub fn is_token_account(account_info: &AccountInfo) -> Result<bool, TokenSdkError> {
53    is_light_token_owner(account_info.owner)
54}
55
56pub const CLOSE_TOKEN_ACCOUNT_DISCRIMINATOR: u8 = 9;
57
58#[derive(Debug, Clone, AnchorSerialize, AnchorDeserialize)]
59pub struct PackedCompressedTokenDataWithContext {
60    pub mint: u8,
61    pub source_or_recipient_token_account: u8,
62    pub multi_input_token_data_with_context: MultiInputTokenDataWithContext,
63}
64
65pub fn account_meta_from_account_info(account_info: &AccountInfo) -> AccountMeta {
66    AccountMeta {
67        pubkey: *account_info.key,
68        is_signer: account_info.is_signer,
69        is_writable: account_info.is_writable,
70    }
71}
72
73#[derive(Debug, Clone)]
74pub struct AccountInfoToCompress<'info> {
75    pub account_info: AccountInfo<'info>,
76    pub signer_seeds: Vec<Vec<u8>>,
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}