The Rpc connection is used to interact with the ZK Compression JSON RPC. It's a thin wrapper extending Solana's web3.js Connection class. You can use Rpc to get compressed account info, build compression transactions, and use regular Connection methods such as confirm transactions, get account info, and more
Example Usage with Devnet
conststateless=require("@lightprotocol/stateless.js");/// Helius exposes Solana and compression RPC endpoints through a single URLconstRPC_ENDPOINT="https://devnet.helius-rpc.com?api-key=<api_key>";constCOMPRESSION_RPC_ENDPOINT=RPC_ENDPOINT;constconnection:Rpc=createRpc(RPC_ENDPOINT,COMPRESSION_RPC_ENDPOINT)asyncfunctionmain() {let slot =awaitconnection.getSlot();console.log(slot);let health =awaitconnection.getIndexerHealth(slot);console.log(health);// "Ok"}main();
The example above shows only a few of the methods on Rpc. Visit the JSON RPC Methods section for the full list of compression endpoints
Quickstart
Starting the test-validator for Local Development
lighttest-validator
The command above will start a single-node Solana cluster, an RPC node, and a prover node at ports 8899, 8784, and 3001, respectively
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";constpayer=Keypair.generate();consttokenRecipient=Keypair.generate();/// Localnet constconnection:Rpc=createRpc();constmain=async () => {/// Airdrop lamports to pay feesawaitconfirmTx( connection,awaitconnection.requestAirdrop(payer.publicKey,10e9) );awaitconfirmTx( connection,awaitconnection.requestAirdrop(tokenRecipient.publicKey,1e6) );/// Create a compressed token mintconst { mint,transactionSignature } =awaitcreateMint( connection, payer,payer.publicKey,9// Number of decimals );console.log(`create-mint success! txId: ${transactionSignature}`);/// Mint compressed tokens to the payer's accountconstmintToTxId=awaitmintTo( 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 tokensconsttransferTxId=awaittransfer( connection, payer, mint,7e8,// Amount payer,// OwnertokenRecipient.publicKey // To address );console.log(`Transfer of 7e8 ${mint} to ${tokenRecipient.publicKey} was a success!`);console.log(`txId: ${transferTxId}`);};main();
Compressing SOL
You can also directly interact with the Light system program to transfer compressed SOL and create compressed accounts and compressed PDAs.