ZK Compression
  • Introduction
    • Overview
    • Intro to Development
  • Event
    • 🗓️Event: 1000x Hackathon
  • Release Notes
    • JS - v0.21.0
  • Learn
    • In a Nutshell
    • Core Concepts
      • Compressed Account Model
      • State Trees
      • Validity Proofs
      • Lifecycle of a Transaction
      • Limitations
  • Developers
    • TypeScript Client
    • JSON RPC Methods
      • getCompressedAccount
      • getCompressedBalance
      • getCompressedTokenAccountBalance
      • getCompressedBalanceByOwner
      • getCompressedMintTokenHolders
      • getCompressedTokenBalancesByOwnerV2
      • getCompressedAccountsByOwner
      • getMultipleCompressedAccounts
      • getCompressedTokenAccountsByOwner
      • getCompressedTokenAccountsByDelegate
      • getTransactionWithCompressionInfo
      • getCompressedAccountProof
      • getMultipleCompressedAccountProofs
      • getMultipleNewAddressProofs
      • getValidityProof
      • getCompressionSignaturesForAccount
      • getCompressionSignaturesForAddress
      • getCompressionSignaturesForOwner
      • getCompressionSignaturesForTokenOwner
      • getLatestCompressionSignatures
      • getLatestNonVotingSignatures
      • getIndexerSlot
      • getIndexerHealth
    • Addresses and URLs
    • Creating Airdrops with Compressed Tokens
    • Using Token-2022
    • Add Compressed Token Support to Your Wallet
    • Create programs with the program-template
  • Node Operators
    • Run a Node
  • resources
    • Security
    • Privacy Policy
Powered by GitBook
On this page
  • Stateless.js API Reference Guide
  • Installation
  • Basics
  • Rpc
  • Quickstart
  • Starting the test-validator for Local Development
  • Creating and Sending Transactions
  • Creating Lookup Tables
  • Examples

Was this helpful?

  1. Developers

TypeScript Client

PreviousLimitationsNextJSON RPC Methods

Last updated 5 months ago

Was this helpful?

Stateless.js API Reference Guide

The lets you build Javascript clients that interact with the ZK Compression primitive via the .

Installation

Package Manager
Command

NPM

Yarn

Basics

Rpc

The Rpc connection is used to interact with the . It's a thin wrapper extending with compression-related endpoints, such as getCompressedAccount, getCompressedTokenBalancesByOwner, and more.

Example Usage with Devnet

const stateless = require("@lightprotocol/stateless.js");


/// Helius exposes Solana and compression RPC endpoints through a single URL
const RPC_ENDPOINT = "https://devnet.helius-rpc.com?api-key=<api_key>";
const COMPRESSION_RPC_ENDPOINT = RPC_ENDPOINT;
const PROVER_ENDPOINT = RPC_ENDPOINT
const connection: Rpc = createRpc(RPC_ENDPOINT, COMPRESSION_RPC_ENDPOINT, PROVER_ENDPOINT)

async function main() {
  let slot = await connection.getSlot();
  console.log(slot);

  let health = await connection.getIndexerHealth(slot);
  console.log(health);
  // "Ok"
}

main();

Quickstart

Starting the test-validator for Local Development

light test-validator 

The command above will start a single-node Solana cluster, an RPC node, and a prover node at ports 8899, 8784, and 3001.

Creating and Sending Transactions

Creating, Minting, and Transferring a Compressed Token

This example uses the compressed token program, which is built using ZK Compression and offers an SPL-compatible token layout.

import {
  LightSystemProgram,
  Rpc,
  confirmTx,
  createRpc,
} from "@lightprotocol/stateless.js";
import { createMint, mintTo, transfer } from "@lightprotocol/compressed-token";
import { Keypair } from "@solana/web3.js";

const payer = Keypair.generate();
const tokenRecipient = Keypair.generate();

/// Localnet 
const connection: Rpc = createRpc();

const main = async () => {
  /// Airdrop lamports to pay fees
  await confirmTx(
    connection,
    await connection.requestAirdrop(payer.publicKey, 10e9)
  );

  await confirmTx(
    connection,
    await connection.requestAirdrop(tokenRecipient.publicKey, 1e6)
  );

  /// Create a compressed token mint
  const { mint, transactionSignature } = await createMint(
    connection,
    payer,
    payer.publicKey,
    9 // Number of decimals
  );

  console.log(`create-mint  success! txId: ${transactionSignature}`);

  /// Mint compressed tokens to the payer's account
  const mintToTxId = await mintTo(
    connection,
    payer,
    mint,
    payer.publicKey, // Destination
    payer,
    1e9 // Amount
  );

  console.log(`Minted 1e9 tokens to ${payer.publicKey} was a success!`);
  console.log(`txId: ${mintToTxId}`);

  /// Transfer compressed tokens
  const transferTxId = await transfer(
    connection,
    payer,
    mint,
    7e8, // Amount
    payer, // Owner
    tokenRecipient.publicKey // To address
  );

  console.log(`Transfer of 7e8 ${mint} to ${tokenRecipient.publicKey} was a success!`);
  console.log(`txId: ${transferTxId}`);
};

main();

Creating Lookup Tables

import { Rpc, confirmTx, createRpc } from "@lightprotocol/stateless.js";
import { createTokenProgramLookupTable } from "@lightprotocol/compressed-token";
import { Keypair, PublicKey} from "@solana/web3.js";
import { RPC_ENDPOINT } from "./constants";
const payer = Keypair.generate();
const authority = payer;
const additionalTokenMints : PublicKey[] = [];
const additionalAccounts : PublicKey[] = [];

// Localnet
const connection: Rpc = createRpc();

const main = async () => {
  /// airdrop lamports to pay gas and rent
  await confirmTx(
    connection,
    await connection.requestAirdrop(payer.publicKey, 1e7)
  );

  /// Create LUT
  const { address } = await createTokenProgramLookupTable(
    connection,
    payer,
    authority,
    additionalTokenMints,
    additionalAccounts
  );

  console.log("Created lookup table:", address.toBase58());
};

main();

Examples

To get started building with examples, check out these GitHub repositories:

Visit the section for the full list of compression endpoints supported in Rpc .

For public networks, we provide for Light's common program IDs and accounts

npm install --save \
    @lightprotocol/stateless.js \
    @lightprotocol/compressed-token \
    @solana/web3.js \
    @lightprotocol/zk-compression-cli
yarn add \
    @lightprotocol/stateless.js \
    @solana/web3.js \
    @lightprotocol/zk-compression-cli
@lightprotocol/stateless.js library
ZK Compression JSON RPC API
Source Documentation
ZK Compression JSON RPC
Solana's web3.js Connection class
JSON RPC Methods
shared lookup tables
Web Example Client
Node Example Client
Token Escrow Program Example