Skip to main content

  • Wrap: Move tokens from SPL/T22 account → light-token ATA (hot balance)
  • Unwrap: Move tokens from light-token ATA (hot balance) → SPL/T22 account
Find the source code: wrap.test.ts | unwrap.test.ts

Get Started

1

Wrap SPL Tokens to Light-Token ATA

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";

async function main() {
  const rpc = createRpc();
  const payer = Keypair.generate();
  await rpc.requestAirdrop(payer.publicKey, 10e9);

  const owner = Keypair.generate();
  await rpc.requestAirdrop(owner.publicKey, 1e9);

  const mintAuthority = Keypair.generate();
  const mintKeypair = Keypair.generate();
  const { mint } = await createMint(
    rpc,
    payer,
    mintAuthority.publicKey,
    9,
    mintKeypair
  );

  const splAta = await createAssociatedTokenAccount(
    rpc,
    payer,
    mint,
    owner.publicKey
  );

  // Mint compressed then decompress to SPL ATA
  await mintTo(rpc, payer, mint, owner.publicKey, mintAuthority, bn(1000));
  await decompress(rpc, payer, mint, bn(1000), owner, splAta);

  // Create c-token ATA (destination)
  const ctokenAta = getAssociatedTokenAddressInterface(mint, owner.publicKey);
  await createAtaInterfaceIdempotent(rpc, payer, mint, owner.publicKey);

  // Wrap SPL tokens to c-token ATA
  const signature = await wrap(
    rpc,
    payer,
    splAta,
    ctokenAta,
    owner,
    mint,
    bn(500)
  );

  console.log("Wrapped 500 tokens to c-token ATA");
  console.log("Transaction:", signature);
}

main().catch(console.error);