lighthouse_sol/instructions/
memory_close.rs1use borsh::BorshDeserialize;
9use borsh::BorshSerialize;
10
11#[derive(Debug)]
13pub struct MemoryClose {
14 pub program_id: solana_pubkey::Pubkey,
16 pub payer: solana_pubkey::Pubkey,
18 pub memory: solana_pubkey::Pubkey,
20}
21
22impl MemoryClose {
23 pub fn instruction(&self, args: MemoryCloseInstructionArgs) -> solana_instruction::Instruction {
24 self.instruction_with_remaining_accounts(args, &[])
25 }
26 #[allow(clippy::arithmetic_side_effects)]
27 #[allow(clippy::vec_init_then_push)]
28 pub fn instruction_with_remaining_accounts(
29 &self,
30 args: MemoryCloseInstructionArgs,
31 remaining_accounts: &[solana_instruction::AccountMeta],
32 ) -> solana_instruction::Instruction {
33 let mut accounts = Vec::with_capacity(3 + remaining_accounts.len());
34 accounts.push(solana_instruction::AccountMeta::new_readonly(
35 self.program_id,
36 false,
37 ));
38 accounts.push(solana_instruction::AccountMeta::new(self.payer, true));
39 accounts.push(solana_instruction::AccountMeta::new(self.memory, false));
40 accounts.extend_from_slice(remaining_accounts);
41 let mut data = borsh::to_vec(&MemoryCloseInstructionData::new()).unwrap();
42 let mut args = borsh::to_vec(&args).unwrap();
43 data.append(&mut args);
44
45 solana_instruction::Instruction {
46 program_id: crate::LIGHTHOUSE_ID,
47 accounts,
48 data,
49 }
50 }
51}
52
53#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
54#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
55pub struct MemoryCloseInstructionData {
56 discriminator: u8,
57}
58
59impl MemoryCloseInstructionData {
60 pub fn new() -> Self {
61 Self { discriminator: 1 }
62 }
63}
64
65impl Default for MemoryCloseInstructionData {
66 fn default() -> Self {
67 Self::new()
68 }
69}
70
71#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
72#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
73pub struct MemoryCloseInstructionArgs {
74 pub memory_id: u8,
75 pub memory_bump: u8,
76}
77
78#[derive(Clone, Debug, Default)]
86pub struct MemoryCloseBuilder {
87 program_id: Option<solana_pubkey::Pubkey>,
88 payer: Option<solana_pubkey::Pubkey>,
89 memory: Option<solana_pubkey::Pubkey>,
90 memory_id: Option<u8>,
91 memory_bump: Option<u8>,
92 __remaining_accounts: Vec<solana_instruction::AccountMeta>,
93}
94
95impl MemoryCloseBuilder {
96 pub fn new() -> Self {
97 Self::default()
98 }
99 #[inline(always)]
101 pub fn program_id(&mut self, program_id: solana_pubkey::Pubkey) -> &mut Self {
102 self.program_id = Some(program_id);
103 self
104 }
105 #[inline(always)]
107 pub fn payer(&mut self, payer: solana_pubkey::Pubkey) -> &mut Self {
108 self.payer = Some(payer);
109 self
110 }
111 #[inline(always)]
113 pub fn memory(&mut self, memory: solana_pubkey::Pubkey) -> &mut Self {
114 self.memory = Some(memory);
115 self
116 }
117 #[inline(always)]
118 pub fn memory_id(&mut self, memory_id: u8) -> &mut Self {
119 self.memory_id = Some(memory_id);
120 self
121 }
122 #[inline(always)]
123 pub fn memory_bump(&mut self, memory_bump: u8) -> &mut Self {
124 self.memory_bump = Some(memory_bump);
125 self
126 }
127 #[inline(always)]
129 pub fn add_remaining_account(&mut self, account: solana_instruction::AccountMeta) -> &mut Self {
130 self.__remaining_accounts.push(account);
131 self
132 }
133 #[inline(always)]
135 pub fn add_remaining_accounts(
136 &mut self,
137 accounts: &[solana_instruction::AccountMeta],
138 ) -> &mut Self {
139 self.__remaining_accounts.extend_from_slice(accounts);
140 self
141 }
142 #[allow(clippy::clone_on_copy)]
143 pub fn instruction(&self) -> solana_instruction::Instruction {
144 let accounts = MemoryClose {
145 program_id: self.program_id.expect("program_id is not set"),
146 payer: self.payer.expect("payer is not set"),
147 memory: self.memory.expect("memory is not set"),
148 };
149 let args = MemoryCloseInstructionArgs {
150 memory_id: self.memory_id.clone().expect("memory_id is not set"),
151 memory_bump: self.memory_bump.clone().expect("memory_bump is not set"),
152 };
153
154 accounts.instruction_with_remaining_accounts(args, &self.__remaining_accounts)
155 }
156}
157
158pub struct MemoryCloseCpiAccounts<'a, 'b> {
160 pub program_id: &'b solana_account_info::AccountInfo<'a>,
162 pub payer: &'b solana_account_info::AccountInfo<'a>,
164 pub memory: &'b solana_account_info::AccountInfo<'a>,
166}
167
168pub struct MemoryCloseCpi<'a, 'b> {
170 pub __program: &'b solana_account_info::AccountInfo<'a>,
172 pub program_id: &'b solana_account_info::AccountInfo<'a>,
174 pub payer: &'b solana_account_info::AccountInfo<'a>,
176 pub memory: &'b solana_account_info::AccountInfo<'a>,
178 pub __args: MemoryCloseInstructionArgs,
180}
181
182impl<'a, 'b> MemoryCloseCpi<'a, 'b> {
183 pub fn new(
184 program: &'b solana_account_info::AccountInfo<'a>,
185 accounts: MemoryCloseCpiAccounts<'a, 'b>,
186 args: MemoryCloseInstructionArgs,
187 ) -> Self {
188 Self {
189 __program: program,
190 program_id: accounts.program_id,
191 payer: accounts.payer,
192 memory: accounts.memory,
193 __args: args,
194 }
195 }
196 #[inline(always)]
197 pub fn invoke(&self) -> solana_program_entrypoint::ProgramResult {
198 self.invoke_signed_with_remaining_accounts(&[], &[])
199 }
200 #[inline(always)]
201 pub fn invoke_with_remaining_accounts(
202 &self,
203 remaining_accounts: &[(&'b solana_account_info::AccountInfo<'a>, bool, bool)],
204 ) -> solana_program_entrypoint::ProgramResult {
205 self.invoke_signed_with_remaining_accounts(&[], remaining_accounts)
206 }
207 #[inline(always)]
208 pub fn invoke_signed(
209 &self,
210 signers_seeds: &[&[&[u8]]],
211 ) -> solana_program_entrypoint::ProgramResult {
212 self.invoke_signed_with_remaining_accounts(signers_seeds, &[])
213 }
214 #[allow(clippy::arithmetic_side_effects)]
215 #[allow(clippy::clone_on_copy)]
216 #[allow(clippy::vec_init_then_push)]
217 pub fn invoke_signed_with_remaining_accounts(
218 &self,
219 signers_seeds: &[&[&[u8]]],
220 remaining_accounts: &[(&'b solana_account_info::AccountInfo<'a>, bool, bool)],
221 ) -> solana_program_entrypoint::ProgramResult {
222 let mut accounts = Vec::with_capacity(3 + remaining_accounts.len());
223 accounts.push(solana_instruction::AccountMeta::new_readonly(
224 *self.program_id.key,
225 false,
226 ));
227 accounts.push(solana_instruction::AccountMeta::new(*self.payer.key, true));
228 accounts.push(solana_instruction::AccountMeta::new(
229 *self.memory.key,
230 false,
231 ));
232 remaining_accounts.iter().for_each(|remaining_account| {
233 accounts.push(solana_instruction::AccountMeta {
234 pubkey: *remaining_account.0.key,
235 is_signer: remaining_account.1,
236 is_writable: remaining_account.2,
237 })
238 });
239 let mut data = borsh::to_vec(&MemoryCloseInstructionData::new()).unwrap();
240 let mut args = borsh::to_vec(&self.__args).unwrap();
241 data.append(&mut args);
242
243 let instruction = solana_instruction::Instruction {
244 program_id: crate::LIGHTHOUSE_ID,
245 accounts,
246 data,
247 };
248 let mut account_infos = Vec::with_capacity(4 + remaining_accounts.len());
249 account_infos.push(self.__program.clone());
250 account_infos.push(self.program_id.clone());
251 account_infos.push(self.payer.clone());
252 account_infos.push(self.memory.clone());
253 remaining_accounts
254 .iter()
255 .for_each(|remaining_account| account_infos.push(remaining_account.0.clone()));
256
257 if signers_seeds.is_empty() {
258 solana_cpi::invoke(&instruction, &account_infos)
259 } else {
260 solana_cpi::invoke_signed(&instruction, &account_infos, signers_seeds)
261 }
262 }
263}
264
265#[derive(Clone, Debug)]
273pub struct MemoryCloseCpiBuilder<'a, 'b> {
274 instruction: Box<MemoryCloseCpiBuilderInstruction<'a, 'b>>,
275}
276
277impl<'a, 'b> MemoryCloseCpiBuilder<'a, 'b> {
278 pub fn new(program: &'b solana_account_info::AccountInfo<'a>) -> Self {
279 let instruction = Box::new(MemoryCloseCpiBuilderInstruction {
280 __program: program,
281 program_id: None,
282 payer: None,
283 memory: None,
284 memory_id: None,
285 memory_bump: None,
286 __remaining_accounts: Vec::new(),
287 });
288 Self { instruction }
289 }
290 #[inline(always)]
292 pub fn program_id(
293 &mut self,
294 program_id: &'b solana_account_info::AccountInfo<'a>,
295 ) -> &mut Self {
296 self.instruction.program_id = Some(program_id);
297 self
298 }
299 #[inline(always)]
301 pub fn payer(&mut self, payer: &'b solana_account_info::AccountInfo<'a>) -> &mut Self {
302 self.instruction.payer = Some(payer);
303 self
304 }
305 #[inline(always)]
307 pub fn memory(&mut self, memory: &'b solana_account_info::AccountInfo<'a>) -> &mut Self {
308 self.instruction.memory = Some(memory);
309 self
310 }
311 #[inline(always)]
312 pub fn memory_id(&mut self, memory_id: u8) -> &mut Self {
313 self.instruction.memory_id = Some(memory_id);
314 self
315 }
316 #[inline(always)]
317 pub fn memory_bump(&mut self, memory_bump: u8) -> &mut Self {
318 self.instruction.memory_bump = Some(memory_bump);
319 self
320 }
321 #[inline(always)]
323 pub fn add_remaining_account(
324 &mut self,
325 account: &'b solana_account_info::AccountInfo<'a>,
326 is_writable: bool,
327 is_signer: bool,
328 ) -> &mut Self {
329 self.instruction
330 .__remaining_accounts
331 .push((account, is_writable, is_signer));
332 self
333 }
334 #[inline(always)]
339 pub fn add_remaining_accounts(
340 &mut self,
341 accounts: &[(&'b solana_account_info::AccountInfo<'a>, bool, bool)],
342 ) -> &mut Self {
343 self.instruction
344 .__remaining_accounts
345 .extend_from_slice(accounts);
346 self
347 }
348 #[inline(always)]
349 pub fn invoke(&self) -> solana_program_entrypoint::ProgramResult {
350 self.invoke_signed(&[])
351 }
352 #[allow(clippy::clone_on_copy)]
353 #[allow(clippy::vec_init_then_push)]
354 pub fn invoke_signed(
355 &self,
356 signers_seeds: &[&[&[u8]]],
357 ) -> solana_program_entrypoint::ProgramResult {
358 let args = MemoryCloseInstructionArgs {
359 memory_id: self
360 .instruction
361 .memory_id
362 .clone()
363 .expect("memory_id is not set"),
364 memory_bump: self
365 .instruction
366 .memory_bump
367 .clone()
368 .expect("memory_bump is not set"),
369 };
370 let instruction = MemoryCloseCpi {
371 __program: self.instruction.__program,
372
373 program_id: self.instruction.program_id.expect("program_id is not set"),
374
375 payer: self.instruction.payer.expect("payer is not set"),
376
377 memory: self.instruction.memory.expect("memory is not set"),
378 __args: args,
379 };
380 instruction.invoke_signed_with_remaining_accounts(
381 signers_seeds,
382 &self.instruction.__remaining_accounts,
383 )
384 }
385}
386
387#[derive(Clone, Debug)]
388struct MemoryCloseCpiBuilderInstruction<'a, 'b> {
389 __program: &'b solana_account_info::AccountInfo<'a>,
390 program_id: Option<&'b solana_account_info::AccountInfo<'a>>,
391 payer: Option<&'b solana_account_info::AccountInfo<'a>>,
392 memory: Option<&'b solana_account_info::AccountInfo<'a>>,
393 memory_id: Option<u8>,
394 memory_bump: Option<u8>,
395 __remaining_accounts: Vec<(&'b solana_account_info::AccountInfo<'a>, bool, bool)>,
397}