avail_rust_core/rpc/
state.rs

1use super::Error;
2use primitive_types::H256;
3use subxt_rpcs::{RpcClient, methods::legacy::RuntimeVersion, rpc_params};
4
5pub async fn call(client: &RpcClient, method: &str, data: &[u8], at: Option<H256>) -> Result<String, Error> {
6	let data = std::format!("0x{}", const_hex::encode(data));
7	let params = rpc_params![method, data, at];
8	let value = client.request("state_call", params).await?;
9	Ok(value)
10}
11
12pub async fn get_storage(client: &RpcClient, key: &str, at: Option<H256>) -> Result<Option<Vec<u8>>, Error> {
13	let params = rpc_params![key, at];
14	let value: Option<String> = client.request("state_getStorage", params).await?;
15	let Some(value) = value else {
16		return Ok(None);
17	};
18	let value = const_hex::decode(value.trim_start_matches("0x"));
19	let value = value.map_err(Error::from)?;
20	Ok(Some(value))
21}
22
23pub async fn get_keys_paged(
24	client: &RpcClient,
25	prefix: Option<&str>,
26	count: u32,
27	start_key: Option<&str>,
28	at: Option<H256>,
29) -> Result<Vec<String>, Error> {
30	let params = rpc_params![prefix, count, start_key, at];
31	let value: Vec<String> = client.request("state_getKeysPaged", params).await?;
32	Ok(value)
33}
34
35pub async fn get_metadata(client: &RpcClient, at: Option<H256>) -> Result<Vec<u8>, Error> {
36	let value: String = client.request("state_getMetadata", rpc_params![at]).await?;
37	let value = const_hex::decode(value.trim_start_matches("0x"));
38	let value = value.map_err(Error::from)?;
39	Ok(value)
40}
41
42pub async fn get_runtime_version(client: &RpcClient, at: Option<H256>) -> Result<RuntimeVersion, Error> {
43	let value = client.request("state_getRuntimeVersion", rpc_params![at]).await?;
44	Ok(value)
45}