- Associated Light Token accounts can hold token balances of light, SPL, or Token 2022 mints.
- Light-ATAs 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.
- TypeScript Client
- Rust Client
- Program
The
createAtaInterface function creates an associated Light Token account in a single call.Compare to SPL:Find the source code
here.
Create Associated Token Account
Installation
Installation
- npm
- yarn
- pnpm
Install packages in your working directory:Install the CLI globally:
Report incorrect code
Copy
Ask AI
npm install @lightprotocol/stateless.js@beta \
@lightprotocol/compressed-token@beta
Report incorrect code
Copy
Ask AI
npm install -g @lightprotocol/zk-compression-cli@beta
Install packages in your working directory:Install the CLI globally:
Report incorrect code
Copy
Ask AI
yarn add @lightprotocol/stateless.js@beta \
@lightprotocol/compressed-token@beta
Report incorrect code
Copy
Ask AI
yarn global add @lightprotocol/zk-compression-cli@beta
Install packages in your working directory:Install the CLI globally:
Report incorrect code
Copy
Ask AI
pnpm add @lightprotocol/stateless.js@beta \
@lightprotocol/compressed-token@beta
Report incorrect code
Copy
Ask AI
pnpm add -g @lightprotocol/zk-compression-cli@beta
- Localnet
- Devnet
Report incorrect code
Copy
Ask AI
# start local test-validator in a separate terminal
light test-validator
In the code examples, use
createRpc() without arguments for localnet.Get an API key from Helius and add to
.env:.env
Report incorrect code
Copy
Ask AI
API_KEY=<your-helius-api-key>
In the code examples, use
createRpc(RPC_URL) with the devnet URL.- Action
- Instruction
Report incorrect code
Copy
Ask AI
import "dotenv/config";
import { Keypair } from "@solana/web3.js";
import { createRpc } from "@lightprotocol/stateless.js";
import {
createMintInterface,
createAtaInterface,
} from "@lightprotocol/compressed-token";
import { homedir } from "os";
import { readFileSync } from "fs";
// devnet:
const RPC_URL = `https://devnet.helius-rpc.com?api-key=${process.env.API_KEY!}`;
const rpc = createRpc(RPC_URL);
// localnet:
// const rpc = createRpc();
const payer = Keypair.fromSecretKey(
new Uint8Array(
JSON.parse(readFileSync(`${homedir()}/.config/solana/id.json`, "utf8"))
)
);
(async function () {
const { mint } = await createMintInterface(rpc, payer, payer, null, 9);
const owner = Keypair.generate();
const ata = await createAtaInterface(rpc, payer, mint, owner.publicKey);
console.log("ATA:", ata.toBase58());
})();
Report incorrect code
Copy
Ask AI
import "dotenv/config";
import {
Keypair,
Transaction,
sendAndConfirmTransaction,
} from "@solana/web3.js";
import { createRpc, CTOKEN_PROGRAM_ID } from "@lightprotocol/stateless.js";
import {
createMintInterface,
createAssociatedTokenAccountInterfaceInstruction,
getAssociatedTokenAddressInterface,
} from "@lightprotocol/compressed-token";
import { homedir } from "os";
import { readFileSync } from "fs";
// devnet:
// const RPC_URL = `https://devnet.helius-rpc.com?api-key=${process.env.API_KEY!}`;
// const rpc = createRpc(RPC_URL);
// localnet:
const rpc = createRpc();
const payer = Keypair.fromSecretKey(
new Uint8Array(
JSON.parse(readFileSync(`${homedir()}/.config/solana/id.json`, "utf8")),
),
);
(async function () {
const { mint } = await createMintInterface(rpc, payer, payer, null, 9);
const owner = Keypair.generate();
const associatedToken = getAssociatedTokenAddressInterface(
mint,
owner.publicKey,
);
const ix = createAssociatedTokenAccountInterfaceInstruction(
payer.publicKey,
associatedToken,
owner.publicKey,
mint,
CTOKEN_PROGRAM_ID,
);
const tx = new Transaction().add(ix);
const signature = await sendAndConfirmTransaction(rpc, tx, [payer]);
console.log("ATA:", associatedToken.toBase58());
console.log("Tx:", signature);
})();
CreateAssociatedTokenAccount creates an on-chain ATA 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 ATA
View the source code and full example with shared test utilities.
- Action
- Instruction
Report incorrect code
Copy
Ask AI
use light_token_client::actions::{CreateAta, CreateMint};
use rust_client::setup_rpc_and_payer;
use solana_sdk::signer::Signer;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let (mut rpc, payer) = setup_rpc_and_payer().await;
// Create mint
let (_signature, mint) = CreateMint {
decimals: 9,
freeze_authority: None,
token_metadata: None,
seed: None,
}
.execute(&mut rpc, &payer, &payer)
.await?;
// Create associated token account
let (_signature, associated_token_account) = CreateAta {
mint,
owner: payer.pubkey(),
idempotent: true,
}
.execute(&mut rpc, &payer)
.await?;
println!("Associated token account: {associated_token_account}");
Ok(())
}
Report incorrect code
Copy
Ask AI
use light_client::rpc::Rpc;
use light_token::instruction::{get_associated_token_address, CreateAssociatedTokenAccount};
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>> {
// You can use Light, SPL, or Token-2022 mints to create a Light associated token account.
let SplMintContext {
mut rpc,
payer,
mint,
} = setup_spl_mint_context().await;
let owner = Keypair::new();
let create_associated_token_account_instruction =
CreateAssociatedTokenAccount::new(payer.pubkey(), owner.pubkey(), mint).instruction()?;
let sig = rpc
.create_and_send_transaction(&[create_associated_token_account_instruction], &payer.pubkey(), &[&payer])
.await?;
let associated_token_account = get_associated_token_address(&owner.pubkey(), &mint);
let data = rpc.get_account(associated_token_account).await?;
println!("Associated token account: {associated_token_account} exists: {} Tx: {sig}", data.is_some());
Ok(())
}
- CPI
- Anchor Macros
Compare to SPL:
Build Account Infos and CPI the Light Token Program
- Pass ATA accounts and call
.rent_free()with rent config accounts. - Use
invokeorinvoke_signed:- When the
payeris an external wallet, useinvoke. - When the
payeris a PDA, useinvoke_signedwith its seeds.
- When the
The light-ATA address is derived from
[owner, light_token_program_id, mint].
Unlike Light Token accounts, owner and mint are passed as accounts, not in
instruction data.- invoke (External signer)
- invoke_signed (PDA signer)
Report incorrect code
Copy
Ask AI
use light_token::instruction::CreateAssociatedAccountCpi;
CreateAssociatedAccountCpi {
payer: payer.clone(),
owner: owner.clone(),
mint: mint.clone(),
ata: associated_token_account.clone(),
bump,
}
.rent_free(
compressible_config.clone(),
rent_sponsor.clone(),
system_program.clone(),
)
.invoke()
Report incorrect code
Copy
Ask AI
use light_token::instruction::CreateAssociatedAccountCpi;
let signer_seeds: &[&[u8]] = &[ATA_SEED, &[authority_bump]];
CreateAssociatedAccountCpi {
payer: payer.clone(),
owner: owner.clone(),
mint: mint.clone(),
ata: associated_token_account.clone(),
bump,
}
.rent_free(
compressible_config.clone(),
rent_sponsor.clone(),
system_program.clone(),
)
.invoke_signed(&[signer_seeds])
| Owner | - |
|
| Mint | - |
|
| Payer | signer, mutable |
|
| light-ATA Account | mutable |
|
| - | Solana System Program. Required for CPI to create the on-chain account. | |
| Bump | u8 | The PDA bump seed for the light-ATA address derivation. |
| Idempotent | bool |
|
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::CreateAssociatedAccountCpi;
declare_id!("35MukgdfpNUbPMhTmEk63ECV8vjgpNVFRH9nP8ovMN58");
#[program]
pub mod light_token_anchor_create_associated_token_account {
use super::*;
pub fn create_associated_token_account(ctx: Context<CreateAssociatedTokenAccountAccounts>, bump: u8, idempotent: bool) -> Result<()> {
let cpi = CreateAssociatedAccountCpi {
payer: ctx.accounts.payer.to_account_info(),
owner: ctx.accounts.owner.to_account_info(),
mint: ctx.accounts.mint.to_account_info(),
ata: ctx.accounts.associated_token_account.to_account_info(),
bump,
};
if idempotent {
cpi.idempotent().rent_free(
ctx.accounts.compressible_config.to_account_info(),
ctx.accounts.rent_sponsor.to_account_info(),
ctx.accounts.system_program.to_account_info(),
)
} else {
cpi.rent_free(
ctx.accounts.compressible_config.to_account_info(),
ctx.accounts.rent_sponsor.to_account_info(),
ctx.accounts.system_program.to_account_info(),
)
}
.invoke()?;
Ok(())
}
}
#[derive(Accounts)]
pub struct CreateAssociatedTokenAccountAccounts<'info> {
/// CHECK: Light token program for CPI
pub light_token_program: AccountInfo<'info>,
/// CHECK: Validated by light-token CPI
pub owner: AccountInfo<'info>,
/// CHECK: Validated by light-token CPI
pub mint: AccountInfo<'info>,
#[account(mut)]
pub payer: Signer<'info>,
/// CHECK: Validated by light-token CPI
#[account(mut)]
pub associated_token_account: AccountInfo<'info>,
pub system_program: Program<'info, System>,
/// CHECK: Validated by light-token CPI
pub compressible_config: AccountInfo<'info>,
/// CHECK: Validated by light-token CPI
#[account(mut)]
pub rent_sponsor: 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_ata {
use super::*;
pub fn create_ata<'info>(
ctx: Context<'_, '_, '_, 'info, CreateAta<'info>>,
params: CreateAtaParams,
) -> 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)]
#[light_account(
init,
associated_token::authority = ata_owner,
associated_token::mint = ata_mint,
associated_token::bump = params.ata_bump
)]
pub ata: 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, LIGHT_TOKEN_PROGRAM_ID};
use light_token::instruction::{COMPRESSIBLE_CONFIG_V1, RENT_SPONSOR as LIGHT_TOKEN_RENT_SPONSOR};
declare_id!("CLsn9MTFv97oMTsujRoQAw1u2rSm2HnKtGuWUbbc8Jfn");
pub const LIGHT_CPI_SIGNER: CpiSigner =
derive_light_cpi_signer!("CLsn9MTFv97oMTsujRoQAw1u2rSm2HnKtGuWUbbc8Jfn");
#[light_program]
#[program]
pub mod light_token_macro_create_associated_token_account {
use super::*;
#[allow(unused_variables)]
pub fn create_associated_token_account<'info>(
ctx: Context<'_, '_, '_, 'info, CreateAssociatedTokenAccount<'info>>,
params: CreateAssociatedTokenAccountParams,
) -> Result<()> {
Ok(())
}
}
#[derive(AnchorSerialize, AnchorDeserialize, Clone)]
pub struct CreateAssociatedTokenAccountParams {
pub create_accounts_proof: CreateAccountsProof,
pub associated_token_account_bump: u8,
}
#[derive(Accounts, LightAccounts)]
#[instruction(params: CreateAssociatedTokenAccountParams)]
pub struct CreateAssociatedTokenAccount<'info> {
#[account(mut)]
pub fee_payer: Signer<'info>,
/// CHECK: Token mint for the associated token account
pub associated_token_account_mint: AccountInfo<'info>,
/// CHECK: Owner of the associated token account
pub associated_token_account_owner: AccountInfo<'info>,
/// CHECK: Validated by light_account macro
#[account(mut)]
#[light_account(init, associated_token::authority = associated_token_account_owner, associated_token::mint = associated_token_account_mint, associated_token::bump = params.associated_token_account_bump)]
pub associated_token_account: 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 program for CPI
#[account(address = LIGHT_TOKEN_PROGRAM_ID.into())]
pub light_token_program: AccountInfo<'info>,
pub system_program: Program<'info, System>,
}