solana-test-framework

solana-test-framework is build on top of the solana-program-test crate and it provides a BanksClient-based Proof of Concept framework for BPF programs. It extends BanksClient, ProgramTest and ProgramTestContext with several convenience methods.

   

Setup

This framework supports: - Solana v1.9 and Anchor v0.24.2 - Solana v1.10 and Anchor v0.25.0 - Solana v1.14 and Anchor v0.26.0

To use it in your project,

  1. add one of the following in your Cargo.toml:

    • Solana ~1.9: solana-test-framework = { git = "https://github.com/halbornlabs/solana-test-framework"}
    • Solana ~1.10: solana-test-framework = { git = "https://github.com/halbornlabs/solana-test-framework", branch = "solana1.10" }
    • Solana ~1.14: solana-test-framework = { git = "https://github.com/halbornlabs/solana-test-framework", branch = "solana1.14" }
  2. include features = ["anchor"] in your dependency declaration if you want to enable Anchor convenience methods

 

Docs

BanksClient extensions

Assemble the given instructions into a transaction and sign it. All transactions created with this method are signed and payed for by the payer.

async fn transaction_from_instructions(
    &mut self,
    ixs: &[Instruction],
    payer: &Keypair,
    signers: Vec<&Keypair>
) -> Result<Transaction, BanksClientError>

 

Return and deserialize an Anchor account at the given address at the time of the most recent root slot. If the account is not found, None is returned.

#[cfg(feature = "anchor")]
fn get_account_with_anchor<T: AccountDeserialize>(
    &mut self,
    address: Pubkey
) -> Pin<Box<dyn Future<Output = Result<T, BanksClientError>> + '_>>

 

Return and deserialize a Borsh account at the given address at the time of the most recent root slot. If the account is not found, None is returned.

fn get_account_with_borsh<T: BorshDeserialize>(
    &mut self,
    address: Pubkey
) -> Pin<Box<dyn Future<Output = Result<T, BanksClientError>> + '_>>

 

Create a new account.

async fn create_account(
    &mut self,
    from: &Keypair,
    to: &Keypair,
    lamports: u64,
    space: u64,
    owner: Pubkey
) -> transport::Result<Pubkey> {

 

Create a new SPL Token Mint account.

async fn create_token_mint(
    &mut self,
    mint: &Keypair,
    authority: &Pubkey,
    freeze_authority: Option<&Pubkey>,
    decimals: u8,
    payer: &Keypair
) -> transport::Result<Pubkey> {

 

Create a new SPL Token Account.

async fn create_token_account(
    &mut self,
    account: &Keypair,
    authority: &Pubkey,
    mint: &Pubkey,
    payer: &Keypair
) -> transport::Result<Pubkey> {

 

Create a new SPL Associated Token account

async fn create_associated_token_account(
    &mut self,
    authority: &Pubkey,
    mint: &Pubkey,
    payer: &Keypair
) -> transport::Result<Pubkey> {

 

ProgramTest extensions

Add a rent-exempt account with some data to the test environment.

pub fn add_account_with_data(
    &mut self,
    pubkey: Pubkey,
    owner: Pubkey,
    data: &[u8],
    executable: bool,
)

 

Add an Anchor account to the test environment.

#[cfg(feature = "anchor")]
pub fn add_account_with_anchor<T: AccountSerialize + AnchorSerialize + Discriminator>(
    &mut self,
    pubkey: Pubkey,
    owner: Pubkey,
    anchor_data: T,
    executable: bool,
)

 

Add an account with the given balance to the test environment.

pub fn add_account_with_lamports(
    &mut self,
    pubkey: Pubkey,
    owner: Pubkey,
    lamports: u64,
)

 

Add a rent-exempt account with some Packable data to the test environment.

pub fn add_account_with_packable<P: Pack>(
    &mut self,
    pubkey: Pubkey,
    owner: Pubkey,
    data: P,
)

 

Add a rent-exempt account with some Borsh-serializable to the test environment

pub fn add_account_with_borsh<B: BorshSerialize>(
    &mut self,
    pubkey: Pubkey,
    owner: Pubkey,
    data: B
)

 

Generate and add multiple accounts to the test environment.

pub fn generate_accounts(
    &mut self,
    number_of_accounts: u8,
    initial_lamports: u64) -> Vec<Keypair>

 

Add an SPL Token Mint account to the test environment.

pub fn add_token_mint(
   &mut self,
   pubkey: Pubkey,
   mint_authority: Option<Pubkey>,
   supply: u64,
   decimals: u8,
   freeze_authority: Option<Pubkey>,
)

 

Add an SPL Token Account to the test environment.

fn add_token_account(
    &mut self,
    pubkey: Pubkey,
    mint: Pubkey,
    owner: Pubkey,
    amount: u64,
    delegate: Option<Pubkey>,
    is_native: Option<u64>,
    delegated_amount: u64,
    close_authority: Option<Pubkey>
)

 

Add an associated SPL Token account to the test environment. Returns the address of the created account.

fn add_associated_token_account(
    &mut self,
    mint: Pubkey,
    owner: Pubkey,
    amount: u64,
    delegate: Option<Pubkey>,
    is_native: Option<u64>,
    delegated_amount: u64,
    close_authority: Option<Pubkey>
) -> Pubkey

 

ProgramTestContext extensions

Advance the internal clock to the provided timestamp.

async fn warp_to_timestamp(
  &mut self,
  timestamp: i64
) -> Result<(), ProgramTestError>

 

Deploy program

async fn deploy_program(
    &mut self,
    path_to_program: &str,
    program_keypair: &Keypair,
    payer: &Keypair,
) -> transport::Result<()>

 

Deploy upgradable program

async fn deploy_upgradable_program(
    &mut self,
    path_to_program: &str,
    buffer_keypair: &Keypair,
    buffer_authority_signer: &Keypair,
    program_keypair: &Keypair,
    payer: &Keypair,
) -> transport::Result<()>