avail_rust_client/clients/
rpc_api.rs1use avail_rust_core::{
2 AvailHeader, H256, HashNumber,
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 state_get_keys_paged(
144 &self,
145 prefix: Option<String>,
146 count: u32,
147 start_key: Option<String>,
148 at: Option<H256>,
149 ) -> Result<Vec<String>, avail_rust_core::Error> {
150 rpc::state::get_keys_paged(&self.client.rpc_client, prefix, count, start_key, at).await
151 }
152
153 pub async fn rpc_methods(&self) -> Result<RpcMethods, avail_rust_core::Error> {
154 Ok(rpc::rpc_methods::call(&self.client.rpc_client).await?)
155 }
156
157 pub async fn chainspec_v1_genesishash(&self) -> Result<H256, avail_rust_core::Error> {
158 rpc::chainspec::v1_genesishash(&self.client.rpc_client).await
159 }
160
161 pub async fn kate_block_length(&self, at: Option<H256>) -> Result<BlockLength, avail_rust_core::Error> {
162 Ok(rpc::kate::block_length(&self.client.rpc_client, at).await?)
163 }
164
165 pub async fn kate_query_data_proof(
166 &self,
167 transaction_index: u32,
168 at: Option<H256>,
169 ) -> Result<ProofResponse, avail_rust_core::Error> {
170 Ok(rpc::kate::query_data_proof(&self.client.rpc_client, transaction_index, at).await?)
171 }
172
173 pub async fn kate_query_proof(
174 &self,
175 cells: Vec<Cell>,
176 at: Option<H256>,
177 ) -> Result<Vec<GDataProof>, avail_rust_core::Error> {
178 Ok(rpc::kate::query_proof(&self.client.rpc_client, cells, at).await?)
179 }
180
181 pub async fn kate_query_rows(&self, rows: Vec<u32>, at: Option<H256>) -> Result<Vec<GRow>, avail_rust_core::Error> {
182 Ok(rpc::kate::query_rows(&self.client.rpc_client, rows, at).await?)
183 }
184
185 pub async fn grandpa_block_justification(
186 &self,
187 at: u32,
188 ) -> Result<Option<GrandpaJustification>, avail_rust_core::Error> {
189 let result = rpc::grandpa::block_justification(&self.client.rpc_client, at).await?;
190 let Some(result) = result else { return Ok(None) };
191
192 let justification =
193 hex::decode(result.trim_start_matches("0x")).map_err(|x| avail_rust_core::Error::from(x.to_string()))?;
194
195 let justification = GrandpaJustification::decode(&mut justification.as_slice()).map_err(|e| e.to_string())?;
196 Ok(Some(justification))
197 }
198
199 pub async fn system_fetch_events_v1(
200 &self,
201 at: H256,
202 options: Option<fetch_events_v1_types::Options>,
203 ) -> Result<fetch_events_v1_types::Output, avail_rust_core::Error> {
204 Ok(rpc::system::fetch_events_v1(&self.client.rpc_client, at, options).await?)
205 }
206
207 pub async fn system_fetch_extrinsics_v1(
208 &self,
209 block_id: HashNumber,
210 options: Option<fetch_extrinsics_v1_types::Options>,
211 ) -> Result<fetch_extrinsics_v1_types::Output, avail_rust_core::Error> {
212 Ok(rpc::system::fetch_extrinsics_v1(&self.client.rpc_client, block_id, options).await?)
213 }
214}