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 token pool for compression
recipient, // recipient address (toPubkey parameter)
payer, // mint authority
amount,
);
Get Started
1
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@alpha \
@lightprotocol/compressed-token@alpha
Report incorrect code
Copy
Ask AI
npm install -g @lightprotocol/zk-compression-cli@alpha
Install packages in your working directory:Install the CLI globally:
Report incorrect code
Copy
Ask AI
yarn add @lightprotocol/stateless.js@alpha \
@lightprotocol/compressed-token@alpha
Report incorrect code
Copy
Ask AI
yarn global add @lightprotocol/zk-compression-cli@alpha
Install packages in your working directory:Install the CLI globally:
Report incorrect code
Copy
Ask AI
pnpm add @lightprotocol/stateless.js@alpha \
@lightprotocol/compressed-token@alpha
Report incorrect code
Copy
Ask AI
pnpm add -g @lightprotocol/zk-compression-cli@alpha
- 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);
})();
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
TokenPool not found
TokenPool not found
Report incorrect code
Copy
Ask AI
// Error message: "TokenPool not found. Please create a compressed token
// pool for mint: [ADDRESS] via createTokenPool().
createMint.Report incorrect code
Copy
Ask AI
// Create mint with token pool for compression
import { createMint } from '@lightprotocol/compressed-token';
const { mint } = await createMint(rpc, payer, payer.publicKey, 9);
TokenPool mint does not match the provided mint
TokenPool mint does not match the provided mint
The token pool info doesn’t correspond to the mint address. Ensure you’re fetching the correct pool:
Report incorrect code
Copy
Ask AI
// Get the correct token pool 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 token pool 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 token pool for compression
recipient.publicKey, // recipient of minted tokens (toPubkey parameter)
mintAuthority, // mint authority
mintAmount,
);