use crate::{client::OnlineClientT, error::Error, events::Events, rpc::types::StorageKey, Config};
use derivative::Derivative;
use std::future::Future;
#[derive(Derivative)]
#[derivative(Clone(bound = "Client: Clone"))]
pub struct EventsClient<T, Client> {
client: Client,
_marker: std::marker::PhantomData<T>,
}
impl<T, Client> EventsClient<T, Client> {
pub fn new(client: Client) -> Self {
Self {
client,
_marker: std::marker::PhantomData,
}
}
}
impl<T, Client> EventsClient<T, Client>
where
T: Config,
Client: OnlineClientT<T>,
{
pub fn at(
&self,
block_hash: T::Hash,
) -> impl Future<Output = Result<Events<T>, Error>> + Send + 'static {
self.at_or_latest(Some(block_hash))
}
pub fn at_latest(&self) -> impl Future<Output = Result<Events<T>, Error>> + Send + 'static {
self.at_or_latest(None)
}
fn at_or_latest(
&self,
block_hash: Option<T::Hash>,
) -> impl Future<Output = Result<Events<T>, Error>> + Send + 'static {
let client = self.client.clone();
async move {
let block_hash = match block_hash {
Some(hash) => hash,
None => client
.rpc()
.block_hash(None)
.await?
.expect("didn't pass a block number; qed"),
};
let event_bytes = get_event_bytes(&client, Some(block_hash)).await?;
Ok(Events::new(client.metadata(), block_hash, event_bytes))
}
}
}
fn system_events_key() -> StorageKey {
let mut storage_key = sp_core_hashing::twox_128(b"System").to_vec();
storage_key.extend(sp_core_hashing::twox_128(b"Events").to_vec());
StorageKey(storage_key)
}
pub(crate) async fn get_event_bytes<T, Client>(
client: &Client,
block_hash: Option<T::Hash>,
) -> Result<Vec<u8>, Error>
where
T: Config,
Client: OnlineClientT<T>,
{
Ok(client
.rpc()
.storage(&system_events_key().0, block_hash)
.await?
.map(|e| e.0)
.unwrap_or_else(Vec::new))
}