light_token/instruction/
burn.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 Burn {
26 pub source: Pubkey,
28 pub mint: Pubkey,
30 pub amount: u64,
32 pub authority: Pubkey,
34 pub fee_payer: Pubkey,
36}
37
38pub struct BurnCpi<'info> {
59 pub source: AccountInfo<'info>,
60 pub mint: AccountInfo<'info>,
61 pub amount: u64,
62 pub authority: AccountInfo<'info>,
63 pub system_program: AccountInfo<'info>,
64 pub fee_payer: AccountInfo<'info>,
66}
67
68impl<'info> BurnCpi<'info> {
69 pub fn instruction(&self) -> Result<Instruction, ProgramError> {
70 Burn::from(self).instruction()
71 }
72
73 pub fn invoke(self) -> Result<(), ProgramError> {
74 let instruction = Burn::from(&self).instruction()?;
75 let account_infos = [
76 self.source,
77 self.mint,
78 self.authority,
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 = Burn::from(&self).instruction()?;
87 let account_infos = [
88 self.source,
89 self.mint,
90 self.authority,
91 self.system_program,
92 self.fee_payer,
93 ];
94 invoke_signed(&instruction, &account_infos, signer_seeds)
95 }
96}
97
98impl<'info> From<&BurnCpi<'info>> for Burn {
99 fn from(cpi: &BurnCpi<'info>) -> Self {
100 Self {
101 source: *cpi.source.key,
102 mint: *cpi.mint.key,
103 amount: cpi.amount,
104 authority: *cpi.authority.key,
105 fee_payer: *cpi.fee_payer.key,
106 }
107 }
108}
109
110impl_with_top_up!(Burn, BurnWithTopUp);
111
112impl Burn {
113 fn build_instruction(self, max_top_up: Option<u16>) -> Result<Instruction, ProgramError> {
114 let accounts = vec![
115 AccountMeta::new(self.source, false),
116 AccountMeta::new(self.mint, false),
117 AccountMeta::new_readonly(self.authority, true),
118 AccountMeta::new_readonly(Pubkey::default(), false),
119 AccountMeta::new(self.fee_payer, true),
120 ];
121
122 let mut data = vec![8u8];
123 data.extend_from_slice(&self.amount.to_le_bytes());
124 if let Some(max_top_up) = max_top_up {
125 data.extend_from_slice(&max_top_up.to_le_bytes());
126 }
127
128 Ok(Instruction {
129 program_id: Pubkey::from(LIGHT_TOKEN_PROGRAM_ID),
130 accounts,
131 data,
132 })
133 }
134}