light_token/instruction/
mint_to.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 MintTo {
25 pub mint: Pubkey,
27 pub destination: Pubkey,
29 pub amount: u64,
31 pub authority: Pubkey,
33 pub max_top_up: Option<u16>,
36}
37
38pub struct MintToCpi<'info> {
58 pub mint: AccountInfo<'info>,
59 pub destination: AccountInfo<'info>,
60 pub amount: u64,
61 pub authority: AccountInfo<'info>,
62 pub system_program: AccountInfo<'info>,
63 pub max_top_up: Option<u16>,
65}
66
67impl<'info> MintToCpi<'info> {
68 pub fn instruction(&self) -> Result<Instruction, ProgramError> {
69 MintTo::from(self).instruction()
70 }
71
72 pub fn invoke(self) -> Result<(), ProgramError> {
73 let instruction = MintTo::from(&self).instruction()?;
74 let account_infos = [
75 self.mint,
76 self.destination,
77 self.authority,
78 self.system_program,
79 ];
80 invoke(&instruction, &account_infos)
81 }
82
83 pub fn invoke_signed(self, signer_seeds: &[&[&[u8]]]) -> Result<(), ProgramError> {
84 let instruction = MintTo::from(&self).instruction()?;
85 let account_infos = [
86 self.mint,
87 self.destination,
88 self.authority,
89 self.system_program,
90 ];
91 invoke_signed(&instruction, &account_infos, signer_seeds)
92 }
93}
94
95impl<'info> From<&MintToCpi<'info>> for MintTo {
96 fn from(cpi: &MintToCpi<'info>) -> Self {
97 Self {
98 mint: *cpi.mint.key,
99 destination: *cpi.destination.key,
100 amount: cpi.amount,
101 authority: *cpi.authority.key,
102 max_top_up: cpi.max_top_up,
103 }
104 }
105}
106
107impl MintTo {
108 pub fn instruction(self) -> Result<Instruction, ProgramError> {
109 Ok(Instruction {
110 program_id: Pubkey::from(LIGHT_TOKEN_PROGRAM_ID),
111 accounts: vec![
112 AccountMeta::new(self.mint, false),
113 AccountMeta::new(self.destination, false),
114 AccountMeta::new(self.authority, true),
115 AccountMeta::new_readonly(Pubkey::default(), false), ],
117 data: {
118 let mut data = vec![7u8]; data.extend_from_slice(&self.amount.to_le_bytes());
120 if let Some(max_top_up) = self.max_top_up {
122 data.extend_from_slice(&max_top_up.to_le_bytes());
123 }
124 data
125 },
126 })
127 }
128}