-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* simple client example created * fine-tuning on simple wRPC client example (with comments) * code fixed after Aspect's suggestions * empty lines cleanup
- Loading branch information
1 parent
5b9c3cf
commit d1dc5dd
Showing
4 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "kaspa-wrpc-simple-client-example" | ||
description = "Kaspa wRPC simple client example" | ||
publish = false | ||
rust-version.workspace = true | ||
version.workspace = true | ||
edition.workspace = true | ||
authors.workspace = true | ||
include.workspace = true | ||
license.workspace = true | ||
repository.workspace = true | ||
|
||
[dependencies] | ||
futures.workspace = true | ||
kaspa-rpc-core.workspace = true | ||
kaspa-wrpc-client.workspace = true | ||
tokio.workspace = true | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Example of simple client to connect with Kaspa node using wRPC connection and collect some node and network basic data | ||
|
||
use kaspa_rpc_core::{api::rpc::RpcApi, GetBlockDagInfoResponse, GetServerInfoResponse}; | ||
use kaspa_wrpc_client::{ | ||
client::{ConnectOptions, ConnectStrategy}, | ||
prelude::NetworkId, | ||
prelude::NetworkType, | ||
result::Result, | ||
KaspaRpcClient, Resolver, WrpcEncoding, | ||
}; | ||
use std::process::ExitCode; | ||
use std::time::Duration; | ||
|
||
#[tokio::main] | ||
async fn main() -> ExitCode { | ||
match check_node_status().await { | ||
Ok(_) => { | ||
println!("Well done! You successfully completed your first client connection to Kaspa node!"); | ||
ExitCode::SUCCESS | ||
} | ||
Err(error) => { | ||
println!("An error occurred: {error}"); | ||
ExitCode::FAILURE | ||
} | ||
} | ||
} | ||
|
||
async fn check_node_status() -> Result<()> { | ||
// Select encoding method to use, depending on node settings | ||
let encoding = WrpcEncoding::Borsh; | ||
|
||
// If you want to connect to your own node, define your node address and wRPC port using let url = Some("ws://0.0.0.0:17110") | ||
// Verify your Kaspa node is runnning with --rpclisten-borsh=0.0.0.0:17110 parameter | ||
// In this example we don't use a specific node but we connect through the resolver, which use a pool of public nodes | ||
let url = None; | ||
let resolver = Some(Resolver::default()); | ||
|
||
// Define the network your Kaspa node is connected to | ||
// You can select NetworkType::Mainnet, NetworkType::Testnet, NetworkType::Devnet, NetworkType::Simnet | ||
let network_type = NetworkType::Mainnet; | ||
let selected_network = Some(NetworkId::new(network_type)); | ||
|
||
// Advanced options | ||
let subscription_context = None; | ||
|
||
// Create new wRPC client with parameters defined above | ||
let client = KaspaRpcClient::new(encoding, url, resolver, selected_network, subscription_context)?; | ||
|
||
// Advanced connection options | ||
let timeout = 5_000; | ||
let options = ConnectOptions { | ||
block_async_connect: true, | ||
connect_timeout: Some(Duration::from_millis(timeout)), | ||
strategy: ConnectStrategy::Fallback, | ||
..Default::default() | ||
}; | ||
|
||
// Connect to selected Kaspa node | ||
client.connect(Some(options)).await?; | ||
|
||
// Retrieve and show Kaspa node information | ||
let GetServerInfoResponse { is_synced, server_version, network_id, has_utxo_index, .. } = client.get_server_info().await?; | ||
|
||
println!("Node version: {server_version}"); | ||
println!("Network: {network_id}"); | ||
println!("Node is synced: {is_synced}"); | ||
println!("Node is indexing UTXOs: {has_utxo_index}"); | ||
|
||
// Retrieve and show Kaspa network information | ||
let GetBlockDagInfoResponse { | ||
block_count, | ||
header_count, | ||
tip_hashes, | ||
difficulty, | ||
past_median_time, | ||
virtual_parent_hashes, | ||
pruning_point_hash, | ||
virtual_daa_score, | ||
sink, | ||
.. | ||
} = client.get_block_dag_info().await?; | ||
|
||
println!("Block count: {block_count}"); | ||
println!("Header count: {header_count}"); | ||
println!("Tip hashes:"); | ||
for tip_hash in tip_hashes { | ||
println!("{tip_hash}"); | ||
} | ||
println!("Difficulty: {difficulty}"); | ||
println!("Past median time: {past_median_time}"); | ||
println!("Virtual parent hashes:"); | ||
for virtual_parent_hash in virtual_parent_hashes { | ||
println!("{virtual_parent_hash}"); | ||
} | ||
println!("Pruning point hash: {pruning_point_hash}"); | ||
println!("Virtual DAA score: {virtual_daa_score}"); | ||
println!("Sink: {sink}"); | ||
|
||
// Disconnect client from Kaspa node | ||
client.disconnect().await?; | ||
|
||
// Return function result | ||
Ok(()) | ||
} |