Skip to content

brimigs/anchor-litesvm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

anchor-litesvm Workspace

Two powerful crates for Solana program testing with LiteSVM

Crate Description crates.io docs.rs
anchor-litesvm Anchor-specific testing with simplified syntax Crates.io docs.rs
litesvm-utils Framework-agnostic testing utilities Crates.io docs.rs

Which Crate Should I Use?

Use anchor-litesvm if:

  • You're testing Anchor programs
  • You want simplified syntax similar to anchor-client
  • You need Anchor account deserialization and event parsing

Use litesvm-utils if:

  • You're testing Native Solana, SPL, or non-Anchor programs
  • You want framework-agnostic utilities
  • You're building your own testing framework

Note: anchor-litesvm includes all of litesvm-utils, so Anchor users get everything automatically.

Crate Relationship

┌─────────────────────────────────────┐
│         anchor-litesvm              │
│  (Anchor-specific features)         │
│  • Simplified syntax                │
│  • Account deserialization          │
│  • Event parsing                    │
│  • Discriminator handling           │
└─────────────┬───────────────────────┘
              │ builds upon
              ▼
┌─────────────────────────────────────┐
│         litesvm-utils               │
│  (Framework-agnostic utilities)     │
│  • Account creation & funding       │
│  • Token operations                 │
│  • Transaction helpers              │
│  • Assertions                       │
│  • PDA derivation                   │
└─────────────┬───────────────────────┘
              │ uses
              ▼
┌─────────────────────────────────────┐
│           LiteSVM                   │
│  (Fast Solana VM for testing)       │
└─────────────────────────────────────┘

Quick Start

For Anchor Programs

[dev-dependencies]
anchor-litesvm = "0.4"
use anchor_litesvm::AnchorLiteSVM;
use litesvm_utils::{AssertionHelpers, TestHelpers};

anchor_lang::declare_program!(my_program);

#[test]
fn test_my_program() {
    // One-line setup
    let mut ctx = AnchorLiteSVM::build_with_program(
        my_program::ID,
        include_bytes!("../target/deploy/my_program.so"),
    );

    // Create accounts
    let user = ctx.svm.create_funded_account(10_000_000_000).unwrap();

    // Build instruction with simplified syntax
    let ix = ctx.program()
        .accounts(my_program::client::accounts::Initialize { user: user.pubkey(), .. })
        .args(my_program::client::args::Initialize { amount: 100 })
        .instruction()
        .unwrap();

    // Execute and verify
    ctx.execute_instruction(ix, &[&user]).unwrap().assert_success();
}

For Non-Anchor Programs

[dev-dependencies]
litesvm-utils = "0.4"
use litesvm_utils::{LiteSVMBuilder, TestHelpers, AssertionHelpers, TransactionHelpers};

#[test]
fn test_my_program() {
    // Setup
    let mut svm = LiteSVMBuilder::build_with_program(program_id, &program_bytes);

    // Create accounts and tokens
    let user = svm.create_funded_account(10_000_000_000).unwrap();
    let mint = svm.create_token_mint(&user, 9).unwrap();

    // Execute and verify
    let result = svm.send_instruction(ix, &[&user]).unwrap();
    result.assert_success();
    svm.assert_token_balance(&token_account, 1_000_000);
}

Why These Crates?

Metric Raw LiteSVM anchor-client anchor-litesvm
Lines of code 493 279 106
Setup lines 20+ 15+ 1
Token mint creation 30+ lines 20+ lines 1 line
Compilation Fast Slow Fast
Mock RPC needed No Yes No

Documentation

Examples

# Run examples
cargo run -p anchor-litesvm --example basic_usage
cargo run -p anchor-litesvm --example advanced_features

Testing

# Run all tests (63 total)
cargo test

# Run specific crate tests
cargo test -p anchor-litesvm    # 11 tests
cargo test -p litesvm-utils     # 52 tests

License

MIT License - see LICENSE for details.

Acknowledgments

Built on top of LiteSVM, a fast and lightweight Solana VM for testing.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages