Superfast Solana Tests for Node.js

Overview

Bankrun

Bankrun is a superfast, powerful and lightweight framework for testing Solana programs in NodeJS.

While people often use solana-test-validator for this, bankrun is orders of magnitude faster and far more convenient.

You can also do things that are not possible with solana-test-validator, such as jumping back and forth in time or dynamically setting account data.

If you've used solana-program-test you'll be familiar with bankrun, since that's what it uses under the hood.

For those unfamiliar, bankrun and solana-program-test work by spinning up a lightweight BanksServer that's like an RPC node but much faster, and creating a BanksClient to talk to the server. This author thought solana-program-test was a boring name, so he chose bankrun instead (you're running Solana Banks).

Minimal example

This example just transfers lamports from Alice to Bob without loading any programs of our own. It uses the jest test runner but you can use any test runner you like.

Note: If you have multiple test files you should disable parallel tests using the --runInBand Jest flag for now. There is an open issue where concurrent Jest tests occasionally fail due to the program name getting garbled.

Note: The underlying Rust process may print a lot of logs. You can control these with the RUST_LOG environment variable. If you want to silence these logs your test command would look like RUST_LOG= jest --runInBand.

import { start } from "solana-bankrun";
import { PublicKey, Transaction, SystemProgram } from "@solana/web3.js";

test("one transfer", async () => {
	const context = await start([], []);
	const client = context.banksClient;
	const payer = context.payer;
	const receiver = PublicKey.unique();
	const blockhash = context.lastBlockhash;
	const transferLamports = 1_000_000n;
	const ixs = [
		SystemProgram.transfer({
			fromPubkey: payer.publicKey,
			toPubkey: receiver,
			lamports: transferLamports,
		}),
	];
	const tx = new Transaction();
	tx.recentBlockhash = blockhash;
	tx.add(...ixs);
	tx.sign(payer);
	await client.processTransaction(tx);
	const balanceAfter = await client.getBalance(receiver);
	expect(balanceAfter).toEqual(transferLamports);
});

Some things to note here:

  • The context object contains a banks_client to talk to the BanksServer, a payer keypair that has been funded with a bunch of SOL, and a last_blockhash that we can use in our transactions.
  • We haven't loaded any specific programs, but by default we have access to the System Program, the SPL token programs and the SPL memo program.

Installation

yarn add solana-bankrun

Contributing

Make sure you have Yarn and the Rust toolchain installed.

Then run yarn to install deps, run yarn build to build the binary and yarn test to run the tests.

You might also like...
A framework for creating PoC's for Solana Smart Contracts in a painless and intuitive way

Solana PoC Framework DISCLAIMER: any illegal usage of this framework is heavily discouraged. Most projects on Solana offer a more than generous bug bo

Generate Nice Solana Address By Template

Yes, I know about GPU generators. https://smith-mcf.medium.com/solana-vanity-address-using-gpus-5a68ad94d1d4 ./solana-nice-address --help solana-nice-

The Voting example based on MoonZoon and Solana + Anchor framework.
The Voting example based on MoonZoon and Solana + Anchor framework.

The Voting example based on MoonZoon and Solana + Anchor framework.

The Solana Program Library (SPL) is a collection of on-chain programs targeting the Sealevel parallel runtime.

Solana Program Library The Solana Program Library (SPL) is a collection of on-chain programs targeting the Sealevel parallel runtime. These programs a

sandbox to play around numerous functionalities on Solana

Solana Sandbox This sandbox is for verifying smart contracts(programs) implemented on Solana for a self-study purpose. Programs Currently implemented

Solana Escrow Program written by RUST.

Environment Setup Install Rust from https://rustup.rs/ Install Solana from https://docs.solana.com/cli/install-solana-cli-tools#use-solanas-install-to

A suite of programs for Solana key management and security.
A suite of programs for Solana key management and security.

🔑 goki Goki is a suite of programs for Solana key management and security. It currently features: Goki Smart Wallet: A wallet loosely based on the Se

Testing a smart contract on the Solana blockchain

Environment Setup Install Rust from https://rustup.rs/ Install Solana from https://docs.solana.com/cli/install-solana-cli-tools#use-solanas-install-to

Solana NFT generative artwork program

resin Solana NFT generative artwork program Installation Depends on imagemagick for art generation, which can be installed here: https://imagemagick.o

Comments
  • Integration with spl-token

    Integration with spl-token

    It would be nice to be able to call the spl-token functions, such as the following:

    underlyingTokenMint = await token.createMint(
          provider.connection.banksClient,
          payer,
          underlyingMintAuthority.publicKey,
          null,
          8
    );
    

    I think this would require implementing some more of the functions on web3.js' Connection, such as getMinimumBalanceForRentExemption

    opened by metaproph3t 8
  • Feature: copy/pull account data from a live environment

    Feature: copy/pull account data from a live environment

    Should be relatively easy to implement. Might have a go at it later.

    • Supply a list of accounts to copy in start and the rpc url to copy from.
    • Get their account data from the environment.
    • Initialize them as AddedAccounts

    Anything I am missing here?

    opened by wjthieme 2
Releases(v0.1.1)
Demonstrates Solana data account versioning used in supporting the Solana Cookbook article: Account Data Versioning

versioning-solana This repo demonstrates ONE rudimentary way to upgrade/migrate account data changes with solana program changes. What is data version

Frank V. Castellucci 6 Sep 30, 2022
My attempt at learning Solana program (smart contract) development through RareSkill's Solana course.

60-days-of-solana My attempt at learning Solana program (smart contract) development through RareSkill's Solana course. Originally, I was trying to cr

Jasper 3 Feb 25, 2024
hello-world geyser plugin to stream accounts and transactions from a solana node

src/lib.rs: entrypoint src/plugin.rs: main plugin code to run: cargo build && solana-test-validator -r --geyser-plugin-config config.json note: make s

null 4 Nov 18, 2022
A node and runtime configuration for polkadot node.

MANTA NODE This repo is a fresh FRAME-based Substrate node, forked from substrate-developer-hub/substrate-node-templte ?? It links to pallet-manta-dap

Manta Network 14 Apr 25, 2021
Simple node and rust script to achieve an easy to use bridge between rust and node.js

Node-Rust Bridge Simple rust and node.js script to achieve a bridge between them. Only 1 bridge can be initialized per rust program. But node.js can h

Pure 5 Apr 30, 2023
Sample lightning node command-line app built on top of Ldk Node (similar to ldk-sample).

ldk-node-sample Sample lightning node command-line app built on top of Ldk Node (similar to ldk-sample ). Installation git clone https://github.com/op

optout 3 Nov 21, 2023
Port path module (and tests) of nodejs to rust using the same algorithms.

rusty_nodejs_path Port path module (and tests) of nodejs to rust using the same algorithms. crates.io Documents Progress posix path.basename(path[, ex

Yunfei He 10 Sep 25, 2022
⚓ Solana Sealevel Framework

Anchor ⚓ Anchor is a framework for Solana's Sealevel runtime providing several convenient developer tools. Rust eDSL for writing Solana programs IDL s

Project Serum 2.6k Jan 2, 2023
Metaplex is a protocol built on top of Solana that allows: Creating/Minting non-fungible tokens;

Metaplex is a protocol built on top of Solana that allows: Creating/Minting non-fungible tokens; Starting a variety of auctions for primary/secondary

Metaplex Foundation 3.2k Jan 4, 2023
⛏ An open protocol for launching liquidity mining programs on Solana.

⛏ Quarry An open protocol for launching liquidity mining programs on Solana. Background Quarry was built with the intention of helping more Solana pro

Quarry Protocol 207 Dec 19, 2022