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