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