pub use jsonrpsee::ws_client::{WsClient, WsClientBuilder};
use crate::jsonrpseeclient::rpcstuff::RpcParams;
use crate::jsonrpseeclient::subscription::Request;
use crate::jsonrpseeclient::JsonrpseeClient;
use crate::rpc_params;
use crate::types::{PreBlock, H256};
use std::str::FromStr;
#[cfg(feature = "metadatadecode")]
use crate::decode_extrinsic::{decode_extrinsic_hex_string, decodec_to_event_summary};
use crate::jsonrpseeclient::subscription::HandleSubscription;
use crate::jsonrpseeclient::subscription::Subscribe;
use crate::jsonrpseeclient::SubscriptionWrapper;
use crate::types::{event_summary, Header, RuntimeVersion};
pub struct Wsclientwrapper();
#[maybe_async::maybe_async(?Send)]
pub async fn get_metadata_version(
client: JsonrpseeClient,
) -> anyhow::Result<u8, crate::error::Error> {
let hex_data: String = client
.request("state_getMetadata", RpcParams::new())
.await?;
let bytes: Vec<u8> = hex::decode(hex_data.trim_start_matches("0x"))?;
Ok(bytes[4])
}
#[maybe_async::maybe_async(?Send)]
pub async fn get_raw_metadata(
client: JsonrpseeClient,
) -> anyhow::Result<Vec<u8>, crate::error::Error> {
let hex_data: String = client
.request("state_getMetadata", RpcParams::new())
.await?;
let bytes: Vec<u8> = hex::decode(hex_data.trim_start_matches("0x"))?;
Ok(bytes)
}
#[maybe_async::maybe_async(?Send)]
pub async fn blocknumber_to_blockhash(
client: JsonrpseeClient,
block_nr: String,
) -> anyhow::Result<H256, crate::error::Error> {
let raw_data: String = client
.request("chain_getBlockHash", rpc_params![block_nr])
.await?;
let hashen: H256 = H256::from_str(&raw_data.as_str())?;
Ok(hashen)
}
#[maybe_async::maybe_async(?Send)]
pub async fn blockhash_to_block(
client: JsonrpseeClient,
block_hash: H256,
) -> anyhow::Result<H256, crate::error::Error> {
let raw_data: String = client
.request("chain_getBlock", rpc_params![block_hash.to_string()])
.await?;
let hashen: H256 = H256::from_str(&raw_data.as_str())?;
Ok(hashen)
}
#[maybe_async::maybe_async(?Send)]
pub async fn get_latest_finalized_head(
client: JsonrpseeClient,
) -> anyhow::Result<H256, crate::error::Error> {
let hex_data: String = client
.request("chain_getFinalizedHead", RpcParams::new())
.await?;
let finb: H256 = H256::from_str(&hex_data.as_str())?;
Ok(finb)
}
#[maybe_async::maybe_async(?Send)]
pub async fn get_block_events(
blockhash: H256,
client: JsonrpseeClient,
) -> anyhow::Result<PreBlock, crate::error::Error> {
let string_hash: String = format!("{:?}", blockhash);
let qq: PreBlock = client
.request("chain_getBlock", rpc_params![string_hash])
.await?; Ok(qq)
}
#[cfg(feature = "metadatadecode")]
#[maybe_async::maybe_async(?Send)]
pub async fn get_decoded_extrinsics_from_blockhash(
blockhash: H256,
metadatablob: Vec<u8>,
client: JsonrpseeClient,
) -> anyhow::Result<Vec<event_summary>, crate::error::Error> {
let preblock: PreBlock = get_block_events(blockhash, client).await.unwrap();
let extrinsics: Vec<String> = preblock.block.extrinsics;
let decodedevent_list: Vec<event_summary> = extrinsics
.clone()
.iter()
.map(|n| decodec_to_event_summary(decode_extrinsic_hex_string(n.as_str(), &metadatablob)))
.collect();
Ok(decodedevent_list)
}
#[maybe_async::maybe_async(?Send)]
pub async fn get_runtime_version(
client: JsonrpseeClient,
) -> anyhow::Result<RuntimeVersion, crate::error::Error> {
let runtimeversion: RuntimeVersion = client
.request("state_getRuntimeVersion", RpcParams::new())
.await?;
Ok(runtimeversion)
}
#[cfg(feature = "metadatadecode")]
#[maybe_async::maybe_async(?Send)]
pub async fn event_watch(
client: JsonrpseeClient,
event: event_summary,
block_limit: u32,
) -> anyhow::Result<H256, crate::error::Error> {
let metadatablob = get_raw_metadata(client.clone()).await.unwrap();
let mut subscrib: SubscriptionWrapper<Header> = client
.subscribe::<Header>(
"chain_subscribeFinalizedHeads",
RpcParams::new(),
"chain_unsubscribeFinalizedHeads",
)
.unwrap();
for _ in 0..block_limit {
let tmp_client = client.clone(); let nextone = subscrib.next();
let block_nr = nextone.unwrap().unwrap().number;
let tmp_blockhash = blocknumber_to_blockhash(tmp_client.clone(), block_nr).await?;
let preblock = get_block_events(tmp_blockhash, tmp_client).await.unwrap();
let extrinsics = preblock.block.extrinsics;
let decodedevent_list: Vec<event_summary> = extrinsics
.clone()
.iter()
.map(|n| {
decodec_to_event_summary(decode_extrinsic_hex_string(n.as_str(), &metadatablob))
})
.collect();
match decodedevent_list.contains(&event) {
true => {
let _ = subscrib.unsubscribe(); return Ok(tmp_blockhash);
}
false => continue,
};
}
println!("unsubscribing");
let _ = subscrib.unsubscribe();
let fake_blockhash: H256 =
H256::from_str("0x89a5dde6705d345117f442dfacf02f4a59bf5cea3ab713a5c07fc4cd78be3a31")
.unwrap();
Ok(fake_blockhash) }