Skip to main content

light_token/instruction/
approve.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/// # Approve a delegate for a Light Token account:
9/// ```rust
10/// # use solana_pubkey::Pubkey;
11/// # use light_token::instruction::Approve;
12/// # let token_account = Pubkey::new_unique();
13/// # let delegate = Pubkey::new_unique();
14/// # let owner = Pubkey::new_unique();
15/// # let fee_payer = Pubkey::new_unique();
16/// let instruction = Approve {
17///     token_account,
18///     delegate,
19///     owner,
20///     amount: 100,
21///     fee_payer,
22/// }.instruction()?;
23/// # Ok::<(), solana_program_error::ProgramError>(())
24/// ```
25pub struct Approve {
26    /// Light Token account to approve delegation for
27    pub token_account: Pubkey,
28    /// Delegate to approve
29    pub delegate: Pubkey,
30    /// Owner of the Light Token account (readonly signer)
31    pub owner: Pubkey,
32    /// Amount of tokens to delegate
33    pub amount: u64,
34    /// Fee payer for rent top-ups.
35    pub fee_payer: Pubkey,
36}
37
38/// # Approve Light Token via CPI:
39/// ```rust,no_run
40/// # use light_token::instruction::ApproveCpi;
41/// # use solana_account_info::AccountInfo;
42/// # let token_account: AccountInfo = todo!();
43/// # let delegate: AccountInfo = todo!();
44/// # let owner: AccountInfo = todo!();
45/// # let system_program: AccountInfo = todo!();
46/// # let fee_payer: AccountInfo = todo!();
47/// ApproveCpi {
48///     token_account,
49///     delegate,
50///     owner,
51///     system_program,
52///     amount: 100,
53///     fee_payer,
54/// }
55/// .invoke()?;
56/// # Ok::<(), solana_program_error::ProgramError>(())
57/// ```
58pub struct ApproveCpi<'info> {
59    pub token_account: AccountInfo<'info>,
60    pub delegate: AccountInfo<'info>,
61    pub owner: AccountInfo<'info>,
62    pub system_program: AccountInfo<'info>,
63    pub amount: u64,
64    /// Fee payer for rent top-ups.
65    pub fee_payer: AccountInfo<'info>,
66}
67
68impl<'info> ApproveCpi<'info> {
69    pub fn instruction(&self) -> Result<Instruction, ProgramError> {
70        Approve::from(self).instruction()
71    }
72
73    pub fn invoke(self) -> Result<(), ProgramError> {
74        let instruction = Approve::from(&self).instruction()?;
75        let account_infos = [
76            self.token_account,
77            self.delegate,
78            self.owner,
79            self.system_program,
80            self.fee_payer,
81        ];
82        invoke(&instruction, &account_infos)
83    }
84
85    pub fn invoke_signed(self, signer_seeds: &[&[&[u8]]]) -> Result<(), ProgramError> {
86        let instruction = Approve::from(&self).instruction()?;
87        let account_infos = [
88            self.token_account,
89            self.delegate,
90            self.owner,
91            self.system_program,
92            self.fee_payer,
93        ];
94        invoke_signed(&instruction, &account_infos, signer_seeds)
95    }
96}
97
98impl<'info> From<&ApproveCpi<'info>> for Approve {
99    fn from(cpi: &ApproveCpi<'info>) -> Self {
100        Self {
101            token_account: *cpi.token_account.key,
102            delegate: *cpi.delegate.key,
103            owner: *cpi.owner.key,
104            amount: cpi.amount,
105            fee_payer: *cpi.fee_payer.key,
106        }
107    }
108}
109
110impl Approve {
111    pub fn instruction(self) -> Result<Instruction, ProgramError> {
112        let mut data = vec![4u8]; // CTokenApprove discriminator
113        data.extend_from_slice(&self.amount.to_le_bytes());
114
115        Ok(Instruction {
116            program_id: Pubkey::from(LIGHT_TOKEN_PROGRAM_ID),
117            accounts: vec![
118                AccountMeta::new(self.token_account, false),
119                AccountMeta::new_readonly(self.delegate, false),
120                AccountMeta::new_readonly(self.owner, true),
121                AccountMeta::new_readonly(Pubkey::default(), false),
122                AccountMeta::new(self.fee_payer, true),
123            ],
124            data,
125        })
126    }
127}