From 9d745bf288b27bcd72dc111d1d211fc0e924b390 Mon Sep 17 00:00:00 2001 From: parazyd Date: Wed, 29 Sep 2021 17:42:07 +0200 Subject: [PATCH] service/sol: Close the SPL token account if it has no remaining balance. --- src/service/sol.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/service/sol.rs b/src/service/sol.rs index b05fe1db6..e6ac5694c 100644 --- a/src/service/sol.rs +++ b/src/service/sol.rs @@ -270,16 +270,28 @@ impl SolClient { amount, decimals as u8, )?; - instructions.push(transfer_ix); + // Close the account and reap the rent if there's no more tokens on it. + let (tok_balance, _) = get_account_token_balance(&rpc, &temp_tok_pk, mint)?; + if tok_balance - amount == 0 { + debug!(target: "SOL BRIDGE", "Adding account close instruction because resulting balance is 0"); + let close_ix = spl_token::instruction::close_account( + &spl_token::id(), + &temp_tok_pk, + &self.keypair.pubkey(), + &keypair.pubkey(), + &[], + )?; + instructions.push(close_ix); + } + let tx = Transaction::new_with_payer(&instructions, Some(&self.keypair.pubkey())); let signature = sign_and_send_transaction(&rpc, tx, vec![&self.keypair, keypair])?; debug!(target: "SOL BRIDGE", "Sent tokens to main wallet: {}", signature); - Ok(signature) - // TODO: Close deposit account? + Ok(signature) } fn send_sol_to_main_wallet(