litesvm_utils/
lib.rs

1//! # litesvm-utils
2//!
3//! Framework-agnostic testing utilities for LiteSVM that dramatically simplify Solana program testing.
4//!
5//! This crate provides essential helpers that work with **any** Solana program (not just Anchor):
6//! - Account creation and funding (one-liners)
7//! - Token operations (mints, accounts, minting)
8//! - Transaction execution with rich result analysis
9//! - Assertion helpers for testing account states
10//! - PDA derivation utilities
11//! - Clock and slot manipulation
12//!
13//! ## Why litesvm-utils?
14//!
15//! **Before (Raw LiteSVM):**
16//! ```rust,ignore
17//! // 30+ lines to create a token mint
18//! let mint = Keypair::new();
19//! let rent = svm.minimum_balance_for_rent_exemption(82);
20//! let create_account_ix = system_instruction::create_account(/*...*/);
21//! let init_mint_ix = spl_token::instruction::initialize_mint(/*...*/);
22//! // ... transaction building, signing, sending ...
23//! ```
24//!
25//! **After (litesvm-utils):**
26//! ```rust,ignore
27//! let mint = svm.create_token_mint(&authority, 9)?; // One line
28//! ```
29//!
30//! ## Features
31//!
32//! ### Test Account Helpers
33//! Create funded accounts, mints, and token accounts in single calls:
34//! ```rust,ignore
35//! let user = svm.create_funded_account(10_000_000_000)?;
36//! let accounts = svm.create_funded_accounts(5, 1_000_000_000)?;
37//! ```
38//!
39//! ### Token Operations
40//! One-line token operations without manual transaction building:
41//! ```rust,ignore
42//! let mint = svm.create_token_mint(&authority, 9)?;
43//! let token_account = svm.create_associated_token_account(&mint.pubkey(), &owner)?;
44//! svm.mint_to(&mint.pubkey(), &token_account, &authority, 1_000_000)?;
45//! ```
46//!
47//! ### Transaction Helpers
48//! Execute transactions with automatic result analysis:
49//! ```rust,ignore
50//! let result = svm.send_instruction(ix, &[&signer])?;
51//! result.assert_success();
52//! assert!(result.compute_units() < 200_000);
53//! ```
54//!
55//! ### Assertion Helpers
56//! Clean, readable test assertions:
57//! ```rust,ignore
58//! svm.assert_token_balance(&token_account, 1_000_000);
59//! svm.assert_sol_balance(&user.pubkey(), 10_000_000_000);
60//! svm.assert_account_exists(&pda);
61//! svm.assert_account_closed(&closed_account);
62//! ```
63//!
64//! ### PDA Utilities
65//! Convenient PDA derivation:
66//! ```rust,ignore
67//! let pda = svm.get_pda(&[b"vault", user.pubkey().as_ref()], &program_id);
68//! let (pda, bump) = svm.get_pda_with_bump(&[b"seed"], &program_id);
69//! ```
70//!
71//! ### Clock Manipulation
72//! Test time-based logic:
73//! ```rust,ignore
74//! let slot = svm.get_current_slot();
75//! svm.advance_slot(100);
76//! ```
77//!
78//! ## Quick Start
79//!
80//! ```rust,ignore
81//! use litesvm_utils::{LiteSVMBuilder, TestHelpers, AssertionHelpers, TransactionHelpers};
82//! use solana_program::pubkey::Pubkey;
83//!
84//! // 1. Initialize with one line
85//! let program_id = Pubkey::new_unique();
86//! let program_bytes = include_bytes!("../target/deploy/program.so");
87//! let mut svm = LiteSVMBuilder::build_with_program(program_id, program_bytes);
88//!
89//! // 2. Create test accounts in one line each
90//! let maker = svm.create_funded_account(10_000_000_000).unwrap();
91//! let taker = svm.create_funded_account(10_000_000_000).unwrap();
92//!
93//! // 3. Create token infrastructure
94//! let mint = svm.create_token_mint(&maker, 9).unwrap();
95//! let maker_ata = svm.create_associated_token_account(&mint.pubkey(), &maker).unwrap();
96//! svm.mint_to(&mint.pubkey(), &maker_ata, &maker, 1_000_000_000).unwrap();
97//!
98//! // 4. Execute instruction and analyze results
99//! let result = svm.send_instruction(ix, &[&maker]).unwrap();
100//! result.assert_success();
101//! assert!(result.has_log("Transfer complete"));
102//!
103//! // 5. Verify with clean assertions
104//! svm.assert_token_balance(&maker_ata, 1_000_000_000);
105//! svm.assert_sol_balance(&maker.pubkey(), 10_000_000_000);
106//! ```
107//!
108//! ## Complete Example
109//!
110//! ```rust,ignore
111//! use litesvm_utils::{LiteSVMBuilder, TestHelpers, AssertionHelpers, TransactionHelpers};
112//!
113//! #[test]
114//! fn test_token_transfer() {
115//!     // Setup
116//!     let mut svm = LiteSVMBuilder::build_with_program(program_id, program_bytes);
117//!
118//!     // Create accounts
119//!     let sender = svm.create_funded_account(10_000_000_000).unwrap();
120//!     let receiver = svm.create_funded_account(10_000_000_000).unwrap();
121//!
122//!     // Setup tokens
123//!     let mint = svm.create_token_mint(&sender, 9).unwrap();
124//!     let sender_ata = svm.create_associated_token_account(&mint.pubkey(), &sender).unwrap();
125//!     let receiver_ata = svm.create_associated_token_account(&mint.pubkey(), &receiver).unwrap();
126//!     svm.mint_to(&mint.pubkey(), &sender_ata, &sender, 1_000_000).unwrap();
127//!
128//!     // Execute transfer
129//!     let result = svm.send_instruction(transfer_ix, &[&sender]).unwrap();
130//!     result.assert_success();
131//!
132//!     // Verify
133//!     svm.assert_token_balance(&sender_ata, 500_000);
134//!     svm.assert_token_balance(&receiver_ata, 500_000);
135//! }
136//! ```
137//!
138//! ## Framework Agnostic
139//!
140//! Unlike `anchor-litesvm`, this crate works with **any** Solana program:
141//! - Native Solana programs
142//! - Anchor programs
143//! - Solana Program Library (SPL) programs
144//! - Custom frameworks
145//!
146//! ## Traits
147//!
148//! - [`TestHelpers`] - Account and token creation helpers
149//! - [`AssertionHelpers`] - Test assertion methods
150//! - [`TransactionHelpers`] - Transaction execution helpers
151//!
152//! ## Modules
153//!
154//! - [`assertions`] - Assertion helper implementations
155//! - [`builder`] - Test environment builders
156//! - [`test_helpers`] - Test helper implementations
157//! - [`transaction`] - Transaction execution and result analysis
158
159pub mod assertions;
160pub mod builder;
161pub mod test_helpers;
162pub mod transaction;
163
164// Re-export main types for convenience
165pub use assertions::AssertionHelpers;
166pub use builder::{LiteSVMBuilder, ProgramTestExt};
167pub use test_helpers::TestHelpers;
168pub use transaction::{TransactionError, TransactionHelpers, TransactionResult};
169
170// Re-export commonly used external types
171pub use litesvm::LiteSVM;
172pub use solana_program::pubkey::Pubkey;
173pub use solana_sdk::signature::Keypair;