avail_rust_client/clients/
rpc_api.rs

1use avail_rust_core::{
2	AvailHeader, H256,
3	ext::{
4		codec::Decode,
5		subxt_rpcs::{
6			client::RpcParams,
7			methods::legacy::{RuntimeVersion, SystemHealth},
8		},
9	},
10	grandpa::GrandpaJustification,
11	rpc::{
12		self, BlockWithJustifications,
13		author::SessionKeys,
14		kate::{BlockLength, Cell, GDataProof, GRow, ProofResponse},
15		rpc_methods::RpcMethods,
16		system::{NodeRole, PeerInfo, SyncState, SystemProperties, fetch_events_v1_types, fetch_extrinsics_v1_types},
17	},
18};
19
20use crate::Client;
21
22#[derive(Clone)]
23pub struct RpcAPI {
24	client: Client,
25}
26
27impl RpcAPI {
28	pub fn new(client: Client) -> Self {
29		Self { client }
30	}
31
32	pub async fn call<T: serde::de::DeserializeOwned>(
33		&self,
34		method: &str,
35		params: RpcParams,
36	) -> Result<T, avail_rust_core::Error> {
37		Ok(rpc::call_raw(&self.client.rpc_client, method, params).await?)
38	}
39
40	pub async fn system_account_next_index(&self, address: &str) -> Result<u32, avail_rust_core::Error> {
41		Ok(rpc::system::account_next_index(&self.client.rpc_client, address).await?)
42	}
43
44	pub async fn system_chain(&self) -> Result<String, avail_rust_core::Error> {
45		Ok(rpc::system::chain(&self.client.rpc_client).await?)
46	}
47
48	pub async fn system_chain_type(&self) -> Result<String, avail_rust_core::Error> {
49		Ok(rpc::system::chain_type(&self.client.rpc_client).await?)
50	}
51
52	pub async fn system_health(&self) -> Result<SystemHealth, avail_rust_core::Error> {
53		Ok(rpc::system::health(&self.client.rpc_client).await?)
54	}
55
56	pub async fn system_local_listen_addresses(&self) -> Result<Vec<String>, avail_rust_core::Error> {
57		Ok(rpc::system::local_listen_addresses(&self.client.rpc_client).await?)
58	}
59
60	pub async fn system_local_peer_id(&self) -> Result<String, avail_rust_core::Error> {
61		Ok(rpc::system::local_peer_id(&self.client.rpc_client).await?)
62	}
63
64	pub async fn system_name(&self) -> Result<String, avail_rust_core::Error> {
65		Ok(rpc::system::name(&self.client.rpc_client).await?)
66	}
67
68	pub async fn system_node_roles(&self) -> Result<Vec<NodeRole>, avail_rust_core::Error> {
69		Ok(rpc::system::node_roles(&self.client.rpc_client).await?)
70	}
71
72	pub async fn system_peers(&self) -> Result<Vec<PeerInfo>, avail_rust_core::Error> {
73		Ok(rpc::system::peers(&self.client.rpc_client).await?)
74	}
75
76	pub async fn system_properties(&self) -> Result<SystemProperties, avail_rust_core::Error> {
77		Ok(rpc::system::properties(&self.client.rpc_client).await?)
78	}
79
80	pub async fn system_sync_state(&self) -> Result<SyncState, avail_rust_core::Error> {
81		Ok(rpc::system::sync_state(&self.client.rpc_client).await?)
82	}
83
84	pub async fn system_version(&self) -> Result<String, avail_rust_core::Error> {
85		Ok(rpc::system::version(&self.client.rpc_client).await?)
86	}
87
88	pub async fn chain_get_block(
89		&self,
90		at: Option<H256>,
91	) -> Result<Option<BlockWithJustifications>, avail_rust_core::Error> {
92		Ok(rpc::chain::get_block(&self.client.rpc_client, at).await?)
93	}
94
95	pub async fn chain_get_block_hash(
96		&self,
97		block_height: Option<u32>,
98	) -> Result<Option<H256>, avail_rust_core::Error> {
99		Ok(rpc::chain::get_block_hash(&self.client.rpc_client, block_height).await?)
100	}
101
102	pub async fn chain_get_finalized_head(&self) -> Result<H256, avail_rust_core::Error> {
103		Ok(rpc::chain::get_finalized_head(&self.client.rpc_client).await?)
104	}
105
106	pub async fn chain_get_header(&self, at: Option<H256>) -> Result<Option<AvailHeader>, avail_rust_core::Error> {
107		Ok(rpc::chain::get_header(&self.client.rpc_client, at).await?)
108	}
109
110	pub async fn author_rotate_keys(&self) -> Result<SessionKeys, avail_rust_core::Error> {
111		rpc::author::rotate_keys(&self.client.rpc_client).await
112	}
113
114	pub async fn author_submit_extrinsic(&self, extrinsic: &[u8]) -> Result<H256, avail_rust_core::Error> {
115		Ok(rpc::author::submit_extrinsic(&self.client.rpc_client, extrinsic).await?)
116	}
117
118	pub async fn state_get_runtime_version(&self, at: Option<H256>) -> Result<RuntimeVersion, avail_rust_core::Error> {
119		Ok(rpc::state::get_runtime_version(&self.client.rpc_client, at).await?)
120	}
121
122	pub async fn state_call(
123		&self,
124		method: &str,
125		data: &[u8],
126		at: Option<H256>,
127	) -> Result<String, avail_rust_core::Error> {
128		Ok(rpc::state::call(&self.client.rpc_client, method, data, at).await?)
129	}
130
131	pub async fn state_get_metadata(&self, at: Option<H256>) -> Result<Vec<u8>, avail_rust_core::Error> {
132		rpc::state::get_metadata(&self.client.rpc_client, at).await
133	}
134
135	pub async fn state_get_storage(
136		&self,
137		key: &str,
138		at: Option<H256>,
139	) -> Result<Option<Vec<u8>>, avail_rust_core::Error> {
140		rpc::state::get_storage(&self.client.rpc_client, key, at).await
141	}
142
143	pub async fn rpc_methods(&self) -> Result<RpcMethods, avail_rust_core::Error> {
144		Ok(rpc::rpc_methods::call(&self.client.rpc_client).await?)
145	}
146
147	pub async fn chainspec_v1_genesishash(&self) -> Result<H256, avail_rust_core::Error> {
148		rpc::chainspec::v1_genesishash(&self.client.rpc_client).await
149	}
150
151	pub async fn kate_block_length(&self, at: Option<H256>) -> Result<BlockLength, avail_rust_core::Error> {
152		Ok(rpc::kate::block_length(&self.client.rpc_client, at).await?)
153	}
154
155	pub async fn kate_query_data_proof(
156		&self,
157		transaction_index: u32,
158		at: Option<H256>,
159	) -> Result<ProofResponse, avail_rust_core::Error> {
160		Ok(rpc::kate::query_data_proof(&self.client.rpc_client, transaction_index, at).await?)
161	}
162
163	pub async fn kate_query_proof(
164		&self,
165		cells: Vec<Cell>,
166		at: Option<H256>,
167	) -> Result<Vec<GDataProof>, avail_rust_core::Error> {
168		Ok(rpc::kate::query_proof(&self.client.rpc_client, cells, at).await?)
169	}
170
171	pub async fn kate_query_rows(&self, rows: Vec<u32>, at: Option<H256>) -> Result<Vec<GRow>, avail_rust_core::Error> {
172		Ok(rpc::kate::query_rows(&self.client.rpc_client, rows, at).await?)
173	}
174
175	pub async fn grandpa_block_justification(
176		&self,
177		at: u32,
178	) -> Result<Option<GrandpaJustification>, avail_rust_core::Error> {
179		let result = rpc::grandpa::block_justification(&self.client.rpc_client, at).await?;
180		let Some(result) = result else { return Ok(None) };
181
182		let justification =
183			hex::decode(result.trim_start_matches("0x")).map_err(|x| avail_rust_core::Error::from(x.to_string()))?;
184
185		let justification = GrandpaJustification::decode(&mut justification.as_slice()).map_err(|e| e.to_string())?;
186		Ok(Some(justification))
187	}
188
189	pub async fn system_fetch_events_v1(
190		&self,
191		params: fetch_events_v1_types::Params,
192		at: H256,
193	) -> Result<fetch_events_v1_types::Output, avail_rust_core::Error> {
194		Ok(rpc::system::fetch_events_v1(&self.client.rpc_client, params, at).await?)
195	}
196
197	pub async fn system_fetch_extrinsics_v1(
198		&self,
199		params: fetch_extrinsics_v1_types::Params,
200	) -> Result<fetch_extrinsics_v1_types::Output, avail_rust_core::Error> {
201		Ok(rpc::system::fetch_extrinsics_v1(&self.client.rpc_client, params).await?)
202	}
203}