solana-pipkit 2.2.0

Pragmatic Rust utilities for Solana development - safety, speed, analytics, and DeFi
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
//! Rent Cleaner - Reclaim SOL from empty or unused accounts.
//!
//! This module provides utilities to scan your wallet for accounts
//! that can be closed to recover rent-exempt SOL. Includes advanced
//! recovery strategies for different account types and batched operations.

use solana_client::nonblocking::rpc_client::RpcClient;
use solana_sdk::{
    commitment_config::CommitmentConfig,
    instruction::Instruction,
    message::Message,
    pubkey::Pubkey,
    signature::{Keypair, Signature, Signer},
    transaction::Transaction,
};
use spl_token::instruction as token_instruction;
use std::collections::HashMap;

use crate::{Result, ToolkitError};

/// Configuration for rent cleaning operations.
#[derive(Debug, Clone)]
pub struct RentCleanerConfig {
    /// Minimum lamports to consider an account "empty"
    pub min_balance_threshold: u64,
    /// Whether to close empty token accounts
    pub close_token_accounts: bool,
    /// Whether to close empty system accounts
    pub close_system_accounts: bool,
    /// Dry run mode (don't actually close accounts)
    pub dry_run: bool,
}

impl Default for RentCleanerConfig {
    fn default() -> Self {
        Self {
            min_balance_threshold: 0,
            close_token_accounts: true,
            close_system_accounts: true,
            dry_run: false,
        }
    }
}

/// Account information for potential cleanup.
#[derive(Debug, Clone)]
pub struct CleanableAccount {
    pub address: Pubkey,
    pub lamports: u64,
    pub account_type: AccountType,
}

#[derive(Debug, Clone, PartialEq)]
pub enum AccountType {
    TokenAccount,
    SystemAccount,
    Unknown,
}

/// Rent cleaner for recovering SOL from empty accounts.
pub struct RentCleaner {
    client: RpcClient,
    payer: Keypair,
    config: RentCleanerConfig,
}

impl RentCleaner {
    /// Create a new RentCleaner instance.
    pub fn new(rpc_url: &str, payer: Keypair) -> Self {
        Self {
            client: RpcClient::new_with_commitment(
                rpc_url.to_string(),
                CommitmentConfig::confirmed(),
            ),
            payer,
            config: RentCleanerConfig::default(),
        }
    }

    /// Create with custom configuration.
    pub fn with_config(rpc_url: &str, payer: Keypair, config: RentCleanerConfig) -> Self {
        Self {
            client: RpcClient::new_with_commitment(
                rpc_url.to_string(),
                CommitmentConfig::confirmed(),
            ),
            payer,
            config,
        }
    }

    /// Scan for empty token accounts owned by the payer.
    pub async fn find_empty_token_accounts(&self) -> Result<Vec<CleanableAccount>> {
        let owner = self.payer.pubkey();
        let token_program = spl_token::id();

        let accounts = self
            .client
            .get_token_accounts_by_owner(&owner, solana_client::rpc_request::TokenAccountsFilter::ProgramId(token_program))
            .await?;

        let mut cleanable = Vec::new();

        for keyed_account in accounts {
            let pubkey = keyed_account.pubkey.parse::<Pubkey>().map_err(|e| {
                ToolkitError::Custom(format!("Failed to parse pubkey: {}", e))
            })?;

            // Check if token account has zero balance
            if let Some(account) = keyed_account.account.decode::<solana_sdk::account::Account>() {
                // Parse token account data
                if account.data.len() >= 64 {
                    let amount = u64::from_le_bytes(
                        account.data[64..72].try_into().unwrap_or([0; 8])
                    );
                    
                    if amount == 0 {
                        cleanable.push(CleanableAccount {
                            address: pubkey,
                            lamports: account.lamports,
                            account_type: AccountType::TokenAccount,
                        });
                    }
                }
            }
        }

        Ok(cleanable)
    }

    /// Close empty token accounts and recover rent.
    pub async fn close_empty_token_accounts(&self) -> Result<u64> {
        let accounts = self.find_empty_token_accounts().await?;
        let mut total_recovered: u64 = 0;

        if self.config.dry_run {
            for account in &accounts {
                println!(
                    "[DRY RUN] Would close token account {} and recover {} lamports",
                    account.address, account.lamports
                );
                total_recovered += account.lamports;
            }
            return Ok(total_recovered);
        }

        for account in accounts {
            match self.close_token_account(&account.address).await {
                Ok(lamports) => {
                    total_recovered += lamports;
                    println!(
                        "Closed token account {} - recovered {} lamports",
                        account.address, lamports
                    );
                }
                Err(e) => {
                    eprintln!("Failed to close {}: {}", account.address, e);
                }
            }
        }

        Ok(total_recovered)
    }

    /// Close a single token account.
    async fn close_token_account(&self, token_account: &Pubkey) -> Result<u64> {
        let account_info = self.client.get_account(token_account).await?;
        let lamports = account_info.lamports;

        let instruction = token_instruction::close_account(
            &spl_token::id(),
            token_account,
            &self.payer.pubkey(),
            &self.payer.pubkey(),
            &[],
        )?;

        self.send_transaction(vec![instruction]).await?;

        Ok(lamports)
    }

    /// Send a transaction with the given instructions.
    async fn send_transaction(&self, instructions: Vec<Instruction>) -> Result<()> {
        let recent_blockhash = self.client.get_latest_blockhash().await?;
        
        let message = Message::new(&instructions, Some(&self.payer.pubkey()));
        let transaction = Transaction::new(&[&self.payer], message, recent_blockhash);

        self.client
            .send_and_confirm_transaction(&transaction)
            .await?;

        Ok(())
    }

    /// Get total recoverable lamports from empty accounts.
    pub async fn estimate_recoverable(&self) -> Result<u64> {
        let accounts = self.find_empty_token_accounts().await?;
        Ok(accounts.iter().map(|a| a.lamports).sum())
    }
}

/// Convenience function to clean all empty accounts.
pub async fn clean_all_empty_accounts(rpc_url: &str, payer: Keypair) -> Result<u64> {
    let cleaner = RentCleaner::new(rpc_url, payer);
    cleaner.close_empty_token_accounts().await
}

// ============================================================================
// Advanced Rent Recovery Strategies
// ============================================================================

/// Strategy for handling different account types during cleanup.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CleanupStrategy {
    /// Close only completely empty token accounts (balance = 0).
    EmptyOnly,
    /// Close accounts below a dust threshold (configurable).
    BelowDustThreshold,
    /// Burn remaining tokens and close (for worthless tokens).
    BurnAndClose,
    /// Aggregate small balances before closing.
    AggregateAndClose,
}

/// Priority level for cleanup operations.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum CleanupPriority {
    /// High value recovery (largest rent amounts first).
    HighValue,
    /// Quick wins (easiest to close first).
    QuickWins,
    /// By mint (group token accounts by their mint).
    ByMint,
    /// Oldest first (by rent epoch).
    OldestFirst,
}

/// Advanced configuration for rent recovery.
#[derive(Debug, Clone)]
pub struct AdvancedCleanupConfig {
    /// Base configuration.
    pub base: RentCleanerConfig,
    /// Cleanup strategy to use.
    pub strategy: CleanupStrategy,
    /// Priority ordering.
    pub priority: CleanupPriority,
    /// Dust threshold in token smallest units (for BelowDustThreshold).
    pub dust_threshold: u64,
    /// Maximum accounts to process per batch.
    pub batch_size: usize,
    /// Whether to skip accounts that fail.
    pub skip_failures: bool,
    /// Mints to exclude from cleanup.
    pub excluded_mints: Vec<Pubkey>,
    /// Only process these mints (if empty, process all).
    pub included_mints: Vec<Pubkey>,
}

impl Default for AdvancedCleanupConfig {
    fn default() -> Self {
        Self {
            base: RentCleanerConfig::default(),
            strategy: CleanupStrategy::EmptyOnly,
            priority: CleanupPriority::HighValue,
            dust_threshold: 1,
            batch_size: 10,
            skip_failures: true,
            excluded_mints: Vec::new(),
            included_mints: Vec::new(),
        }
    }
}

impl AdvancedCleanupConfig {
    /// Create a config for aggressive cleanup (burn worthless tokens).
    pub fn aggressive() -> Self {
        Self {
            strategy: CleanupStrategy::BurnAndClose,
            skip_failures: true,
            ..Default::default()
        }
    }

    /// Create a config for conservative cleanup (empty only, no burns).
    pub fn conservative() -> Self {
        Self {
            strategy: CleanupStrategy::EmptyOnly,
            skip_failures: false,
            ..Default::default()
        }
    }

    /// Set the dust threshold.
    pub fn with_dust_threshold(mut self, threshold: u64) -> Self {
        self.dust_threshold = threshold;
        self
    }

    /// Set the batch size.
    pub fn with_batch_size(mut self, size: usize) -> Self {
        self.batch_size = size;
        self
    }

    /// Exclude specific mints.
    pub fn exclude_mints(mut self, mints: Vec<Pubkey>) -> Self {
        self.excluded_mints = mints;
        self
    }

    /// Only include specific mints.
    pub fn include_mints(mut self, mints: Vec<Pubkey>) -> Self {
        self.included_mints = mints;
        self
    }
}

/// Result of an advanced cleanup operation.
#[derive(Debug, Clone)]
pub struct CleanupResult {
    /// Total lamports recovered.
    pub lamports_recovered: u64,
    /// Number of accounts closed.
    pub accounts_closed: usize,
    /// Accounts that failed to close.
    pub failed_accounts: Vec<(Pubkey, String)>,
    /// Tokens burned (mint -> amount).
    pub tokens_burned: HashMap<Pubkey, u64>,
    /// Transaction signatures.
    pub signatures: Vec<Signature>,
}

impl CleanupResult {
    /// Create a new empty result.
    pub fn new() -> Self {
        Self {
            lamports_recovered: 0,
            accounts_closed: 0,
            failed_accounts: Vec::new(),
            tokens_burned: HashMap::new(),
            signatures: Vec::new(),
        }
    }

    /// Check if the cleanup was fully successful.
    pub fn is_complete_success(&self) -> bool {
        self.failed_accounts.is_empty()
    }

    /// Get the SOL equivalent recovered (assuming 1 SOL = 1e9 lamports).
    pub fn sol_recovered(&self) -> f64 {
        self.lamports_recovered as f64 / 1_000_000_000.0
    }
}

impl Default for CleanupResult {
    fn default() -> Self {
        Self::new()
    }
}

/// Extended cleanable account with additional metadata.
#[derive(Debug, Clone)]
pub struct ExtendedCleanableAccount {
    /// Base cleanable account info.
    pub base: CleanableAccount,
    /// Token mint (if token account).
    pub mint: Option<Pubkey>,
    /// Token balance (if token account).
    pub token_balance: u64,
    /// Whether this account can be burned and closed.
    pub can_burn: bool,
}

/// Advanced rent cleaner with multiple recovery strategies.
pub struct AdvancedRentCleaner {
    client: RpcClient,
    payer: Keypair,
    config: AdvancedCleanupConfig,
}

impl AdvancedRentCleaner {
    /// Create a new advanced rent cleaner.
    pub fn new(rpc_url: &str, payer: Keypair) -> Self {
        Self {
            client: RpcClient::new_with_commitment(
                rpc_url.to_string(),
                CommitmentConfig::confirmed(),
            ),
            payer,
            config: AdvancedCleanupConfig::default(),
        }
    }

    /// Create with custom configuration.
    pub fn with_config(rpc_url: &str, payer: Keypair, config: AdvancedCleanupConfig) -> Self {
        Self {
            client: RpcClient::new_with_commitment(
                rpc_url.to_string(),
                CommitmentConfig::confirmed(),
            ),
            payer,
            config,
        }
    }

    /// Scan for all cleanable accounts with extended information.
    pub async fn scan_accounts(&self) -> Result<Vec<ExtendedCleanableAccount>> {
        let owner = self.payer.pubkey();
        let token_program = spl_token::id();

        let accounts = self
            .client
            .get_token_accounts_by_owner(
                &owner,
                solana_client::rpc_request::TokenAccountsFilter::ProgramId(token_program),
            )
            .await?;

        let mut cleanable = Vec::new();

        for keyed_account in accounts {
            let pubkey = keyed_account.pubkey.parse::<Pubkey>().map_err(|e| {
                ToolkitError::Custom(format!("Failed to parse pubkey: {}", e))
            })?;

            if let Some(account) = keyed_account.account.decode::<solana_sdk::account::Account>() {
                if account.data.len() >= 72 {
                    // Parse token account data
                    let mint = Pubkey::try_from(&account.data[0..32]).ok();
                    let token_balance = u64::from_le_bytes(
                        account.data[64..72].try_into().unwrap_or([0; 8]),
                    );

                    // Check if we should include this account
                    if !self.should_include_account(mint.as_ref(), token_balance) {
                        continue;
                    }

                    let can_close = match self.config.strategy {
                        CleanupStrategy::EmptyOnly => token_balance == 0,
                        CleanupStrategy::BelowDustThreshold => {
                            token_balance <= self.config.dust_threshold
                        }
                        CleanupStrategy::BurnAndClose => true,
                        CleanupStrategy::AggregateAndClose => true,
                    };

                    if can_close {
                        cleanable.push(ExtendedCleanableAccount {
                            base: CleanableAccount {
                                address: pubkey,
                                lamports: account.lamports,
                                account_type: AccountType::TokenAccount,
                            },
                            mint,
                            token_balance,
                            can_burn: token_balance > 0,
                        });
                    }
                }
            }
        }

        // Sort by priority
        self.sort_by_priority(&mut cleanable);

        Ok(cleanable)
    }

    /// Check if an account should be included based on config.
    fn should_include_account(&self, mint: Option<&Pubkey>, _balance: u64) -> bool {
        if let Some(mint) = mint {
            // Check excluded mints
            if self.config.excluded_mints.contains(mint) {
                return false;
            }

            // Check included mints (if specified)
            if !self.config.included_mints.is_empty()
                && !self.config.included_mints.contains(mint)
            {
                return false;
            }
        }

        true
    }

    /// Sort accounts by priority.
    fn sort_by_priority(&self, accounts: &mut [ExtendedCleanableAccount]) {
        match self.config.priority {
            CleanupPriority::HighValue => {
                accounts.sort_by(|a, b| b.base.lamports.cmp(&a.base.lamports));
            }
            CleanupPriority::QuickWins => {
                // Empty accounts first, then by lamports
                accounts.sort_by(|a, b| {
                    let a_empty = a.token_balance == 0;
                    let b_empty = b.token_balance == 0;
                    b_empty.cmp(&a_empty).then(b.base.lamports.cmp(&a.base.lamports))
                });
            }
            CleanupPriority::ByMint => {
                accounts.sort_by(|a, b| {
                    a.mint.map(|m| m.to_string()).cmp(&b.mint.map(|m| m.to_string()))
                });
            }
            CleanupPriority::OldestFirst => {
                // Without rent epoch data, just sort by address for consistency
                accounts.sort_by(|a, b| a.base.address.cmp(&b.base.address));
            }
        }
    }

    /// Execute the cleanup with the configured strategy.
    pub async fn execute_cleanup(&self) -> Result<CleanupResult> {
        let accounts = self.scan_accounts().await?;
        let mut result = CleanupResult::new();

        if self.config.base.dry_run {
            for account in &accounts {
                println!(
                    "[DRY RUN] Would close {} (mint: {:?}, balance: {}, rent: {} lamports)",
                    account.base.address,
                    account.mint,
                    account.token_balance,
                    account.base.lamports
                );
                result.lamports_recovered += account.base.lamports;
                result.accounts_closed += 1;
            }
            return Ok(result);
        }

        // Process in batches
        for batch in accounts.chunks(self.config.batch_size) {
            let batch_result = self.process_batch(batch).await;
            match batch_result {
                Ok(sig) => {
                    for account in batch {
                        result.lamports_recovered += account.base.lamports;
                        result.accounts_closed += 1;
                        if account.token_balance > 0 {
                            if let Some(mint) = account.mint {
                                *result.tokens_burned.entry(mint).or_insert(0) +=
                                    account.token_balance;
                            }
                        }
                    }
                    result.signatures.push(sig);
                }
                Err(e) => {
                    if !self.config.skip_failures {
                        return Err(e);
                    }
                    // Process individually on batch failure
                    for account in batch {
                        match self.close_single_account(account).await {
                            Ok(sig) => {
                                result.lamports_recovered += account.base.lamports;
                                result.accounts_closed += 1;
                                if account.token_balance > 0 {
                                    if let Some(mint) = account.mint {
                                        *result.tokens_burned.entry(mint).or_insert(0) +=
                                            account.token_balance;
                                    }
                                }
                                result.signatures.push(sig);
                            }
                            Err(e) => {
                                result
                                    .failed_accounts
                                    .push((account.base.address, e.to_string()));
                            }
                        }
                    }
                }
            }
        }

        Ok(result)
    }

    /// Process a batch of accounts.
    async fn process_batch(&self, accounts: &[ExtendedCleanableAccount]) -> Result<Signature> {
        let mut instructions = Vec::new();
        let payer_pubkey = self.payer.pubkey();

        for account in accounts {
            // Burn tokens if needed
            if account.token_balance > 0 && self.config.strategy == CleanupStrategy::BurnAndClose {
                if let Some(mint) = account.mint {
                    instructions.push(token_instruction::burn(
                        &spl_token::id(),
                        &account.base.address,
                        &mint,
                        &payer_pubkey,
                        &[],
                        account.token_balance,
                    )?);
                }
            }

            // Close the account
            instructions.push(token_instruction::close_account(
                &spl_token::id(),
                &account.base.address,
                &payer_pubkey,
                &payer_pubkey,
                &[],
            )?);
        }

        self.send_transaction(instructions).await
    }

    /// Close a single account.
    async fn close_single_account(
        &self,
        account: &ExtendedCleanableAccount,
    ) -> Result<Signature> {
        let mut instructions = Vec::new();
        let payer_pubkey = self.payer.pubkey();

        // Burn tokens if needed
        if account.token_balance > 0 && self.config.strategy == CleanupStrategy::BurnAndClose {
            if let Some(mint) = account.mint {
                instructions.push(token_instruction::burn(
                    &spl_token::id(),
                    &account.base.address,
                    &mint,
                    &payer_pubkey,
                    &[],
                    account.token_balance,
                )?);
            }
        }

        // Close the account
        instructions.push(token_instruction::close_account(
            &spl_token::id(),
            &account.base.address,
            &payer_pubkey,
            &payer_pubkey,
            &[],
        )?);

        self.send_transaction(instructions).await
    }

    /// Send a transaction with the given instructions.
    async fn send_transaction(&self, instructions: Vec<Instruction>) -> Result<Signature> {
        let recent_blockhash = self.client.get_latest_blockhash().await?;
        let message = Message::new(&instructions, Some(&self.payer.pubkey()));
        let transaction = Transaction::new(&[&self.payer], message, recent_blockhash);

        self.client
            .send_and_confirm_transaction(&transaction)
            .await
            .map_err(|e| ToolkitError::TransactionError(e.to_string()))
    }

    /// Estimate total recoverable lamports.
    pub async fn estimate_recovery(&self) -> Result<u64> {
        let accounts = self.scan_accounts().await?;
        Ok(accounts.iter().map(|a| a.base.lamports).sum())
    }

    /// Get detailed breakdown of recoverable accounts.
    pub async fn get_recovery_breakdown(&self) -> Result<RecoveryBreakdown> {
        let accounts = self.scan_accounts().await?;

        let mut breakdown = RecoveryBreakdown {
            total_accounts: accounts.len(),
            total_lamports: 0,
            empty_accounts: 0,
            dust_accounts: 0,
            accounts_with_balance: 0,
            by_mint: HashMap::new(),
        };

        for account in accounts {
            breakdown.total_lamports += account.base.lamports;

            if account.token_balance == 0 {
                breakdown.empty_accounts += 1;
            } else if account.token_balance <= self.config.dust_threshold {
                breakdown.dust_accounts += 1;
            } else {
                breakdown.accounts_with_balance += 1;
            }

            if let Some(mint) = account.mint {
                let entry = breakdown.by_mint.entry(mint).or_insert(MintBreakdown {
                    mint,
                    account_count: 0,
                    total_lamports: 0,
                    total_tokens: 0,
                });
                entry.account_count += 1;
                entry.total_lamports += account.base.lamports;
                entry.total_tokens += account.token_balance;
            }
        }

        Ok(breakdown)
    }
}

/// Breakdown of recoverable rent by category.
#[derive(Debug, Clone)]
pub struct RecoveryBreakdown {
    /// Total number of cleanable accounts.
    pub total_accounts: usize,
    /// Total lamports recoverable.
    pub total_lamports: u64,
    /// Number of empty accounts (0 balance).
    pub empty_accounts: usize,
    /// Number of dust accounts (below threshold).
    pub dust_accounts: usize,
    /// Number of accounts with significant balance.
    pub accounts_with_balance: usize,
    /// Breakdown by mint.
    pub by_mint: HashMap<Pubkey, MintBreakdown>,
}

impl RecoveryBreakdown {
    /// Get total SOL recoverable.
    pub fn sol_recoverable(&self) -> f64 {
        self.total_lamports as f64 / 1_000_000_000.0
    }
}

/// Breakdown for a specific mint.
#[derive(Debug, Clone)]
pub struct MintBreakdown {
    /// The mint address.
    pub mint: Pubkey,
    /// Number of accounts for this mint.
    pub account_count: usize,
    /// Total lamports in these accounts.
    pub total_lamports: u64,
    /// Total token balance across accounts.
    pub total_tokens: u64,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_config_default() {
        let config = RentCleanerConfig::default();
        assert!(config.close_token_accounts);
        assert!(config.close_system_accounts);
        assert!(!config.dry_run);
    }
}