use std::collections::HashMap;
use crate::{
client::{node_manager::node::Node, Client},
types::api::plugins::participation::types::{
ParticipationEventId, ParticipationEventStatus, ParticipationEventType,
},
wallet::account::{
operations::participation::ParticipationEventWithNodes,
types::participation::ParticipationEventRegistrationOptions, AccountHandle,
},
};
impl AccountHandle {
pub async fn register_participation_events(
&self,
options: &ParticipationEventRegistrationOptions,
) -> crate::wallet::Result<HashMap<ParticipationEventId, ParticipationEventWithNodes>> {
let client = Client::builder()
.with_ignore_node_health()
.with_node_auth(options.node.url.as_str(), options.node.auth.clone())?
.finish()?;
let events_to_register = match &options.events_to_register {
Some(events_to_register_) => {
if events_to_register_.is_empty() {
self.get_participation_event_ids(&options.node, Some(ParticipationEventType::Voting))
.await?
} else {
events_to_register_.clone()
}
}
None => {
self.get_participation_event_ids(&options.node, Some(ParticipationEventType::Voting))
.await?
}
};
let mut registered_participation_events = HashMap::new();
for event_id in events_to_register {
if let Some(events_to_ignore) = &options.events_to_ignore {
if events_to_ignore.contains(&event_id) {
continue;
}
}
let event_data = client.event(&event_id).await?;
let event_with_node = ParticipationEventWithNodes {
id: event_id,
data: event_data,
nodes: vec![options.node.clone()],
};
let account_index = self.read().await.index;
self.storage_manager
.lock()
.await
.insert_participation_event(account_index, event_with_node.clone())
.await?;
registered_participation_events.insert(event_id, event_with_node.clone());
}
Ok(registered_participation_events)
}
pub async fn deregister_participation_event(&self, id: &ParticipationEventId) -> crate::wallet::Result<()> {
let account_index = self.read().await.index;
self.storage_manager
.lock()
.await
.remove_participation_event(account_index, id)
.await?;
Ok(())
}
pub async fn get_participation_event(
&self,
id: ParticipationEventId,
) -> crate::wallet::Result<Option<ParticipationEventWithNodes>> {
let account_index = self.read().await.index;
Ok(self
.storage_manager
.lock()
.await
.get_participation_events(account_index)
.await?
.get(&id)
.cloned())
}
pub async fn get_participation_events(
&self,
) -> crate::wallet::Result<HashMap<ParticipationEventId, ParticipationEventWithNodes>> {
let account_index = self.read().await.index;
self.storage_manager
.lock()
.await
.get_participation_events(account_index)
.await
}
pub async fn get_participation_event_ids(
&self,
node: &Node,
event_type: Option<ParticipationEventType>,
) -> crate::wallet::Result<Vec<ParticipationEventId>> {
let client = Client::builder()
.with_ignore_node_health()
.with_node_auth(node.url.as_str(), node.auth.clone())?
.finish()?;
Ok(client.events(event_type).await?.event_ids)
}
pub async fn get_participation_event_status(
&self,
id: &ParticipationEventId,
) -> crate::wallet::Result<ParticipationEventStatus> {
Ok(self.get_client_for_event(id).await?.event_status(id, None).await?)
}
}