1use borsh::{BorshDeserialize, BorshSerialize};
8
9pub const MARKET_DEPOSIT_DISCRIMINATOR: u8 = 10;
10
11#[derive(Debug)]
13pub struct MarketDeposit {
14 pub authority: solana_address::Address,
16 pub market: solana_address::Address,
18 pub mint_a: solana_address::Address,
20 pub mint_b: solana_address::Address,
22 pub authority_a: solana_address::Address,
24 pub authority_b: solana_address::Address,
26 pub market_a: solana_address::Address,
28 pub market_b: solana_address::Address,
30 pub token_program_a: solana_address::Address,
32 pub token_program_b: solana_address::Address,
34 pub memo_program: solana_address::Address,
36}
37
38impl MarketDeposit {
39 pub fn instruction(
40 &self,
41 args: MarketDepositInstructionArgs,
42 ) -> solana_instruction::Instruction {
43 self.instruction_with_remaining_accounts(args, &[])
44 }
45 #[allow(clippy::arithmetic_side_effects)]
46 #[allow(clippy::vec_init_then_push)]
47 pub fn instruction_with_remaining_accounts(
48 &self,
49 args: MarketDepositInstructionArgs,
50 remaining_accounts: &[solana_instruction::AccountMeta],
51 ) -> solana_instruction::Instruction {
52 let mut accounts = Vec::with_capacity(11 + remaining_accounts.len());
53 accounts.push(solana_instruction::AccountMeta::new_readonly(
54 self.authority,
55 true,
56 ));
57 accounts.push(solana_instruction::AccountMeta::new_readonly(
58 self.market,
59 false,
60 ));
61 accounts.push(solana_instruction::AccountMeta::new_readonly(
62 self.mint_a,
63 false,
64 ));
65 accounts.push(solana_instruction::AccountMeta::new_readonly(
66 self.mint_b,
67 false,
68 ));
69 accounts.push(solana_instruction::AccountMeta::new(
70 self.authority_a,
71 false,
72 ));
73 accounts.push(solana_instruction::AccountMeta::new(
74 self.authority_b,
75 false,
76 ));
77 accounts.push(solana_instruction::AccountMeta::new(self.market_a, false));
78 accounts.push(solana_instruction::AccountMeta::new(self.market_b, false));
79 accounts.push(solana_instruction::AccountMeta::new_readonly(
80 self.token_program_a,
81 false,
82 ));
83 accounts.push(solana_instruction::AccountMeta::new_readonly(
84 self.token_program_b,
85 false,
86 ));
87 accounts.push(solana_instruction::AccountMeta::new_readonly(
88 self.memo_program,
89 false,
90 ));
91 accounts.extend_from_slice(remaining_accounts);
92 let mut data = MarketDepositInstructionData::new().try_to_vec().unwrap();
93 let mut args = args.try_to_vec().unwrap();
94 data.append(&mut args);
95
96 solana_instruction::Instruction {
97 program_id: crate::RIPTIDE_ID,
98 accounts,
99 data,
100 }
101 }
102}
103
104#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
105pub struct MarketDepositInstructionData {
106 discriminator: u8,
107}
108
109impl MarketDepositInstructionData {
110 pub fn new() -> Self {
111 Self { discriminator: 10 }
112 }
113
114 pub(crate) fn try_to_vec(&self) -> Result<Vec<u8>, std::io::Error> {
115 borsh::to_vec(self)
116 }
117}
118
119impl Default for MarketDepositInstructionData {
120 fn default() -> Self {
121 Self::new()
122 }
123}
124
125#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, Eq, PartialEq)]
126pub struct MarketDepositInstructionArgs {
127 pub amount_a: u64,
128 pub amount_b: u64,
129}
130
131impl MarketDepositInstructionArgs {
132 pub(crate) fn try_to_vec(&self) -> Result<Vec<u8>, std::io::Error> {
133 borsh::to_vec(self)
134 }
135}
136
137#[derive(Clone, Debug, Default)]
153pub struct MarketDepositBuilder {
154 authority: Option<solana_address::Address>,
155 market: Option<solana_address::Address>,
156 mint_a: Option<solana_address::Address>,
157 mint_b: Option<solana_address::Address>,
158 authority_a: Option<solana_address::Address>,
159 authority_b: Option<solana_address::Address>,
160 market_a: Option<solana_address::Address>,
161 market_b: Option<solana_address::Address>,
162 token_program_a: Option<solana_address::Address>,
163 token_program_b: Option<solana_address::Address>,
164 memo_program: Option<solana_address::Address>,
165 amount_a: Option<u64>,
166 amount_b: Option<u64>,
167 __remaining_accounts: Vec<solana_instruction::AccountMeta>,
168}
169
170impl MarketDepositBuilder {
171 pub fn new() -> Self {
172 Self::default()
173 }
174 #[inline(always)]
176 pub fn authority(&mut self, authority: solana_address::Address) -> &mut Self {
177 self.authority = Some(authority);
178 self
179 }
180 #[inline(always)]
182 pub fn market(&mut self, market: solana_address::Address) -> &mut Self {
183 self.market = Some(market);
184 self
185 }
186 #[inline(always)]
188 pub fn mint_a(&mut self, mint_a: solana_address::Address) -> &mut Self {
189 self.mint_a = Some(mint_a);
190 self
191 }
192 #[inline(always)]
194 pub fn mint_b(&mut self, mint_b: solana_address::Address) -> &mut Self {
195 self.mint_b = Some(mint_b);
196 self
197 }
198 #[inline(always)]
200 pub fn authority_a(&mut self, authority_a: solana_address::Address) -> &mut Self {
201 self.authority_a = Some(authority_a);
202 self
203 }
204 #[inline(always)]
206 pub fn authority_b(&mut self, authority_b: solana_address::Address) -> &mut Self {
207 self.authority_b = Some(authority_b);
208 self
209 }
210 #[inline(always)]
212 pub fn market_a(&mut self, market_a: solana_address::Address) -> &mut Self {
213 self.market_a = Some(market_a);
214 self
215 }
216 #[inline(always)]
218 pub fn market_b(&mut self, market_b: solana_address::Address) -> &mut Self {
219 self.market_b = Some(market_b);
220 self
221 }
222 #[inline(always)]
224 pub fn token_program_a(&mut self, token_program_a: solana_address::Address) -> &mut Self {
225 self.token_program_a = Some(token_program_a);
226 self
227 }
228 #[inline(always)]
230 pub fn token_program_b(&mut self, token_program_b: solana_address::Address) -> &mut Self {
231 self.token_program_b = Some(token_program_b);
232 self
233 }
234 #[inline(always)]
236 pub fn memo_program(&mut self, memo_program: solana_address::Address) -> &mut Self {
237 self.memo_program = Some(memo_program);
238 self
239 }
240 #[inline(always)]
241 pub fn amount_a(&mut self, amount_a: u64) -> &mut Self {
242 self.amount_a = Some(amount_a);
243 self
244 }
245 #[inline(always)]
246 pub fn amount_b(&mut self, amount_b: u64) -> &mut Self {
247 self.amount_b = Some(amount_b);
248 self
249 }
250 #[inline(always)]
252 pub fn add_remaining_account(&mut self, account: solana_instruction::AccountMeta) -> &mut Self {
253 self.__remaining_accounts.push(account);
254 self
255 }
256 #[inline(always)]
258 pub fn add_remaining_accounts(
259 &mut self,
260 accounts: &[solana_instruction::AccountMeta],
261 ) -> &mut Self {
262 self.__remaining_accounts.extend_from_slice(accounts);
263 self
264 }
265 #[allow(clippy::clone_on_copy)]
266 pub fn instruction(&self) -> solana_instruction::Instruction {
267 let accounts = MarketDeposit {
268 authority: self.authority.expect("authority is not set"),
269 market: self.market.expect("market is not set"),
270 mint_a: self.mint_a.expect("mint_a is not set"),
271 mint_b: self.mint_b.expect("mint_b is not set"),
272 authority_a: self.authority_a.expect("authority_a is not set"),
273 authority_b: self.authority_b.expect("authority_b is not set"),
274 market_a: self.market_a.expect("market_a is not set"),
275 market_b: self.market_b.expect("market_b is not set"),
276 token_program_a: self.token_program_a.expect("token_program_a is not set"),
277 token_program_b: self.token_program_b.expect("token_program_b is not set"),
278 memo_program: self.memo_program.expect("memo_program is not set"),
279 };
280 let args = MarketDepositInstructionArgs {
281 amount_a: self.amount_a.clone().expect("amount_a is not set"),
282 amount_b: self.amount_b.clone().expect("amount_b is not set"),
283 };
284
285 accounts.instruction_with_remaining_accounts(args, &self.__remaining_accounts)
286 }
287}
288
289pub struct MarketDepositCpiAccounts<'a, 'b> {
291 pub authority: &'b solana_account_info::AccountInfo<'a>,
293 pub market: &'b solana_account_info::AccountInfo<'a>,
295 pub mint_a: &'b solana_account_info::AccountInfo<'a>,
297 pub mint_b: &'b solana_account_info::AccountInfo<'a>,
299 pub authority_a: &'b solana_account_info::AccountInfo<'a>,
301 pub authority_b: &'b solana_account_info::AccountInfo<'a>,
303 pub market_a: &'b solana_account_info::AccountInfo<'a>,
305 pub market_b: &'b solana_account_info::AccountInfo<'a>,
307 pub token_program_a: &'b solana_account_info::AccountInfo<'a>,
309 pub token_program_b: &'b solana_account_info::AccountInfo<'a>,
311 pub memo_program: &'b solana_account_info::AccountInfo<'a>,
313}
314
315pub struct MarketDepositCpi<'a, 'b> {
317 pub __program: &'b solana_account_info::AccountInfo<'a>,
319 pub authority: &'b solana_account_info::AccountInfo<'a>,
321 pub market: &'b solana_account_info::AccountInfo<'a>,
323 pub mint_a: &'b solana_account_info::AccountInfo<'a>,
325 pub mint_b: &'b solana_account_info::AccountInfo<'a>,
327 pub authority_a: &'b solana_account_info::AccountInfo<'a>,
329 pub authority_b: &'b solana_account_info::AccountInfo<'a>,
331 pub market_a: &'b solana_account_info::AccountInfo<'a>,
333 pub market_b: &'b solana_account_info::AccountInfo<'a>,
335 pub token_program_a: &'b solana_account_info::AccountInfo<'a>,
337 pub token_program_b: &'b solana_account_info::AccountInfo<'a>,
339 pub memo_program: &'b solana_account_info::AccountInfo<'a>,
341 pub __args: MarketDepositInstructionArgs,
343}
344
345impl<'a, 'b> MarketDepositCpi<'a, 'b> {
346 pub fn new(
347 program: &'b solana_account_info::AccountInfo<'a>,
348 accounts: MarketDepositCpiAccounts<'a, 'b>,
349 args: MarketDepositInstructionArgs,
350 ) -> Self {
351 Self {
352 __program: program,
353 authority: accounts.authority,
354 market: accounts.market,
355 mint_a: accounts.mint_a,
356 mint_b: accounts.mint_b,
357 authority_a: accounts.authority_a,
358 authority_b: accounts.authority_b,
359 market_a: accounts.market_a,
360 market_b: accounts.market_b,
361 token_program_a: accounts.token_program_a,
362 token_program_b: accounts.token_program_b,
363 memo_program: accounts.memo_program,
364 __args: args,
365 }
366 }
367 #[inline(always)]
368 pub fn invoke(&self) -> solana_program_error::ProgramResult {
369 self.invoke_signed_with_remaining_accounts(&[], &[])
370 }
371 #[inline(always)]
372 pub fn invoke_with_remaining_accounts(
373 &self,
374 remaining_accounts: &[(&'b solana_account_info::AccountInfo<'a>, bool, bool)],
375 ) -> solana_program_error::ProgramResult {
376 self.invoke_signed_with_remaining_accounts(&[], remaining_accounts)
377 }
378 #[inline(always)]
379 pub fn invoke_signed(&self, signers_seeds: &[&[&[u8]]]) -> solana_program_error::ProgramResult {
380 self.invoke_signed_with_remaining_accounts(signers_seeds, &[])
381 }
382 #[allow(clippy::arithmetic_side_effects)]
383 #[allow(clippy::clone_on_copy)]
384 #[allow(clippy::vec_init_then_push)]
385 pub fn invoke_signed_with_remaining_accounts(
386 &self,
387 signers_seeds: &[&[&[u8]]],
388 remaining_accounts: &[(&'b solana_account_info::AccountInfo<'a>, bool, bool)],
389 ) -> solana_program_error::ProgramResult {
390 let mut accounts = Vec::with_capacity(11 + remaining_accounts.len());
391 accounts.push(solana_instruction::AccountMeta::new_readonly(
392 *self.authority.key,
393 true,
394 ));
395 accounts.push(solana_instruction::AccountMeta::new_readonly(
396 *self.market.key,
397 false,
398 ));
399 accounts.push(solana_instruction::AccountMeta::new_readonly(
400 *self.mint_a.key,
401 false,
402 ));
403 accounts.push(solana_instruction::AccountMeta::new_readonly(
404 *self.mint_b.key,
405 false,
406 ));
407 accounts.push(solana_instruction::AccountMeta::new(
408 *self.authority_a.key,
409 false,
410 ));
411 accounts.push(solana_instruction::AccountMeta::new(
412 *self.authority_b.key,
413 false,
414 ));
415 accounts.push(solana_instruction::AccountMeta::new(
416 *self.market_a.key,
417 false,
418 ));
419 accounts.push(solana_instruction::AccountMeta::new(
420 *self.market_b.key,
421 false,
422 ));
423 accounts.push(solana_instruction::AccountMeta::new_readonly(
424 *self.token_program_a.key,
425 false,
426 ));
427 accounts.push(solana_instruction::AccountMeta::new_readonly(
428 *self.token_program_b.key,
429 false,
430 ));
431 accounts.push(solana_instruction::AccountMeta::new_readonly(
432 *self.memo_program.key,
433 false,
434 ));
435 remaining_accounts.iter().for_each(|remaining_account| {
436 accounts.push(solana_instruction::AccountMeta {
437 pubkey: *remaining_account.0.key,
438 is_signer: remaining_account.1,
439 is_writable: remaining_account.2,
440 })
441 });
442 let mut data = MarketDepositInstructionData::new().try_to_vec().unwrap();
443 let mut args = self.__args.try_to_vec().unwrap();
444 data.append(&mut args);
445
446 let instruction = solana_instruction::Instruction {
447 program_id: crate::RIPTIDE_ID,
448 accounts,
449 data,
450 };
451 let mut account_infos = Vec::with_capacity(12 + remaining_accounts.len());
452 account_infos.push(self.__program.clone());
453 account_infos.push(self.authority.clone());
454 account_infos.push(self.market.clone());
455 account_infos.push(self.mint_a.clone());
456 account_infos.push(self.mint_b.clone());
457 account_infos.push(self.authority_a.clone());
458 account_infos.push(self.authority_b.clone());
459 account_infos.push(self.market_a.clone());
460 account_infos.push(self.market_b.clone());
461 account_infos.push(self.token_program_a.clone());
462 account_infos.push(self.token_program_b.clone());
463 account_infos.push(self.memo_program.clone());
464 remaining_accounts
465 .iter()
466 .for_each(|remaining_account| account_infos.push(remaining_account.0.clone()));
467
468 if signers_seeds.is_empty() {
469 solana_cpi::invoke(&instruction, &account_infos)
470 } else {
471 solana_cpi::invoke_signed(&instruction, &account_infos, signers_seeds)
472 }
473 }
474}
475
476#[derive(Clone, Debug)]
492pub struct MarketDepositCpiBuilder<'a, 'b> {
493 instruction: Box<MarketDepositCpiBuilderInstruction<'a, 'b>>,
494}
495
496impl<'a, 'b> MarketDepositCpiBuilder<'a, 'b> {
497 pub fn new(program: &'b solana_account_info::AccountInfo<'a>) -> Self {
498 let instruction = Box::new(MarketDepositCpiBuilderInstruction {
499 __program: program,
500 authority: None,
501 market: None,
502 mint_a: None,
503 mint_b: None,
504 authority_a: None,
505 authority_b: None,
506 market_a: None,
507 market_b: None,
508 token_program_a: None,
509 token_program_b: None,
510 memo_program: None,
511 amount_a: None,
512 amount_b: None,
513 __remaining_accounts: Vec::new(),
514 });
515 Self { instruction }
516 }
517 #[inline(always)]
519 pub fn authority(&mut self, authority: &'b solana_account_info::AccountInfo<'a>) -> &mut Self {
520 self.instruction.authority = Some(authority);
521 self
522 }
523 #[inline(always)]
525 pub fn market(&mut self, market: &'b solana_account_info::AccountInfo<'a>) -> &mut Self {
526 self.instruction.market = Some(market);
527 self
528 }
529 #[inline(always)]
531 pub fn mint_a(&mut self, mint_a: &'b solana_account_info::AccountInfo<'a>) -> &mut Self {
532 self.instruction.mint_a = Some(mint_a);
533 self
534 }
535 #[inline(always)]
537 pub fn mint_b(&mut self, mint_b: &'b solana_account_info::AccountInfo<'a>) -> &mut Self {
538 self.instruction.mint_b = Some(mint_b);
539 self
540 }
541 #[inline(always)]
543 pub fn authority_a(
544 &mut self,
545 authority_a: &'b solana_account_info::AccountInfo<'a>,
546 ) -> &mut Self {
547 self.instruction.authority_a = Some(authority_a);
548 self
549 }
550 #[inline(always)]
552 pub fn authority_b(
553 &mut self,
554 authority_b: &'b solana_account_info::AccountInfo<'a>,
555 ) -> &mut Self {
556 self.instruction.authority_b = Some(authority_b);
557 self
558 }
559 #[inline(always)]
561 pub fn market_a(&mut self, market_a: &'b solana_account_info::AccountInfo<'a>) -> &mut Self {
562 self.instruction.market_a = Some(market_a);
563 self
564 }
565 #[inline(always)]
567 pub fn market_b(&mut self, market_b: &'b solana_account_info::AccountInfo<'a>) -> &mut Self {
568 self.instruction.market_b = Some(market_b);
569 self
570 }
571 #[inline(always)]
573 pub fn token_program_a(
574 &mut self,
575 token_program_a: &'b solana_account_info::AccountInfo<'a>,
576 ) -> &mut Self {
577 self.instruction.token_program_a = Some(token_program_a);
578 self
579 }
580 #[inline(always)]
582 pub fn token_program_b(
583 &mut self,
584 token_program_b: &'b solana_account_info::AccountInfo<'a>,
585 ) -> &mut Self {
586 self.instruction.token_program_b = Some(token_program_b);
587 self
588 }
589 #[inline(always)]
591 pub fn memo_program(
592 &mut self,
593 memo_program: &'b solana_account_info::AccountInfo<'a>,
594 ) -> &mut Self {
595 self.instruction.memo_program = Some(memo_program);
596 self
597 }
598 #[inline(always)]
599 pub fn amount_a(&mut self, amount_a: u64) -> &mut Self {
600 self.instruction.amount_a = Some(amount_a);
601 self
602 }
603 #[inline(always)]
604 pub fn amount_b(&mut self, amount_b: u64) -> &mut Self {
605 self.instruction.amount_b = Some(amount_b);
606 self
607 }
608 #[inline(always)]
610 pub fn add_remaining_account(
611 &mut self,
612 account: &'b solana_account_info::AccountInfo<'a>,
613 is_writable: bool,
614 is_signer: bool,
615 ) -> &mut Self {
616 self.instruction
617 .__remaining_accounts
618 .push((account, is_writable, is_signer));
619 self
620 }
621 #[inline(always)]
627 pub fn add_remaining_accounts(
628 &mut self,
629 accounts: &[(&'b solana_account_info::AccountInfo<'a>, bool, bool)],
630 ) -> &mut Self {
631 self.instruction
632 .__remaining_accounts
633 .extend_from_slice(accounts);
634 self
635 }
636 #[inline(always)]
637 pub fn invoke(&self) -> solana_program_error::ProgramResult {
638 self.invoke_signed(&[])
639 }
640 #[allow(clippy::clone_on_copy)]
641 #[allow(clippy::vec_init_then_push)]
642 pub fn invoke_signed(&self, signers_seeds: &[&[&[u8]]]) -> solana_program_error::ProgramResult {
643 let args = MarketDepositInstructionArgs {
644 amount_a: self
645 .instruction
646 .amount_a
647 .clone()
648 .expect("amount_a is not set"),
649 amount_b: self
650 .instruction
651 .amount_b
652 .clone()
653 .expect("amount_b is not set"),
654 };
655 let instruction = MarketDepositCpi {
656 __program: self.instruction.__program,
657
658 authority: self.instruction.authority.expect("authority is not set"),
659
660 market: self.instruction.market.expect("market is not set"),
661
662 mint_a: self.instruction.mint_a.expect("mint_a is not set"),
663
664 mint_b: self.instruction.mint_b.expect("mint_b is not set"),
665
666 authority_a: self
667 .instruction
668 .authority_a
669 .expect("authority_a is not set"),
670
671 authority_b: self
672 .instruction
673 .authority_b
674 .expect("authority_b is not set"),
675
676 market_a: self.instruction.market_a.expect("market_a is not set"),
677
678 market_b: self.instruction.market_b.expect("market_b is not set"),
679
680 token_program_a: self
681 .instruction
682 .token_program_a
683 .expect("token_program_a is not set"),
684
685 token_program_b: self
686 .instruction
687 .token_program_b
688 .expect("token_program_b is not set"),
689
690 memo_program: self
691 .instruction
692 .memo_program
693 .expect("memo_program is not set"),
694 __args: args,
695 };
696 instruction.invoke_signed_with_remaining_accounts(
697 signers_seeds,
698 &self.instruction.__remaining_accounts,
699 )
700 }
701}
702
703#[derive(Clone, Debug)]
704struct MarketDepositCpiBuilderInstruction<'a, 'b> {
705 __program: &'b solana_account_info::AccountInfo<'a>,
706 authority: Option<&'b solana_account_info::AccountInfo<'a>>,
707 market: Option<&'b solana_account_info::AccountInfo<'a>>,
708 mint_a: Option<&'b solana_account_info::AccountInfo<'a>>,
709 mint_b: Option<&'b solana_account_info::AccountInfo<'a>>,
710 authority_a: Option<&'b solana_account_info::AccountInfo<'a>>,
711 authority_b: Option<&'b solana_account_info::AccountInfo<'a>>,
712 market_a: Option<&'b solana_account_info::AccountInfo<'a>>,
713 market_b: Option<&'b solana_account_info::AccountInfo<'a>>,
714 token_program_a: Option<&'b solana_account_info::AccountInfo<'a>>,
715 token_program_b: Option<&'b solana_account_info::AccountInfo<'a>>,
716 memo_program: Option<&'b solana_account_info::AccountInfo<'a>>,
717 amount_a: Option<u64>,
718 amount_b: Option<u64>,
719 __remaining_accounts: Vec<(&'b solana_account_info::AccountInfo<'a>, bool, bool)>,
721}