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{}", 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 { return Ok(None) };
21	let value = hex::decode(value.trim_start_matches("0x"));
22	let value = value.map_err(|e| Error::from(e.to_string()))?;
23	Ok(Some(value))
24}
25
26pub async fn get_keys_paged(
27	client: &RpcClient,
28	prefix: Option<String>,
29	count: u32,
30	start_key: Option<String>,
31	at: Option<H256>,
32) -> Result<Vec<String>, Error> {
33	let params = rpc_params![prefix, count, start_key, at];
34	let value: Vec<String> = client.request("state_getKeysPaged", params).await?;
35	Ok(value)
36}
37
38pub async fn get_metadata(client: &RpcClient, at: Option<H256>) -> Result<Vec<u8>, Error> {
39	let value: String = client.request("state_getMetadata", rpc_params![at]).await?;
40	Ok(hex::decode(value.trim_start_matches("0x")).map_err(|e| e.to_string())?)
41}
42
43pub async fn get_runtime_version(client: &RpcClient, at: Option<H256>) -> Result<RuntimeVersion, subxt_rpcs::Error> {
44	let value = client.request("state_getRuntimeVersion", rpc_params![at]).await?;
45	Ok(value)
46}