- 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 rent-exemption for light account creation is sponsored by the Light Token Program.
- Transaction payer’s pay rent
to keep accounts “active” - “Inactive” accounts (rent below one epoch) get automatically compressed.
- The account’s state is cryptographically preserved and will be loaded into hot account state in-flight, when the account is used again.
- At account creation ~17,208 lamports
and . - When the account’s rent is below 3h, the transaction payer tops up 776 lamports.
- TypeScript Client
- Rust Client
- Program Guide
The
createAtaInterface function creates an associated light-token account in a single call.Compare to SPL:Find the source code
here.
1
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@alpha \
@lightprotocol/compressed-token@alpha
Report incorrect code
Copy
Ask AI
npm install -g @lightprotocol/zk-compression-cli@alpha
Install packages in your working directory:Install the CLI globally:
Report incorrect code
Copy
Ask AI
yarn add @lightprotocol/stateless.js@alpha \
@lightprotocol/compressed-token@alpha
Report incorrect code
Copy
Ask AI
yarn global add @lightprotocol/zk-compression-cli@alpha
Install packages in your working directory:Install the CLI globally:
Report incorrect code
Copy
Ask AI
pnpm add @lightprotocol/stateless.js@alpha \
@lightprotocol/compressed-token@alpha
Report incorrect code
Copy
Ask AI
pnpm add -g @lightprotocol/zk-compression-cli@alpha
- 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!}`;
// localnet:
// const RPC_URL = undefined;
const payer = Keypair.fromSecretKey(
new Uint8Array(
JSON.parse(readFileSync(`${homedir()}/.config/solana/id.json`, "utf8"))
)
);
(async function () {
// devnet:
const rpc = createRpc(RPC_URL);
// localnet:
// const rpc = createRpc();
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: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
Create ATA
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::{get_associated_token_address, CreateAssociatedTokenAccount};
use shared::SplMintContext;
use solana_sdk::{signature::Keypair, signer::Signer};
#[tokio::test(flavor = "multi_thread")]
async fn create_ata() {
// You can use light, spl, t22 mints to create a light token ATA.
let SplMintContext {
mut rpc,
payer,
mint,
} = shared::setup_spl_mint_context().await;
let owner = Keypair::new();
let create_ata_ix = CreateAssociatedTokenAccount::new(
payer.pubkey(),
owner.pubkey(),
mint,
)
.instruction()
.unwrap();
rpc.create_and_send_transaction(&[create_ata_ix], &payer.pubkey(), &[&payer])
.await
.unwrap();
let expected_ata = get_associated_token_address(&owner.pubkey(), &mint);
let ata_account = rpc.get_account(expected_ata).await.unwrap();
assert!(ata_account.is_some());
}
#[tokio::test(flavor = "multi_thread")]
async fn create_ata_idempotent() {
let SplMintContext {
mut rpc,
payer,
mint,
} = shared::setup_spl_mint_context().await;
let create_ata_ix = CreateAssociatedTokenAccount::new(
payer.pubkey(),
payer.pubkey(),
mint,
)
.idempotent()
.instruction()
.unwrap();
rpc.create_and_send_transaction(&[create_ata_ix.clone()], &payer.pubkey(), &[&payer])
.await
.unwrap();
let result = rpc
.create_and_send_transaction(&[create_ata_ix], &payer.pubkey(), &[&payer])
.await;
assert!(result.is_ok());
}
1
Define Rent Config Accounts
Report incorrect code
Copy
Ask AI
use light_token_sdk::token::CompressibleParamsCpi;
let compressible = CompressibleParamsCpi::new_ata(
compressible_config.clone(),
rent_sponsor.clone(),
system_program.clone(),
);
| Protocol PDA that stores account rent config. | |
| Light token program PDA that pays rent exemption at creation and claims rent when account compresses. | |
| Solana System Program to create the on-chain account. |
2
Build Account Infos and CPI the Compressed Token Program
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 is signer)
Report incorrect code
Copy
Ask AI
use light_token_sdk::token::{CompressibleParamsCpi, CreateAssociatedAccountCpi};
let compressible = CompressibleParamsCpi::new_ata(
compressible_config.clone(),
rent_sponsor.clone(),
system_program.clone(),
);
CreateAssociatedAccountCpi {
owner: owner.clone(),
mint: mint.clone(),
payer: payer.clone(),
associated_token_account: associated_token_account.clone(),
system_program: system_program.clone(),
bump,
compressible,
idempotent,
}
.invoke()
Report incorrect code
Copy
Ask AI
use light_token_sdk::token::{CompressibleParamsCpi, CreateAssociatedAccountCpi};
let compressible = CompressibleParamsCpi::new_ata(
compressible_config.clone(),
rent_sponsor.clone(),
system_program.clone(),
);
let signer_seeds = authority_seeds!(authority_bump);
CreateAssociatedAccountCpi {
owner: owner.clone(),
mint: mint.clone(),
payer: payer.clone(),
associated_token_account: associated_token_account.clone(),
system_program: system_program.clone(),
bump,
compressible,
idempotent,
}
.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 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::{CreateAssociatedAccountCpi, CompressibleParamsCpi};
declare_id!("77bt3j6A3g9s1WtwYnRFTGP9y8H1nigW7mLtywGKPmMi");
#[program]
pub mod light_token_anchor_create_ata {
use super::*;
pub fn create_ata<'info>(
ctx: Context<'_, '_, '_, 'info, CreateAtaAccounts<'info>>,
bump: u8,
idempotent: bool,
) -> Result<()> {
let compressible = CompressibleParamsCpi::new_ata(
ctx.accounts.compressible_config.to_account_info(),
ctx.accounts.rent_sponsor.to_account_info(),
ctx.accounts.system_program.to_account_info(),
);
CreateAssociatedAccountCpi {
owner: ctx.accounts.owner.to_account_info(),
mint: ctx.accounts.mint.to_account_info(),
payer: ctx.accounts.payer.to_account_info(),
associated_token_account: ctx.accounts.associated_token_account.to_account_info(),
system_program: ctx.accounts.system_program.to_account_info(),
bump,
compressible,
idempotent,
}
.invoke()?;
Ok(())
}
}
#[derive(Accounts)]
pub struct CreateAtaAccounts<'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>,
}
Report incorrect code
Copy
Ask AI
use super::authority_seeds;
use light_token_sdk::token::{
CompressibleParamsCpi, CreateAssociatedAccountCpi,
};
use solana_program::{
account_info::AccountInfo, entrypoint::ProgramResult,
program_error::ProgramError,
};
pub fn create_ata_invoke(accounts: &[AccountInfo], data: &[u8]) -> ProgramResult {
let [owner, mint, payer, associated_token_account, system_program, compressible_config, rent_sponsor, _token_program] =
accounts
else {
return Err(ProgramError::NotEnoughAccountKeys);
};
if data.is_empty() {
return Err(ProgramError::InvalidInstructionData);
}
let bump = data[0];
let idempotent = data.get(1).copied().unwrap_or(0) != 0;
let compressible = CompressibleParamsCpi::new_ata(
compressible_config.clone(),
rent_sponsor.clone(),
system_program.clone(),
);
CreateAssociatedAccountCpi {
owner: owner.clone(),
mint: mint.clone(),
payer: payer.clone(),
associated_token_account: associated_token_account.clone(),
system_program: system_program.clone(),
bump,
compressible,
idempotent,
}
.invoke()
}
pub fn create_ata_invoke_signed(accounts: &[AccountInfo], data: &[u8]) -> ProgramResult {
let [owner, mint, payer, associated_token_account, system_program, compressible_config, rent_sponsor, _token_program] =
accounts
else {
return Err(ProgramError::NotEnoughAccountKeys);
};
if data.len() < 3 {
return Err(ProgramError::InvalidInstructionData);
}
let bump = data[0];
let idempotent = data[1] != 0;
let authority_bump = data[2];
let signer_seeds = authority_seeds!(authority_bump);
let compressible = CompressibleParamsCpi::new_ata(
compressible_config.clone(),
rent_sponsor.clone(),
system_program.clone(),
);
CreateAssociatedAccountCpi {
owner: owner.clone(),
mint: mint.clone(),
payer: payer.clone(),
associated_token_account: associated_token_account.clone(),
system_program: system_program.clone(),
bump,
compressible,
idempotent,
}
.invoke_signed(&[signer_seeds])
}