Add bindings to the events from the coins module to serai-client-serai

This commit is contained in:
Luke Parker
2025-11-15 16:13:25 -05:00
parent fce26eaee1
commit d219b77bd0
2 changed files with 85 additions and 0 deletions

View File

@@ -0,0 +1,76 @@
pub use serai_abi::coins::Event;
use crate::{RpcError, TemporalSerai};
/// A `TemporalSerai` scoped to the coins module.
#[derive(Clone)]
pub struct Coins<'a>(pub(super) &'a TemporalSerai<'a>);
impl<'a> Coins<'a> {
/// The events from the coins module.
pub async fn events(&self) -> Result<Vec<Event>, RpcError> {
Ok(
self
.0
.events_borrowed()
.await?
.as_ref()
.expect("`TemporalSerai::events` returned None")
.iter()
.flat_map(IntoIterator::into_iter)
.filter_map(|event| match event {
serai_abi::Event::Coins(event) => Some(event.clone()),
_ => None,
})
.collect(),
)
}
/// The `Mint` events from the coins module.
pub async fn mint_events(&self) -> Result<Vec<Event>, RpcError> {
Ok(
self
.events()
.await?
.into_iter()
.filter(|event| matches!(event, Event::Mint { .. }))
.collect(),
)
}
/// The `Transfer` events from the coins module.
pub async fn transfer_events(&self) -> Result<Vec<Event>, RpcError> {
Ok(
self
.events()
.await?
.into_iter()
.filter(|event| matches!(event, Event::Transfer { .. }))
.collect(),
)
}
/// The `Burn` events from the coins module.
pub async fn burn_events(&self) -> Result<Vec<Event>, RpcError> {
Ok(
self
.events()
.await?
.into_iter()
.filter(|event| matches!(event, Event::Burn { .. }))
.collect(),
)
}
/// The `BurnWithInstruction` events from the coins module.
pub async fn burn_with_instruction_events(&self) -> Result<Vec<Event>, RpcError> {
Ok(
self
.events()
.await?
.into_iter()
.filter(|event| matches!(event, Event::BurnWithInstruction { .. }))
.collect(),
)
}
}

View File

@@ -19,6 +19,10 @@ use abi::{
use async_lock::RwLock; use async_lock::RwLock;
/// RPC client functionality for the coins module.
pub mod coins;
use coins::*;
/// RPC client functionality for the validator sets module. /// RPC client functionality for the validator sets module.
pub mod validator_sets; pub mod validator_sets;
use validator_sets::*; use validator_sets::*;
@@ -245,6 +249,11 @@ impl<'a> TemporalSerai<'a> {
Ok(self.events_borrowed().await?.clone().expect("`TemporalSerai::events` returned None")) Ok(self.events_borrowed().await?.clone().expect("`TemporalSerai::events` returned None"))
} }
/// Scope to the coins module.
pub fn coins(&self) -> Coins<'_> {
Coins(self)
}
/// Scope to the validator sets module. /// Scope to the validator sets module.
pub fn validator_sets(&self) -> ValidatorSets<'_> { pub fn validator_sets(&self) -> ValidatorSets<'_> {
ValidatorSets(self) ValidatorSets(self)