close_token_account
Closes a Solana token account to reclaim rent lamports by transferring remaining funds to a specified destination address.
Instructions
Close a token account and reclaim rent
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| walletName | Yes | Name of the wallet that owns the account | |
| tokenMint | Yes | Token mint address | |
| destinationAddress | Yes | Address to send remaining lamports to |
Implementation Reference
- src/index.ts:1143-1175 (handler)The handler function that executes the close_token_account tool. It retrieves the wallet, computes the associated token account address, and calls closeAccount from @solana/spl-token to close the account and transfer lamports to the destination.async function handleCloseTokenAccount(args: any) { const { walletName, tokenMint, destinationAddress } = args; const wallet = wallets.get(walletName); if (!wallet) { throw new Error(`Wallet '${walletName}' not found`); } ensureConnection(); const tokenMintPubkey = new PublicKey(tokenMint); const destinationPubkey = new PublicKey(destinationAddress); const tokenAccount = await getAssociatedTokenAddress( tokenMintPubkey, wallet.keypair.publicKey ); const signature = await closeAccount( connection, wallet.keypair, tokenAccount, destinationPubkey, wallet.keypair ); return { success: true, signature, closedAccount: tokenAccount.toString(), destination: destinationAddress, explorerUrl: `https://explorer.solana.com/tx/${signature}?cluster=${currentNetwork}` }; }
- src/index.ts:458-479 (registration)Registers the close_token_account tool in the tools array, including name, description, and input schema. This array is returned by the ListToolsRequestSchema handler.{ name: "close_token_account", description: "Close a token account and reclaim rent", inputSchema: { type: "object", properties: { walletName: { type: "string", description: "Name of the wallet that owns the account" }, tokenMint: { type: "string", description: "Token mint address" }, destinationAddress: { type: "string", description: "Address to send remaining lamports to" } }, required: ["walletName", "tokenMint", "destinationAddress"] } },
- src/index.ts:1351-1352 (handler)Switch case in the main CallToolRequestSchema handler that dispatches to the specific handleCloseTokenAccount function.case "close_token_account": result = await handleCloseTokenAccount(args);