Skip to content

Commit d1dc5dd

Browse files
authored
Simple wRPC client example (#550)
* simple client example created * fine-tuning on simple wRPC client example (with comments) * code fixed after Aspect's suggestions * empty lines cleanup
1 parent 5b9c3cf commit d1dc5dd

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ members = [
4141
"rpc/wrpc/proxy",
4242
"rpc/wrpc/wasm",
4343
"rpc/wrpc/examples/subscriber",
44+
"rpc/wrpc/examples/simple_client",
4445
"mining",
4546
"mining/errors",
4647
"protocol/p2p",
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "kaspa-wrpc-simple-client-example"
3+
description = "Kaspa wRPC simple client example"
4+
publish = false
5+
rust-version.workspace = true
6+
version.workspace = true
7+
edition.workspace = true
8+
authors.workspace = true
9+
include.workspace = true
10+
license.workspace = true
11+
repository.workspace = true
12+
13+
[dependencies]
14+
futures.workspace = true
15+
kaspa-rpc-core.workspace = true
16+
kaspa-wrpc-client.workspace = true
17+
tokio.workspace = true
18+
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Example of simple client to connect with Kaspa node using wRPC connection and collect some node and network basic data
2+
3+
use kaspa_rpc_core::{api::rpc::RpcApi, GetBlockDagInfoResponse, GetServerInfoResponse};
4+
use kaspa_wrpc_client::{
5+
client::{ConnectOptions, ConnectStrategy},
6+
prelude::NetworkId,
7+
prelude::NetworkType,
8+
result::Result,
9+
KaspaRpcClient, Resolver, WrpcEncoding,
10+
};
11+
use std::process::ExitCode;
12+
use std::time::Duration;
13+
14+
#[tokio::main]
15+
async fn main() -> ExitCode {
16+
match check_node_status().await {
17+
Ok(_) => {
18+
println!("Well done! You successfully completed your first client connection to Kaspa node!");
19+
ExitCode::SUCCESS
20+
}
21+
Err(error) => {
22+
println!("An error occurred: {error}");
23+
ExitCode::FAILURE
24+
}
25+
}
26+
}
27+
28+
async fn check_node_status() -> Result<()> {
29+
// Select encoding method to use, depending on node settings
30+
let encoding = WrpcEncoding::Borsh;
31+
32+
// 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")
33+
// Verify your Kaspa node is runnning with --rpclisten-borsh=0.0.0.0:17110 parameter
34+
// In this example we don't use a specific node but we connect through the resolver, which use a pool of public nodes
35+
let url = None;
36+
let resolver = Some(Resolver::default());
37+
38+
// Define the network your Kaspa node is connected to
39+
// You can select NetworkType::Mainnet, NetworkType::Testnet, NetworkType::Devnet, NetworkType::Simnet
40+
let network_type = NetworkType::Mainnet;
41+
let selected_network = Some(NetworkId::new(network_type));
42+
43+
// Advanced options
44+
let subscription_context = None;
45+
46+
// Create new wRPC client with parameters defined above
47+
let client = KaspaRpcClient::new(encoding, url, resolver, selected_network, subscription_context)?;
48+
49+
// Advanced connection options
50+
let timeout = 5_000;
51+
let options = ConnectOptions {
52+
block_async_connect: true,
53+
connect_timeout: Some(Duration::from_millis(timeout)),
54+
strategy: ConnectStrategy::Fallback,
55+
..Default::default()
56+
};
57+
58+
// Connect to selected Kaspa node
59+
client.connect(Some(options)).await?;
60+
61+
// Retrieve and show Kaspa node information
62+
let GetServerInfoResponse { is_synced, server_version, network_id, has_utxo_index, .. } = client.get_server_info().await?;
63+
64+
println!("Node version: {server_version}");
65+
println!("Network: {network_id}");
66+
println!("Node is synced: {is_synced}");
67+
println!("Node is indexing UTXOs: {has_utxo_index}");
68+
69+
// Retrieve and show Kaspa network information
70+
let GetBlockDagInfoResponse {
71+
block_count,
72+
header_count,
73+
tip_hashes,
74+
difficulty,
75+
past_median_time,
76+
virtual_parent_hashes,
77+
pruning_point_hash,
78+
virtual_daa_score,
79+
sink,
80+
..
81+
} = client.get_block_dag_info().await?;
82+
83+
println!("Block count: {block_count}");
84+
println!("Header count: {header_count}");
85+
println!("Tip hashes:");
86+
for tip_hash in tip_hashes {
87+
println!("{tip_hash}");
88+
}
89+
println!("Difficulty: {difficulty}");
90+
println!("Past median time: {past_median_time}");
91+
println!("Virtual parent hashes:");
92+
for virtual_parent_hash in virtual_parent_hashes {
93+
println!("{virtual_parent_hash}");
94+
}
95+
println!("Pruning point hash: {pruning_point_hash}");
96+
println!("Virtual DAA score: {virtual_daa_score}");
97+
println!("Sink: {sink}");
98+
99+
// Disconnect client from Kaspa node
100+
client.disconnect().await?;
101+
102+
// Return function result
103+
Ok(())
104+
}

0 commit comments

Comments
 (0)