Expand description
§solana-accountgen
A utility crate for generating mock Solana accounts for testing purposes.
§Features
- Create accounts with custom balances, owners, and data using a fluent API
- Serialize account data using Borsh (with JSON support for the bincode module)
- Support for creating PDAs (Program Derived Addresses)
- Integration with solana-program-test for end-to-end testing
- Support for Anchor programs with discriminator handling
§Example
use solana_accountgen::AccountBuilder;
use solana_pubkey::Pubkey;
use borsh::{BorshSerialize, BorshDeserialize};
#[derive(BorshSerialize, BorshDeserialize)]
struct MyData { value: u64 }
let program_id = Pubkey::new_unique();
let account = AccountBuilder::new()
.balance(100_000_000)
.owner(program_id)
.data(MyData { value: 42 })
.unwrap()
.build();§Anchor Program Testing
solana-accountgen provides special support for testing Anchor programs:
use solana_accountgen::extensions::anchor::{create_anchor_account, create_anchor_instruction};
use solana_pubkey::Pubkey;
use borsh::{BorshSerialize, BorshDeserialize};
use solana_instruction::AccountMeta;
#[derive(BorshSerialize, BorshDeserialize)]
struct GameState {
player: Pubkey,
score: u64,
}
// Create an account with Anchor's discriminator
let program_id = Pubkey::new_unique();
let player = Pubkey::new_unique();
let game_state = GameState {
player,
score: 100,
};
let account = create_anchor_account(
"game", // Account type name in Anchor program
program_id,
game_state,
100_000, // lamports
).unwrap();
// Create an instruction with Anchor's method discriminator
let ix = create_anchor_instruction(
program_id,
"update_score", // Method name in Anchor program
vec![
AccountMeta::new(Pubkey::new_unique(), false),
AccountMeta::new_readonly(player, true),
],
42u64, // Instruction data
).unwrap();Re-exports§
pub use borsh;
Modules§
- extensions
- Extensions for specific account types and testing scenarios.
- serialization
- Serialization support for different formats.
Structs§
- Account
Builder - A builder for creating mock Solana accounts for testing purposes.
- Account
Map - A collection of accounts indexed by their pubkeys.
Enums§
- Account
GenError - Errors that can occur when using the AccountBuilder.
Functions§
- create_
account - Creates an account with the given pubkey and properties.
- create_
accounts - Creates multiple accounts with their pubkeys.