lighthouse_sol/instructions/
memory_write.rs

1//! This code was AUTOGENERATED using the codama library.
2//! Please DO NOT EDIT THIS FILE, instead use visitors
3//! to add features, then rerun codama to update it.
4//!
5//! <https://github.com/codama-idl/codama>
6//!
7
8use crate::types::WriteType;
9use crate::types::compact_u64::CompactU64;
10use borsh::BorshDeserialize;
11use borsh::BorshSerialize;
12
13/// Accounts.
14#[derive(Debug)]
15pub struct MemoryWrite {
16    /// Lighthouse program
17    pub program_id: solana_pubkey::Pubkey,
18    /// System program
19    pub system_program: solana_pubkey::Pubkey,
20    /// Payer account
21    pub payer: solana_pubkey::Pubkey,
22    /// Memory account
23    pub memory: solana_pubkey::Pubkey,
24    /// Account to be written to memory
25    pub source_account: solana_pubkey::Pubkey,
26}
27
28impl MemoryWrite {
29    pub fn instruction(&self, args: MemoryWriteInstructionArgs) -> solana_instruction::Instruction {
30        self.instruction_with_remaining_accounts(args, &[])
31    }
32    #[allow(clippy::arithmetic_side_effects)]
33    #[allow(clippy::vec_init_then_push)]
34    pub fn instruction_with_remaining_accounts(
35        &self,
36        args: MemoryWriteInstructionArgs,
37        remaining_accounts: &[solana_instruction::AccountMeta],
38    ) -> solana_instruction::Instruction {
39        let mut accounts = Vec::with_capacity(5 + remaining_accounts.len());
40        accounts.push(solana_instruction::AccountMeta::new_readonly(
41            self.program_id,
42            false,
43        ));
44        accounts.push(solana_instruction::AccountMeta::new_readonly(
45            self.system_program,
46            false,
47        ));
48        accounts.push(solana_instruction::AccountMeta::new(self.payer, true));
49        accounts.push(solana_instruction::AccountMeta::new(self.memory, false));
50        accounts.push(solana_instruction::AccountMeta::new_readonly(
51            self.source_account,
52            false,
53        ));
54        accounts.extend_from_slice(remaining_accounts);
55        let mut data = borsh::to_vec(&MemoryWriteInstructionData::new()).unwrap();
56        let mut args = borsh::to_vec(&args).unwrap();
57        data.append(&mut args);
58
59        solana_instruction::Instruction {
60            program_id: crate::LIGHTHOUSE_ID,
61            accounts,
62            data,
63        }
64    }
65}
66
67#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
68#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
69pub struct MemoryWriteInstructionData {
70    discriminator: u8,
71}
72
73impl MemoryWriteInstructionData {
74    pub fn new() -> Self {
75        Self { discriminator: 0 }
76    }
77}
78
79impl Default for MemoryWriteInstructionData {
80    fn default() -> Self {
81        Self::new()
82    }
83}
84
85#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
86#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
87pub struct MemoryWriteInstructionArgs {
88    pub memory_id: u8,
89    pub memory_bump: u8,
90    pub write_offset: CompactU64,
91    pub write_type: WriteType,
92}
93
94/// Instruction builder for `MemoryWrite`.
95///
96/// ### Accounts:
97///
98///   0. `[]` program_id
99///   1. `[optional]` system_program (default to `11111111111111111111111111111111`)
100///   2. `[writable, signer]` payer
101///   3. `[writable]` memory
102///   4. `[]` source_account
103#[derive(Clone, Debug, Default)]
104pub struct MemoryWriteBuilder {
105    program_id: Option<solana_pubkey::Pubkey>,
106    system_program: Option<solana_pubkey::Pubkey>,
107    payer: Option<solana_pubkey::Pubkey>,
108    memory: Option<solana_pubkey::Pubkey>,
109    source_account: Option<solana_pubkey::Pubkey>,
110    memory_id: Option<u8>,
111    memory_bump: Option<u8>,
112    write_offset: Option<CompactU64>,
113    write_type: Option<WriteType>,
114    __remaining_accounts: Vec<solana_instruction::AccountMeta>,
115}
116
117impl MemoryWriteBuilder {
118    pub fn new() -> Self {
119        Self::default()
120    }
121    /// Lighthouse program
122    #[inline(always)]
123    pub fn program_id(&mut self, program_id: solana_pubkey::Pubkey) -> &mut Self {
124        self.program_id = Some(program_id);
125        self
126    }
127    /// `[optional account, default to '11111111111111111111111111111111']`
128    /// System program
129    #[inline(always)]
130    pub fn system_program(&mut self, system_program: solana_pubkey::Pubkey) -> &mut Self {
131        self.system_program = Some(system_program);
132        self
133    }
134    /// Payer account
135    #[inline(always)]
136    pub fn payer(&mut self, payer: solana_pubkey::Pubkey) -> &mut Self {
137        self.payer = Some(payer);
138        self
139    }
140    /// Memory account
141    #[inline(always)]
142    pub fn memory(&mut self, memory: solana_pubkey::Pubkey) -> &mut Self {
143        self.memory = Some(memory);
144        self
145    }
146    /// Account to be written to memory
147    #[inline(always)]
148    pub fn source_account(&mut self, source_account: solana_pubkey::Pubkey) -> &mut Self {
149        self.source_account = Some(source_account);
150        self
151    }
152    #[inline(always)]
153    pub fn memory_id(&mut self, memory_id: u8) -> &mut Self {
154        self.memory_id = Some(memory_id);
155        self
156    }
157    #[inline(always)]
158    pub fn memory_bump(&mut self, memory_bump: u8) -> &mut Self {
159        self.memory_bump = Some(memory_bump);
160        self
161    }
162    #[inline(always)]
163    pub fn write_offset(&mut self, write_offset: CompactU64) -> &mut Self {
164        self.write_offset = Some(write_offset);
165        self
166    }
167    #[inline(always)]
168    pub fn write_type(&mut self, write_type: WriteType) -> &mut Self {
169        self.write_type = Some(write_type);
170        self
171    }
172    /// Add an additional account to the instruction.
173    #[inline(always)]
174    pub fn add_remaining_account(&mut self, account: solana_instruction::AccountMeta) -> &mut Self {
175        self.__remaining_accounts.push(account);
176        self
177    }
178    /// Add additional accounts to the instruction.
179    #[inline(always)]
180    pub fn add_remaining_accounts(
181        &mut self,
182        accounts: &[solana_instruction::AccountMeta],
183    ) -> &mut Self {
184        self.__remaining_accounts.extend_from_slice(accounts);
185        self
186    }
187    #[allow(clippy::clone_on_copy)]
188    pub fn instruction(&self) -> solana_instruction::Instruction {
189        let accounts = MemoryWrite {
190            program_id: self.program_id.expect("program_id is not set"),
191            system_program: self
192                .system_program
193                .unwrap_or(solana_pubkey::pubkey!("11111111111111111111111111111111")),
194            payer: self.payer.expect("payer is not set"),
195            memory: self.memory.expect("memory is not set"),
196            source_account: self.source_account.expect("source_account is not set"),
197        };
198        let args = MemoryWriteInstructionArgs {
199            memory_id: self.memory_id.clone().expect("memory_id is not set"),
200            memory_bump: self.memory_bump.clone().expect("memory_bump is not set"),
201            write_offset: self.write_offset.clone().expect("write_offset is not set"),
202            write_type: self.write_type.clone().expect("write_type is not set"),
203        };
204
205        accounts.instruction_with_remaining_accounts(args, &self.__remaining_accounts)
206    }
207}
208
209/// `memory_write` CPI accounts.
210pub struct MemoryWriteCpiAccounts<'a, 'b> {
211    /// Lighthouse program
212    pub program_id: &'b solana_account_info::AccountInfo<'a>,
213    /// System program
214    pub system_program: &'b solana_account_info::AccountInfo<'a>,
215    /// Payer account
216    pub payer: &'b solana_account_info::AccountInfo<'a>,
217    /// Memory account
218    pub memory: &'b solana_account_info::AccountInfo<'a>,
219    /// Account to be written to memory
220    pub source_account: &'b solana_account_info::AccountInfo<'a>,
221}
222
223/// `memory_write` CPI instruction.
224pub struct MemoryWriteCpi<'a, 'b> {
225    /// The program to invoke.
226    pub __program: &'b solana_account_info::AccountInfo<'a>,
227    /// Lighthouse program
228    pub program_id: &'b solana_account_info::AccountInfo<'a>,
229    /// System program
230    pub system_program: &'b solana_account_info::AccountInfo<'a>,
231    /// Payer account
232    pub payer: &'b solana_account_info::AccountInfo<'a>,
233    /// Memory account
234    pub memory: &'b solana_account_info::AccountInfo<'a>,
235    /// Account to be written to memory
236    pub source_account: &'b solana_account_info::AccountInfo<'a>,
237    /// The arguments for the instruction.
238    pub __args: MemoryWriteInstructionArgs,
239}
240
241impl<'a, 'b> MemoryWriteCpi<'a, 'b> {
242    pub fn new(
243        program: &'b solana_account_info::AccountInfo<'a>,
244        accounts: MemoryWriteCpiAccounts<'a, 'b>,
245        args: MemoryWriteInstructionArgs,
246    ) -> Self {
247        Self {
248            __program: program,
249            program_id: accounts.program_id,
250            system_program: accounts.system_program,
251            payer: accounts.payer,
252            memory: accounts.memory,
253            source_account: accounts.source_account,
254            __args: args,
255        }
256    }
257    #[inline(always)]
258    pub fn invoke(&self) -> solana_program_entrypoint::ProgramResult {
259        self.invoke_signed_with_remaining_accounts(&[], &[])
260    }
261    #[inline(always)]
262    pub fn invoke_with_remaining_accounts(
263        &self,
264        remaining_accounts: &[(&'b solana_account_info::AccountInfo<'a>, bool, bool)],
265    ) -> solana_program_entrypoint::ProgramResult {
266        self.invoke_signed_with_remaining_accounts(&[], remaining_accounts)
267    }
268    #[inline(always)]
269    pub fn invoke_signed(
270        &self,
271        signers_seeds: &[&[&[u8]]],
272    ) -> solana_program_entrypoint::ProgramResult {
273        self.invoke_signed_with_remaining_accounts(signers_seeds, &[])
274    }
275    #[allow(clippy::arithmetic_side_effects)]
276    #[allow(clippy::clone_on_copy)]
277    #[allow(clippy::vec_init_then_push)]
278    pub fn invoke_signed_with_remaining_accounts(
279        &self,
280        signers_seeds: &[&[&[u8]]],
281        remaining_accounts: &[(&'b solana_account_info::AccountInfo<'a>, bool, bool)],
282    ) -> solana_program_entrypoint::ProgramResult {
283        let mut accounts = Vec::with_capacity(5 + remaining_accounts.len());
284        accounts.push(solana_instruction::AccountMeta::new_readonly(
285            *self.program_id.key,
286            false,
287        ));
288        accounts.push(solana_instruction::AccountMeta::new_readonly(
289            *self.system_program.key,
290            false,
291        ));
292        accounts.push(solana_instruction::AccountMeta::new(*self.payer.key, true));
293        accounts.push(solana_instruction::AccountMeta::new(
294            *self.memory.key,
295            false,
296        ));
297        accounts.push(solana_instruction::AccountMeta::new_readonly(
298            *self.source_account.key,
299            false,
300        ));
301        remaining_accounts.iter().for_each(|remaining_account| {
302            accounts.push(solana_instruction::AccountMeta {
303                pubkey: *remaining_account.0.key,
304                is_signer: remaining_account.1,
305                is_writable: remaining_account.2,
306            })
307        });
308        let mut data = borsh::to_vec(&MemoryWriteInstructionData::new()).unwrap();
309        let mut args = borsh::to_vec(&self.__args).unwrap();
310        data.append(&mut args);
311
312        let instruction = solana_instruction::Instruction {
313            program_id: crate::LIGHTHOUSE_ID,
314            accounts,
315            data,
316        };
317        let mut account_infos = Vec::with_capacity(6 + remaining_accounts.len());
318        account_infos.push(self.__program.clone());
319        account_infos.push(self.program_id.clone());
320        account_infos.push(self.system_program.clone());
321        account_infos.push(self.payer.clone());
322        account_infos.push(self.memory.clone());
323        account_infos.push(self.source_account.clone());
324        remaining_accounts
325            .iter()
326            .for_each(|remaining_account| account_infos.push(remaining_account.0.clone()));
327
328        if signers_seeds.is_empty() {
329            solana_cpi::invoke(&instruction, &account_infos)
330        } else {
331            solana_cpi::invoke_signed(&instruction, &account_infos, signers_seeds)
332        }
333    }
334}
335
336/// Instruction builder for `MemoryWrite` via CPI.
337///
338/// ### Accounts:
339///
340///   0. `[]` program_id
341///   1. `[]` system_program
342///   2. `[writable, signer]` payer
343///   3. `[writable]` memory
344///   4. `[]` source_account
345#[derive(Clone, Debug)]
346pub struct MemoryWriteCpiBuilder<'a, 'b> {
347    instruction: Box<MemoryWriteCpiBuilderInstruction<'a, 'b>>,
348}
349
350impl<'a, 'b> MemoryWriteCpiBuilder<'a, 'b> {
351    pub fn new(program: &'b solana_account_info::AccountInfo<'a>) -> Self {
352        let instruction = Box::new(MemoryWriteCpiBuilderInstruction {
353            __program: program,
354            program_id: None,
355            system_program: None,
356            payer: None,
357            memory: None,
358            source_account: None,
359            memory_id: None,
360            memory_bump: None,
361            write_offset: None,
362            write_type: None,
363            __remaining_accounts: Vec::new(),
364        });
365        Self { instruction }
366    }
367    /// Lighthouse program
368    #[inline(always)]
369    pub fn program_id(
370        &mut self,
371        program_id: &'b solana_account_info::AccountInfo<'a>,
372    ) -> &mut Self {
373        self.instruction.program_id = Some(program_id);
374        self
375    }
376    /// System program
377    #[inline(always)]
378    pub fn system_program(
379        &mut self,
380        system_program: &'b solana_account_info::AccountInfo<'a>,
381    ) -> &mut Self {
382        self.instruction.system_program = Some(system_program);
383        self
384    }
385    /// Payer account
386    #[inline(always)]
387    pub fn payer(&mut self, payer: &'b solana_account_info::AccountInfo<'a>) -> &mut Self {
388        self.instruction.payer = Some(payer);
389        self
390    }
391    /// Memory account
392    #[inline(always)]
393    pub fn memory(&mut self, memory: &'b solana_account_info::AccountInfo<'a>) -> &mut Self {
394        self.instruction.memory = Some(memory);
395        self
396    }
397    /// Account to be written to memory
398    #[inline(always)]
399    pub fn source_account(
400        &mut self,
401        source_account: &'b solana_account_info::AccountInfo<'a>,
402    ) -> &mut Self {
403        self.instruction.source_account = Some(source_account);
404        self
405    }
406    #[inline(always)]
407    pub fn memory_id(&mut self, memory_id: u8) -> &mut Self {
408        self.instruction.memory_id = Some(memory_id);
409        self
410    }
411    #[inline(always)]
412    pub fn memory_bump(&mut self, memory_bump: u8) -> &mut Self {
413        self.instruction.memory_bump = Some(memory_bump);
414        self
415    }
416    #[inline(always)]
417    pub fn write_offset(&mut self, write_offset: CompactU64) -> &mut Self {
418        self.instruction.write_offset = Some(write_offset);
419        self
420    }
421    #[inline(always)]
422    pub fn write_type(&mut self, write_type: WriteType) -> &mut Self {
423        self.instruction.write_type = Some(write_type);
424        self
425    }
426    /// Add an additional account to the instruction.
427    #[inline(always)]
428    pub fn add_remaining_account(
429        &mut self,
430        account: &'b solana_account_info::AccountInfo<'a>,
431        is_writable: bool,
432        is_signer: bool,
433    ) -> &mut Self {
434        self.instruction
435            .__remaining_accounts
436            .push((account, is_writable, is_signer));
437        self
438    }
439    /// Add additional accounts to the instruction.
440    ///
441    /// Each account is represented by a tuple of the `AccountInfo`, a `bool` indicating whether the account is writable or not,
442    /// and a `bool` indicating whether the account is a signer or not.
443    #[inline(always)]
444    pub fn add_remaining_accounts(
445        &mut self,
446        accounts: &[(&'b solana_account_info::AccountInfo<'a>, bool, bool)],
447    ) -> &mut Self {
448        self.instruction
449            .__remaining_accounts
450            .extend_from_slice(accounts);
451        self
452    }
453    #[inline(always)]
454    pub fn invoke(&self) -> solana_program_entrypoint::ProgramResult {
455        self.invoke_signed(&[])
456    }
457    #[allow(clippy::clone_on_copy)]
458    #[allow(clippy::vec_init_then_push)]
459    pub fn invoke_signed(
460        &self,
461        signers_seeds: &[&[&[u8]]],
462    ) -> solana_program_entrypoint::ProgramResult {
463        let args = MemoryWriteInstructionArgs {
464            memory_id: self
465                .instruction
466                .memory_id
467                .clone()
468                .expect("memory_id is not set"),
469            memory_bump: self
470                .instruction
471                .memory_bump
472                .clone()
473                .expect("memory_bump is not set"),
474            write_offset: self
475                .instruction
476                .write_offset
477                .clone()
478                .expect("write_offset is not set"),
479            write_type: self
480                .instruction
481                .write_type
482                .clone()
483                .expect("write_type is not set"),
484        };
485        let instruction = MemoryWriteCpi {
486            __program: self.instruction.__program,
487
488            program_id: self.instruction.program_id.expect("program_id is not set"),
489
490            system_program: self
491                .instruction
492                .system_program
493                .expect("system_program is not set"),
494
495            payer: self.instruction.payer.expect("payer is not set"),
496
497            memory: self.instruction.memory.expect("memory is not set"),
498
499            source_account: self
500                .instruction
501                .source_account
502                .expect("source_account is not set"),
503            __args: args,
504        };
505        instruction.invoke_signed_with_remaining_accounts(
506            signers_seeds,
507            &self.instruction.__remaining_accounts,
508        )
509    }
510}
511
512#[derive(Clone, Debug)]
513struct MemoryWriteCpiBuilderInstruction<'a, 'b> {
514    __program: &'b solana_account_info::AccountInfo<'a>,
515    program_id: Option<&'b solana_account_info::AccountInfo<'a>>,
516    system_program: Option<&'b solana_account_info::AccountInfo<'a>>,
517    payer: Option<&'b solana_account_info::AccountInfo<'a>>,
518    memory: Option<&'b solana_account_info::AccountInfo<'a>>,
519    source_account: Option<&'b solana_account_info::AccountInfo<'a>>,
520    memory_id: Option<u8>,
521    memory_bump: Option<u8>,
522    write_offset: Option<CompactU64>,
523    write_type: Option<WriteType>,
524    /// Additional instruction accounts `(AccountInfo, is_writable, is_signer)`.
525    __remaining_accounts: Vec<(&'b solana_account_info::AccountInfo<'a>, bool, bool)>,
526}