|
| 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