- 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 reinstated in flight with the same state the next time it is accessed.
- Rust Client
- Program Guide
Use
CloseTokenAccount to close an empty light-token account.Compare to SPL:1
Prerequisites
Dependencies
Dependencies
Cargo.toml
Report incorrect code
Copy
Ask AI
[dependencies]
light-compressed-token-sdk = "0.1"
light-client = "0.1"
light-token-types = "0.1"
solana-sdk = "2.2"
borsh = "0.10"
tokio = { version = "1.36", features = ["full"] }
[dev-dependencies]
light-program-test = "0.1" # For in-memory tests with LiteSVM
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@alpha
Report incorrect code
Copy
Ask AI
yarn global add @lightprotocol/zk-compression-cli@alpha
Report incorrect code
Copy
Ask AI
pnpm add -g @lightprotocol/zk-compression-cli@alpha
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(())
}
2
Close light-token Account
View Source Code or find full examples with tests: examples-light-token.
Report incorrect code
Copy
Ask AI
mod shared;
use light_client::rpc::Rpc;
use light_token_sdk::token::{CloseAccount, LIGHT_TOKEN_PROGRAM_ID};
use shared::SetupContext;
use solana_sdk::signer::Signer;
#[tokio::test(flavor = "multi_thread")]
async fn close_account() {
// Setup creates mint and empty ATA (must be empty to close).
let SetupContext {
mut rpc,
payer,
ata,
..
} = shared::setup_empty_ata().await;
let close_ix = CloseAccount::new(
LIGHT_TOKEN_PROGRAM_ID,
ata,
payer.pubkey(),
payer.pubkey(),
)
.instruction()
.unwrap();
rpc.create_and_send_transaction(&[close_ix], &payer.pubkey(), &[&payer])
.await
.unwrap();
let account_after = rpc.get_account(ata).await.unwrap();
assert!(account_after.is_none());
}
1
Build Account Infos and CPI the light token program
- invoke (External Signer)
- invoke_signed (PDA Owner)
Report incorrect code
Copy
Ask AI
use light_token_sdk::token::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_sdk::token::CloseAccountCpi;
let signer_seeds = authority_seeds!(bump);
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 Source Code or full examples with tests: examples-light-token.
- Anchor
- Native
Report incorrect code
Copy
Ask AI
#![allow(unexpected_cfgs)]
use anchor_lang::prelude::*;
use light_token_sdk::token::CloseAccountCpi;
declare_id!("4fi27siKEvKXJYN5WCzWuHdAw1rLed6Tprv9ZARv3Gxu");
#[program]
pub mod light_token_anchor_close {
use super::*;
pub fn close_account<'info>(
ctx: Context<'_, '_, '_, 'info, CloseAccountAccounts<'info>>,
) -> Result<()> {
CloseAccountCpi {
token_program: ctx.accounts.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: Validated by light-token CPI
pub 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>,
}
Report incorrect code
Copy
Ask AI
use super::authority_seeds;
use light_token_sdk::token::CloseAccountCpi;
use solana_program::{
account_info::AccountInfo, entrypoint::ProgramResult,
program_error::ProgramError,
};
pub fn close_invoke(accounts: &[AccountInfo], _data: &[u8]) -> ProgramResult {
let [token_program, account, destination, owner, rent_sponsor] = accounts
else {
return Err(ProgramError::NotEnoughAccountKeys);
};
CloseAccountCpi {
token_program: token_program.clone(),
account: account.clone(),
destination: destination.clone(),
owner: owner.clone(),
rent_sponsor: rent_sponsor.clone(),
}
.invoke()
}
pub fn close_invoke_signed(accounts: &[AccountInfo], data: &[u8]) -> ProgramResult {
let [token_program, account, destination, owner, rent_sponsor] = accounts
else {
return Err(ProgramError::NotEnoughAccountKeys);
};
if data.is_empty() {
return Err(ProgramError::InvalidInstructionData);
}
let bump = data[0];
let signer_seeds = authority_seeds!(bump);
CloseAccountCpi {
token_program: token_program.clone(),
account: account.clone(),
destination: destination.clone(),
owner: owner.clone(),
rent_sponsor: rent_sponsor.clone(),
}
.invoke_signed(&[signer_seeds])
}