avail_rust_core/rpc/system/
mod.rs

1pub mod fetch_events;
2pub mod fetch_extrinsics;
3
4use crate::{BlockInfo, rpc::Error, types::metadata::ChainInfo};
5use primitive_types::H256;
6use serde::Deserialize;
7use subxt_rpcs::{RpcClient, methods::legacy::SystemHealth, rpc_params};
8
9pub use fetch_events::fetch_events_v1;
10pub use fetch_extrinsics::fetch_extrinsics_v1;
11
12/// Network Peer information
13#[derive(Clone, Debug, PartialEq, Deserialize)]
14#[serde(rename_all = "camelCase")]
15pub struct PeerInfo {
16	/// Peer ID
17	pub peer_id: String,
18	/// Roles
19	pub roles: String,
20	/// Peer best block hash
21	pub best_hash: H256,
22	/// Peer best block number
23	pub best_number: u32,
24}
25
26/// Arbitrary properties defined in chain spec as a JSON object
27pub type SystemProperties = serde_json::map::Map<String, serde_json::Value>;
28
29/// The role the node is running as
30#[derive(Clone, Debug, PartialEq, Deserialize)]
31pub enum NodeRole {
32	/// The node is a full node
33	Full,
34	/// The node is an authority
35	Authority,
36}
37
38/// The state of the syncing of the node.
39#[derive(Clone, Debug, PartialEq, Deserialize)]
40#[serde(rename_all = "camelCase")]
41pub struct SyncState {
42	/// Height of the block at which syncing started.
43	pub starting_block: u32,
44	/// Height of the current best block of the node.
45	pub current_block: u32,
46	/// Height of the highest block in the network.
47	pub highest_block: u32,
48}
49
50pub async fn account_next_index(client: &RpcClient, address: &str) -> Result<u32, Error> {
51	let params = rpc_params![address];
52	let value = client.request("system_accountNextIndex", params).await?;
53	Ok(value)
54}
55
56pub async fn chain(client: &RpcClient) -> Result<String, Error> {
57	let params = rpc_params![];
58	let value = client.request("system_chain", params).await?;
59	Ok(value)
60}
61
62pub async fn chain_type(client: &RpcClient) -> Result<String, Error> {
63	let params = rpc_params![];
64	let value = client.request("system_chainType", params).await?;
65	Ok(value)
66}
67
68pub async fn health(client: &RpcClient) -> Result<SystemHealth, Error> {
69	let params = rpc_params![];
70	let value = client.request("system_health", params).await?;
71	Ok(value)
72}
73
74pub async fn local_listen_addresses(client: &RpcClient) -> Result<Vec<String>, Error> {
75	let params = rpc_params![];
76	let value = client.request("system_localListenAddresses", params).await?;
77	Ok(value)
78}
79
80pub async fn local_peer_id(client: &RpcClient) -> Result<String, Error> {
81	let params = rpc_params![];
82	let value = client.request("system_localPeerId", params).await?;
83	Ok(value)
84}
85
86pub async fn name(client: &RpcClient) -> Result<String, Error> {
87	let params = rpc_params![];
88	let value = client.request("system_name", params).await?;
89	Ok(value)
90}
91
92pub async fn node_roles(client: &RpcClient) -> Result<Vec<NodeRole>, Error> {
93	let params = rpc_params![];
94	let value = client.request("system_nodeRoles", params).await?;
95	Ok(value)
96}
97
98pub async fn peers(client: &RpcClient) -> Result<Vec<PeerInfo>, Error> {
99	let params = rpc_params![];
100	let value = client.request("system_peers", params).await?;
101	Ok(value)
102}
103
104pub async fn properties(client: &RpcClient) -> Result<SystemProperties, Error> {
105	let params = rpc_params![];
106	let value = client.request("system_properties", params).await?;
107	Ok(value)
108}
109
110pub async fn sync_state(client: &RpcClient) -> Result<SyncState, Error> {
111	let params = rpc_params![];
112	let value = client.request("system_syncState", params).await?;
113	Ok(value)
114}
115
116pub async fn version(client: &RpcClient) -> Result<String, Error> {
117	let params = rpc_params![];
118	let value = client.request("system_version", params).await?;
119	Ok(value)
120}
121
122pub async fn get_block_number(client: &RpcClient, at: H256) -> Result<Option<u32>, Error> {
123	let params = rpc_params![at];
124	let value = client.request("system_getBlockNumber", params).await?;
125	Ok(value)
126}
127
128pub async fn latest_block_info(client: &RpcClient, use_best_block: bool) -> Result<BlockInfo, Error> {
129	let params = rpc_params![use_best_block];
130	let value = client.request("system_latestBlockInfo", params).await?;
131	Ok(value)
132}
133
134pub async fn latest_chain_info(client: &RpcClient) -> Result<ChainInfo, Error> {
135	let params = rpc_params![];
136	let value = client.request("system_latestChainInfo", params).await?;
137	Ok(value)
138}