lighthouse_sdk/generated/instructions/
memory_write.rs

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