polymesh_api_client_extras/
lib.rs

1use 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  /// Target identity which is authorized to make an operation.
27  pub target_id: IdentityId,
28  /// It HAS TO be `target_id` authorization nonce: See `Identity::offchain_authorization_nonce`.
29  pub nonce: AuthorizationNonce,
30  /// Expire timestamp to limit how long the authorization is valid for.
31  pub expires_at: Moment,
32}
33
34/// An offchain transaction receipt.
35#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug)]
36pub struct Receipt {
37  /// Unique receipt number set by the signer for their receipts.
38  pub uid: u64,
39  /// The [`InstructionId`] of the instruction for which the receipt is for.
40  pub instruction_id: InstructionId,
41  /// The [`LegId`] of the leg for which the receipt is for.
42  pub leg_id: LegId,
43  /// The [`IdentityId`] of the sender.
44  pub sender_identity: IdentityId,
45  /// The [`IdentityId`] of the receiver.
46  pub receiver_identity: IdentityId,
47  /// [`Ticker`] of the asset being transferred.
48  pub ticker: Ticker,
49  /// The amount transferred.
50  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
65/// Get ids from *Created events.
66pub 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
108/// Search transaction events for newly created identity id.
109pub 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
126/// Search transaction events for VenueId.
127pub 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
141/// Search transaction events for InstructionId.
142pub 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
156/// Search transaction events for AssetId.
157pub 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}