use light_sdk_types::LIGHT_TOKEN_PROGRAM_ID;
use solana_account_info::AccountInfo;
use solana_cpi::{invoke, invoke_signed};
use solana_instruction::{AccountMeta, Instruction};
use solana_program_error::ProgramError;
use solana_pubkey::Pubkey;
pub struct Approve {
pub token_account: Pubkey,
pub delegate: Pubkey,
pub owner: Pubkey,
pub amount: u64,
pub fee_payer: Pubkey,
}
pub struct ApproveCpi<'info> {
pub token_account: AccountInfo<'info>,
pub delegate: AccountInfo<'info>,
pub owner: AccountInfo<'info>,
pub system_program: AccountInfo<'info>,
pub amount: u64,
pub fee_payer: AccountInfo<'info>,
}
impl<'info> ApproveCpi<'info> {
pub fn instruction(&self) -> Result<Instruction, ProgramError> {
Approve::from(self).instruction()
}
pub fn invoke(self) -> Result<(), ProgramError> {
let instruction = Approve::from(&self).instruction()?;
let account_infos = [
self.token_account,
self.delegate,
self.owner,
self.system_program,
self.fee_payer,
];
invoke(&instruction, &account_infos)
}
pub fn invoke_signed(self, signer_seeds: &[&[&[u8]]]) -> Result<(), ProgramError> {
let instruction = Approve::from(&self).instruction()?;
let account_infos = [
self.token_account,
self.delegate,
self.owner,
self.system_program,
self.fee_payer,
];
invoke_signed(&instruction, &account_infos, signer_seeds)
}
}
impl<'info> From<&ApproveCpi<'info>> for Approve {
fn from(cpi: &ApproveCpi<'info>) -> Self {
Self {
token_account: *cpi.token_account.key,
delegate: *cpi.delegate.key,
owner: *cpi.owner.key,
amount: cpi.amount,
fee_payer: *cpi.fee_payer.key,
}
}
}
impl Approve {
pub fn instruction(self) -> Result<Instruction, ProgramError> {
let mut data = vec![4u8]; data.extend_from_slice(&self.amount.to_le_bytes());
Ok(Instruction {
program_id: Pubkey::from(LIGHT_TOKEN_PROGRAM_ID),
accounts: vec![
AccountMeta::new(self.token_account, false),
AccountMeta::new_readonly(self.delegate, false),
AccountMeta::new_readonly(self.owner, true),
AccountMeta::new_readonly(Pubkey::default(), false),
AccountMeta::new(self.fee_payer, true),
],
data,
})
}
}