SDKCookbook
Reclaim SOL
Close a wallet's empty token accounts and get their rent back, with your wallet's signer.
Every empty token account holds rent that closing it returns. This closes them all, quoting first so the person sees what comes back and how many approvals it takes.
const items = scan.assets
.filter((asset) => asset.kind === 'empty_account' && !asset.frozen)
.map((asset) => ({ assetId: asset.id, action: 'close' as const }))
const quote = await bnc.quote({ wallet, items, transactionVersions: [0, 1] })
console.log(`${quote.totals.netLamports} lamports back, in ${quote.approvals} approval(s)`)
const result = await reclaim(bnc, {
wallet,
items,
transactionVersions: [0, 1],
signTransactions: signWithYourWallet,
onProgress: (event) => {
if (event.type === 'approval') console.log(`Approval ${event.number} of ${event.of}`)
},
})
console.log(`${result.totals.netLamports} lamports reclaimed`)
for (const record of result.confirmed) console.log(record.signature)
for (const signature of result.unconfirmed) {
console.log(await bnc.transaction(signature).catch(() => 'not recorded yet'))
}unconfirmed holds transactions the wallet signed whose outcome isn't known yet. Look each one up before you try its items again, so nothing is signed twice.