- Wrap: Move tokens from SPL/T22 account → light-token ATA (hot balance)
- Unwrap: Move tokens from light-token ATA (hot balance) → SPL/T22 account
Get Started
- Wrap
- Unwrap
1
Wrap SPL Tokens to Light Token ATA
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, bn } from "@lightprotocol/stateless.js";
import {
createMint,
mintTo,
decompress,
wrap,
getAssociatedTokenAddressInterface,
createAtaInterfaceIdempotent,
} from "@lightprotocol/compressed-token";
import { createAssociatedTokenAccount } from "@solana/spl-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: Get SPL tokens (needed to wrap)
const { mint } = await createMint(rpc, payer, payer.publicKey, 9);
const splAta = await createAssociatedTokenAccount(
rpc,
payer,
mint,
payer.publicKey
);
await mintTo(rpc, payer, mint, payer.publicKey, payer, bn(1000));
await decompress(rpc, payer, mint, bn(1000), payer, splAta);
// Wrap SPL tokens to rent-free token ATA
const ctokenAta = getAssociatedTokenAddressInterface(mint, payer.publicKey);
await createAtaInterfaceIdempotent(rpc, payer, mint, payer.publicKey);
const tx = await wrap(rpc, payer, splAta, ctokenAta, payer, mint, bn(500));
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 } from "@lightprotocol/stateless.js";
import {
createMint,
mintTo,
decompress,
createWrapInstruction,
getAssociatedTokenAddressInterface,
createAtaInterfaceIdempotent,
getSplInterfaceInfos,
} from "@lightprotocol/compressed-token";
import { createAssociatedTokenAccount } from "@solana/spl-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 () {
// Setup: Get SPL tokens (needed to wrap)
const { mint } = await createMint(rpc, payer, payer.publicKey, 9);
const splAta = await createAssociatedTokenAccount(
rpc,
payer,
mint,
payer.publicKey
);
await mintTo(rpc, payer, mint, payer.publicKey, payer, bn(1000));
await decompress(rpc, payer, mint, bn(1000), payer, splAta);
// Create wrap instruction
const ctokenAta = getAssociatedTokenAddressInterface(mint, payer.publicKey);
await createAtaInterfaceIdempotent(rpc, payer, mint, payer.publicKey);
const splInterfaceInfos = await getSplInterfaceInfos(rpc, mint);
const splInterfaceInfo = splInterfaceInfos.find(
(info) => info.isInitialized
);
if (!splInterfaceInfo) throw new Error("No SPL interface found");
const ix = createWrapInstruction(
splAta,
ctokenAta,
payer.publicKey,
mint,
bn(500),
splInterfaceInfo,
payer.publicKey
);
const tx = new Transaction().add(
ComputeBudgetProgram.setComputeUnitLimit({ units: 200_000 }),
ix
);
const signature = await sendAndConfirmTransaction(rpc, tx, [payer]);
console.log("Tx:", signature);
})();
1
Unwrap Light Tokens to SPL Account
- Action
- Instruction
Report incorrect code
Copy
Ask AI
import "dotenv/config";
import { Keypair } from "@solana/web3.js";
import { createRpc, bn } from "@lightprotocol/stateless.js";
import { createMint, mintTo } from "@lightprotocol/compressed-token";
import { unwrap } from "@lightprotocol/compressed-token/unified";
import { createAssociatedTokenAccount } from "@solana/spl-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: Get compressed tokens (cold storage)
const { mint } = await createMint(rpc, payer, payer.publicKey, 9);
await mintTo(rpc, payer, mint, payer.publicKey, payer, bn(1000));
// Unwrap rent-free tokens to SPL ATA
const splAta = await createAssociatedTokenAccount(
rpc,
payer,
mint,
payer.publicKey
);
const tx = await unwrap(rpc, payer, splAta, payer, mint, bn(500));
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 } from "@lightprotocol/stateless.js";
import {
createMint,
mintTo,
loadAta,
getAssociatedTokenAddressInterface,
getSplInterfaceInfos,
} from "@lightprotocol/compressed-token";
import { createUnwrapInstruction } from "@lightprotocol/compressed-token/unified";
import { createAssociatedTokenAccount } from "@solana/spl-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 () {
// Setup: Get compressed tokens (cold storage)
const { mint } = await createMint(rpc, payer, payer.publicKey, 9);
await mintTo(rpc, payer, mint, payer.publicKey, payer, bn(1000));
// Load compressed tokens to hot balance, then create unwrap instruction
const ctokenAta = getAssociatedTokenAddressInterface(mint, payer.publicKey);
await loadAta(rpc, ctokenAta, payer, mint, payer);
const splAta = await createAssociatedTokenAccount(
rpc,
payer,
mint,
payer.publicKey
);
const splInterfaceInfos = await getSplInterfaceInfos(rpc, mint);
const splInterfaceInfo = splInterfaceInfos.find(
(info) => info.isInitialized
);
if (!splInterfaceInfo) throw new Error("No SPL interface found");
const ix = createUnwrapInstruction(
ctokenAta,
splAta,
payer.publicKey,
mint,
bn(500),
splInterfaceInfo,
payer.publicKey
);
const tx = new Transaction().add(
ComputeBudgetProgram.setComputeUnitLimit({ units: 200_000 }),
ix
);
const signature = await sendAndConfirmTransaction(rpc, tx, [payer]);
console.log("Tx:", signature);
})();