Compress Complete SPL Token Accounts
Complete guide to compress complete SPL Token Accounts with compressSplTokenAccount() for account migration and to reclaim rent afterwards.
The compressSplTokenAccount function compresses the balance of an SPL token account, with an optional remainingAmount parameter to leave tokens in the original account.
Before compressing SPL token accounts, you need:
An SPL mint with a token pool for compression. This token pool can be created for new SPL mints via
createMint(), andSPL token account (ATA) with tokens to compress.
After compression, empty token accounts can now be closed to reclaim rent with closeAccount().
// Compress entire SPL token account balance
const transactionSignature = await compressSplTokenAccount(
rpc,
payer,
mint, // SPL mint with token pool for compression
owner,
tokenAccount, // SPL token account to compress
);// Compress account while keeping some tokens in SPL format
const transactionSignature = await compressSplTokenAccount(
rpc,
payer,
mint, // SPL mint with token pool for compression
owner,
tokenAccount, // SPL token account to compress
remainingAmount, // amount to keep in SPL format
);Function Difference and Best Practice:
compressSplTokenAccount(tokenAccount, remainingAmount)compresses the entire SPL token account balance minus optional remaining amount only to the same owner. Use to migrate complete token accounts with optional partial retention.compress(amount, sourceTokenAccount, toAddress)compresses specific amounts from source to a specified recipient. Use for transfers and precise amounts. Here is how.
Full Code Example
Compress SPL Token Accounts
Compress entire SPL token account balance.
// 1. Setup funded payer and connect to local validator
// 2. Create SPL mint with token pool and mint SPL tokens to ATA
// 3. Call compressSplTokenAccount() to compress entire account
// 4. Verify balances and rent reclaim eligibility
import { Keypair } from '@solana/web3.js';
import { createRpc, bn } from '@lightprotocol/stateless.js';
import {
createMint,
compressSplTokenAccount
} from '@lightprotocol/compressed-token';
import {
createAssociatedTokenAccount,
mintTo,
TOKEN_PROGRAM_ID
} from '@solana/spl-token';
async function compressFullAccount() {
// Step 1: Setup funded payer and connect to local validator
const rpc = createRpc(); // defaults to localhost:8899
const payer = Keypair.generate();
const airdropSignature = await rpc.requestAirdrop(payer.publicKey, 1000000000); // 1 SOL
await rpc.confirmTransaction(airdropSignature);
// Step 2: Create SPL mint with token pool and mint SPL tokens to ATA
const { mint } = await createMint(rpc, payer, payer.publicKey, 9);
console.log("Mint with token pool created:", mint.toBase58());
const tokenOwner = Keypair.generate();
const tokenAccount = await createAssociatedTokenAccount(
rpc,
payer,
mint,
tokenOwner.publicKey
);
console.log("SPL token account created:", tokenAccount.toBase58());
// Mint SPL tokens to the ATA
const splAmount = 2_000_000_000; // 2 tokens with 9 decimals
await mintTo(rpc, payer, mint, tokenAccount, payer, splAmount);
console.log("SPL tokens minted:", splAmount / 1_000_000_000, "tokens");
// Check balances before compression
const splBalanceBefore = await rpc.getTokenAccountBalance(tokenAccount);
console.log("\nBefore Compression:");
console.log("SPL token balance:", Number(splBalanceBefore.value.amount) / 1_000_000_000, "tokens");
// Step 3: Compress entire SPL token account
const compressTx = await compressSplTokenAccount(
rpc,
payer,
mint, // SPL mint with token pool for compression
tokenOwner,
tokenAccount, // SPL token account to compress
);
console.log("SPL token account compressed!");
console.log("Transaction:", compressTx);
// Step 4: Verify balances and rent reclaim eligibility
const splBalanceAfter = await rpc.getTokenAccountBalance(tokenAccount);
const compressedAccountsAfter = await rpc.getCompressedTokenAccountsByOwner(
tokenOwner.publicKey,
{ mint }
);
const totalCompressed = compressedAccountsAfter.items.reduce(
(sum, account) => sum.add(account.parsed.amount),
bn(0)
);
console.log("\nAfter Compression:");
console.log("SPL token balance:", Number(splBalanceAfter.value.amount) / 1_000_000_000, "tokens");
console.log("Compressed accounts:", compressedAccountsAfter.items.length);
console.log("Total compressed balance:", totalCompressed.toNumber() / 1_000_000_000, "tokens");
if (Number(splBalanceAfter.value.amount) === 0) {
console.log("\nSPL token account is now empty and can be closed to reclaim rent!");
}
return {
compressTransaction: compressTx,
tokenAccount,
splBalanceAfter: Number(splBalanceAfter.value.amount),
compressedBalance: totalCompressed.toNumber()
};
}
compressFullAccount().catch(console.error);Troubleshooting
Advanced Configuration
Next Steps
Compressed tokens need an SPL mint with token pool for compression. Learn how to create and register SPL mints with token pools for compression in the next guide.
Create and Register a Mint Account for CompressionLast updated
Was this helpful?