Report incorrect code
Copy
Ask AI
// Mint compressed tokens - mints SPL tokens to pool, creates compressed token accounts
const transactionSignature = await mintTo(
rpc,
payer,
mint, // SPL mint with SPL interface for compression
recipient, // recipient address (toPubkey parameter)
payer, // mint authority
amount,
);
Get Started
Mint Compressed Tokens
Installation
Installation
- npm
- yarn
- pnpm
Install packages in your working directory:Install the CLI globally:
Report incorrect code
Copy
Ask AI
npm install @lightprotocol/stateless.js@beta \
@lightprotocol/compressed-token@beta
Report incorrect code
Copy
Ask AI
npm install -g @lightprotocol/zk-compression-cli@beta
Install packages in your working directory:Install the CLI globally:
Report incorrect code
Copy
Ask AI
yarn add @lightprotocol/stateless.js@beta \
@lightprotocol/compressed-token@beta
Report incorrect code
Copy
Ask AI
yarn global add @lightprotocol/zk-compression-cli@beta
Install packages in your working directory:Install the CLI globally:
Report incorrect code
Copy
Ask AI
pnpm add @lightprotocol/stateless.js@beta \
@lightprotocol/compressed-token@beta
Report incorrect code
Copy
Ask AI
pnpm add -g @lightprotocol/zk-compression-cli@beta
- Localnet
- Devnet
Report incorrect code
Copy
Ask AI
# start local test-validator in a separate terminal
light test-validator
In the code examples, use
createRpc() without arguments for localnet.Get an API key from Helius and add to
.env:.env
Report incorrect code
Copy
Ask AI
API_KEY=<your-helius-api-key>
In the code examples, use
createRpc(RPC_URL) with the devnet URL.- Action
- Instruction
Report incorrect code
Copy
Ask AI
import "dotenv/config";
import { Keypair } from "@solana/web3.js";
import { createRpc } from "@lightprotocol/stateless.js";
import { createMint, mintTo } from "@lightprotocol/compressed-token";
import { homedir } from "os";
import { readFileSync } from "fs";
// devnet:
const RPC_URL = `https://devnet.helius-rpc.com?api-key=${process.env.API_KEY!}`;
// localnet:
// const RPC_URL = undefined;
const payer = Keypair.fromSecretKey(
new Uint8Array(
JSON.parse(readFileSync(`${homedir()}/.config/solana/id.json`, "utf8"))
)
);
(async function () {
// devnet:
const rpc = createRpc(RPC_URL);
// localnet:
// const rpc = createRpc();
// Setup: Create mint
const { mint } = await createMint(rpc, payer, payer.publicKey, 9);
// Mint compressed tokens
const recipient = Keypair.generate();
const tx = await mintTo(rpc, payer, mint, recipient.publicKey, payer, 1_000_000_000);
console.log("Mint:", mint.toBase58());
console.log("Recipient:", recipient.publicKey.toBase58());
console.log("Tx:", tx);
})();
Report incorrect code
Copy
Ask AI
import "dotenv/config";
import { Keypair, ComputeBudgetProgram, Transaction, sendAndConfirmTransaction } from "@solana/web3.js";
import { createRpc, bn, DerivationMode } from "@lightprotocol/stateless.js";
import {
createMintInterface,
createAtaInterface,
createMintToInterfaceInstruction,
getMintInterface,
getAssociatedTokenAddressInterface,
} from "@lightprotocol/compressed-token";
import { homedir } from "os";
import { readFileSync } from "fs";
// devnet:
const RPC_URL = `https://devnet.helius-rpc.com?api-key=${process.env.API_KEY!}`;
const rpc = createRpc(RPC_URL);
// localnet:
// const rpc = createRpc();
const payer = Keypair.fromSecretKey(
new Uint8Array(
JSON.parse(readFileSync(`${homedir()}/.config/solana/id.json`, "utf8"))
)
);
(async function () {
const { mint } = await createMintInterface(rpc, payer, payer, null, 9);
const recipient = Keypair.generate();
await createAtaInterface(rpc, payer, mint, recipient.publicKey);
const destination = getAssociatedTokenAddressInterface(mint, recipient.publicKey);
const mintInterface = await getMintInterface(rpc, mint);
let validityProof;
if (mintInterface.merkleContext) {
validityProof = await rpc.getValidityProofV2(
[
{
hash: bn(mintInterface.merkleContext.hash),
leafIndex: mintInterface.merkleContext.leafIndex,
treeInfo: mintInterface.merkleContext.treeInfo,
proveByIndex: mintInterface.merkleContext.proveByIndex,
},
],
[],
DerivationMode.compressible
);
}
const ix = createMintToInterfaceInstruction(
mintInterface,
destination,
payer.publicKey,
payer.publicKey,
1_000_000_000,
validityProof
);
const tx = new Transaction().add(
ComputeBudgetProgram.setComputeUnitLimit({ units: 500_000 }),
ix
);
const signature = await sendAndConfirmTransaction(rpc, tx, [payer]);
console.log("Mint:", mint.toBase58());
console.log("Tx:", signature);
})();
The SPL mint must have an SPL Interface PDA for compression.
The script creates it for you.For development, create a new mint with SPL interface via
The script creates it for you.For development, create a new mint with SPL interface via
createMint() or add an SPL interface to an existing mint via createSplInterface().Troubleshooting
SPL interface not found
SPL interface not found
Report incorrect code
Copy
Ask AI
// Error: SPL interface not found for this mint. Create a compressed token
// SPL interface PDA for mint: [ADDRESS] via createSplInterface().
createMint or call createSplInterface() for an existing mint.Report incorrect code
Copy
Ask AI
// Create mint with SPL interface for compression
import { createMint } from '@lightprotocol/compressed-token';
const { mint } = await createMint(rpc, payer, payer.publicKey, 9);
SPL interface mint mismatch
SPL interface mint mismatch
The SPL interface info doesn’t correspond to the mint address. Ensure you’re fetching the correct interface:
Report incorrect code
Copy
Ask AI
// Get SPL interface infos for your mint
const tokenPoolInfo = await getTokenPoolInfos(rpc, mint);
Amount and toPubkey arrays must have the same length
Amount and toPubkey arrays must have the same length
When minting to multiple recipients, ensure arrays are the same size.
Report incorrect code
Copy
Ask AI
// Wrong: Mismatched array lengths
const recipients = [addr1, addr2, addr3];
const amounts = [100, 200]; // Only 2 amounts for 3 recipients
// Correct: Same length arrays
const recipients = [addr1, addr2, addr3];
const amounts = [100, 200, 300]; // 3 amounts for 3 recipients
Advanced Configuration
Mint to Multiple Recipients
Mint to Multiple Recipients
Report incorrect code
Copy
Ask AI
// Mint different amounts to multiple recipients
const recipients = [
Keypair.generate().publicKey,
Keypair.generate().publicKey,
Keypair.generate().publicKey,
];
const amounts = [
1_000_000_000, // 1 token
2_000_000_000, // 2 tokens
500_000_000, // 0.5 tokens
];
const transactionSignature = await mintTo(
rpc,
payer,
mint, // SPL mint with SPL interface for compression
recipients, // array of recipients (toPubkey parameter)
payer, // mint authority
amounts, // array of amounts (amount parameter)
);
With Custom Mint Authority
With Custom Mint Authority
Mint tokens using a custom mint authority with
approveAndMintTo():Report incorrect code
Copy
Ask AI
import { approveAndMintTo } from '@lightprotocol/compressed-token';
// Mint tokens with a separate mint authority
const transactionSignature = await approveAndMintTo(
rpc,
payer,
mint, // SPL mint with SPL interface for compression
recipient.publicKey, // recipient of minted tokens (toPubkey parameter)
mintAuthority, // mint authority
mintAmount,
);