Overview
This guide shows how to integrate Light SDK into Solana programs using different frameworks: Anchor, native Solana, and Pinocchio.Anchor Program Integration
Setup
Add Light SDK to your Anchor program’sCargo.toml:
Cargo.toml
Basic Structure
Here’s a complete example of an Anchor program with compressed accounts:lib.rs
Key Components
CPI Signer
CPI Signer
The
LIGHT_CPI_SIGNER is derived from your program ID and used to authorize CPI calls to the Light System Program.Instruction Decoder
Instruction Decoder
The
#[instruction_decoder] macro generates instruction discriminator functions for client-side instruction building.Account Derivation
Account Derivation
Derive deterministic addresses for compressed accounts using seed phrases:
CPI Accounts
CPI Accounts
Parse remaining accounts for Light System Program CPI:This handles:
- State Merkle trees
- Address Merkle trees
- System programs
- Account compression program
Native Solana Program Integration
Setup
Cargo.toml
Basic Structure
lib.rs
Token Operations Integration
Setup
Cargo.toml
Create Mint
Transfer Tokens
Compress and Decompress
CPI Context (Advanced)
CPI Context enables multiple programs to share a single validity proof in one instruction. This is useful for complex operations involving both tokens and custom compressed accounts.
Setup
Enable thecpi-context feature:
Cargo.toml
Example: Transfer Tokens + Update PDA
Testing
Setup Test Environment
Cargo.toml
Test Example
tests/integration.rs
Best Practices
Account Size Limits
Account Size Limits
Compressed account data is sent as instruction data, so keep account size reasonable:
- Recommended: < 1 KB per account
- Maximum: ~10 KB (instruction data limit)
- For larger data, use multiple accounts or reference on-chain storage
Address Derivation
Address Derivation
Use deterministic address derivation for predictable account addresses:
Error Handling
Error Handling
Handle Light SDK errors properly:
Validity Proof Reuse
Validity Proof Reuse
With CPI context, reuse validity proofs across multiple operations:
Common Patterns
Counter Program
User Profile
Escrow Account
Troubleshooting
Invalid Proof Error
Invalid Proof Error
Cause: Validity proof doesn’t match account stateSolution:
- Ensure proof includes all input account hashes
- Verify address tree matches derived addresses
- Check that account state hasn’t changed since proof generation
Account Not Found
Account Not Found
Cause: Compressed account doesn’t exist or wrong addressSolution:
- Use indexer to query existing accounts
- Verify address derivation seeds
- Check that account hasn’t been closed
Tree Full Error
Tree Full Error
Cause: State or address tree has reached capacitySolution:
- Use different tree (change
tree_index) - Trees are managed by protocol, typically shouldn’t happen
- Contact support if persistent
CPI Context Error
CPI Context Error
Cause: Mismatched CPI context configurationSolution:
- Enable
cpi-contextfeature on all dependencies - Ensure all CPIs in instruction use same context
- First CPI must write to context
- Subsequent CPIs must read from context
Next Steps
Program Examples
Explore complete program examples
Testing Guide
Learn how to test your programs
API Reference
Complete API documentation