light_token/instruction/
thaw.rs

1use light_sdk_types::LIGHT_TOKEN_PROGRAM_ID;
2use solana_account_info::AccountInfo;
3use solana_cpi::{invoke, invoke_signed};
4use solana_instruction::{AccountMeta, Instruction};
5use solana_program_error::ProgramError;
6use solana_pubkey::Pubkey;
7
8/// # Thaw a frozen Light Token account:
9/// ```rust
10/// # use solana_pubkey::Pubkey;
11/// # use light_token::instruction::Thaw;
12/// # let token_account = Pubkey::new_unique();
13/// # let mint = Pubkey::new_unique();
14/// # let freeze_authority = Pubkey::new_unique();
15/// let instruction = Thaw {
16///     token_account,
17///     mint,
18///     freeze_authority,
19/// }.instruction()?;
20/// # Ok::<(), solana_program_error::ProgramError>(())
21/// ```
22pub struct Thaw {
23    /// Light Token account to thaw
24    pub token_account: Pubkey,
25    /// Mint of the token account
26    pub mint: Pubkey,
27    /// Freeze authority (signer)
28    pub freeze_authority: Pubkey,
29}
30
31/// # Thaw Light Token via CPI:
32/// ```rust,no_run
33/// # use light_token::instruction::ThawCpi;
34/// # use solana_account_info::AccountInfo;
35/// # let token_account: AccountInfo = todo!();
36/// # let mint: AccountInfo = todo!();
37/// # let freeze_authority: AccountInfo = todo!();
38/// ThawCpi {
39///     token_account,
40///     mint,
41///     freeze_authority,
42/// }
43/// .invoke()?;
44/// # Ok::<(), solana_program_error::ProgramError>(())
45/// ```
46pub struct ThawCpi<'info> {
47    pub token_account: AccountInfo<'info>,
48    pub mint: AccountInfo<'info>,
49    pub freeze_authority: AccountInfo<'info>,
50}
51
52impl<'info> ThawCpi<'info> {
53    pub fn instruction(&self) -> Result<Instruction, ProgramError> {
54        Thaw::from(self).instruction()
55    }
56
57    pub fn invoke(self) -> Result<(), ProgramError> {
58        let instruction = Thaw::from(&self).instruction()?;
59        let account_infos = [self.token_account, self.mint, self.freeze_authority];
60        invoke(&instruction, &account_infos)
61    }
62
63    pub fn invoke_signed(self, signer_seeds: &[&[&[u8]]]) -> Result<(), ProgramError> {
64        let instruction = Thaw::from(&self).instruction()?;
65        let account_infos = [self.token_account, self.mint, self.freeze_authority];
66        invoke_signed(&instruction, &account_infos, signer_seeds)
67    }
68}
69
70impl<'info> From<&ThawCpi<'info>> for Thaw {
71    fn from(cpi: &ThawCpi<'info>) -> Self {
72        Self {
73            token_account: *cpi.token_account.key,
74            mint: *cpi.mint.key,
75            freeze_authority: *cpi.freeze_authority.key,
76        }
77    }
78}
79
80impl Thaw {
81    pub fn instruction(self) -> Result<Instruction, ProgramError> {
82        Ok(Instruction {
83            program_id: Pubkey::from(LIGHT_TOKEN_PROGRAM_ID),
84            accounts: vec![
85                AccountMeta::new(self.token_account, false),
86                AccountMeta::new_readonly(self.mint, false),
87                AccountMeta::new_readonly(self.freeze_authority, true),
88            ],
89            data: vec![11u8], // CTokenThawAccount discriminator
90        })
91    }
92}