hpsvm_token/
transfer_checked.rs1use hpsvm::{HPSVM, types::FailedTransactionMetadata};
2use smallvec::{SmallVec, smallvec};
3use solana_address::Address;
4use solana_keypair::Keypair;
5use solana_signer::{Signer, signers::Signers};
6use solana_transaction::Transaction;
7
8use super::{
9 TOKEN_ID, get_multisig_signers, get_spl_account,
10 spl_token::{instruction::transfer_checked, state::Mint},
11};
12
13#[derive(Debug)]
22pub struct TransferChecked<'a> {
23 svm: &'a mut HPSVM,
24 payer: &'a Keypair,
25 mint: &'a Address,
26 source: Option<&'a Address>,
27 destination: &'a Address,
28 token_program_id: Option<&'a Address>,
29 amount: u64,
30 decimals: Option<u8>,
31 signers: SmallVec<[&'a Keypair; 1]>,
32 owner: Option<Address>,
33}
34
35impl<'a> TransferChecked<'a> {
36 pub fn new(
38 svm: &'a mut HPSVM,
39 payer: &'a Keypair,
40 mint: &'a Address,
41 destination: &'a Address,
42 amount: u64,
43 ) -> Self {
44 TransferChecked {
45 svm,
46 payer,
47 mint,
48 source: None,
49 destination,
50 token_program_id: None,
51 amount,
52 decimals: None,
53 owner: None,
54 signers: smallvec![payer],
55 }
56 }
57
58 pub fn token_program_id(mut self, program_id: &'a Address) -> Self {
60 self.token_program_id = Some(program_id);
61 self
62 }
63
64 pub fn decimals(mut self, value: u8) -> Self {
66 self.decimals = Some(value);
67 self
68 }
69
70 pub fn source(mut self, source: &'a Address) -> Self {
72 self.source = Some(source);
73 self
74 }
75
76 pub fn owner(mut self, owner: &'a Keypair) -> Self {
78 self.owner = Some(owner.pubkey());
79 self.signers = smallvec![owner];
80 self
81 }
82
83 pub fn multisig(mut self, multisig: &'a Address, signers: &'a [&'a Keypair]) -> Self {
85 self.owner = Some(*multisig);
86 self.signers = SmallVec::from(signers);
87 self
88 }
89
90 pub fn send(self) -> Result<(), FailedTransactionMetadata> {
92 let payer_pk = self.payer.pubkey();
93 let token_program_id = self.token_program_id.unwrap_or(&TOKEN_ID);
94
95 let authority = self.owner.unwrap_or(payer_pk);
96 let signing_keys = self.signers.pubkeys();
97 let signer_keys = get_multisig_signers(&authority, &signing_keys);
98
99 let source_pk = if let Some(source) = self.source {
100 *source
101 } else {
102 spl_associated_token_account_interface::address::get_associated_token_address_with_program_id(
103 &authority,
104 self.mint,
105 token_program_id,
106 )
107 };
108
109 let mint: Mint = get_spl_account(self.svm, self.mint)?;
110 let ix = transfer_checked(
111 token_program_id,
112 &source_pk,
113 self.mint,
114 self.destination,
115 &authority,
116 &signer_keys,
117 self.amount,
118 self.decimals.unwrap_or(mint.decimals),
119 )?;
120
121 let block_hash = self.svm.latest_blockhash();
122 let mut tx = Transaction::new_with_payer(&[ix], Some(&payer_pk));
123 tx.partial_sign(&[self.payer], block_hash);
124 tx.partial_sign(self.signers.as_ref(), block_hash);
125
126 self.svm.send_transaction(tx)?;
127
128 Ok(())
129 }
130}