LiteSVM Docs
Additional Crateslitesvm-utils

Quick Start

Learn how to use the litesvm-utils crate for streamlined Solana program testing

Installation

Make sure you have all the needed dependencies:

cargo add --dev litesvm litesvm-utils solana-sdk

What is litesvm-utils?

The litesvm-utils crate provides essential helper traits and utilities that simplify Solana program testing. It reduces common testing boilerplate from 30+ lines to minimal, readable code through ergonomic APIs.

TestHelpers Trait

  • Create funded accounts with a single method call
  • Set up token mints and token accounts easily
  • Derive PDAs without boilerplate
  • Manage slots for time-based testing

AssertionHelpers Trait

  • Assert account states (exists, closed, owner)
  • Verify token balances and mint supplies
  • Check SOL balances
  • Validate account data lengths

TransactionHelpers Trait

  • Execute instructions with rich result handling
  • Assert transaction success or failure
  • Check for specific error codes
  • Inspect transaction logs and compute units

LiteSVMBuilder

  • Fluent builder pattern for test environment setup
  • Deploy programs with method chaining
  • Convenient static factory methods

Quick Example

Here's a complete example showing the power of litesvm-utils:

use litesvm::LiteSVM;
use litesvm_utils::{TestHelpers, AssertionHelpers, TransactionHelpers};
use solana_sdk::{
    native_token::LAMPORTS_PER_SOL,
    signature::Signer,
    system_instruction,
};

#[test]
fn test_with_utils() {
    let mut svm = LiteSVM::new();

    // Create funded accounts in one line
    let alice = svm.create_funded_account(10 * LAMPORTS_PER_SOL);
    let bob = svm.create_funded_account(0);

    // Create a token mint easily
    let mint = svm.create_token_mint(&alice, 9);

    // Create token accounts
    let alice_ata = svm.create_associated_token_account(&mint, &alice);
    let bob_ata = svm.create_associated_token_account(&mint, &bob);

    // Mint tokens
    svm.mint_to(&mint, &alice_ata, &alice, 1000);

    // Assert balances
    svm.assert_token_balance(&alice_ata, 1000);
    svm.assert_token_balance(&bob_ata, 0);

    // Execute a SOL transfer with rich result handling
    let transfer_ix = system_instruction::transfer(
        &alice.pubkey(),
        &bob.pubkey(),
        LAMPORTS_PER_SOL,
    );

    let result = svm.send_instruction(transfer_ix, &[&alice]);
    result.assert_success();

    // Verify the transfer
    svm.assert_sol_balance(&bob.pubkey(), LAMPORTS_PER_SOL);
}

Key Benefits

Before litesvm-utils

// Creating a funded account manually
let keypair = Keypair::new();
let airdrop_tx = Transaction::new_signed_with_payer(
    &[system_instruction::transfer(
        &payer.pubkey(),
        &keypair.pubkey(),
        lamports,
    )],
    Some(&payer.pubkey()),
    &[&payer],
    svm.latest_blockhash(),
);
svm.send_transaction(airdrop_tx).unwrap();

After litesvm-utils

// One line to create a funded account
let keypair = svm.create_funded_account(lamports);

Trait Overview

TestHelpers

MethodDescription
create_funded_account(lamports)Creates a keypair and funds it
create_funded_accounts(count, lamports)Creates multiple funded keypairs
create_token_mint(authority, decimals)Creates an SPL token mint
create_token_account(mint, owner)Creates a token account
create_associated_token_account(mint, owner)Creates an ATA
mint_to(mint, account, authority, amount)Mints tokens to an account
derive_pda(seeds, program_id)Derives a PDA with bump
get_pda(seeds, program_id)Gets just the PDA pubkey
get_current_slot()Returns current slot
advance_slot(slots)Advances time by slots

AssertionHelpers

MethodDescription
assert_account_exists(pubkey)Asserts account exists
assert_account_closed(pubkey)Asserts account is closed
assert_token_balance(account, amount)Verifies token balance
assert_sol_balance(pubkey, lamports)Verifies SOL balance
assert_mint_supply(mint, supply)Verifies mint total supply
assert_account_owner(pubkey, owner)Verifies account owner
assert_account_data_len(pubkey, len)Verifies data length

TransactionHelpers

MethodDescription
send_instruction(ix, signers)Sends a single instruction
send_instructions(ixs, signers)Sends multiple instructions
send_transaction_result(tx)Sends transaction with result wrapper

Troubleshooting

Common Errors

ErrorCauseSolution
AccountNotFoundTrying to use an account that doesn't existUse create_funded_account first
InsufficientFundsNot enough lamports for transactionIncrease initial funding amount
OwnerMismatchAccount owned by wrong programVerify correct program ID
AssertionFailedBalance or state doesn't match expectedCheck your test logic