use codec::{Decode, Encode};
use polymesh_api::client::basic_types::{AccountId, IdentityId};
use polymesh_api::client::error::Result;
use polymesh_api::types::{
polymesh_common_utilities::traits::checkpoint::ScheduleId,
polymesh_primitives::asset::CheckpointId,
polymesh_primitives::settlement::{InstructionId, VenueId},
runtime::{events::*, RuntimeEvent},
};
use polymesh_api::TransactionResults;
mod user;
pub use user::*;
pub const ONE_POLYX: u128 = 1_000_000;
pub type Moment = u64;
pub type AuthorizationNonce = u64;
#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug)]
pub struct TargetIdAuthorization {
pub target_id: IdentityId,
pub nonce: AuthorizationNonce,
pub expires_at: Moment,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum CreatedIds {
IdentityCreated(IdentityId),
ChildIdentityCreated(IdentityId),
MultiSigCreated(AccountId),
VenueCreated(VenueId),
InstructionCreated(InstructionId),
CheckpointCreated(CheckpointId),
ScheduleCreated(ScheduleId),
}
pub async fn get_created_ids(res: &mut TransactionResults) -> Result<Vec<CreatedIds>> {
Ok(
res
.events()
.await?
.map(|events| {
let mut ids = Vec::new();
for rec in &events.0 {
match &rec.event {
RuntimeEvent::Settlement(SettlementEvent::VenueCreated(_, id, ..)) => {
ids.push(CreatedIds::VenueCreated(*id));
}
RuntimeEvent::Settlement(SettlementEvent::InstructionCreated(_, _, id, ..)) => {
ids.push(CreatedIds::InstructionCreated(*id));
}
RuntimeEvent::Checkpoint(CheckpointEvent::CheckpointCreated(_, _, id, ..)) => {
ids.push(CreatedIds::CheckpointCreated(id.clone()));
}
RuntimeEvent::Checkpoint(CheckpointEvent::ScheduleCreated(_, _, id, ..)) => {
ids.push(CreatedIds::ScheduleCreated(id.clone()));
}
RuntimeEvent::Identity(IdentityEvent::DidCreated(id, ..)) => {
ids.push(CreatedIds::IdentityCreated(*id));
}
RuntimeEvent::Identity(IdentityEvent::ChildDidCreated(_, id, ..)) => {
ids.push(CreatedIds::ChildIdentityCreated(*id));
}
RuntimeEvent::MultiSig(MultiSigEvent::MultiSigCreated(_, id, ..)) => {
ids.push(CreatedIds::MultiSigCreated(*id));
}
_ => (),
}
}
ids
})
.unwrap_or_default(),
)
}
pub async fn get_identity_id(res: &mut TransactionResults) -> Result<Option<IdentityId>> {
Ok(res.events().await?.and_then(|events| {
for rec in &events.0 {
match &rec.event {
RuntimeEvent::Identity(IdentityEvent::DidCreated(id, ..)) => {
return Some(*id);
}
RuntimeEvent::Identity(IdentityEvent::ChildDidCreated(_, id, ..)) => {
return Some(*id);
}
_ => (),
}
}
None
}))
}
pub async fn get_venue_id(res: &mut TransactionResults) -> Result<Option<VenueId>> {
Ok(res.events().await?.and_then(|events| {
for rec in &events.0 {
match &rec.event {
RuntimeEvent::Settlement(SettlementEvent::VenueCreated(_, venue_id, ..)) => {
return Some(*venue_id);
}
_ => (),
}
}
None
}))
}
pub async fn get_instruction_id(res: &mut TransactionResults) -> Result<Option<InstructionId>> {
Ok(res.events().await?.and_then(|events| {
for rec in &events.0 {
match &rec.event {
RuntimeEvent::Settlement(SettlementEvent::InstructionCreated(_, _, instruction_id, ..)) => {
return Some(*instruction_id);
}
_ => (),
}
}
None
}))
}