Skip to main content
Install the agent skill:
npx skills add https://zkcompression.com
See the AI tools guide for dedicated skills.
1

Prerequisites

Install packages in your working directory:
npm install @lightprotocol/stateless.js@beta \
            @lightprotocol/compressed-token@beta
Install the CLI globally:
npm install -g @lightprotocol/zk-compression-cli@beta
# start local test-validator in a separate terminal
light test-validator
In the code examples, use createRpc() without arguments for localnet.
2

Mint SPL tokens, wrap them to a rent-free account and transfer them to a recipient

import "dotenv/config";
import { Keypair, PublicKey } from "@solana/web3.js";
import { createRpc } from "@lightprotocol/stateless.js";
import {
    createMintInterface,
    createAtaInterfaceIdempotent,
    getAssociatedTokenAddressInterface,
    wrap,
    transferInterface,
} from "@lightprotocol/compressed-token";
import {
    TOKEN_2022_PROGRAM_ID,
    createAssociatedTokenAccount,
    mintTo,
} 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 () {
    const decimals = 9;
    const tokenAmount = BigInt(100 * Math.pow(10, decimals));
    const recipient = Keypair.generate();

    // Creates on-chain SPL or Token-2022 mint and registers SPL interface PDA.
    // SPL interface PDA enables wrap/unwrap between SPL/Token-2022 and Light Token.
    const mintKeypair = Keypair.generate();
    const { mint } = await createMintInterface(
        rpc,
        payer,
        payer,
        null,
        decimals,
        mintKeypair,
        undefined,
        TOKEN_2022_PROGRAM_ID,
    );

    // 1. Mint SPL tokens to payer's associated token account
    const payerSplAta = await createAssociatedTokenAccount(
        rpc,
        payer,
        mint,
        payer.publicKey,
        undefined,
        TOKEN_2022_PROGRAM_ID,
    );
    await mintTo(
        rpc,
        payer,
        mint,
        payerSplAta,
        payer,
        tokenAmount,
        [],
        undefined,
        TOKEN_2022_PROGRAM_ID,
    );

    // 2. Wrap SPL tokens into payer's light associated token account
    await createAtaInterfaceIdempotent(rpc, payer, mint, payer.publicKey);
    const payerLightAta = getAssociatedTokenAddressInterface(mint, payer.publicKey);
    await wrap(rpc, payer, payerSplAta, payerLightAta, payer, mint, tokenAmount);

    // 3. Transfer to recipient's light associated token account
    await createAtaInterfaceIdempotent(rpc, payer, mint, recipient.publicKey);
    const recipientLightAta = getAssociatedTokenAddressInterface(mint, recipient.publicKey);
    await transferInterface(
        rpc,
        payer,
        payerLightAta,
        mint,
        recipientLightAta,
        payer,
        tokenAmount,
    );

    console.log("Transferred", 100, "tokens to", recipient.publicKey.toBase58());
})();

Next Steps

FAQ

Find answers to frequently answered questions.