avail_rust_core/rpc/
state.rs

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