- Wrap: Move tokens from SPL/T22 account → Light Token ATA (hot balance)
- Unwrap: Move tokens from Light Token ATA (hot balance) → SPL/T22 account
- TypeScript Client
- Rust Client
- Wrap
- Unwrap
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@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, 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!}`;
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);
// Wrap SPL tokens to rent-free token ATA
const lightTokenAta = getAssociatedTokenAddressInterface(mint, payer.publicKey);
await createAtaInterfaceIdempotent(rpc, payer, mint, payer.publicKey);
const tx = await wrap(rpc, payer, splAta, lightTokenAta, 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 lightTokenAta = 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,
lightTokenAta,
payer.publicKey,
mint,
bn(500),
splInterfaceInfo,
9, // decimals - must match the mint decimals
payer.publicKey
);
const tx = new Transaction().add(
ComputeBudgetProgram.setComputeUnitLimit({ units: 200_000 }),
ix
);
const signature = await sendAndConfirmTransaction(rpc, tx, [payer]);
console.log("Tx:", signature);
})();
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!}`;
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));
// 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 lightTokenAta = getAssociatedTokenAddressInterface(mint, payer.publicKey);
await loadAta(rpc, lightTokenAta, 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(
lightTokenAta,
splAta,
payer.publicKey,
mint,
bn(500),
splInterfaceInfo,
9, // decimals - must match the mint decimals
payer.publicKey
);
const tx = new Transaction().add(
ComputeBudgetProgram.setComputeUnitLimit({ units: 200_000 }),
ix
);
const signature = await sendAndConfirmTransaction(rpc, tx, [payer]);
console.log("Tx:", signature);
})();
- Wrap
- Unwrap
Prerequisites
Dependencies
Dependencies
Cargo.toml
Report incorrect code
Copy
Ask AI
[dependencies]
light-token = "0.4.0"
light-client = { version = "0.19.0", features = ["v2"] }
solana-sdk = "2"
borsh = "0.10.4"
tokio = { version = "1", features = ["full"] }
Developer Environment
Developer Environment
- In-Memory (LightProgramTest)
- Localnet (LightClient)
- Devnet (LightClient)
Test with Lite-SVM (…)
Report incorrect code
Copy
Ask AI
# Initialize project
cargo init my-light-project
cd my-light-project
# Run tests
cargo test
Report incorrect code
Copy
Ask AI
use light_program_test::{LightProgramTest, ProgramTestConfig};
use solana_sdk::signer::Signer;
#[tokio::test]
async fn test_example() {
// In-memory test environment
let mut rpc = LightProgramTest::new(ProgramTestConfig::default())
.await
.unwrap();
let payer = rpc.get_payer().insecure_clone();
println!("Payer: {}", payer.pubkey());
}
Connects to a local test validator.
- npm
- yarn
- pnpm
Report incorrect code
Copy
Ask AI
npm install -g @lightprotocol/zk-compression-cli@beta
Report incorrect code
Copy
Ask AI
yarn global add @lightprotocol/zk-compression-cli@beta
Report incorrect code
Copy
Ask AI
pnpm add -g @lightprotocol/zk-compression-cli@beta
Report incorrect code
Copy
Ask AI
# Initialize project
cargo init my-light-project
cd my-light-project
# Start local test validator (in separate terminal)
light test-validator
Report incorrect code
Copy
Ask AI
use light_client::rpc::{LightClient, LightClientConfig, Rpc};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Connects to http://localhost:8899
let rpc = LightClient::new(LightClientConfig::local()).await?;
let slot = rpc.get_slot().await?;
println!("Current slot: {}", slot);
Ok(())
}
Replace
<your-api-key> with your actual API key. Get your API key here.Report incorrect code
Copy
Ask AI
use light_client::rpc::{LightClient, LightClientConfig, Rpc};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let rpc_url = "https://devnet.helius-rpc.com?api-key=<your_api_key>";
let rpc = LightClient::new(
LightClientConfig::new(rpc_url.to_string(), None, None)
).await?;
println!("Connected to Devnet");
Ok(())
}
Wrap SPL tokens to Light Token ATA
View the full example with shared test utilities.
- Action
Report incorrect code
Copy
Ask AI
use borsh::BorshDeserialize;
use light_client::rpc::Rpc;
use light_token_client::actions::Wrap;
use rust_client::{setup_for_wrap, WrapContext};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Setup creates SPL associated token account with tokens and empty Light associated token account
let WrapContext {
mut rpc,
payer,
mint,
source_associated_token_account,
light_associated_token_account,
decimals,
} = setup_for_wrap().await;
// Wrap tokens from SPL associated token account to Light Token associated token account
let sig = Wrap {
source_spl_ata: source_associated_token_account,
destination: light_associated_token_account,
mint,
amount: 500_000,
decimals,
}
.execute(&mut rpc, &payer, &payer)
.await?;
let data = rpc
.get_account(light_associated_token_account)
.await?
.ok_or("Account not found")?;
let token = light_token_interface::state::Token::deserialize(&mut &data.data[..])?;
println!("Balance: {} Tx: {sig}", token.amount);
Ok(())
}
Prerequisites
Dependencies
Dependencies
Cargo.toml
Report incorrect code
Copy
Ask AI
[dependencies]
light-token = "0.4.0"
light-client = { version = "0.19.0", features = ["v2"] }
solana-sdk = "2"
borsh = "0.10.4"
tokio = { version = "1", features = ["full"] }
Developer Environment
Developer Environment
- In-Memory (LightProgramTest)
- Localnet (LightClient)
- Devnet (LightClient)
Test with Lite-SVM (…)
Report incorrect code
Copy
Ask AI
# Initialize project
cargo init my-light-project
cd my-light-project
# Run tests
cargo test
Report incorrect code
Copy
Ask AI
use light_program_test::{LightProgramTest, ProgramTestConfig};
use solana_sdk::signer::Signer;
#[tokio::test]
async fn test_example() {
// In-memory test environment
let mut rpc = LightProgramTest::new(ProgramTestConfig::default())
.await
.unwrap();
let payer = rpc.get_payer().insecure_clone();
println!("Payer: {}", payer.pubkey());
}
Connects to a local test validator.
- npm
- yarn
- pnpm
Report incorrect code
Copy
Ask AI
npm install -g @lightprotocol/zk-compression-cli@beta
Report incorrect code
Copy
Ask AI
yarn global add @lightprotocol/zk-compression-cli@beta
Report incorrect code
Copy
Ask AI
pnpm add -g @lightprotocol/zk-compression-cli@beta
Report incorrect code
Copy
Ask AI
# Initialize project
cargo init my-light-project
cd my-light-project
# Start local test validator (in separate terminal)
light test-validator
Report incorrect code
Copy
Ask AI
use light_client::rpc::{LightClient, LightClientConfig, Rpc};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Connects to http://localhost:8899
let rpc = LightClient::new(LightClientConfig::local()).await?;
let slot = rpc.get_slot().await?;
println!("Current slot: {}", slot);
Ok(())
}
Replace
<your-api-key> with your actual API key. Get your API key here.Report incorrect code
Copy
Ask AI
use light_client::rpc::{LightClient, LightClientConfig, Rpc};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let rpc_url = "https://devnet.helius-rpc.com?api-key=<your_api_key>";
let rpc = LightClient::new(
LightClientConfig::new(rpc_url.to_string(), None, None)
).await?;
println!("Connected to Devnet");
Ok(())
}
Unwrap Light Tokens to SPL account
View the full example with shared test utilities.
- Action
Report incorrect code
Copy
Ask AI
use anchor_spl::token::spl_token::state::Account as SplAccount;
use light_client::rpc::Rpc;
use light_token_client::actions::Unwrap;
use rust_client::{setup_for_unwrap, UnwrapContext};
use solana_sdk::program_pack::Pack;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Setup creates Light associated token account with tokens and empty SPL associated token account
let UnwrapContext {
mut rpc,
payer,
mint,
destination_associated_token_account,
light_associated_token_account,
decimals,
} = setup_for_unwrap().await;
// Unwrap tokens from Light Token associated token account to SPL associated token account
let sig = Unwrap {
source: light_associated_token_account,
destination_spl_ata: destination_associated_token_account,
mint,
amount: 500_000,
decimals,
}
.execute(&mut rpc, &payer, &payer)
.await?;
let data = rpc
.get_account(destination_associated_token_account)
.await?
.ok_or("Account not found")?;
let token = SplAccount::unpack(&data.data)?;
println!("Balance: {} Tx: {sig}", token.amount);
Ok(())
}