Solana Development Practice: Full Process of Rust Client Calling On-Chain Programs
Following the exploration of the Solana SDK in practice: the dual paths and lightweight modularization of Web3 development, this article will delve into practical implementation, focusing on how to interact with Solana on-chain programs using a Rust client. Whether you want to quickly get started with blockchain development or wish to master the high-performance potential of Solana, this article provides clear steps and reproducible code. From project configuration to program deployment and transaction invocation, we will gradually unveil the powerful charm of Rust in Solana development, helping you accelerate towards the forefront of Web3 development!
This article demonstrates the detailed Rust client development process for calling on-chain programs on the Solana blockchain. The content covers the entire process from project environment setup, dependency configuration, client script implementation, local validator node operation, to program deployment and closure. Based on the solana-client library, by creating a keypair, requesting an airdrop, and constructing transactions, readers can easily reproduce a complete Solana interaction example. This article is suitable for developers interested in Solana and Rust development, providing practical code and operational guidance, making it an ideal reference for advanced Web3 development.
Practical Operation
View Current Project Directory
solana-sandbox/sol-program on ๎ main [?] is ๐ฆ 0.1.0 via ๐ฆ 1.87.0 on ๐ณ v28.2.2 (orbstack)
โ tree . -L 6 -I "coverage_report|lib|.vscode|out|test-ledger|target|node_modules"
.
โโโ Cargo.lock
โโโ Cargo.toml
โโโ src
โโโ lib.rs
โโโ lib2.rs
2 directories, 4 files
Create an <span>examples</span> Directory and a <span>client.rs</span> File
solana-sandbox/sol-program on ๎ main [?] is ๐ฆ 0.1.0 via ๐ฆ 1.87.0 on ๐ณ v28.2.2 (orbstack)
โ mkdir -p examples
touch examples/client.rs
Add the Following Content to <span>Cargo.toml</span>
[[example]]
name = "client"
path = "examples/client.rs"
Add Relevant Dependencies
solana-sandbox/sol-program on ๎ main [?] is ๐ฆ 0.1.0 via ๐ฆ 1.87.0 on ๐ณ v28.2.2 (orbstack)
โ cargo add solana-client --dev
Updating crates.io index
Adding solana-client v2.2.7 to dev-dependencies
solana-sandbox/sol-program on ๎ main [!?] is ๐ฆ 0.1.0 via ๐ฆ 1.87.0 on ๐ณ v28.2.2 (orbstack) took 2.6s
โ cargo add [email protected] --dev
Updating crates.io index
Adding solana-native-token v2.2.1 to dev-dependencies
solana-sandbox/sol-program on ๎ main [!?] is ๐ฆ 0.1.0 via ๐ฆ 1.87.0 on ๐ณ v28.2.2 (orbstack)
โ cargo add anyhow
Updating crates.io index
Adding anyhow v1.0.98 to dependencies
Features:
+ std
- backtrace
View Cargo.toml
cargo-features = ["edition2024"]
[package]
name = "sol-program"
version = "0.1.0"
edition = "2024"
[lib]
crate-type = ["cdylib", "lib"]
[[example]]
name = "client"
path = "examples/client.rs"
[dependencies]
solana-account-info = "2.2.1"
solana-msg = "2.2.1"
solana-program-entrypoint = "2.2.1"
solana-program-error = "2.2.2"
solana-pubkey = "2.2.1"
# solana-program = "2.2.1"
[dev-dependencies]
solana-client = "2.2.7"
solana-program-test = "2.2.7"
solana-sdk = "2.2.2"
tokio = "1.45.1"
Implement examples/client.rs
use anyhow::Result;
use solana_client::rpc_client::RpcClient;
use solana_native_token::LAMPORTS_PER_SOL;
use solana_sdk::{
commitment_config::CommitmentConfig,
instruction::Instruction,
pubkey::Pubkey,
signature::{Keypair, Signer},
transaction::Transaction,
};
use std::str::FromStr;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::error="">> {
let program_id = Pubkey::from_str("GGBjDqYdicSE6Qmtu6SAsueX1biM5LjbJ8R8vZvFfofA")?;
let rpc_url = String::from("http://127.0.0.1:8899");
let commitment_config = CommitmentConfig::confirmed();
let rpc_client = RpcClient::new_with_commitment(rpc_url, commitment_config);
let keypair = Keypair::new();
println!("Keypair: {}", keypair.pubkey());
println!("Requesting airdrop...");
let signature = rpc_client
.request_airdrop(&keypair.pubkey(), 2 * LAMPORTS_PER_SOL)
.expect("Failed to request airdrop");
loop {
let confirmed =
rpc_client.confirm_transaction_with_commitment(&signature, commitment_config)?;
if confirmed.value {
break;
}
}
let instruction = Instruction::new_with_borsh(program_id, &(), vec![]);
let mut transaction = Transaction::new_with_payer(&[instruction], Some(&keypair.pubkey()));
transaction.sign(&[&keypair], rpc_client.get_latest_blockhash()?);
match rpc_client.send_and_confirm_transaction(&transaction) {
Ok(signature) => println!("Transaction Signature: {}", signature),
Err(err) => eprintln!("Error sending transaction: {}", err),
}
Ok(())
}
</dyn>
This is a Rust client script that funds a new keypair to pay for transaction fees and then calls the sol_program.
Start Local Validator Node
solana-sandbox/sol-program on ๎ main [!?] is ๐ฆ 0.1.0 via ๐ฆ 1.87.0 on ๐ณ v28.2.2 (orbstack)
โ solana-test-validator -r
Ledger location: test-ledger
Log: test-ledger/validator.log
โ Initializing... Waiting for fees to stabilize 1...
Identity: nTSnjt8VZY9M74QaeAWi7oW6gChLKwRGUgATpw5qMU7
Genesis Hash: EzBss1P6qHe74g8WcZGmeu1MfEtxZiE48c7ogQEtWfXu
Version: 2.1.22
Shred Version: 9856
Gossip Address: 127.0.0.1:1024
TPU Address: 127.0.0.1:1027
JSON RPC URL: http://127.0.0.1:8899
WebSocket PubSub URL: ws://127.0.0.1:8900
โ 00:00:11 | Processed Slot: 23 | Confirmed Slot: 23 | Finalized Slot: 0 | Full Snapshot Slot: - | Incremental Snapshot Slot: - | Transactions: 22 | โ499.999890000
Compile and Test Deployment
solana-sandbox/sol-program on ๎ main [!?] is ๐ฆ 0.1.0 via ๐ฆ 1.87.0 on ๐ณ v28.2.2 (orbstack) took 2.7s
โ cargo build-sbf
Finished `release` profile [optimized] target(s) in 0.20s
solana-sandbox/sol-program on ๎ main [!?] is ๐ฆ 0.1.0 via ๐ฆ 1.87.0 on ๐ณ v28.2.2 (orbstack)
โ cargo test-sbf
Finished `release` profile [optimized] target(s) in 0.15s
warning: the cargo feature `edition2024` has been stabilized in the 1.85 release and is no longer necessary to be listed in the manifest
See https://doc.rust-lang.org/cargo/reference/manifest.html#the-edition-field for more information about using this feature.
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.50s
Running unittests src/lib.rs (target/debug/deps/sol_program-08f16897dcbb8ceb)
running 1 test
[2025-06-14T14:09:00.591057000Z INFO solana_program_test] "sol_program" SBF program from /Users/qiaopengjun/Code/Solana/solana-sandbox/sol-program/target/deploy/sol_program.so, modified 2 minutes, 57 seconds, 791 ms, 306 ยตs and 863 ns ago
[2025-06-14T14:09:00.688000000Z DEBUG solana_runtime::message_processor::stable_log] Program 1111111QLbz7JHiBTspS962RLKV8GndWFwiEaqKM invoke [1]
[2025-06-14T14:09:00.688713000Z DEBUG solana_runtime::message_processor::stable_log] Program log: Hello, Solana!
[2025-06-14T14:09:00.690443000Z DEBUG solana_runtime::message_processor::stable_log] Program 1111111QLbz7JHiBTspS962RLKV8GndWFwiEaqKM consumed 137 of 200000 compute units
[2025-06-14T14:09:00.690471000Z DEBUG solana_runtime::message_processor::stable_log] Program 1111111QLbz7JHiBTspS962RLKV8GndWFwiEaqKM success
testtest::test_sol_program ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.22s
Doc-tests sol_program
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
solana-sandbox/sol-program on ๎ main [!?] is ๐ฆ 0.1.0 via ๐ฆ 1.87.0 on ๐ณ v28.2.2 (orbstack)
โ solana config set -ul
Config File: /Users/qiaopengjun/.config/solana/cli/config.yml
RPC URL: http://localhost:8899
WebSocket URL: ws://localhost:8900/ (computed)
Keypair Path: /Users/qiaopengjun/.config/solana/id.json
Commitment: confirmed
solana-sandbox/sol-program on ๎ main [!?] is ๐ฆ 0.1.0 via ๐ฆ 1.87.0 on ๐ณ v28.2.2 (orbstack)
โ solana program deploy ./target/deploy/sol_program.so
Program Id: GGBjDqYdicSE6Qmtu6SAsueX1biM5LjbJ8R8vZvFfofA
Signature: 2khrNAScTpLBJjwQvV4Z5EgVbbZME12QyAtsHrz4gKNzqDcJaXr5P2kDopWCnQBCsmDA7ycq8P2nbsurA1Fu9A
Query Program ID
solana-sandbox/sol-program on ๎ main [!?] is ๐ฆ 0.1.0 via ๐ฆ 1.87.0 on ๐ณ v28.2.2 (orbstack)
โ solana address -k ./target/deploy/sol_program-keypair.json
GGBjDqYdicSE6Qmtu6SAsueX1biM5LjbJ8R8vZvFfofA
Run Client Script
solana-sandbox/sol-program on ๎ main [!?] is ๐ฆ 0.1.0 via ๐ฆ 1.87.0 on ๐ณ v28.2.2 (orbstack)
โ cargo run --example client
warning: the cargo feature `edition2024` has been stabilized in the 1.85 release and is no longer necessary to be listed in the manifest
See https://doc.rust-lang.org/cargo/reference/manifest.html#the-edition-field for more information about using this feature.
Compiling sol-program v0.1.0 (/Users/qiaopengjun/Code/Solana/solana-sandbox/sol-program)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 3.10s
Running `target/debug/examples/client`
Keypair: 5NkBWXrxkFy6B5r4X9NMcA3edN8CRvJqjz2jFpcJDBKg
Requesting airdrop...
Transaction Signature: 2Ya4oev7cgGKvLMTq4zcVyjSbz72iuxhDdXf6iBfJQDQv4WFoLfG3RnYv31AnkmBxNYBYM4ZNn9xTbux6KGuRfUx
Close Program
Close the Solana program to reclaim the SOL allocated to the account. Closing the program is an irreversible operation, so it should be done with caution.
solana-sandbox/sol-program on ๎ main [!?] is ๐ฆ 0.1.0 via ๐ฆ 1.87.0 on ๐ณ v28.2.2 (orbstack) took 2.3s
โ solana program close GGBjDqYdicSE6Qmtu6SAsueX1biM5LjbJ8R8vZvFfofA --bypass-warning
Closed Program Id GGBjDqYdicSE6Qmtu6SAsueX1biM5LjbJ8R8vZvFfofA, 0.12492504 SOL reclaimed
Conclusion
Through the practical exercises in this article, you have mastered the core skills of using a Rust client to call Solana on-chain programs, from environment configuration to transaction execution, and safely closing the program to reclaim SOL, completing the entire process seamlessly. The high throughput of Solana combined with the safety of Rust offers infinite possibilities for blockchain development. Coupled with our previous article, “Exploring Solana SDK in Practice”, you now possess the development capabilities from basic to advanced. We look forward to seeing you apply this knowledge to real projects and create your own Web3 applications! Stay tuned for our series of articles, as more Solana development insights are coming soon!
References
- https://spl.solana.com/token#example-transferring-tokens-to-an-explicit-recipient-token-account
- https://docs.rs/solana-native-token/latest/solana_native_token/
- https://crates.io/crates/solana-native-token
- https://explorer.solana.com/
- https://solscan.io/?cluster=devnet
