solana-development
by @tenequm
Build, test, deploy, and audit Solana programs with Anchor or native Rust, and build with ZK Compression (Light Protocol). Use when developing Solana smart c...
clawhub install solana-developmentπ About This Skill
name: solana-development description: Build Solana programs with Anchor framework or native Rust. Use when developing Solana smart contracts, implementing token operations, testing programs, deploying to networks, or working with Solana development. Covers both high-level Anchor framework (recommended) and low-level native Rust for advanced use cases. metadata: version: "0.6.1"
Solana Development
Build Solana programs using Anchor framework or native Rust. Both approaches share the same core concepts (accounts, PDAs, CPIs, tokens) but differ in syntax and abstraction level.
Quick Start
Recommended: Anchor Framework
Anchor provides macros and tooling that reduce boilerplate and increase developer productivity:
use anchor_lang::prelude::*;declare_id!("YourProgramID");
#[program]
pub mod my_program {
use super::*;
pub fn initialize(ctx: Context, data: u64) -> Result<()> {
ctx.accounts.account.data = data;
Ok(())
}
}
#[derive(Accounts)]
pub struct Initialize<'info> {
#[account(init, payer = user, space = 8 + 8)]
pub account: Account<'info, MyAccount>,
#[account(mut)]
pub user: Signer<'info>,
pub system_program: Program<'info, System>,
}
#[account]
pub struct MyAccount {
pub data: u64,
}
When to use Anchor:
Installation:
cargo install --git https://github.com/coral-xyz/anchor avm --locked --force
avm install latest
avm use latest
anchor --version
Create project:
anchor init my_project
cd my_project
anchor build
anchor test
β See references/anchor.md for complete Anchor guide
Advanced: Native Rust
Native Rust provides maximum control, optimization potential, and deeper understanding of Solana's runtime:
use solana_program::{
account_info::AccountInfo,
entrypoint,
entrypoint::ProgramResult,
pubkey::Pubkey,
msg,
};entrypoint!(process_instruction);
pub fn process_instruction(
program_id: &Pubkey,
accounts: &[AccountInfo],
instruction_data: &[u8],
) -> ProgramResult {
msg!("Processing instruction");
// Manual account parsing and validation
// Manual instruction routing
Ok(())
}
When to use Native Rust:
Setup:
cargo new my_program --lib
cd my_program
Configure Cargo.toml (see native-rust.md)
cargo build-sbf
β See references/native-rust.md for complete native Rust guide
Core Concepts
Essential knowledge for all Solana developers, regardless of framework:
Foundational Concepts
Program Integration
Client-Side Development
Implementation Details
Advanced Features
Low-Level Details
Resources
Common Tasks Quick Reference
Create a new program:
anchor init my_project β anchor.md#getting-startedcargo new my_program --lib β native-rust.md#setupInitialize a PDA account:
#[account(init, seeds = [...], bump)] β pda.md#anchorinvoke_signed with System Program β pda.md#nativeTransfer SPL tokens:
anchor_spl::token::transfer β tokens-operations.md#transferring-tokensTest your program:
anchor test for integration tests β testing-frameworks.md#anchor-specific-testingLocal development with mainnet forking:
surfpool start for mainnet-forked local network β surfpool.mdTest with mainnet state (Jupiter, Raydium CPIs):
Profile compute units in detail:
surfnet_profileTransaction cheatcode β surfpool.md#transaction-profilingDeploy to devnet:
anchor deploy β deployment.md#anchorsolana program deploy β deployment.md#nativeDeploy to production (verified builds):
solana-verify build + solana program deploy β production-deployment.mdOptimize compute units:
Handle 40+ accounts:
Offline transaction signing:
Decision Guide
| Your Need | Recommended Approach | Reason | |-----------|---------------------|---------| | Standard DeFi/NFT program | Anchor | Faster development, proven patterns | | TypeScript client needed | Anchor | Auto-generates IDL and client types | | Learning Solana fundamentals | Native Rust first | Understand the platform deeply | | Compute optimization critical | Native Rust | Direct control, minimal overhead | | Advanced tx features (ALTs, nonces) | Either (slight edge to Native) | Framework-agnostic features | | Fast prototyping | Anchor | Less boilerplate, faster iteration | | Maximum control over every detail | Native Rust | No abstraction layer | | Team familiar with frameworks | Anchor | Lower learning curve | | Program size matters | Native Rust | Smaller compiled programs |
Note: You can also start with Anchor for rapid development, then optimize critical paths with native Rust patterns if needed.
Framework Comparison
| Aspect | Anchor | Native Rust |
|--------|--------|-------------|
| Setup complexity | Simple (anchor init) | Manual (Cargo.toml, entrypoint) |
| Boilerplate | Minimal (macros handle it) | Significant (manual everything) |
| Account validation | Declarative (#[account(...)]) | Manual (explicit checks) |
| Serialization | Automatic (Borsh via macros) | Manual (Borsh or custom) |
| Type safety | High (compile-time checks) | High (but more verbose) |
| IDL generation | Automatic | Manual or tools |
| Client library | TypeScript + Rust auto-gen | Manual client code |
| Testing | anchor test, Mollusk | Mollusk, cargo test |
| Deployment | anchor deploy | solana program deploy |
| Compute overhead | Small (~1-3% typical) | None (direct) |
| Program size | Slightly larger | Smaller |
| Learning curve | Gentler (abstractions help) | Steeper (need SVM knowledge) |
| Debugging | Good (clear macro errors) | More complex (lower level) |
| Community | Large (most use Anchor) | Growing (optimization focus) |
Typical Development Workflow
Anchor Workflow
1. Init: anchor init my_project
2. Define accounts: Use #[derive(Accounts)] with constraints
3. Implement instructions: Write functions in #[program] module
4. Define state: Use #[account] macro for account structures
5. Test: Write tests in tests/, run anchor test
6. Deploy: anchor deploy to configured network
7. Client: Import generated IDL and types in TypeScript/Rust
Native Rust Workflow
1. Setup: cargo new my_program --lib, configure Cargo.toml
2. Define entrypoint: Implement process_instruction function
3. Define state: Manual Borsh serialization structs
4. Implement instructions: Manual routing and account parsing
5. Validate accounts: Explicit ownership, signer, writable checks
6. Test: Write Mollusk tests, run cargo test
7. Build: cargo build-sbf
8. Deploy: solana program deploy target/deploy/program.so
9. Client: Build client manually or use web3.js/rs
Best Practices
General (Both Approaches)
β
Always validate accounts: Check ownership, signers, mutability
β
Use checked arithmetic: .checked_add(), .checked_sub(), etc.
β
Test extensively: Unit tests, integration tests, edge cases
β
Handle errors gracefully: Return descriptive errors
β
Document your code: Explain account requirements and constraints
β
Version your programs: Plan for upgrades and migrations
β
Use PDAs for program-owned accounts: Don't pass private keys
β
Minimize compute units: Profile and optimize hot paths
β
Add security.txt: Make it easy for researchers to contact you
Anchor-Specific
β
Use InitSpace derive: Auto-calculate account space
β
Prefer has_one constraints: Clearer than custom constraints
β
Use Program<'info, T>: Validate program accounts in CPIs
β
Emit events: Use emit! for important state changes
β
Group related constraints: Keep account validation readable
Native Rust-Specific
β
Use next_account_info: Safe account iteration
β
Cache PDA bumps: Store bump in account, use create_program_address
β
Zero-copy when possible: 50%+ CU savings for large structs
β
Minimize logging: Especially avoid pubkey formatting (expensive)
β
Build verifiable: Use solana-verify build for production
Security Considerations
Both frameworks require security vigilance:
β οΈ Common vulnerabilities:
β For defensive programming patterns and secure coding practices, see security.md
That guide provides:
β For comprehensive security audits, use the solana-security skill
That skill provides:
When to Switch or Combine
Start with Anchor, optimize later:
Start with Native, add Anchor features:
Use both in a workspace:
[workspace]
members = [
"programs/core", # Native Rust
"programs/wrapper", # Anchor facade
]
Getting Help
Next Steps
New to Solana? 1. Read accounts.md - Understand the account model 2. Read anchor.md - Start with Anchor framework 3. Read security.md - Learn secure coding from the start 4. Build a simple program following testing-overview.md 5. Deploy to devnet using deployment.md
Coming from another blockchain? 1. Read accounts.md - Solana's model is different 2. Read pda.md - Unique to Solana 3. Choose Anchor for familiar framework experience 4. Explore resources.md for migration guides
Want to optimize? 1. Start with working Anchor program 2. Profile with compute-optimization.md 3. Learn native patterns from native-rust.md 4. Refactor bottlenecks selectively
Building production apps?
1. Master security considerations
2. Use testing-practices.md for comprehensive best practices
3. Follow production-deployment.md for verified builds
4. Get security audit with solana-security skill
π‘ Examples
Recommended: Anchor Framework
Anchor provides macros and tooling that reduce boilerplate and increase developer productivity:
use anchor_lang::prelude::*;declare_id!("YourProgramID");
#[program]
pub mod my_program {
use super::*;
pub fn initialize(ctx: Context, data: u64) -> Result<()> {
ctx.accounts.account.data = data;
Ok(())
}
}
#[derive(Accounts)]
pub struct Initialize<'info> {
#[account(init, payer = user, space = 8 + 8)]
pub account: Account<'info, MyAccount>,
#[account(mut)]
pub user: Signer<'info>,
pub system_program: Program<'info, System>,
}
#[account]
pub struct MyAccount {
pub data: u64,
}
When to use Anchor:
Installation:
cargo install --git https://github.com/coral-xyz/anchor avm --locked --force
avm install latest
avm use latest
anchor --version
Create project:
anchor init my_project
cd my_project
anchor build
anchor test
β See references/anchor.md for complete Anchor guide
Advanced: Native Rust
Native Rust provides maximum control, optimization potential, and deeper understanding of Solana's runtime:
use solana_program::{
account_info::AccountInfo,
entrypoint,
entrypoint::ProgramResult,
pubkey::Pubkey,
msg,
};entrypoint!(process_instruction);
pub fn process_instruction(
program_id: &Pubkey,
accounts: &[AccountInfo],
instruction_data: &[u8],
) -> ProgramResult {
msg!("Processing instruction");
// Manual account parsing and validation
// Manual instruction routing
Ok(())
}
When to use Native Rust:
Setup:
cargo new my_program --lib
cd my_program
Configure Cargo.toml (see native-rust.md)
cargo build-sbf
β See references/native-rust.md for complete native Rust guide
π Tips & Best Practices
General (Both Approaches)
β
Always validate accounts: Check ownership, signers, mutability
β
Use checked arithmetic: .checked_add(), .checked_sub(), etc.
β
Test extensively: Unit tests, integration tests, edge cases
β
Handle errors gracefully: Return descriptive errors
β
Document your code: Explain account requirements and constraints
β
Version your programs: Plan for upgrades and migrations
β
Use PDAs for program-owned accounts: Don't pass private keys
β
Minimize compute units: Profile and optimize hot paths
β
Add security.txt: Make it easy for researchers to contact you
Anchor-Specific
β
Use InitSpace derive: Auto-calculate account space
β
Prefer has_one constraints: Clearer than custom constraints
β
Use Program<'info, T>: Validate program accounts in CPIs
β
Emit events: Use emit! for important state changes
β
Group related constraints: Keep account validation readable
Native Rust-Specific
β
Use next_account_info: Safe account iteration
β
Cache PDA bumps: Store bump in account, use create_program_address
β
Zero-copy when possible: 50%+ CU savings for large structs
β
Minimize logging: Especially avoid pubkey formatting (expensive)
β
Build verifiable: Use solana-verify build for production