How to Transfer Compressed Token
Complete guide to transfer compressed SPL tokens between compressed or regular accounts with `transfer()`, troubleshooting and advanced configurations.
The transfer()
function moves compressed tokens between accounts. Unlike regular SPL transfers that update existing account balances, compressed transfers consume input accounts from the sender and create new output accounts for sender and recipient with updated balances.
Before we can transfer compressed tokens, we need:
SPL mint registered with the compressed token program via
createMint()
orcreateTokenPool()
Source compressed token account with sufficient balance for the transfer amount. Regular SPL token accounts can be compressed in the same transaction with
compress_or_decompress_amount
, if needed.
import { transfer } from '@lightprotocol/compressed-token';
import { PublicKey } from '@solana/web3.js';
// Use existing mint with token pool for compression to transfer compressed tokens
const mint = new PublicKey("MINT_ADDRESS");
const recipient = new PublicKey("RECIPIENT_WALLET_ADDRESS");
const amount = 1_000_000_000; // 1 token (9 decimals)
// Transfer compressed tokens
const transactionSignature = await transfer(
rpc,
payer,
mint, // SPL mint with token pool for compression
amount,
payer,
recipient, // destination address (toAddress parameter)
Full Code Example
Transferring Compressed Tokens
Run this script to transfer compressed tokens to a recipient!
// 1. Setup funded payer and connect to local validator
// 2. Create SPL mint and token pool for compression with initial tokens
// 3. Call transfer() with mint, amount, owner, recipient
// 4. Verify transferred tokens via getCompressedTokenAccountsByOwner
import { Keypair, PublicKey } from '@solana/web3.js';
import { createRpc } from '@lightprotocol/stateless.js';
import { createMint, mintTo, transfer } from '@lightprotocol/compressed-token';
async function transferCompressedTokens() {
// 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 2a: Create SPL mint with token pool for compression
const { mint, transactionSignature: mintCreateTx } = await createMint(
rpc,
payer,
payer.publicKey, // mint authority
9
);
console.log("SPL mint with token pool for compression created");
console.log("Mint address:", mint.toBase58());
console.log("Create mint transaction:", mintCreateTx);
// Step 2b: Create token owner and mint initial tokens
const tokenOwner = Keypair.generate();
const initialMintAmount = 1_000_000_000; // 1 token with 9 decimals
const mintToTx = await mintTo(
rpc,
payer,
mint, // SPL mint with token pool for compression
tokenOwner.publicKey, // recipient
payer, // mint authority
initialMintAmount
);
console.log("\nCompressed Tokens minted:", initialMintAmount / 1_000_000_000, "tokens");
console.log("Mint tokens transaction:", mintToTx);
// Generate recipient address and define transfer amount
const recipient = Keypair.generate();
const transferAmount = 500_000_000; // 0.5 tokens
// Step 3: Call transfer() with mint, amount, owner, recipient
const transferTx = await transfer(
rpc,
payer,
mint, // SPL mint with token pool for compression
transferAmount,
tokenOwner, // owner keypair
recipient.publicKey // recipient address
);
console.log("\nCompressed tokens transferred!");
console.log("From:", tokenOwner.publicKey.toBase58());
console.log("To:", recipient.publicKey.toBase58());
console.log("Amount transferred:", transferAmount / 1_000_000_000, "tokens");
console.log("Transfer transaction:", transferTx);
// Step 4: Verify transferred tokens via getCompressedTokenAccountsByOwner
const recipientAccounts = await rpc.getCompressedTokenAccountsByOwner(
recipient.publicKey,
{ mint }
);
// Check recipient received the tokens
if (recipientAccounts.items.length > 0) {
const receivedBalance = recipientAccounts.items[0].parsed.amount;
}
return {
transferTransaction: transferTx,
recipient: recipient.publicKey,
amount: transferAmount
};
}
transferCompressedTokens().catch(console.error);
Success!
You've successfully created and transferred compressed tokens. The output shows:
Transfer confirmation: Tokens moved from sender to recipient
Amount verification: Exact tokens transferred with decimal precision
Balance verification: Both sender and recipient balances confirmed
Troubleshooting
Advanced Configuration
Next Steps
Learn how compress and decompress SPL Tokens.
How to compress and decompress SPL TokensLast updated