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};
#[derive(Debug, Clone)]
pub struct RentCleanerConfig {
pub min_balance_threshold: u64,
pub close_token_accounts: bool,
pub close_system_accounts: bool,
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,
}
}
}
#[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,
}
pub struct RentCleaner {
client: RpcClient,
payer: Keypair,
config: RentCleanerConfig,
}
impl RentCleaner {
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(),
}
}
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,
}
}
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))
})?;
if let Some(account) = keyed_account.account.decode::<solana_sdk::account::Account>() {
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)
}
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)
}
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)
}
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(())
}
pub async fn estimate_recoverable(&self) -> Result<u64> {
let accounts = self.find_empty_token_accounts().await?;
Ok(accounts.iter().map(|a| a.lamports).sum())
}
}
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
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CleanupStrategy {
EmptyOnly,
BelowDustThreshold,
BurnAndClose,
AggregateAndClose,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum CleanupPriority {
HighValue,
QuickWins,
ByMint,
OldestFirst,
}
#[derive(Debug, Clone)]
pub struct AdvancedCleanupConfig {
pub base: RentCleanerConfig,
pub strategy: CleanupStrategy,
pub priority: CleanupPriority,
pub dust_threshold: u64,
pub batch_size: usize,
pub skip_failures: bool,
pub excluded_mints: Vec<Pubkey>,
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 {
pub fn aggressive() -> Self {
Self {
strategy: CleanupStrategy::BurnAndClose,
skip_failures: true,
..Default::default()
}
}
pub fn conservative() -> Self {
Self {
strategy: CleanupStrategy::EmptyOnly,
skip_failures: false,
..Default::default()
}
}
pub fn with_dust_threshold(mut self, threshold: u64) -> Self {
self.dust_threshold = threshold;
self
}
pub fn with_batch_size(mut self, size: usize) -> Self {
self.batch_size = size;
self
}
pub fn exclude_mints(mut self, mints: Vec<Pubkey>) -> Self {
self.excluded_mints = mints;
self
}
pub fn include_mints(mut self, mints: Vec<Pubkey>) -> Self {
self.included_mints = mints;
self
}
}
#[derive(Debug, Clone)]
pub struct CleanupResult {
pub lamports_recovered: u64,
pub accounts_closed: usize,
pub failed_accounts: Vec<(Pubkey, String)>,
pub tokens_burned: HashMap<Pubkey, u64>,
pub signatures: Vec<Signature>,
}
impl CleanupResult {
pub fn new() -> Self {
Self {
lamports_recovered: 0,
accounts_closed: 0,
failed_accounts: Vec::new(),
tokens_burned: HashMap::new(),
signatures: Vec::new(),
}
}
pub fn is_complete_success(&self) -> bool {
self.failed_accounts.is_empty()
}
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()
}
}
#[derive(Debug, Clone)]
pub struct ExtendedCleanableAccount {
pub base: CleanableAccount,
pub mint: Option<Pubkey>,
pub token_balance: u64,
pub can_burn: bool,
}
pub struct AdvancedRentCleaner {
client: RpcClient,
payer: Keypair,
config: AdvancedCleanupConfig,
}
impl AdvancedRentCleaner {
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(),
}
}
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,
}
}
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 {
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]),
);
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,
});
}
}
}
}
self.sort_by_priority(&mut cleanable);
Ok(cleanable)
}
fn should_include_account(&self, mint: Option<&Pubkey>, _balance: u64) -> bool {
if let Some(mint) = mint {
if self.config.excluded_mints.contains(mint) {
return false;
}
if !self.config.included_mints.is_empty()
&& !self.config.included_mints.contains(mint)
{
return false;
}
}
true
}
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 => {
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 => {
accounts.sort_by(|a, b| a.base.address.cmp(&b.base.address));
}
}
}
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);
}
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);
}
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)
}
async fn process_batch(&self, accounts: &[ExtendedCleanableAccount]) -> Result<Signature> {
let mut instructions = Vec::new();
let payer_pubkey = self.payer.pubkey();
for account in accounts {
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,
)?);
}
}
instructions.push(token_instruction::close_account(
&spl_token::id(),
&account.base.address,
&payer_pubkey,
&payer_pubkey,
&[],
)?);
}
self.send_transaction(instructions).await
}
async fn close_single_account(
&self,
account: &ExtendedCleanableAccount,
) -> Result<Signature> {
let mut instructions = Vec::new();
let payer_pubkey = self.payer.pubkey();
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,
)?);
}
}
instructions.push(token_instruction::close_account(
&spl_token::id(),
&account.base.address,
&payer_pubkey,
&payer_pubkey,
&[],
)?);
self.send_transaction(instructions).await
}
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()))
}
pub async fn estimate_recovery(&self) -> Result<u64> {
let accounts = self.scan_accounts().await?;
Ok(accounts.iter().map(|a| a.base.lamports).sum())
}
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)
}
}
#[derive(Debug, Clone)]
pub struct RecoveryBreakdown {
pub total_accounts: usize,
pub total_lamports: u64,
pub empty_accounts: usize,
pub dust_accounts: usize,
pub accounts_with_balance: usize,
pub by_mint: HashMap<Pubkey, MintBreakdown>,
}
impl RecoveryBreakdown {
pub fn sol_recoverable(&self) -> f64 {
self.total_lamports as f64 / 1_000_000_000.0
}
}
#[derive(Debug, Clone)]
pub struct MintBreakdown {
pub mint: Pubkey,
pub account_count: usize,
pub total_lamports: u64,
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);
}
}