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.
- 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 viacreateTokenPool().
State trees where compressed account’s are stored, are append only.
mergeTokenAccounts() reduces account fragmentation to simplify balance calculations from getCompressedTokenAccountsByOwner.Full Code Example
Prerequisites
Make sure you have dependencies and developer environment set up!Prerequisites & Setup
Prerequisites & Setup
DependenciesAlternatives:Developer EnvironmentBy default, this guide uses Localnet.Alternative: Using DevnetFollow these steps to develop create an RPC Connection. Replace
<your_api_key> with your API key before running.Get your API key here, if you don’t have one yet.
Merging Compressed Accounts
Run this script to merge multiple compressed token accounts into one!merge-compressed-accounts.ts
Troubleshooting
No compressed token accounts found
No compressed token accounts found
The owner has no compressed token accounts for the specified mint:
Advanced Configuration
Conditional Merging
Conditional Merging
Merge Multiple Mints
Merge Multiple Mints