litesvm_token/
close_account.rs1use {
2 super::{get_multisig_signers, spl_token::instruction::close_account, TOKEN_ID},
3 litesvm::{types::FailedTransactionMetadata, LiteSVM},
4 smallvec::{smallvec, SmallVec},
5 solana_keypair::Keypair,
6 solana_pubkey::Pubkey,
7 solana_signer::{signers::Signers, Signer},
8 solana_transaction::Transaction,
9};
10
11pub struct CloseAccount<'a> {
18 svm: &'a mut LiteSVM,
19 payer: &'a Keypair,
20 account: &'a Pubkey,
21 destination: &'a Pubkey,
22 token_program_id: Option<&'a Pubkey>,
23 signers: SmallVec<[&'a Keypair; 1]>,
24 owner: Option<Pubkey>,
25}
26
27impl<'a> CloseAccount<'a> {
28 pub fn new(
30 svm: &'a mut LiteSVM,
31 payer: &'a Keypair,
32 account: &'a Pubkey,
33 destination: &'a Pubkey,
34 ) -> Self {
35 CloseAccount {
36 svm,
37 payer,
38 account,
39 destination,
40 owner: None,
41 signers: smallvec![payer],
42 token_program_id: None,
43 }
44 }
45
46 pub fn owner(mut self, owner: &'a Keypair) -> Self {
48 self.owner = Some(owner.pubkey());
49 self.signers = smallvec![owner];
50 self
51 }
52
53 pub fn multisig(mut self, multisig: &'a Pubkey, signers: &'a [&'a Keypair]) -> Self {
55 self.owner = Some(*multisig);
56 self.signers = SmallVec::from(signers);
57 self
58 }
59
60 pub fn token_program_id(mut self, program_id: &'a Pubkey) -> Self {
62 self.token_program_id = Some(program_id);
63 self
64 }
65
66 pub fn send(self) -> Result<(), FailedTransactionMetadata> {
68 let token_program_id = self.token_program_id.unwrap_or(&TOKEN_ID);
69 let payer_pk = self.payer.pubkey();
70
71 let authority = self.owner.unwrap_or(payer_pk);
72 let signing_keys = self.signers.pubkeys();
73 let signer_keys = get_multisig_signers(&authority, &signing_keys);
74
75 let ix = close_account(
76 token_program_id,
77 self.account,
78 self.destination,
79 &authority,
80 &signer_keys,
81 )?;
82
83 let block_hash = self.svm.latest_blockhash();
84 let mut tx = Transaction::new_with_payer(&[ix], Some(&payer_pk));
85 tx.partial_sign(&[self.payer], block_hash);
86 tx.partial_sign(self.signers.as_ref(), block_hash);
87
88 self.svm.send_transaction(tx)?;
89
90 Ok(())
91 }
92}