- Light token accounts are Solana accounts that hold token balances of light, SPL, or Token 2022 mints.
- Light token accounts are on-chain accounts like SPL ATA’s, but the light token program sponsors the rent-exemption cost for you.
Light Rent Config Explained
Light Rent Config Explained
- The Light Token Program pays the rent-exemption cost for the account.
- Transaction fee payers bump a virtual rent balance when writing to the account, which keeps the account “hot”.
- “Cold” accounts virtual rent balance below threshold (eg 24h without write bump) get auto-compressed.
- The cold account’s state is cryptographically preserved on the Solana ledger. Users can load a cold account into hot state in-flight when using the account again.
- Rust Client
- Program
CreateTokenAccount creates an on-chain token account to store token balances of light, SPL, or Token 2022 mints.Compare to SPL:Prerequisites
Dependencies
Dependencies
Cargo.toml
Report incorrect code
Copy
Ask AI
[dependencies]
light-token = "0.4.0"
light-client = { version = "0.19.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(())
}
Create Token Account
View the source code and full example with shared test utilities.
- Instruction
Report incorrect code
Copy
Ask AI
use light_client::rpc::Rpc;
use light_token::instruction::CreateTokenAccount;
use rust_client::{setup_spl_mint_context, SplMintContext};
use solana_sdk::{signature::Keypair, signer::Signer};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Setup creates mint
// You can use Light, SPL, or Token-2022 mints to create a light token account.
let SplMintContext {
mut rpc,
payer,
mint,
} = setup_spl_mint_context().await;
let account = Keypair::new();
let create_token_account_instruction =
CreateTokenAccount::new(payer.pubkey(), account.pubkey(), mint, payer.pubkey())
.instruction()?;
let sig = rpc
.create_and_send_transaction(&[create_token_account_instruction], &payer.pubkey(), &[&payer, &account])
.await?;
let data = rpc.get_account(account.pubkey()).await?;
println!(
"Account: {} exists: {} Tx: {sig}",
account.pubkey(),
data.is_some()
);
Ok(())
}
- CPI
- Anchor Macros
Compare to SPL:
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 signer)
Report incorrect code
Copy
Ask AI
use light_token::instruction::CreateTokenAccountCpi;
CreateTokenAccountCpi {
payer: payer.clone(),
account: account.clone(),
mint: mint.clone(),
owner,
}
.rent_free(
compressible_config.clone(),
rent_sponsor.clone(),
system_program.clone(),
token_program.key, // light token program
)
.invoke()
| Payer | signer, mutable |
|
| light-token Account | signer*, mutable |
|
| Mint | - | The SPL or light-mint token mint. |
| Owner | Pubkey | The owner of the token account. Controls transfers and other operations. |
Report incorrect code
Copy
Ask AI
use light_token::instruction::CreateTokenAccountCpi;
let signer_seeds = authority_seeds!(authority_bump);
CreateTokenAccountCpi {
payer: payer.clone(),
account: account.clone(),
mint: mint.clone(),
owner,
}
.rent_free(
compressible_config.clone(),
rent_sponsor.clone(),
system_program.clone(),
program_id,
)
.invoke_signed(signer_seeds)
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::CreateTokenAccountCpi;
declare_id!("zXK1CnWj4WFfFHCArxxr4sh3Qqx2p3oui8ahqpjArgS");
#[program]
pub mod light_token_anchor_create_token_account {
use super::*;
pub fn create_token_account(ctx: Context<CreateTokenAccountAccounts>, owner: Pubkey) -> Result<()> {
CreateTokenAccountCpi {
payer: ctx.accounts.payer.to_account_info(),
account: ctx.accounts.account.to_account_info(),
mint: ctx.accounts.mint.to_account_info(),
owner,
}
.rent_free(
ctx.accounts.compressible_config.to_account_info(),
ctx.accounts.rent_sponsor.to_account_info(),
ctx.accounts.system_program.to_account_info(),
&ctx.accounts.light_token_program.key(),
)
.invoke()?;
Ok(())
}
}
#[derive(Accounts)]
pub struct CreateTokenAccountAccounts<'info> {
#[account(mut)]
pub payer: Signer<'info>,
/// CHECK: Validated by light-token CPI
#[account(mut)]
pub account: Signer<'info>,
/// CHECK: Validated by light-token CPI
pub mint: AccountInfo<'info>,
/// CHECK: Validated by light-token CPI
pub compressible_config: AccountInfo<'info>,
/// CHECK: Validated by light-token CPI
#[account(mut)]
pub rent_sponsor: AccountInfo<'info>,
pub system_program: Program<'info, System>,
/// CHECK: Light token program for CPI
pub light_token_program: AccountInfo<'info>,
}
Compare to SPL:
Dependencies
Report incorrect code
Copy
Ask AI
[dependencies]
light-sdk = { version = "0.18.0", features = ["anchor", "v2", "cpi-context"] }
light-sdk-macros = "0.18.0"
light-compressible = "0.1.0"
anchor-lang = "0.31"
Program
Add#[light_program] above #[program]:Report incorrect code
Copy
Ask AI
use light_sdk_macros::light_program;
#[light_program]
#[program]
pub mod light_token_macro_create_token_account {
use super::*;
pub fn create_token_account<'info>(
ctx: Context<'_, '_, '_, 'info, CreateTokenAccount<'info>>,
params: CreateTokenAccountParams,
) -> Result<()> {
Ok(())
}
}
Accounts struct
DeriveLightAccounts on your Accounts struct and add #[light_account(...)] next to #[account(...)].Report incorrect code
Copy
Ask AI
/// CHECK: Validated by light-token CPI
#[account(
mut,
seeds = [VAULT_SEED, mint.key().as_ref()],
bump,
)]
#[light_account(init,
token::authority = [VAULT_SEED, self.mint.key()],
token::mint = mint,
token::owner = vault_authority,
token::bump = params.vault_bump
)]
pub vault: UncheckedAccount<'info>,
Full code example
View the source code and full example with shared test utilities.
Report incorrect code
Copy
Ask AI
#![allow(deprecated)]
use anchor_lang::prelude::*;
use light_compressible::CreateAccountsProof;
use light_sdk::derive_light_cpi_signer;
use light_sdk_macros::{light_program, LightAccounts};
use light_sdk_types::CpiSigner;
use light_token::instruction::{COMPRESSIBLE_CONFIG_V1, RENT_SPONSOR as LIGHT_TOKEN_RENT_SPONSOR};
declare_id!("9p5BUDtVmRRJqp2sN73ZUZDbaYtYvEWuxzrHH3A2ni9y");
pub const LIGHT_CPI_SIGNER: CpiSigner =
derive_light_cpi_signer!("9p5BUDtVmRRJqp2sN73ZUZDbaYtYvEWuxzrHH3A2ni9y");
pub const VAULT_AUTH_SEED: &[u8] = b"vault_auth";
pub const VAULT_SEED: &[u8] = b"vault";
#[derive(AnchorSerialize, AnchorDeserialize, Clone)]
pub struct CreateTokenVaultParams {
pub create_accounts_proof: CreateAccountsProof,
pub vault_bump: u8,
}
#[derive(Accounts, LightAccounts)]
#[instruction(params: CreateTokenVaultParams)]
pub struct CreateTokenVault<'info> {
#[account(mut)]
pub fee_payer: Signer<'info>,
/// CHECK: Token mint for the vault
pub mint: AccountInfo<'info>,
/// CHECK: Validated by seeds constraint
#[account(
seeds = [VAULT_AUTH_SEED],
bump,
)]
pub vault_authority: UncheckedAccount<'info>,
/// CHECK: Validated by seeds constraint and light_account macro
#[account(
mut,
seeds = [VAULT_SEED, mint.key().as_ref()],
bump,
)]
#[light_account(
init,
token::authority = [VAULT_SEED, self.mint.key()],
token::mint = mint,
token::owner = vault_authority,
token::bump = params.vault_bump
)]
pub vault: UncheckedAccount<'info>,
/// CHECK: Validated by address constraint
#[account(address = COMPRESSIBLE_CONFIG_V1)]
pub light_token_compressible_config: AccountInfo<'info>,
/// CHECK: Validated by address constraint
#[account(mut, address = LIGHT_TOKEN_RENT_SPONSOR)]
pub light_token_rent_sponsor: AccountInfo<'info>,
/// CHECK: Light token CPI authority
pub light_token_cpi_authority: AccountInfo<'info>,
/// CHECK: Light token program for CPI
pub light_token_program: AccountInfo<'info>,
pub system_program: Program<'info, System>,
}
#[light_program]
#[program]
pub mod light_token_macro_create_token_account {
use super::*;
#[allow(unused_variables)]
pub fn create_token_vault<'info>(
ctx: Context<'_, '_, '_, 'info, CreateTokenVault<'info>>,
params: CreateTokenVaultParams,
) -> Result<()> {
Ok(())
}
}