Installation
Addmollusk-svm as a dependency in Cargo.toml:
Terminal
Cargo.toml
solana-sdk, solana-sdk-ids, and the
System Program interface. Pin the versions that match Mollusk 0.7:
Terminal
Cargo.toml
Keep the
@0.7 pin on every mollusk-svm crate. cargo add mollusk-svm
without a version installs a newer Mollusk release that doesn’t compile with
these Solana crate versions.mollusk-svm-bencher as dependency in
Cargo.toml:
Terminal
Cargo.toml
mollusk-svm-programs-token as
dependency in Cargo.toml:
Terminal
Cargo.toml
Mollusk SVM
The following example shows a minimal setup for testing a basic Solana program using Mollusk.1
Hello World Program
This example demonstrates how to test a basic Solana program using Mollusk. The
program simply prints “Hello, world!” to the program logs when invoked.Running To test a Solana program with Mollusk:To execute the test, run
cargo build-sbf --arch v0 generates the compiled program at
/target/deploy/<program_name>.so. Build with --arch v0: Mollusk
0.7 cannot load programs built with the current default SBPF v3 architecture
and cargo test fails with FailedToParse("invalid file header").src/lib.rs
- Create a
Molluskinstance - Initialize Mollusk with a program ID and the path to the compiled program (.sofile) - Build an instruction - Create an instruction to invoke the program
- Process and validate - Process the instruction using Mollusk and validate the result
src/lib.rs
cargo build-sbf --arch v0 and then cargo test.When the test runs successfully, you’ll see output similar to the following:Terminal
Cargo.toml
target/deploy/hello_world.so
Mollusk
struct provides a simple interface for testing Solana programs. All fields can
be manipulated through a handful of helper methods, but users can also directly
access and modify them if they desire more control.
To initialize Mollusk with a default instance use the Mollusk::default method.
Example
Mollusk::new method.
Example
Mollusk::add_program method.
Example
When providing the file path, do not include the
.so extension. For example,
"path/to/my_program" is correct, but "path/to/my_program.so" is not.Processing Instructions
Mollusk provides four main methods for processing instructions:| Method | Description |
|---|---|
process_instruction | Process an instruction and return the result. |
process_and_validate_instruction | Process an instruction and perform a series of checks on the result, panicking if any checks fail. |
process_instruction_chain | Process multiple instructions and return the result. |
process_and_validate_instruction_chain | Process multiple instructions and perform a series of checks on each result, panicking if any checks fail. |
InstructionResult
contains the details of a processed instruction.
Single Instruction
Use theprocess_instruction method to process a single instruction without
checks on the result. You can manually validate the results after processing.
Method Signature
The examples below run Mollusk in the
main function for demonstration
purposes. Because mollusk-svm is a dev-dependency, save each example as
examples/<name>.rs and run it with cargo run --example <name>. The
main function in src/main.rs cannot import dev-dependencies. In practice,
you’ll typically use Mollusk in a test module annotated with the #[test]
attribute.Single Instruction
Single Instruction with Checks
Use theprocess_and_validate_instruction method to process a single
instruction with validation checks. This method will panic if any check fails.
Method Signature
With Validation
Multiple Instructions
Use theprocess_instruction_chain method to process multiple instructions
sequentially without validation checks.
Method Signature
Multiple Instructions
Multiple Instructions with Checks
Use theprocess_and_validate_instruction_chain method to process multiple
instructions with validation checks after each instruction. Each instruction has
its own set of checks that must pass.
Method Signature
With Validation
Validation Checks
Mollusk provides a set of helper methods to check the results of a processed instruction.Example
Example
Example
Persistent Account State
TheMolluskContext
is a wrapper around Mollusk that maintains account state across multiple
instruction calls through its account_store. The methods for processing
instructions are identical to Mollusk.
Unlike Mollusk, which requires passing accounts to each method (e.g.
process_instruction), MolluskContext manages accounts internally through its
account_store. This eliminates the need for the accounts parameter when
processing instructions.
Create an account_store using the with_context method:
Example
account_store.
Stateful Testing
Mollusk Sysvars
Mollusk provides a customSysvars
struct for modifying its values for testing.
Use the warp_to_slot method to update the sysvar clock to simulate moving
forward or backward in time to a specific slot.
Warp to Slot
sysvars field to change the rent parameters. You can modify
other sysvar values the same way.
Modify Sysvars
Compute Unit Benchmarking
MolluskComputeUnitBencher
tracks compute unit usage of a program’s instructions. Results are written to a
markdown file.
Requires
mollusk-svm-bencher
as a dependency.
The following example benchmarks the compute unit usage of a SOL transfer
instruction.
Benchmark Compute Units
benches/compute_units.rs and register it in
Cargo.toml with the default test harness disabled:
Cargo.toml
cargo bench. You can also save the same file under examples/ and
run it with cargo run --example compute_units. The results are written to the
specified out_dir as a markdown file named compute_units.md.
compute_units.md
Token Program Testing
Use themollusk-svm-programs-token
crate add the token program, token2022 program (token extensions), and
associated token program to Mollusk for testing.
Example
The example below manually defines the test accounts for demonstration
purposes. The
mollusk-svm-programs-token also includes helper functions to
create the mint and token accounts.Token Transfer Example