1use borsh::{BorshDeserialize, BorshSerialize};
9
10pub const TEST_INIT_DISCRIMINATOR: [u8; 8] = [48, 51, 92, 122, 81, 19, 112, 41];
11
12#[derive(Debug)]
14pub struct TestInit {
15 pub upgrade_authority: solana_program::pubkey::Pubkey,
16
17 pub admin: solana_program::pubkey::Pubkey,
18
19 pub transfer_authority: solana_program::pubkey::Pubkey,
20
21 pub perpetuals: solana_program::pubkey::Pubkey,
22
23 pub system_program: solana_program::pubkey::Pubkey,
24
25 pub token_program: solana_program::pubkey::Pubkey,
26}
27
28impl TestInit {
29 pub fn instruction(
30 &self,
31 args: TestInitInstructionArgs,
32 ) -> solana_program::instruction::Instruction {
33 self.instruction_with_remaining_accounts(args, &[])
34 }
35 #[allow(clippy::arithmetic_side_effects)]
36 #[allow(clippy::vec_init_then_push)]
37 pub fn instruction_with_remaining_accounts(
38 &self,
39 args: TestInitInstructionArgs,
40 remaining_accounts: &[solana_program::instruction::AccountMeta],
41 ) -> solana_program::instruction::Instruction {
42 let mut accounts = Vec::with_capacity(6 + remaining_accounts.len());
43 accounts.push(solana_program::instruction::AccountMeta::new(
44 self.upgrade_authority,
45 true,
46 ));
47 accounts.push(solana_program::instruction::AccountMeta::new_readonly(
48 self.admin, false,
49 ));
50 accounts.push(solana_program::instruction::AccountMeta::new(
51 self.transfer_authority,
52 false,
53 ));
54 accounts.push(solana_program::instruction::AccountMeta::new(
55 self.perpetuals,
56 false,
57 ));
58 accounts.push(solana_program::instruction::AccountMeta::new_readonly(
59 self.system_program,
60 false,
61 ));
62 accounts.push(solana_program::instruction::AccountMeta::new_readonly(
63 self.token_program,
64 false,
65 ));
66 accounts.extend_from_slice(remaining_accounts);
67 let mut data = borsh::to_vec(&TestInitInstructionData::new()).unwrap();
68 let mut args = borsh::to_vec(&args).unwrap();
69 data.append(&mut args);
70
71 solana_program::instruction::Instruction {
72 program_id: crate::PERPETUALS_ID,
73 accounts,
74 data,
75 }
76 }
77}
78
79#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, PartialEq)]
80#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
81pub struct TestInitInstructionData {
82 discriminator: [u8; 8],
83}
84
85impl TestInitInstructionData {
86 pub fn new() -> Self {
87 Self {
88 discriminator: [48, 51, 92, 122, 81, 19, 112, 41],
89 }
90 }
91}
92
93impl Default for TestInitInstructionData {
94 fn default() -> Self {
95 Self::new()
96 }
97}
98
99#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, PartialEq)]
100#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
101pub struct TestInitInstructionArgs {
102 pub allow_swap: bool,
103 pub allow_add_liquidity: bool,
104 pub allow_remove_liquidity: bool,
105 pub allow_increase_position: bool,
106 pub allow_decrease_position: bool,
107 pub allow_collateral_withdrawal: bool,
108 pub allow_liquidate_position: bool,
109}
110
111#[derive(Clone, Debug, Default)]
122pub struct TestInitBuilder {
123 upgrade_authority: Option<solana_program::pubkey::Pubkey>,
124 admin: Option<solana_program::pubkey::Pubkey>,
125 transfer_authority: Option<solana_program::pubkey::Pubkey>,
126 perpetuals: Option<solana_program::pubkey::Pubkey>,
127 system_program: Option<solana_program::pubkey::Pubkey>,
128 token_program: Option<solana_program::pubkey::Pubkey>,
129 allow_swap: Option<bool>,
130 allow_add_liquidity: Option<bool>,
131 allow_remove_liquidity: Option<bool>,
132 allow_increase_position: Option<bool>,
133 allow_decrease_position: Option<bool>,
134 allow_collateral_withdrawal: Option<bool>,
135 allow_liquidate_position: Option<bool>,
136 __remaining_accounts: Vec<solana_program::instruction::AccountMeta>,
137}
138
139impl TestInitBuilder {
140 pub fn new() -> Self {
141 Self::default()
142 }
143 #[inline(always)]
144 pub fn upgrade_authority(
145 &mut self,
146 upgrade_authority: solana_program::pubkey::Pubkey,
147 ) -> &mut Self {
148 self.upgrade_authority = Some(upgrade_authority);
149 self
150 }
151 #[inline(always)]
152 pub fn admin(&mut self, admin: solana_program::pubkey::Pubkey) -> &mut Self {
153 self.admin = Some(admin);
154 self
155 }
156 #[inline(always)]
157 pub fn transfer_authority(
158 &mut self,
159 transfer_authority: solana_program::pubkey::Pubkey,
160 ) -> &mut Self {
161 self.transfer_authority = Some(transfer_authority);
162 self
163 }
164 #[inline(always)]
165 pub fn perpetuals(&mut self, perpetuals: solana_program::pubkey::Pubkey) -> &mut Self {
166 self.perpetuals = Some(perpetuals);
167 self
168 }
169 #[inline(always)]
171 pub fn system_program(&mut self, system_program: solana_program::pubkey::Pubkey) -> &mut Self {
172 self.system_program = Some(system_program);
173 self
174 }
175 #[inline(always)]
177 pub fn token_program(&mut self, token_program: solana_program::pubkey::Pubkey) -> &mut Self {
178 self.token_program = Some(token_program);
179 self
180 }
181 #[inline(always)]
182 pub fn allow_swap(&mut self, allow_swap: bool) -> &mut Self {
183 self.allow_swap = Some(allow_swap);
184 self
185 }
186 #[inline(always)]
187 pub fn allow_add_liquidity(&mut self, allow_add_liquidity: bool) -> &mut Self {
188 self.allow_add_liquidity = Some(allow_add_liquidity);
189 self
190 }
191 #[inline(always)]
192 pub fn allow_remove_liquidity(&mut self, allow_remove_liquidity: bool) -> &mut Self {
193 self.allow_remove_liquidity = Some(allow_remove_liquidity);
194 self
195 }
196 #[inline(always)]
197 pub fn allow_increase_position(&mut self, allow_increase_position: bool) -> &mut Self {
198 self.allow_increase_position = Some(allow_increase_position);
199 self
200 }
201 #[inline(always)]
202 pub fn allow_decrease_position(&mut self, allow_decrease_position: bool) -> &mut Self {
203 self.allow_decrease_position = Some(allow_decrease_position);
204 self
205 }
206 #[inline(always)]
207 pub fn allow_collateral_withdrawal(&mut self, allow_collateral_withdrawal: bool) -> &mut Self {
208 self.allow_collateral_withdrawal = Some(allow_collateral_withdrawal);
209 self
210 }
211 #[inline(always)]
212 pub fn allow_liquidate_position(&mut self, allow_liquidate_position: bool) -> &mut Self {
213 self.allow_liquidate_position = Some(allow_liquidate_position);
214 self
215 }
216 #[inline(always)]
218 pub fn add_remaining_account(
219 &mut self,
220 account: solana_program::instruction::AccountMeta,
221 ) -> &mut Self {
222 self.__remaining_accounts.push(account);
223 self
224 }
225 #[inline(always)]
227 pub fn add_remaining_accounts(
228 &mut self,
229 accounts: &[solana_program::instruction::AccountMeta],
230 ) -> &mut Self {
231 self.__remaining_accounts.extend_from_slice(accounts);
232 self
233 }
234 #[allow(clippy::clone_on_copy)]
235 pub fn instruction(&self) -> solana_program::instruction::Instruction {
236 let accounts = TestInit {
237 upgrade_authority: self
238 .upgrade_authority
239 .expect("upgrade_authority is not set"),
240 admin: self.admin.expect("admin is not set"),
241 transfer_authority: self
242 .transfer_authority
243 .expect("transfer_authority is not set"),
244 perpetuals: self.perpetuals.expect("perpetuals is not set"),
245 system_program: self
246 .system_program
247 .unwrap_or(solana_program::pubkey!("11111111111111111111111111111111")),
248 token_program: self.token_program.unwrap_or(solana_program::pubkey!(
249 "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
250 )),
251 };
252 let args = TestInitInstructionArgs {
253 allow_swap: self.allow_swap.clone().expect("allow_swap is not set"),
254 allow_add_liquidity: self
255 .allow_add_liquidity
256 .clone()
257 .expect("allow_add_liquidity is not set"),
258 allow_remove_liquidity: self
259 .allow_remove_liquidity
260 .clone()
261 .expect("allow_remove_liquidity is not set"),
262 allow_increase_position: self
263 .allow_increase_position
264 .clone()
265 .expect("allow_increase_position is not set"),
266 allow_decrease_position: self
267 .allow_decrease_position
268 .clone()
269 .expect("allow_decrease_position is not set"),
270 allow_collateral_withdrawal: self
271 .allow_collateral_withdrawal
272 .clone()
273 .expect("allow_collateral_withdrawal is not set"),
274 allow_liquidate_position: self
275 .allow_liquidate_position
276 .clone()
277 .expect("allow_liquidate_position is not set"),
278 };
279
280 accounts.instruction_with_remaining_accounts(args, &self.__remaining_accounts)
281 }
282}
283
284pub struct TestInitCpiAccounts<'a, 'b> {
286 pub upgrade_authority: &'b solana_program::account_info::AccountInfo<'a>,
287
288 pub admin: &'b solana_program::account_info::AccountInfo<'a>,
289
290 pub transfer_authority: &'b solana_program::account_info::AccountInfo<'a>,
291
292 pub perpetuals: &'b solana_program::account_info::AccountInfo<'a>,
293
294 pub system_program: &'b solana_program::account_info::AccountInfo<'a>,
295
296 pub token_program: &'b solana_program::account_info::AccountInfo<'a>,
297}
298
299pub struct TestInitCpi<'a, 'b> {
301 pub __program: &'b solana_program::account_info::AccountInfo<'a>,
303
304 pub upgrade_authority: &'b solana_program::account_info::AccountInfo<'a>,
305
306 pub admin: &'b solana_program::account_info::AccountInfo<'a>,
307
308 pub transfer_authority: &'b solana_program::account_info::AccountInfo<'a>,
309
310 pub perpetuals: &'b solana_program::account_info::AccountInfo<'a>,
311
312 pub system_program: &'b solana_program::account_info::AccountInfo<'a>,
313
314 pub token_program: &'b solana_program::account_info::AccountInfo<'a>,
315 pub __args: TestInitInstructionArgs,
317}
318
319impl<'a, 'b> TestInitCpi<'a, 'b> {
320 pub fn new(
321 program: &'b solana_program::account_info::AccountInfo<'a>,
322 accounts: TestInitCpiAccounts<'a, 'b>,
323 args: TestInitInstructionArgs,
324 ) -> Self {
325 Self {
326 __program: program,
327 upgrade_authority: accounts.upgrade_authority,
328 admin: accounts.admin,
329 transfer_authority: accounts.transfer_authority,
330 perpetuals: accounts.perpetuals,
331 system_program: accounts.system_program,
332 token_program: accounts.token_program,
333 __args: args,
334 }
335 }
336 #[inline(always)]
337 pub fn invoke(&self) -> solana_program::entrypoint::ProgramResult {
338 self.invoke_signed_with_remaining_accounts(&[], &[])
339 }
340 #[inline(always)]
341 pub fn invoke_with_remaining_accounts(
342 &self,
343 remaining_accounts: &[(
344 &'b solana_program::account_info::AccountInfo<'a>,
345 bool,
346 bool,
347 )],
348 ) -> solana_program::entrypoint::ProgramResult {
349 self.invoke_signed_with_remaining_accounts(&[], remaining_accounts)
350 }
351 #[inline(always)]
352 pub fn invoke_signed(
353 &self,
354 signers_seeds: &[&[&[u8]]],
355 ) -> solana_program::entrypoint::ProgramResult {
356 self.invoke_signed_with_remaining_accounts(signers_seeds, &[])
357 }
358 #[allow(clippy::arithmetic_side_effects)]
359 #[allow(clippy::clone_on_copy)]
360 #[allow(clippy::vec_init_then_push)]
361 pub fn invoke_signed_with_remaining_accounts(
362 &self,
363 signers_seeds: &[&[&[u8]]],
364 remaining_accounts: &[(
365 &'b solana_program::account_info::AccountInfo<'a>,
366 bool,
367 bool,
368 )],
369 ) -> solana_program::entrypoint::ProgramResult {
370 let mut accounts = Vec::with_capacity(6 + remaining_accounts.len());
371 accounts.push(solana_program::instruction::AccountMeta::new(
372 *self.upgrade_authority.key,
373 true,
374 ));
375 accounts.push(solana_program::instruction::AccountMeta::new_readonly(
376 *self.admin.key,
377 false,
378 ));
379 accounts.push(solana_program::instruction::AccountMeta::new(
380 *self.transfer_authority.key,
381 false,
382 ));
383 accounts.push(solana_program::instruction::AccountMeta::new(
384 *self.perpetuals.key,
385 false,
386 ));
387 accounts.push(solana_program::instruction::AccountMeta::new_readonly(
388 *self.system_program.key,
389 false,
390 ));
391 accounts.push(solana_program::instruction::AccountMeta::new_readonly(
392 *self.token_program.key,
393 false,
394 ));
395 remaining_accounts.iter().for_each(|remaining_account| {
396 accounts.push(solana_program::instruction::AccountMeta {
397 pubkey: *remaining_account.0.key,
398 is_signer: remaining_account.1,
399 is_writable: remaining_account.2,
400 })
401 });
402 let mut data = borsh::to_vec(&TestInitInstructionData::new()).unwrap();
403 let mut args = borsh::to_vec(&self.__args).unwrap();
404 data.append(&mut args);
405
406 let instruction = solana_program::instruction::Instruction {
407 program_id: crate::PERPETUALS_ID,
408 accounts,
409 data,
410 };
411 let mut account_infos = Vec::with_capacity(7 + remaining_accounts.len());
412 account_infos.push(self.__program.clone());
413 account_infos.push(self.upgrade_authority.clone());
414 account_infos.push(self.admin.clone());
415 account_infos.push(self.transfer_authority.clone());
416 account_infos.push(self.perpetuals.clone());
417 account_infos.push(self.system_program.clone());
418 account_infos.push(self.token_program.clone());
419 remaining_accounts
420 .iter()
421 .for_each(|remaining_account| account_infos.push(remaining_account.0.clone()));
422
423 if signers_seeds.is_empty() {
424 solana_program::program::invoke(&instruction, &account_infos)
425 } else {
426 solana_program::program::invoke_signed(&instruction, &account_infos, signers_seeds)
427 }
428 }
429}
430
431#[derive(Clone, Debug)]
442pub struct TestInitCpiBuilder<'a, 'b> {
443 instruction: Box<TestInitCpiBuilderInstruction<'a, 'b>>,
444}
445
446impl<'a, 'b> TestInitCpiBuilder<'a, 'b> {
447 pub fn new(program: &'b solana_program::account_info::AccountInfo<'a>) -> Self {
448 let instruction = Box::new(TestInitCpiBuilderInstruction {
449 __program: program,
450 upgrade_authority: None,
451 admin: None,
452 transfer_authority: None,
453 perpetuals: None,
454 system_program: None,
455 token_program: None,
456 allow_swap: None,
457 allow_add_liquidity: None,
458 allow_remove_liquidity: None,
459 allow_increase_position: None,
460 allow_decrease_position: None,
461 allow_collateral_withdrawal: None,
462 allow_liquidate_position: None,
463 __remaining_accounts: Vec::new(),
464 });
465 Self { instruction }
466 }
467 #[inline(always)]
468 pub fn upgrade_authority(
469 &mut self,
470 upgrade_authority: &'b solana_program::account_info::AccountInfo<'a>,
471 ) -> &mut Self {
472 self.instruction.upgrade_authority = Some(upgrade_authority);
473 self
474 }
475 #[inline(always)]
476 pub fn admin(&mut self, admin: &'b solana_program::account_info::AccountInfo<'a>) -> &mut Self {
477 self.instruction.admin = Some(admin);
478 self
479 }
480 #[inline(always)]
481 pub fn transfer_authority(
482 &mut self,
483 transfer_authority: &'b solana_program::account_info::AccountInfo<'a>,
484 ) -> &mut Self {
485 self.instruction.transfer_authority = Some(transfer_authority);
486 self
487 }
488 #[inline(always)]
489 pub fn perpetuals(
490 &mut self,
491 perpetuals: &'b solana_program::account_info::AccountInfo<'a>,
492 ) -> &mut Self {
493 self.instruction.perpetuals = Some(perpetuals);
494 self
495 }
496 #[inline(always)]
497 pub fn system_program(
498 &mut self,
499 system_program: &'b solana_program::account_info::AccountInfo<'a>,
500 ) -> &mut Self {
501 self.instruction.system_program = Some(system_program);
502 self
503 }
504 #[inline(always)]
505 pub fn token_program(
506 &mut self,
507 token_program: &'b solana_program::account_info::AccountInfo<'a>,
508 ) -> &mut Self {
509 self.instruction.token_program = Some(token_program);
510 self
511 }
512 #[inline(always)]
513 pub fn allow_swap(&mut self, allow_swap: bool) -> &mut Self {
514 self.instruction.allow_swap = Some(allow_swap);
515 self
516 }
517 #[inline(always)]
518 pub fn allow_add_liquidity(&mut self, allow_add_liquidity: bool) -> &mut Self {
519 self.instruction.allow_add_liquidity = Some(allow_add_liquidity);
520 self
521 }
522 #[inline(always)]
523 pub fn allow_remove_liquidity(&mut self, allow_remove_liquidity: bool) -> &mut Self {
524 self.instruction.allow_remove_liquidity = Some(allow_remove_liquidity);
525 self
526 }
527 #[inline(always)]
528 pub fn allow_increase_position(&mut self, allow_increase_position: bool) -> &mut Self {
529 self.instruction.allow_increase_position = Some(allow_increase_position);
530 self
531 }
532 #[inline(always)]
533 pub fn allow_decrease_position(&mut self, allow_decrease_position: bool) -> &mut Self {
534 self.instruction.allow_decrease_position = Some(allow_decrease_position);
535 self
536 }
537 #[inline(always)]
538 pub fn allow_collateral_withdrawal(&mut self, allow_collateral_withdrawal: bool) -> &mut Self {
539 self.instruction.allow_collateral_withdrawal = Some(allow_collateral_withdrawal);
540 self
541 }
542 #[inline(always)]
543 pub fn allow_liquidate_position(&mut self, allow_liquidate_position: bool) -> &mut Self {
544 self.instruction.allow_liquidate_position = Some(allow_liquidate_position);
545 self
546 }
547 #[inline(always)]
549 pub fn add_remaining_account(
550 &mut self,
551 account: &'b solana_program::account_info::AccountInfo<'a>,
552 is_writable: bool,
553 is_signer: bool,
554 ) -> &mut Self {
555 self.instruction
556 .__remaining_accounts
557 .push((account, is_writable, is_signer));
558 self
559 }
560 #[inline(always)]
565 pub fn add_remaining_accounts(
566 &mut self,
567 accounts: &[(
568 &'b solana_program::account_info::AccountInfo<'a>,
569 bool,
570 bool,
571 )],
572 ) -> &mut Self {
573 self.instruction
574 .__remaining_accounts
575 .extend_from_slice(accounts);
576 self
577 }
578 #[inline(always)]
579 pub fn invoke(&self) -> solana_program::entrypoint::ProgramResult {
580 self.invoke_signed(&[])
581 }
582 #[allow(clippy::clone_on_copy)]
583 #[allow(clippy::vec_init_then_push)]
584 pub fn invoke_signed(
585 &self,
586 signers_seeds: &[&[&[u8]]],
587 ) -> solana_program::entrypoint::ProgramResult {
588 let args = TestInitInstructionArgs {
589 allow_swap: self
590 .instruction
591 .allow_swap
592 .clone()
593 .expect("allow_swap is not set"),
594 allow_add_liquidity: self
595 .instruction
596 .allow_add_liquidity
597 .clone()
598 .expect("allow_add_liquidity is not set"),
599 allow_remove_liquidity: self
600 .instruction
601 .allow_remove_liquidity
602 .clone()
603 .expect("allow_remove_liquidity is not set"),
604 allow_increase_position: self
605 .instruction
606 .allow_increase_position
607 .clone()
608 .expect("allow_increase_position is not set"),
609 allow_decrease_position: self
610 .instruction
611 .allow_decrease_position
612 .clone()
613 .expect("allow_decrease_position is not set"),
614 allow_collateral_withdrawal: self
615 .instruction
616 .allow_collateral_withdrawal
617 .clone()
618 .expect("allow_collateral_withdrawal is not set"),
619 allow_liquidate_position: self
620 .instruction
621 .allow_liquidate_position
622 .clone()
623 .expect("allow_liquidate_position is not set"),
624 };
625 let instruction = TestInitCpi {
626 __program: self.instruction.__program,
627
628 upgrade_authority: self
629 .instruction
630 .upgrade_authority
631 .expect("upgrade_authority is not set"),
632
633 admin: self.instruction.admin.expect("admin is not set"),
634
635 transfer_authority: self
636 .instruction
637 .transfer_authority
638 .expect("transfer_authority is not set"),
639
640 perpetuals: self.instruction.perpetuals.expect("perpetuals is not set"),
641
642 system_program: self
643 .instruction
644 .system_program
645 .expect("system_program is not set"),
646
647 token_program: self
648 .instruction
649 .token_program
650 .expect("token_program is not set"),
651 __args: args,
652 };
653 instruction.invoke_signed_with_remaining_accounts(
654 signers_seeds,
655 &self.instruction.__remaining_accounts,
656 )
657 }
658}
659
660#[derive(Clone, Debug)]
661struct TestInitCpiBuilderInstruction<'a, 'b> {
662 __program: &'b solana_program::account_info::AccountInfo<'a>,
663 upgrade_authority: Option<&'b solana_program::account_info::AccountInfo<'a>>,
664 admin: Option<&'b solana_program::account_info::AccountInfo<'a>>,
665 transfer_authority: Option<&'b solana_program::account_info::AccountInfo<'a>>,
666 perpetuals: Option<&'b solana_program::account_info::AccountInfo<'a>>,
667 system_program: Option<&'b solana_program::account_info::AccountInfo<'a>>,
668 token_program: Option<&'b solana_program::account_info::AccountInfo<'a>>,
669 allow_swap: Option<bool>,
670 allow_add_liquidity: Option<bool>,
671 allow_remove_liquidity: Option<bool>,
672 allow_increase_position: Option<bool>,
673 allow_decrease_position: Option<bool>,
674 allow_collateral_withdrawal: Option<bool>,
675 allow_liquidate_position: Option<bool>,
676 __remaining_accounts: Vec<(
678 &'b solana_program::account_info::AccountInfo<'a>,
679 bool,
680 bool,
681 )>,
682}