Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple wRPC client example #550

Merged
merged 6 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ members = [
"rpc/wrpc/proxy",
"rpc/wrpc/wasm",
"rpc/wrpc/examples/subscriber",
"rpc/wrpc/examples/simple_client",
"mining",
"mining/errors",
"protocol/p2p",
Expand Down
18 changes: 18 additions & 0 deletions rpc/wrpc/examples/simple_client/Cargo.toml
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

104 changes: 104 additions & 0 deletions rpc/wrpc/examples/simple_client/src/main.rs
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(())
}