I now have a solana account and want to transfer all the SOL in it. At first I found that the OKX wallet could not be transferred because there were SPL ACCOUNT and CNFTS in my wallet, so I transferred SPL ACCOUNT It was closed and CNFTS was burned. After burning, I used the code to transfer, but still got the error "Transaction results in an account (0) with insufficient funds for rent"
"In fact, my wallet no longer has any account, so I set skipPreflight to flase. At this time, transactions can be uploaded to the chain, but they all failed(https://solscan.io/tx/333qyxDM4CDE8hEV7XxnfMRSXmasDYK14dpNdALcKXZcbMA2yWfxyE7sc2FcthTdCA2RRQk9DMvp9KXTzY2GjktM), so I tried the transfer from the OKX wallet again, and was surprised to find that OKX The transfer was successful(https://solscan.io/tx/47sWkY9tXR1fRRmmjEJzp2r6TZ74hdJmG8NwYo2akekehHSw11D6JCXAi6kWhw48iogyj2tNT7pr8QdBfyUrwzxm) and all the SOL in the wallet was transferred. Then I recharged the wallet and found that the error still occurred when using the code. Can anyone help me solve this problem? Here is my code (This code runs normally for ordinary transfers. Problems only occur when all SOL of the account is transferred away.
,because I am testing it) , all a bit messy)
In addition, I also tested the sendSmartTransaction interface provided by helius, and the same result was "Transaction simulation failed: Transaction results in an account (0) with insufficient funds for rent". This drove me crazy. Look at the TX details, OKX The instructions structure of the transfer is the same as the transaction request I sent.
const modifyComputeUnits = ComputeBudgetProgram.setComputeUnitLimit({ units: 100000});const addPriorityFee = ComputeBudgetProgram.setComputeUnitPrice({ microLamports: 30000 //50000= 0.00001 SOL ($0.001402)});const blockhashResponse = await connection.getLatestBlockhashAndContext();const lastValidBlockHeight = blockhashResponse.context.slot + 150;const transaction = new Transaction({ feePayer: fromWallet.publicKey, blockhash: blockhashResponse.value.blockhash, lastValidBlockHeight: lastValidBlockHeight,}).add(modifyComputeUnits) .add(addPriorityFee).add( SystemProgram.transfer({ fromPubkey: fromWallet.publicKey, toPubkey: toWalletPublicKey, lamports: Math.floor(ownerSolAmount * LAMPORTS_PER_SOL) - 100000, // lamports: 10000, }), );// transaction.recentBlockhash = await connection.getLatestBlockhash('confirmed');try{ console.log(Math.floor(ownerSolAmount * LAMPORTS_PER_SOL)); // // const result = await sendAndConfirmTransaction(connection, transaction, [fromWallet]); // const result = await sendAndConfirmTransaction(connection, transaction, [fromWallet]); // console.log(result); const message = transaction.serializeMessage(); const signature1 = nacl.sign.detached(message, fromWallet.secretKey); transaction.addSignature(fromWallet.publicKey, Buffer.from(signature1)); const rawTransaction = transaction.serialize(); let blockheight = await connection.getBlockHeight(); const result2 = await connection.sendRawTransaction(rawTransaction, { skipPreflight: false, preflightCommitment: "processed" , }); console.log(result2) // const instructions = [ // SystemProgram.transfer({ // fromPubkey: fromWallet.publicKey, // toPubkey: toWalletPublicKey, // lamports: Math.floor(ownerSolAmount * LAMPORTS_PER_SOL) - 100000, // }), // ]; // // // const transactionSignature = await helius.rpc.sendSmartTransaction(instructions, fromWallet, false, 2); // console.log(`Successful transfer: ${transactionSignature}`);}catch (e) { console.log(`${fromWallet.publicKey.toString()} - ` + JSON.stringify(e.message)); throw e; return}