lighthouse_sol/instructions/
assert_token_account.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::{LogLevel, TokenAccountAssertion};
9use borsh::BorshDeserialize;
10use borsh::BorshSerialize;
11
12/// Accounts.
13#[derive(Debug)]
14pub struct AssertTokenAccount {
15    /// Target account to be asserted
16    pub target_account: solana_pubkey::Pubkey,
17}
18
19impl AssertTokenAccount {
20    pub fn instruction(
21        &self,
22        args: AssertTokenAccountInstructionArgs,
23    ) -> 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: AssertTokenAccountInstructionArgs,
31        remaining_accounts: &[solana_instruction::AccountMeta],
32    ) -> solana_instruction::Instruction {
33        let mut accounts = Vec::with_capacity(1 + remaining_accounts.len());
34        accounts.push(solana_instruction::AccountMeta::new_readonly(
35            self.target_account,
36            false,
37        ));
38        accounts.extend_from_slice(remaining_accounts);
39        let mut data = borsh::to_vec(&AssertTokenAccountInstructionData::new()).unwrap();
40        let mut args = borsh::to_vec(&args).unwrap();
41        data.append(&mut args);
42
43        solana_instruction::Instruction {
44            program_id: crate::LIGHTHOUSE_ID,
45            accounts,
46            data,
47        }
48    }
49}
50
51#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
52#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
53pub struct AssertTokenAccountInstructionData {
54    discriminator: u8,
55}
56
57impl AssertTokenAccountInstructionData {
58    pub fn new() -> Self {
59        Self { discriminator: 9 }
60    }
61}
62
63impl Default for AssertTokenAccountInstructionData {
64    fn default() -> Self {
65        Self::new()
66    }
67}
68
69#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
70#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
71pub struct AssertTokenAccountInstructionArgs {
72    pub log_level: LogLevel,
73    pub assertion: TokenAccountAssertion,
74}
75
76/// Instruction builder for `AssertTokenAccount`.
77///
78/// ### Accounts:
79///
80///   0. `[]` target_account
81#[derive(Clone, Debug, Default)]
82pub struct AssertTokenAccountBuilder {
83    target_account: Option<solana_pubkey::Pubkey>,
84    log_level: Option<LogLevel>,
85    assertion: Option<TokenAccountAssertion>,
86    __remaining_accounts: Vec<solana_instruction::AccountMeta>,
87}
88
89impl AssertTokenAccountBuilder {
90    pub fn new() -> Self {
91        Self::default()
92    }
93    /// Target account to be asserted
94    #[inline(always)]
95    pub fn target_account(&mut self, target_account: solana_pubkey::Pubkey) -> &mut Self {
96        self.target_account = Some(target_account);
97        self
98    }
99    #[inline(always)]
100    pub fn log_level(&mut self, log_level: LogLevel) -> &mut Self {
101        self.log_level = Some(log_level);
102        self
103    }
104    #[inline(always)]
105    pub fn assertion(&mut self, assertion: TokenAccountAssertion) -> &mut Self {
106        self.assertion = Some(assertion);
107        self
108    }
109    /// Add an additional account to the instruction.
110    #[inline(always)]
111    pub fn add_remaining_account(&mut self, account: solana_instruction::AccountMeta) -> &mut Self {
112        self.__remaining_accounts.push(account);
113        self
114    }
115    /// Add additional accounts to the instruction.
116    #[inline(always)]
117    pub fn add_remaining_accounts(
118        &mut self,
119        accounts: &[solana_instruction::AccountMeta],
120    ) -> &mut Self {
121        self.__remaining_accounts.extend_from_slice(accounts);
122        self
123    }
124    #[allow(clippy::clone_on_copy)]
125    pub fn instruction(&self) -> solana_instruction::Instruction {
126        let accounts = AssertTokenAccount {
127            target_account: self.target_account.expect("target_account is not set"),
128        };
129        let args = AssertTokenAccountInstructionArgs {
130            log_level: self.log_level.clone().expect("log_level is not set"),
131            assertion: self.assertion.clone().expect("assertion is not set"),
132        };
133
134        accounts.instruction_with_remaining_accounts(args, &self.__remaining_accounts)
135    }
136}
137
138/// `assert_token_account` CPI accounts.
139pub struct AssertTokenAccountCpiAccounts<'a, 'b> {
140    /// Target account to be asserted
141    pub target_account: &'b solana_account_info::AccountInfo<'a>,
142}
143
144/// `assert_token_account` CPI instruction.
145pub struct AssertTokenAccountCpi<'a, 'b> {
146    /// The program to invoke.
147    pub __program: &'b solana_account_info::AccountInfo<'a>,
148    /// Target account to be asserted
149    pub target_account: &'b solana_account_info::AccountInfo<'a>,
150    /// The arguments for the instruction.
151    pub __args: AssertTokenAccountInstructionArgs,
152}
153
154impl<'a, 'b> AssertTokenAccountCpi<'a, 'b> {
155    pub fn new(
156        program: &'b solana_account_info::AccountInfo<'a>,
157        accounts: AssertTokenAccountCpiAccounts<'a, 'b>,
158        args: AssertTokenAccountInstructionArgs,
159    ) -> Self {
160        Self {
161            __program: program,
162            target_account: accounts.target_account,
163            __args: args,
164        }
165    }
166    #[inline(always)]
167    pub fn invoke(&self) -> solana_program_entrypoint::ProgramResult {
168        self.invoke_signed_with_remaining_accounts(&[], &[])
169    }
170    #[inline(always)]
171    pub fn invoke_with_remaining_accounts(
172        &self,
173        remaining_accounts: &[(&'b solana_account_info::AccountInfo<'a>, bool, bool)],
174    ) -> solana_program_entrypoint::ProgramResult {
175        self.invoke_signed_with_remaining_accounts(&[], remaining_accounts)
176    }
177    #[inline(always)]
178    pub fn invoke_signed(
179        &self,
180        signers_seeds: &[&[&[u8]]],
181    ) -> solana_program_entrypoint::ProgramResult {
182        self.invoke_signed_with_remaining_accounts(signers_seeds, &[])
183    }
184    #[allow(clippy::arithmetic_side_effects)]
185    #[allow(clippy::clone_on_copy)]
186    #[allow(clippy::vec_init_then_push)]
187    pub fn invoke_signed_with_remaining_accounts(
188        &self,
189        signers_seeds: &[&[&[u8]]],
190        remaining_accounts: &[(&'b solana_account_info::AccountInfo<'a>, bool, bool)],
191    ) -> solana_program_entrypoint::ProgramResult {
192        let mut accounts = Vec::with_capacity(1 + remaining_accounts.len());
193        accounts.push(solana_instruction::AccountMeta::new_readonly(
194            *self.target_account.key,
195            false,
196        ));
197        remaining_accounts.iter().for_each(|remaining_account| {
198            accounts.push(solana_instruction::AccountMeta {
199                pubkey: *remaining_account.0.key,
200                is_signer: remaining_account.1,
201                is_writable: remaining_account.2,
202            })
203        });
204        let mut data = borsh::to_vec(&AssertTokenAccountInstructionData::new()).unwrap();
205        let mut args = borsh::to_vec(&self.__args).unwrap();
206        data.append(&mut args);
207
208        let instruction = solana_instruction::Instruction {
209            program_id: crate::LIGHTHOUSE_ID,
210            accounts,
211            data,
212        };
213        let mut account_infos = Vec::with_capacity(2 + remaining_accounts.len());
214        account_infos.push(self.__program.clone());
215        account_infos.push(self.target_account.clone());
216        remaining_accounts
217            .iter()
218            .for_each(|remaining_account| account_infos.push(remaining_account.0.clone()));
219
220        if signers_seeds.is_empty() {
221            solana_cpi::invoke(&instruction, &account_infos)
222        } else {
223            solana_cpi::invoke_signed(&instruction, &account_infos, signers_seeds)
224        }
225    }
226}
227
228/// Instruction builder for `AssertTokenAccount` via CPI.
229///
230/// ### Accounts:
231///
232///   0. `[]` target_account
233#[derive(Clone, Debug)]
234pub struct AssertTokenAccountCpiBuilder<'a, 'b> {
235    instruction: Box<AssertTokenAccountCpiBuilderInstruction<'a, 'b>>,
236}
237
238impl<'a, 'b> AssertTokenAccountCpiBuilder<'a, 'b> {
239    pub fn new(program: &'b solana_account_info::AccountInfo<'a>) -> Self {
240        let instruction = Box::new(AssertTokenAccountCpiBuilderInstruction {
241            __program: program,
242            target_account: None,
243            log_level: None,
244            assertion: None,
245            __remaining_accounts: Vec::new(),
246        });
247        Self { instruction }
248    }
249    /// Target account to be asserted
250    #[inline(always)]
251    pub fn target_account(
252        &mut self,
253        target_account: &'b solana_account_info::AccountInfo<'a>,
254    ) -> &mut Self {
255        self.instruction.target_account = Some(target_account);
256        self
257    }
258    #[inline(always)]
259    pub fn log_level(&mut self, log_level: LogLevel) -> &mut Self {
260        self.instruction.log_level = Some(log_level);
261        self
262    }
263    #[inline(always)]
264    pub fn assertion(&mut self, assertion: TokenAccountAssertion) -> &mut Self {
265        self.instruction.assertion = Some(assertion);
266        self
267    }
268    /// Add an additional account to the instruction.
269    #[inline(always)]
270    pub fn add_remaining_account(
271        &mut self,
272        account: &'b solana_account_info::AccountInfo<'a>,
273        is_writable: bool,
274        is_signer: bool,
275    ) -> &mut Self {
276        self.instruction
277            .__remaining_accounts
278            .push((account, is_writable, is_signer));
279        self
280    }
281    /// Add additional accounts to the instruction.
282    ///
283    /// Each account is represented by a tuple of the `AccountInfo`, a `bool` indicating whether the account is writable or not,
284    /// and a `bool` indicating whether the account is a signer or not.
285    #[inline(always)]
286    pub fn add_remaining_accounts(
287        &mut self,
288        accounts: &[(&'b solana_account_info::AccountInfo<'a>, bool, bool)],
289    ) -> &mut Self {
290        self.instruction
291            .__remaining_accounts
292            .extend_from_slice(accounts);
293        self
294    }
295    #[inline(always)]
296    pub fn invoke(&self) -> solana_program_entrypoint::ProgramResult {
297        self.invoke_signed(&[])
298    }
299    #[allow(clippy::clone_on_copy)]
300    #[allow(clippy::vec_init_then_push)]
301    pub fn invoke_signed(
302        &self,
303        signers_seeds: &[&[&[u8]]],
304    ) -> solana_program_entrypoint::ProgramResult {
305        let args = AssertTokenAccountInstructionArgs {
306            log_level: self
307                .instruction
308                .log_level
309                .clone()
310                .expect("log_level is not set"),
311            assertion: self
312                .instruction
313                .assertion
314                .clone()
315                .expect("assertion is not set"),
316        };
317        let instruction = AssertTokenAccountCpi {
318            __program: self.instruction.__program,
319
320            target_account: self
321                .instruction
322                .target_account
323                .expect("target_account is not set"),
324            __args: args,
325        };
326        instruction.invoke_signed_with_remaining_accounts(
327            signers_seeds,
328            &self.instruction.__remaining_accounts,
329        )
330    }
331}
332
333#[derive(Clone, Debug)]
334struct AssertTokenAccountCpiBuilderInstruction<'a, 'b> {
335    __program: &'b solana_account_info::AccountInfo<'a>,
336    target_account: Option<&'b solana_account_info::AccountInfo<'a>>,
337    log_level: Option<LogLevel>,
338    assertion: Option<TokenAccountAssertion>,
339    /// Additional instruction accounts `(AccountInfo, is_writable, is_signer)`.
340    __remaining_accounts: Vec<(&'b solana_account_info::AccountInfo<'a>, bool, bool)>,
341}