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