avail_rust_core/rpc/
system.rs

1use primitive_types::H256;
2use serde::{Deserialize, Serialize};
3use subxt_rpcs::{RpcClient, methods::legacy::SystemHealth, rpc_params};
4
5use crate::decoded_transaction::RuntimePhase;
6
7/// Network Peer information
8#[derive(Clone, Debug, PartialEq, Deserialize)]
9#[serde(rename_all = "camelCase")]
10pub struct PeerInfo {
11	/// Peer ID
12	pub peer_id: String,
13	/// Roles
14	pub roles: String,
15	/// Peer best block hash
16	pub best_hash: H256,
17	/// Peer best block number
18	pub best_number: u32,
19}
20
21/// Arbitrary properties defined in chain spec as a JSON object
22pub type SystemProperties = serde_json::map::Map<String, serde_json::Value>;
23
24/// The role the node is running as
25#[derive(Clone, Debug, PartialEq, Deserialize)]
26pub enum NodeRole {
27	/// The node is a full node
28	Full,
29	/// The node is an authority
30	Authority,
31}
32
33/// The state of the syncing of the node.
34#[derive(Clone, Debug, PartialEq, Deserialize)]
35#[serde(rename_all = "camelCase")]
36pub struct SyncState {
37	/// Height of the block at which syncing started.
38	pub starting_block: u32,
39	/// Height of the current best block of the node.
40	pub current_block: u32,
41	/// Height of the highest block in the network.
42	pub highest_block: u32,
43}
44
45pub async fn account_next_index(client: &RpcClient, address: &str) -> Result<u32, subxt_rpcs::Error> {
46	let params = rpc_params![address];
47	let value = client.request("system_accountNextIndex", params).await?;
48	Ok(value)
49}
50
51pub async fn chain(client: &RpcClient) -> Result<String, subxt_rpcs::Error> {
52	let params = rpc_params![];
53	let value = client.request("system_chain", params).await?;
54	Ok(value)
55}
56
57pub async fn chain_type(client: &RpcClient) -> Result<String, subxt_rpcs::Error> {
58	let params = rpc_params![];
59	let value = client.request("system_chainType", params).await?;
60	Ok(value)
61}
62
63pub async fn health(client: &RpcClient) -> Result<SystemHealth, subxt_rpcs::Error> {
64	let params = rpc_params![];
65	let value = client.request("system_health", params).await?;
66	Ok(value)
67}
68
69pub async fn local_listen_addresses(client: &RpcClient) -> Result<Vec<String>, subxt_rpcs::Error> {
70	let params = rpc_params![];
71	let value = client.request("system_localListenAddresses", params).await?;
72	Ok(value)
73}
74
75pub async fn local_peer_id(client: &RpcClient) -> Result<String, subxt_rpcs::Error> {
76	let params = rpc_params![];
77	let value = client.request("system_localPeerId", params).await?;
78	Ok(value)
79}
80
81pub async fn name(client: &RpcClient) -> Result<String, subxt_rpcs::Error> {
82	let params = rpc_params![];
83	let value = client.request("system_name", params).await?;
84	Ok(value)
85}
86
87pub async fn node_roles(client: &RpcClient) -> Result<Vec<NodeRole>, subxt_rpcs::Error> {
88	let params = rpc_params![];
89	let value = client.request("system_nodeRoles", params).await?;
90	Ok(value)
91}
92
93pub async fn peers(client: &RpcClient) -> Result<Vec<PeerInfo>, subxt_rpcs::Error> {
94	let params = rpc_params![];
95	let value = client.request("system_peers", params).await?;
96	Ok(value)
97}
98
99pub async fn properties(client: &RpcClient) -> Result<SystemProperties, subxt_rpcs::Error> {
100	let params = rpc_params![];
101	let value = client.request("system_properties", params).await?;
102	Ok(value)
103}
104
105pub async fn sync_state(client: &RpcClient) -> Result<SyncState, subxt_rpcs::Error> {
106	let params = rpc_params![];
107	let value = client.request("system_syncState", params).await?;
108	Ok(value)
109}
110
111pub async fn version(client: &RpcClient) -> Result<String, subxt_rpcs::Error> {
112	let params = rpc_params![];
113	let value = client.request("system_version", params).await?;
114	Ok(value)
115}
116
117pub async fn fetch_events_v1(
118	client: &RpcClient,
119	params: fetch_events_v1_types::Params,
120	at: H256,
121) -> Result<fetch_events_v1_types::Output, subxt_rpcs::Error> {
122	let params = rpc_params![params, at];
123	let value = client.request("system_fetchEventsV1", params).await?;
124	Ok(value)
125}
126
127pub async fn fetch_extrinsics_v1(
128	client: &RpcClient,
129	params: fetch_extrinsics_v1_types::Params,
130) -> Result<fetch_extrinsics_v1_types::Output, subxt_rpcs::Error> {
131	let params = rpc_params![params];
132	let value = client.request("system_fetchExtrinsicsV1", params).await?;
133	Ok(value)
134}
135
136pub mod fetch_events_v1_types {
137	pub use super::*;
138
139	pub type FetchEventsV1Params = Params;
140	pub type Output = Vec<GroupedRuntimeEvents>;
141
142	#[derive(Default, Clone, Debug, Serialize, Deserialize)]
143	pub struct Params {
144		pub filter: Option<Filter>,
145		pub enable_encoding: Option<bool>,
146		pub enable_decoding: Option<bool>,
147	}
148
149	impl Params {
150		pub fn new(filter: Option<Filter>, enable_encoding: Option<bool>, enable_decoding: Option<bool>) -> Self {
151			Self {
152				filter,
153				enable_encoding,
154				enable_decoding,
155			}
156		}
157	}
158
159	#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
160	#[repr(u8)]
161	pub enum Filter {
162		All = 0,
163		OnlyExtrinsics = 1,
164		OnlyNonExtrinsics = 2,
165		Only(Vec<u32>) = 3,
166	}
167
168	impl Default for Filter {
169		fn default() -> Self {
170			Self::All
171		}
172	}
173
174	#[derive(Clone, Debug, PartialEq, Deserialize)]
175	pub struct GroupedRuntimeEvents {
176		pub phase: RuntimePhase,
177		pub events: Vec<RuntimeEvent>,
178	}
179
180	#[derive(Clone, Debug, PartialEq, Deserialize)]
181	pub struct RuntimeEvent {
182		pub index: u32,
183		// (Pallet Id, Event Id)
184		pub emitted_index: (u8, u8),
185		pub encoded: Option<String>,
186		pub decoded: Option<String>,
187	}
188}
189
190pub mod fetch_extrinsics_v1_types {
191	use super::*;
192	use crate::config::HashNumber;
193
194	pub type Output = Vec<ExtrinsicInformation>;
195	pub type FetchExtrinsicsV1Params = Params;
196
197	#[derive(Clone, Serialize, Deserialize)]
198	pub struct Params {
199		pub block_id: HashNumber,
200		pub filter: Option<Filter>,
201		pub encode_selector: Option<EncodeSelector>,
202	}
203
204	impl Params {
205		pub fn new(block_id: HashNumber, filter: Option<Filter>, selector: Option<EncodeSelector>) -> Self {
206			Self {
207				block_id,
208				filter,
209				encode_selector: selector,
210			}
211		}
212	}
213
214	#[derive(Default, Clone, Serialize, Deserialize)]
215	pub struct Filter {
216		pub transaction: Option<TransactionFilter>,
217		pub signature: Option<SignatureFilter>,
218	}
219
220	impl Filter {
221		pub fn new(tx: Option<TransactionFilter>, sig: Option<SignatureFilter>) -> Self {
222			Self {
223				transaction: tx,
224				signature: sig,
225			}
226		}
227	}
228
229	#[derive(Clone, Serialize, Deserialize)]
230	#[repr(u8)]
231	pub enum EncodeSelector {
232		None = 0,
233		Call = 1,
234		Extrinsic = 2,
235	}
236
237	#[derive(Debug, Clone, Serialize, Deserialize)]
238	pub struct ExtrinsicInformation {
239		// Hex string encoded
240		pub encoded: Option<String>,
241		pub tx_hash: H256,
242		pub tx_index: u32,
243		pub pallet_id: u8,
244		pub call_id: u8,
245		pub signature: Option<TransactionSignature>,
246	}
247
248	#[derive(Clone, Serialize, Deserialize)]
249	pub enum TransactionFilter {
250		All,
251		TxHash(Vec<H256>),
252		TxIndex(Vec<u32>),
253		Pallet(Vec<u8>),
254		PalletCall(Vec<(u8, u8)>),
255	}
256
257	impl TransactionFilter {
258		pub fn new() -> Self {
259			Self::default()
260		}
261	}
262
263	impl Default for TransactionFilter {
264		fn default() -> Self {
265			Self::All
266		}
267	}
268
269	#[derive(Default, Clone, Serialize, Deserialize)]
270	pub struct SignatureFilter {
271		pub ss58_address: Option<String>,
272		pub app_id: Option<u32>,
273		pub nonce: Option<u32>,
274	}
275
276	impl SignatureFilter {
277		pub fn new(ss58_address: Option<String>, app_id: Option<u32>, nonce: Option<u32>) -> Self {
278			Self {
279				ss58_address,
280				app_id,
281				nonce,
282			}
283		}
284	}
285
286	#[derive(Debug, Clone, Serialize, Deserialize)]
287	pub struct TransactionSignature {
288		pub ss58_address: Option<String>,
289		pub nonce: u32,
290		pub app_id: u32,
291		pub mortality: Option<(u64, u64)>,
292	}
293}