compressSplTokenAccount function compresses the balance of an SPL token account, with an optional remainingAmount parameter to leave tokens in the original account.
After compression, empty token accounts can now be closed to reclaim rent with closeAccount().
Full Code Example
1
Prerequisites
Make sure you have dependencies and developer environment set up!Prerequisites & Setup
Prerequisites & Setup
DependenciesDeveloper Environment
- npm
- yarn
- pnpm
- Localnet
- Devnet
By default, all guides use Localnet.
- npm
- yarn
- pnpm
2
Compress SPL Token Accounts
Run this script to compress the entire SPL token account balance.compress-full-account.ts
Make sure the SPL mint has a token pool for compression.
The script creates this token pool for you.For development, you can create a new mint with token pool via
The script creates this token pool for you.For development, you can create a new mint with token pool via
createMint() or add a token pool to an existing mint via createTokenPool().Troubleshooting
Insufficient balance in token account
Insufficient balance in token account
The token account doesn’t have enough tokens for the operation.
Remaining amount exceeds balance
Remaining amount exceeds balance
The
remainingAmount parameter exceeds the current account balance.Advanced Configuration
Partial Account Compression
Partial Account Compression
Compress most tokens while leaving some in SPL format:
Compress Multiple Accounts
Compress Multiple Accounts
Compress several token accounts for the same mint: