avail_rust_core/rpc/
chain.rs1use super::{AvailHeader, Error};
2use primitive_types::H256;
3use serde::{Deserialize, Deserializer};
4use subxt_core::config::substrate::ConsensusEngineId;
5use subxt_rpcs::{RpcClient, rpc_params};
6
7#[derive(Debug, Clone, Deserialize)]
9pub struct LegacyBlock {
10 pub block: Block,
12 pub justifications: Option<Vec<BlockJustification>>,
14}
15
16#[derive(Debug, Clone, Deserialize)]
17pub struct Block {
18 pub header: AvailHeader,
20 #[serde(deserialize_with = "from_string_to_vec")]
21 pub extrinsics: Vec<Vec<u8>>,
22}
23
24fn from_string_to_vec<'de, D>(deserializer: D) -> Result<Vec<Vec<u8>>, D::Error>
25where
26 D: Deserializer<'de>,
27{
28 let buf = Vec::<String>::deserialize(deserializer)?;
29 let result: Result<Vec<Vec<u8>>, _> = buf
30 .into_iter()
31 .map(|x| const_hex::decode(x.trim_start_matches("0x")))
32 .collect();
33 match result {
34 Ok(res) => Ok(res),
35 Err(err) => Err(serde::de::Error::custom(err)),
36 }
37}
38
39pub type BlockJustification = (ConsensusEngineId, EncodedJustification);
41pub type EncodedJustification = Vec<u8>;
43
44pub async fn get_block(client: &RpcClient, at: Option<H256>) -> Result<Option<LegacyBlock>, Error> {
45 let params = rpc_params![at];
46 let res: Option<LegacyBlock> = client.request("chain_getBlock", params).await?;
47 let Some(value) = res else {
48 return Ok(None);
49 };
50 Ok(Some(value))
51}
52
53pub async fn get_block_hash(client: &RpcClient, block_height: Option<u32>) -> Result<Option<H256>, Error> {
54 let params = rpc_params![block_height];
55 let value = client.request("chain_getBlockHash", params).await?;
56 Ok(value)
57}
58
59pub async fn get_header(client: &RpcClient, at: Option<H256>) -> Result<Option<AvailHeader>, Error> {
60 let params = rpc_params![at];
61 let value = client.request("chain_getHeader", params).await?;
62 Ok(value)
63}
64
65pub async fn get_finalized_head(client: &RpcClient) -> Result<H256, Error> {
66 let value = client.request("chain_getFinalizedHead", rpc_params![]).await?;
67 Ok(value)
68}