43 releases
new 0.1.29-solana-2.1.4 | Jan 15, 2025 |
---|---|
0.1.28-solana-2.1.4 | Jan 10, 2025 |
0.1.13-solana-1.18.26 | Dec 31, 2024 |
#317 in Magic Beans
4,015 downloads per month
Used in solana_toolbox_anchor
51KB
1K
SLoC
Solana Toolbox Endpoint
Abstract
This crate provide boilerplate types to enable writing endpoint-independent code that interact interchangebly with:
- mainnet-meta (using an RpcClient internally)
- devnet (using an RpcClient internally)
- testnet (using an RpcClient internally)
- localnet (using an RpcClient internally)
- memnet (using a ProgramTest mocked Solana runtime)
The same code can then be run on a different cluster or a different runtime, just by swapping the ToolboxEndpoint
object
Install
Make sure to install the version that matches your solana version. Each solana version may come with a different set of dependency hell, so you can specify the solana version you're using directly in the version of this crate.
# For example when using solana version 1.18.26"
solana_toolbox_endpoint = "=0.1.27-solana-1.18.26"
# Or when using solana version 2.1.4"
solana_toolbox_endpoint = "=0.1.27-solana-2.1.4"
Example
The main type that we will be using is the ToolboxEndpoint
that can be initialized from many different types of runtimes/RPCs.
This ToolboxEndpoint
type then expose capabilities to fetch accounts, execute transactions and a lot of useful boilerplate utilities.
// First we create an endpoint object (here we use "program_test" or "memnet")
let mut endpoint = ToolboxEndpoint::new_program_test_with_builtin_programs(&[
toolbox_endpoint_program_test_builtin_program_anchor!(
"my_smart_contract",
my_smart_contract::ID,
my_smart_contract::entry
),
])
.await;
// We can also create the endpoint to point to devnet instead for example
let mut endpoint = ToolboxEndpoint::new_rpc_with_url_and_commitment(
"https://api.devnet.solana.com".to_string(),
CommitmentConfig::confirmed(),
);
// Optionally make the endpoint print all transaction/fetching
endpoint.add_logger(Box::new(ToolboxEndpointLoggerPrint::new()));
// The endpoint object provides a lot of useful boilerplate utility functions
endpoint
.process_system_transfer(
&payer,
&source,
&destination.pubkey(),
1_000_000_000,
)
.await
.unwrap();
// Then we can use the endpoint to run arbitrary transaction instructions
endpoint
.process_instruction(
Instruction {
program_id: my_smart_contract::ID,
accounts: vec![],
data: vec![],
},
&payer,
)
.await
.unwrap();
Documentation
See the docs for the exhaustive list of the ToolboxEndpoint
capabilities:
Dependencies
~97MB
~2M SLoC