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