polymesh_api_client_extras/
lib.rs1use codec::{Decode, Encode};
2
3use polymesh_api::client::basic_types::{AccountId, AssetId, IdentityId};
4use polymesh_api::client::error::Result;
5use polymesh_api::types::{
6 polymesh_primitives::{
7 asset::CheckpointId,
8 checkpoint::ScheduleId,
9 settlement::{InstructionId, LegId, VenueId},
10 ticker::Ticker,
11 },
12 runtime::{events::*, RuntimeEvent},
13};
14use polymesh_api::TransactionResults;
15
16mod user;
17pub use user::*;
18
19pub const ONE_POLYX: u128 = 1_000_000;
20
21pub type Moment = u64;
22pub type AuthorizationNonce = u64;
23
24#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug)]
25pub struct TargetIdAuthorization {
26 pub target_id: IdentityId,
28 pub nonce: AuthorizationNonce,
30 pub expires_at: Moment,
32}
33
34#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug)]
36pub struct Receipt {
37 pub uid: u64,
39 pub instruction_id: InstructionId,
41 pub leg_id: LegId,
43 pub sender_identity: IdentityId,
45 pub receiver_identity: IdentityId,
47 pub ticker: Ticker,
49 pub amount: u128,
51}
52
53#[derive(Clone, Debug, PartialEq, Eq)]
54pub enum CreatedIds {
55 AssetCreated(AssetId),
56 IdentityCreated(IdentityId),
57 ChildIdentityCreated(IdentityId),
58 MultiSigCreated(AccountId),
59 VenueCreated(VenueId),
60 InstructionCreated(InstructionId),
61 CheckpointCreated(CheckpointId),
62 ScheduleCreated(ScheduleId),
63}
64
65pub async fn get_created_ids(res: &mut TransactionResults) -> Result<Vec<CreatedIds>> {
67 Ok(
68 res
69 .events()
70 .await?
71 .map(|events| {
72 let mut ids = Vec::new();
73 for rec in &events.0 {
74 match &rec.event {
75 RuntimeEvent::Asset(AssetEvent::AssetCreated(_, id, ..)) => {
76 ids.push(CreatedIds::AssetCreated(*id));
77 }
78 RuntimeEvent::Settlement(SettlementEvent::VenueCreated(_, id, ..)) => {
79 ids.push(CreatedIds::VenueCreated(*id));
80 }
81 RuntimeEvent::Settlement(SettlementEvent::InstructionCreated(_, _, id, ..)) => {
82 ids.push(CreatedIds::InstructionCreated(*id));
83 }
84 RuntimeEvent::Checkpoint(CheckpointEvent::CheckpointCreated(_, _, id, ..)) => {
85 ids.push(CreatedIds::CheckpointCreated(id.clone()));
86 }
87 RuntimeEvent::Checkpoint(CheckpointEvent::ScheduleCreated(_, _, id, ..)) => {
88 ids.push(CreatedIds::ScheduleCreated(id.clone()));
89 }
90 RuntimeEvent::Identity(IdentityEvent::DidCreated(id, ..)) => {
91 ids.push(CreatedIds::IdentityCreated(*id));
92 }
93 RuntimeEvent::Identity(IdentityEvent::ChildDidCreated(_, id, ..)) => {
94 ids.push(CreatedIds::ChildIdentityCreated(*id));
95 }
96 RuntimeEvent::MultiSig(MultiSigEvent::MultiSigCreated { multisig, .. }) => {
97 ids.push(CreatedIds::MultiSigCreated(*multisig));
98 }
99 _ => (),
100 }
101 }
102 ids
103 })
104 .unwrap_or_default(),
105 )
106}
107
108pub async fn get_identity_id(res: &mut TransactionResults) -> Result<Option<IdentityId>> {
110 Ok(res.events().await?.and_then(|events| {
111 for rec in &events.0 {
112 match &rec.event {
113 RuntimeEvent::Identity(IdentityEvent::DidCreated(id, ..)) => {
114 return Some(*id);
115 }
116 RuntimeEvent::Identity(IdentityEvent::ChildDidCreated(_, id, ..)) => {
117 return Some(*id);
118 }
119 _ => (),
120 }
121 }
122 None
123 }))
124}
125
126pub async fn get_venue_id(res: &mut TransactionResults) -> Result<Option<VenueId>> {
128 Ok(res.events().await?.and_then(|events| {
129 for rec in &events.0 {
130 match &rec.event {
131 RuntimeEvent::Settlement(SettlementEvent::VenueCreated(_, venue_id, ..)) => {
132 return Some(*venue_id);
133 }
134 _ => (),
135 }
136 }
137 None
138 }))
139}
140
141pub async fn get_instruction_id(res: &mut TransactionResults) -> Result<Option<InstructionId>> {
143 Ok(res.events().await?.and_then(|events| {
144 for rec in &events.0 {
145 match &rec.event {
146 RuntimeEvent::Settlement(SettlementEvent::InstructionCreated(_, _, instruction_id, ..)) => {
147 return Some(*instruction_id);
148 }
149 _ => (),
150 }
151 }
152 None
153 }))
154}
155
156pub async fn get_asset_id(res: &mut TransactionResults) -> Result<Option<AssetId>> {
158 Ok(res.events().await?.and_then(|events| {
159 for rec in &events.0 {
160 match &rec.event {
161 RuntimeEvent::Asset(AssetEvent::AssetCreated(_, asset_id, ..)) => {
162 return Some(*asset_id);
163 }
164 _ => (),
165 }
166 }
167 None
168 }))
169}