light_token/instruction/
approve.rs1use 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
8pub struct Approve {
26 pub token_account: Pubkey,
28 pub delegate: Pubkey,
30 pub owner: Pubkey,
32 pub amount: u64,
34 pub fee_payer: Pubkey,
36}
37
38pub 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 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]; 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}