- Closing a Light Token account transfers remaining lamports to a destination account and the rent sponsor can reclaim sponsored rent.
- Light token accounts can be closed by the owner.
The
closes the account and preserves the balance as compressed token account when the account becomes .
The account is loaded in flight with the same state the next time it is accessed.
Agent skill
Agent skill
Install or view dedicated agent skills.Install orchestrator agent skill or view skill.md:
Report incorrect code
Copy
Ask AI
npx skills add Lightprotocol/skills
Report incorrect code
Copy
Ask AI
npx skills add https://zkcompression.com
- Rust Client
- Program
Use
CloseTokenAccount to close an empty Light Token account.Compare to SPL:- Guide
- AI Prompt
Prerequisites
Dependencies
Dependencies
Cargo.toml
Report incorrect code
Copy
Ask AI
[dependencies]
light-token = "0.23.0"
light-client = { version = "0.23.0", features = ["v2"] }
solana-sdk = "2"
borsh = "0.10.4"
tokio = { version = "1", features = ["full"] }
Developer Environment
Developer Environment
- In-Memory (LightProgramTest)
- Localnet (LightClient)
- Devnet (LightClient)
Test with Lite-SVM (…)
Report incorrect code
Copy
Ask AI
# Initialize project
cargo init my-light-project
cd my-light-project
# Run tests
cargo test
Report incorrect code
Copy
Ask AI
use light_program_test::{LightProgramTest, ProgramTestConfig};
use solana_sdk::signer::Signer;
#[tokio::test]
async fn test_example() {
// In-memory test environment
let mut rpc = LightProgramTest::new(ProgramTestConfig::default())
.await
.unwrap();
let payer = rpc.get_payer().insecure_clone();
println!("Payer: {}", payer.pubkey());
}
Connects to a local test validator.
- npm
- yarn
- pnpm
Report incorrect code
Copy
Ask AI
npm install -g @lightprotocol/zk-compression-cli@beta
Report incorrect code
Copy
Ask AI
yarn global add @lightprotocol/zk-compression-cli@beta
Report incorrect code
Copy
Ask AI
pnpm add -g @lightprotocol/zk-compression-cli@beta
Report incorrect code
Copy
Ask AI
# Initialize project
cargo init my-light-project
cd my-light-project
# Start local test validator (in separate terminal)
light test-validator
Report incorrect code
Copy
Ask AI
use light_client::rpc::{LightClient, LightClientConfig, Rpc};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Connects to http://localhost:8899
let rpc = LightClient::new(LightClientConfig::local()).await?;
let slot = rpc.get_slot().await?;
println!("Current slot: {}", slot);
Ok(())
}
Replace
<your-api-key> with your actual API key. Get your API key here.Report incorrect code
Copy
Ask AI
use light_client::rpc::{LightClient, LightClientConfig, Rpc};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let rpc_url = "https://devnet.helius-rpc.com?api-key=<your_api_key>";
let rpc = LightClient::new(
LightClientConfig::new(rpc_url.to_string(), None, None)
).await?;
println!("Connected to Devnet");
Ok(())
}
Close Light Token Account
Find the source code here.
- Instruction
Report incorrect code
Copy
Ask AI
use light_client::rpc::Rpc;
use light_token::instruction::{CloseAccount, LIGHT_TOKEN_PROGRAM_ID};
use rust_client::{setup_empty_associated_token_account, SetupContext};
use solana_sdk::signer::Signer;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Setup creates mint and empty associated token account (must be empty to close).
let SetupContext {
mut rpc,
payer,
associated_token_account,
..
} = setup_empty_associated_token_account().await;
let close_instruction = CloseAccount::new(LIGHT_TOKEN_PROGRAM_ID, associated_token_account, payer.pubkey(), payer.pubkey())
.instruction()?;
let sig = rpc
.create_and_send_transaction(&[close_instruction], &payer.pubkey(), &[&payer])
.await?;
let account = rpc.get_account(associated_token_account).await?;
println!("Closed: {} Tx: {sig}", account.is_none());
Ok(())
}
Close Light Token account
Report incorrect code
Copy
Ask AI
---
description: Close Light Token account
allowed-tools: Bash, Read, Write, Edit, Glob, Grep, WebFetch, AskUserQuestion, Task, TaskCreate, TaskGet, TaskList, TaskUpdate, TaskOutput, mcp__deepwiki, mcp__zkcompression
---
## Close Light Token account
Context:
- Guide: https://zkcompression.com/light-token/cookbook/close-token-account
- Skills and resources index: https://zkcompression.com/skill.md
- SPL to Light reference: https://zkcompression.com/api-reference/solana-to-light-comparison
- Crates: light-token-client (actions), light-token (instructions), light-client (RPC)
SPL equivalent: spl_token::instruction::close_account → Light Token: CloseAccount
### 1. Index project
- Grep `light_token::|light_token_client::|solana_sdk|Keypair|async|CloseAccount|close_account` across src/
- Glob `**/*.rs` for project structure
- Identify: RPC setup, existing token ops, entry point for close account
- Check Cargo.toml for existing light-* dependencies and solana-sdk version
- Task subagent (Grep/Read/WebFetch) if project has multiple crates to scan in parallel
### 2. Read references
- WebFetch the guide above — follow the Rust Client tab
- WebFetch skill.md — check for a dedicated skill and resources matching this task
- TaskCreate one todo per phase below to track progress
### 3. Clarify intention
- AskUserQuestion: what is the goal? (new feature, migrate existing SPL code, add alongside existing)
- AskUserQuestion: does the project already have close operations to extend, or is this greenfield?
- AskUserQuestion: action-level API (high-level, fewer lines) or instruction-level API (low-level, full control)?
- Summarize findings and wait for user confirmation before implementing
### 4. Create plan
- Based on steps 1–3, draft an implementation plan: which files to modify, what code to add, dependency changes
- Verify existing Rpc/signer setup is compatible with the cookbook prerequisites (light_client::rpc::Rpc, solana_sdk::signature::Keypair)
- If anything is unclear or ambiguous, loop back to step 3 (AskUserQuestion)
- Present the plan to the user for approval before proceeding
### 5. Implement
- Add deps if missing: Bash `cargo add light-token-client light-token light-client --features light-client/v2`
- Follow the cookbook guide and the approved plan
- Write/Edit to create or modify files
- TaskUpdate to mark each step done
### 6. Verify
- Bash `cargo check`
- Bash `cargo test` if tests exist
- TaskUpdate to mark complete
### Tools
- mcp__zkcompression__SearchLightProtocol("<query>") for API details
- mcp__deepwiki__ask_question("Lightprotocol/light-protocol", "<q>") for architecture
- Task subagent with Grep/Read/WebFetch for parallel lookups
- TaskList to check remaining work
- Guide
- AI Prompt
Build Account Infos and CPI the Light Token Program
Useinvoke for external signers or invoke_signed when the authority is a PDA.- invoke (External signer)
- invoke_signed (PDA owner)
Report incorrect code
Copy
Ask AI
use light_token::instruction::CloseAccountCpi;
CloseAccountCpi {
token_program: token_program.clone(),
account: account.clone(),
destination: destination.clone(),
owner: owner.clone(),
rent_sponsor: rent_sponsor.clone(),
}
.invoke()?;
Report incorrect code
Copy
Ask AI
use light_token::instruction::CloseAccountCpi;
CloseAccountCpi {
token_program: token_program.clone(),
account: account.clone(),
destination: destination.clone(),
owner: owner.clone(),
rent_sponsor: rent_sponsor.clone(),
}
.invoke_signed(&[signer_seeds])?;
Account List
Account List
| - | The light token program for CPI. | |
| mutable | The light-token account to close. | |
| mutable | Receives remaining lamports from the closed account. | |
| signer* |
| |
| mutable, optional |
|
Full Code Example
View the source code and full example with shared test utilities.
Report incorrect code
Copy
Ask AI
#![allow(unexpected_cfgs, deprecated)]
use anchor_lang::prelude::*;
use light_token::instruction::CloseAccountCpi;
declare_id!("GXLCuNhnkRVp596eCdbNsZ9ua1ePbKbb344VKS7V3zQQ");
#[program]
pub mod light_token_anchor_close {
use super::*;
pub fn close_account(ctx: Context<CloseAccountAccounts>) -> Result<()> {
CloseAccountCpi {
token_program: ctx.accounts.light_token_program.to_account_info(),
account: ctx.accounts.account.to_account_info(),
destination: ctx.accounts.destination.to_account_info(),
owner: ctx.accounts.owner.to_account_info(),
rent_sponsor: ctx.accounts.rent_sponsor.to_account_info(),
}
.invoke()?;
Ok(())
}
}
#[derive(Accounts)]
pub struct CloseAccountAccounts<'info> {
/// CHECK: Light token program for CPI
pub light_token_program: AccountInfo<'info>,
/// CHECK: Validated by light-token CPI
#[account(mut)]
pub account: AccountInfo<'info>,
/// CHECK: Validated by light-token CPI
#[account(mut)]
pub destination: AccountInfo<'info>,
pub owner: Signer<'info>,
/// CHECK: Validated by light-token CPI
#[account(mut)]
pub rent_sponsor: AccountInfo<'info>,
}
Add close-account CPI to an Anchor program
Report incorrect code
Copy
Ask AI
---
description: Add close-account CPI to an Anchor program
allowed-tools: Bash, Read, Write, Edit, Glob, Grep, WebFetch, AskUserQuestion, Task, TaskCreate, TaskGet, TaskList, TaskUpdate, TaskOutput, mcp__deepwiki, mcp__zkcompression
---
## Add close-account CPI to an Anchor program
Context:
- Guide: https://zkcompression.com/light-token/cookbook/close-token-account
- Skills and resources index: https://zkcompression.com/skill.md
- SPL to Light reference: https://zkcompression.com/api-reference/solana-to-light-comparison
- Crate: light-token (CloseAccountCpi)
- Example: https://github.com/Lightprotocol/examples-light-token/tree/main/programs/anchor/basic-instructions/close
Key CPI struct: `light_token::instruction::CloseAccountCpi`
### 1. Index project
- Grep `declare_id|#\[program\]|anchor_lang|Account<|Pubkey|invoke|close|destination|rent` across src/
- Glob `**/*.rs` and `**/Cargo.toml` for project structure
- Identify: program ID, existing instructions, account structs, token accounts
- Read Cargo.toml — note existing dependencies and framework version
- Task subagent (Grep/Read/WebFetch) if project has multiple crates to scan in parallel
### 2. Read references
- WebFetch the guide above — review the Program tab CPI code samples
- WebFetch skill.md — check for a dedicated skill and resources matching this task
- TaskCreate one todo per phase below to track progress
### 3. Clarify intention
- AskUserQuestion: what is the goal? (add close-account to existing program, new program from scratch, migrate from SPL close_account)
- AskUserQuestion: should the owner be an external signer or a PDA? (determines invoke vs invoke_signed)
- Summarize findings and wait for user confirmation before implementing
### 4. Create plan
- Based on steps 1–3, draft an implementation plan: which files to modify, what code to add, dependency changes
- Follow the guide's step order: Build CloseAccountCpi struct → call .invoke() or .invoke_signed()
- If anything is unclear or ambiguous, loop back to step 3 (AskUserQuestion)
- Present the plan to the user for approval before proceeding
### 5. Implement
- Add deps if missing: Bash `cargo add light-token anchor-lang@0.31`
- Follow the guide and the approved plan
- Write/Edit to create or modify files
- TaskUpdate to mark each step done
### 6. Verify
- Bash `anchor build`
- Bash `anchor test` if tests exist
- TaskUpdate to mark complete
### Tools
- mcp__zkcompression__SearchLightProtocol("<query>") for API details
- mcp__deepwiki__ask_question("Lightprotocol/light-protocol", "<q>") for architecture
- Task subagent with Grep/Read/WebFetch for parallel lookups
- TaskList to check remaining work