# Start a local test validatorlight test-validator## ensure you have the Solana CLI accessible in your system PATH
Report incorrect code
Copy
Ask AI
// createRpc() defaults to local test validator endpointsimport { Rpc, createRpc,} from "@lightprotocol/stateless.js";const connection: Rpc = createRpc();async function main() { let slot = await connection.getSlot(); console.log(slot); let health = await connection.getIndexerHealth(slot); console.log(health); // "Ok"}main();
Replace <your-api-key> with your actual API key. Get your API key here, if you don’t have one yet.
Report incorrect code
Copy
Ask AI
import { createRpc } from "@lightprotocol/stateless.js";// Helius exposes Solana and Photon RPC endpoints through a single URLconst RPC_ENDPOINT = "https://devnet.helius-rpc.com?api-key=<your_api_key>";const connection = createRpc(RPC_ENDPOINT, RPC_ENDPOINT, RPC_ENDPOINT);console.log("Connection created!");console.log("RPC Endpoint:", RPC_ENDPOINT);
Run this script to transfer compressed tokens to a recipient!
transfer-compressed-tokens.ts
Report incorrect code
Copy
Ask AI
// 1. Setup funded payer and connect to local validator// 2. Create SPL mint and token pool for compression with initial tokens// 3. Call transfer() with mint, amount, owner, recipient// 4. Verify transferred tokens via getCompressedTokenAccountsByOwnerimport { Keypair, PublicKey } from '@solana/web3.js';import { createRpc } from '@lightprotocol/stateless.js';import { createMint, mintTo, transfer } from '@lightprotocol/compressed-token';async function transferCompressedTokens() { // Step 1: Setup funded payer and connect to local validator const rpc = createRpc(); // defaults to localhost:8899 const payer = Keypair.generate(); const airdropSignature = await rpc.requestAirdrop(payer.publicKey, 1000000000); // 1 SOL await rpc.confirmTransaction(airdropSignature); // Step 2a: Create SPL mint with token pool for compression const { mint, transactionSignature: mintCreateTx } = await createMint( rpc, payer, payer.publicKey, // mint authority 9 ); console.log("SPL mint with token pool for compression created"); console.log("Mint address:", mint.toBase58()); console.log("Create mint transaction:", mintCreateTx); // Step 2b: Create token owner and mint initial tokens const tokenOwner = Keypair.generate(); const initialMintAmount = 1_000_000_000; // 1 token with 9 decimals const mintToTx = await mintTo( rpc, payer, mint, // SPL mint with token pool for compression tokenOwner.publicKey, // recipient payer, // mint authority initialMintAmount ); console.log("\nCompressed Tokens minted:", initialMintAmount / 1_000_000_000, "tokens"); console.log("Mint tokens transaction:", mintToTx); // Generate recipient address and define transfer amount const recipient = Keypair.generate(); const transferAmount = 500_000_000; // 0.5 tokens // Step 3: Call transfer() with mint, amount, owner, recipient const transferTx = await transfer( rpc, payer, mint, // SPL mint with token pool for compression transferAmount, tokenOwner, // owner keypair recipient.publicKey // recipient address ); console.log("\nCompressed tokens transferred!"); console.log("From:", tokenOwner.publicKey.toBase58()); console.log("To:", recipient.publicKey.toBase58()); console.log("Amount transferred:", transferAmount / 1_000_000_000, "tokens"); console.log("Transfer transaction:", transferTx); // Step 4: Verify transferred tokens via getCompressedTokenAccountsByOwner const recipientAccounts = await rpc.getCompressedTokenAccountsByOwner( recipient.publicKey, { mint } ); // Check recipient received the tokens if (recipientAccounts.items.length > 0) { const receivedBalance = recipientAccounts.items[0].parsed.amount; } return { transferTransaction: transferTx, recipient: recipient.publicKey, amount: transferAmount };}transferCompressedTokens().catch(console.error);
Make sure the SPL mint has a token pool for compression. The script creates this token pool for you.For development, you can create a new mint with token pool via createMint() or add a token pool to an existing mint via createTokenPool().