Learn how to create, transfer, and manage compressed SPL tokens
Compressed tokens on Light Protocol provide the same functionality as SPL tokens while reducing on-chain storage costs by up to 10,000x. This guide shows you how to build applications using compressed tokens.
Compressed tokens are compatible with SPL Token 2022 extensions and maintain full composability with other programs. They use zero-knowledge proofs to compress token account state into Merkle trees.
To create a compressed token account, you need to specify compression parameters:
import { createRpc, CompressedTokenProgram} from '@lightprotocol/stateless.js';import { Keypair } from '@solana/web3.js';const rpc = createRpc( "http://127.0.0.1:8899", "http://127.0.0.1:8784", "http://127.0.0.1:3001");const payer = Keypair.generate();const tokenAccount = Keypair.generate();const mint = new PublicKey("...");// Create compressed token accountconst createIx = await CompressedTokenProgram.createTokenAccount({ payer: payer.publicKey, account: tokenAccount.publicKey, mint: mint, owner: payer.publicKey,});const tx = new Transaction().add(createIx);await rpc.sendTransaction(tx, [payer, tokenAccount]);
Compression Parameters: The pre_pay_num_epochs determines how many write operations are pre-funded. Set this based on your application’s expected activity level.
Token transfers work similarly to SPL Token, with the source and destination accounts automatically handled:
use light_compressed_token;use anchor_lang::prelude::AccountMeta;use solana_sdk::instruction::Instruction;// Build transfer instruction: discriminator (3) + amount (8 bytes)let mut data = vec![3u8];data.extend_from_slice(&amount.to_le_bytes());let transfer_ix = Instruction { program_id: light_compressed_token::ID, accounts: vec![ AccountMeta::new(source, false), AccountMeta::new(destination, false), AccountMeta::new(authority, true), // Must sign and be writable ], data,};// Execute transfercontext .rpc .create_and_send_transaction( &[transfer_ix], &payer_pubkey, &[&payer, &authority], ) .await?;
Authority Must Be Writable: The transfer authority account must be writable because compressible accounts may require automatic top-ups during the transaction.
Compressed tokens support transfer fees with automatic fee calculation:
Transfer Fee Extension
use spl_token_2022::extension::transfer_fee::TransferFee;// The compressed token program automatically:// 1. Calculates transfer fees from mint configuration// 2. Deducts fees from transfer amount// 3. Credits fees to the fee authority// No additional code needed - fees are handled transparentlylet transfer_ix = build_transfer_instruction( source, destination, amount, // Gross amount (fees deducted automatically) authority.pubkey(),);