pengdu
pengdu

Reputation: 1351

How to close and transfer all SOL balance programatically without reserving the rent?

I have a Solana account A which is a fee payer, and another Solana account B with some SOL balance, I want to close and transfer all account B's SOL balance to another account C. How do I do that?

I saw the following error:

SendTransactionError: failed to send transaction: Transaction simulation failed: Transaction results in an account (1) without insufficient funds for rent

The code:

const lamports = await connection.getBalance(account_b_public_key);
const transaction = new solana.Transaction();
transaction.add(solana.SystemProgram.transfer({
    fromPubkey: account_b_public_key,
    toPubkey: account_c_public_key,
    lamports: lamports,
}));
transaction.feePayer = account_a_public_key;
const signature = await solana.sendAndConfirmTransaction(connection, transaction, [account_a_signer, account_b_signer]);

I know something is missing there, but I cannot find an interface to close account B and send all SOL balances to another account.

Upvotes: 2

Views: 1368

Answers (0)

Related Questions