Complete guide to merge multiple compressed token accounts into a single account with mergeTokenAccounts(), troubleshooting and advanced configurations.
The mergeTokenAccounts() function consolidates multiple compressed accounts of the same mint into a single compressed account.The function
consumes multiple compressed token accounts (up to 8 accounts), and
creates a single output compressed token account with combined balance for the owner.
Before we merge compressed accounts, we need
Multiple compressed token accounts of the same mint owned by the same wallet, and
an SPL mint with a token pool for compression. This token pool can be created for new SPL mints via createMint() or added to existing SPL mints via createTokenPool().
State trees where compressed account’s are stored, are append only. mergeTokenAccounts() reduces account fragmentation to simplify balance calculations from getCompressedTokenAccountsByOwner.
Copy
Ask AI
```typescript// Combines multiple compressed token accounts into single compressed accountconst transactionSignature = await mergeTokenAccounts( rpc, payer, mint, // SPL mint with token pool for compression owner,);
Developer EnvironmentBy default, this guide uses Localnet.
Copy
Ask AI
# Install the development CLInpm install @lightprotocol/zk-compression-cli
Copy
Ask AI
# Start a local test validatorlight test-validator## ensure you have the Solana CLI accessible in your system PATH
Copy
Ask AI
// createRpc() defaults to local test validator endpointsimport { Rpc, createRpc,} from "@lightprotocol/stateless.js";const connection: Rpc = createRpc();async function main() { let slot = await connection.getSlot(); console.log(slot); let health = await connection.getIndexerHealth(slot); console.log(health); // "Ok"}main();
Alternative: Using DevnetFollow these steps to develop create an RPC Connection. Replace <your_api_key> with your API key before running.