LiteSVM Documentation
repository·master·Indexed 20 days ago
https://github.com/litesvm/litesvmA 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.
What's inside LiteSVM
- 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.
When to use LiteSVM vs `solana-test-validator`
masterChoosing between LiteSVM and the standard
solana-test-validatordepends on your testing requirements:Feature LiteSVM solana-test-validatorSpeed Extremely fast Slower Convenience High (programmatic control) Lower (requires RPC/CLI) RPC Support Limited (focused on program/client logic) Full RPC support Realism Simulated environment Real validator behavior Use LiteSVM when: You want to test program logic, client code, or complex state transitions quickly. Use
solana-test-validatorwhen: You need to call specific RPC methods not supported by LiteSVM or need to test behavior that depends on actual validator mechanics.Install litesvm-utils for test boilerplate reduction
masterThe
litesvm-utilscrate provides three ergonomic traits to extendLiteSVM: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
LiteSVMBuilderfor fluent environment setup.cargo add --dev litesvm-utilsCopy accounts from a live environment
masterYou can bring real-world state into your LiteSVM tests using two methods:
- 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. - Programmatic method: Use the Solana web3.js library to fetch account data from devnet/mainnet and pass that data directly into LiteSVM.
- Manual CLI method: Use
Install anchor-litesvm for Anchor-native testing
masterThe
anchor-litesvmcrate allows testing Anchor programs with syntax mirroringanchor-clientbut without RPC overhead. Key features include:AnchorContext: Manages theLiteSVMinstance, payer, and program. UseAnchorLiteSVM::build_with_program()for setup.Programbuilder: Type-safe instruction building viaaccounts(),args(), andinstruction()using types fromdeclare_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-litesvmManipulate time with `setClock` and `warpToSlot`
masterLiteSVM allows you to control the
Clocksysvar to test time-dependent logic (e.g., minting schedules or expiration dates).- Use
svm.setClock()to dynamically overwrite theClocksysvar (e.g., setting a specificunix_timestamp). - Use
svm.warpToSlot()to jump the SVM state forward to a future slot.
- Use
Install LiteSVM for NodeJS
masterTo use the LiteSVM NodeJS wrapper in your project, install the
litesvmpackage using yarn.yarn add litesvmInstall litesvm-loader for upgradeable programs
masterThe
litesvm-loadercrate 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-loaderDeploy programs using `addProgramFromFile`
masterTo test your own Solana programs in LiteSVM, you can load a compiled program into the SVM instance using the
addProgramFromFilemethod.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);Install litesvm-token for SPL Token testing
masterThe
litesvm-tokencrate provides a builder-style API for testing SPL Token programs, including operations likeCreateMint,CreateAssociatedTokenAccount,MintTo,Transfer,Burn,Approve, and authority management (SetAuthority,FreezeAccount,ThawAccount).cargo add --dev litesvm-tokenInstall LiteSVM
masterTo use LiteSVM in your Rust development environment, add it as a development dependency using cargo:
cargo add --dev litesvmUse SPL Token account utilities in LiteSVM
masterThe
litesvm-tokencrate 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 ascreate_mint,mint_to,transfer,burn, andcreate_ata.Depending on the enabled features, it uses either the
spl_token_interfaceorspl_token_2022_interface.