LiteSVM Documentation

repository·master·Indexed 20 days ago

https://github.com/litesvm/litesvm

A fast, lightweight, in-process Solana VM for program developers to run tests faster than solana-test-validator or solana-program-test. It supports Rust and NodeJS, offering capabilities such as program loading, transaction simulation, time travel, and compute budget control. The ecosystem includes specialized crates: litesvm-token for SPL Token testing, litesvm-loader for upgradeable programs, litesvm-utils for test boilerplate reduction, anchor-litesvm for Anchor-native testing, and litesvm-cpi-tree for parsing and rendering Solana transaction logs as CPI call trees.

Tokens
31.1K
Snippets
99
Records
129
Agent score
71%

What's inside LiteSVM

  1. Write arbitrary account data

    master
    LiteSVM enables you to write any account data you want to the SVM state, even if that state would be impossible in a real environment (e.g., creating an account with a large balance without owning the mint keypair). This is useful for bypassing the need to manage complex setup or fake tokens in tests.
  2. When to use LiteSVM vs `solana-test-validator`

    master

    Choosing between LiteSVM and the standard solana-test-validator depends on your testing requirements:

    FeatureLiteSVMsolana-test-validator
    SpeedExtremely fastSlower
    ConvenienceHigh (programmatic control)Lower (requires RPC/CLI)
    RPC SupportLimited (focused on program/client logic)Full RPC support
    RealismSimulated environmentReal validator behavior

    Use LiteSVM when: You want to test program logic, client code, or complex state transitions quickly. Use solana-test-validator when: You need to call specific RPC methods not supported by LiteSVM or need to test behavior that depends on actual validator mechanics.

  3. Install litesvm-utils for test boilerplate reduction

    master

    The litesvm-utils crate provides three ergonomic traits to extend LiteSVM:

    • TestHelpers: Create funded accounts, token mints, ATAs, derive PDAs, and manipulate slots.
    • AssertionHelpers: One-liner assertions for account existence, ownership, SOL/token balances, and data length.
    • TransactionHelpers: Execute instructions and assert success/failure/error codes.

    It also includes a LiteSVMBuilder for fluent environment setup.

    cargo add --dev litesvm-utils
  4. Copy accounts from a live environment

    master

    You can bring real-world state into your LiteSVM tests using two methods:

    1. Manual CLI method: Use solana account <ACCOUNT_PUBKEY> --output-file <FILE_PATH> from the Solana CLI to save account data to a file, then load it into LiteSVM.
    2. Programmatic method: Use the Solana web3.js library to fetch account data from devnet/mainnet and pass that data directly into LiteSVM.
  5. Install anchor-litesvm for Anchor-native testing

    master

    The anchor-litesvm crate allows testing Anchor programs with syntax mirroring anchor-client but without RPC overhead. Key features include:

    • AnchorContext: Manages the LiteSVM instance, payer, and program. Use AnchorLiteSVM::build_with_program() for setup.
    • Program builder: Type-safe instruction building via accounts(), args(), and instruction() using types from declare_program!.
    • Account deserialization: Automatically handles Anchor accounts and PDAs, including discriminators.
    • Event parsing: Extracts and deserializes typed events from transaction logs.
    cargo add --dev anchor-litesvm
  6. Manipulate time with `setClock` and `warpToSlot`

    master

    LiteSVM allows you to control the Clock sysvar to test time-dependent logic (e.g., minting schedules or expiration dates).

    • Use svm.setClock() to dynamically overwrite the Clock sysvar (e.g., setting a specific unix_timestamp).
    • Use svm.warpToSlot() to jump the SVM state forward to a future slot.
  7. Install litesvm-loader for upgradeable programs

    master

    The litesvm-loader crate provides helpers for working with Solana's upgradeable BPF loader, handling the deployment flow (buffer account creation, chunked writing, and deployment) and changing the upgrade authority.

    cargo add --dev litesvm-loader
  8. Deploy programs using `addProgramFromFile`

    master

    To test your own Solana programs in LiteSVM, you can load a compiled program into the SVM instance using the addProgramFromFile method.

    If you need to pull an existing program from mainnet or devnet first, use the Solana CLI command: solana program dump <PROGRAM_ID> <FILE_PATH>.

    // Example usage (referencing the splLogging test pattern)
    // svm.addProgramFromFile(path_to_compiled_program_file);
  9. Install litesvm-token for SPL Token testing

    master

    The litesvm-token crate provides a builder-style API for testing SPL Token programs, including operations like CreateMint, CreateAssociatedTokenAccount, MintTo, Transfer, Burn, Approve, and authority management (SetAuthority, FreezeAccount, ThawAccount).

    cargo add --dev litesvm-token
  10. Use SPL Token account utilities in LiteSVM

    master

    The litesvm-token crate provides high-level utilities for interacting with SPL Token and Token-2022 programs within a LiteSVM environment. It re-exports standard SPL Token operations such as create_mint, mint_to, transfer, burn, and create_ata.

    Depending on the enabled features, it uses either the spl_token_interface or spl_token_2022_interface.