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_metadata(client: &RpcClient, at: Option<H256>) -> Result<Vec<u8>, Error> {
27	let value: String = client.request("state_getMetadata", rpc_params![at]).await?;
28	Ok(hex::decode(value.trim_start_matches("0x")).map_err(|e| e.to_string())?)
29}
30
31pub async fn get_runtime_version(client: &RpcClient, at: Option<H256>) -> Result<RuntimeVersion, subxt_rpcs::Error> {
32	let value = client.request("state_getRuntimeVersion", rpc_params![at]).await?;
33	Ok(value)
34}