1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use codec::{Decode, Encode};

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

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;

pub type Moment = u64;
pub type AuthorizationNonce = u64;

#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug)]
pub struct TargetIdAuthorization {
  /// Target identity which is authorized to make an operation.
  pub target_id: IdentityId,
  /// It HAS TO be `target_id` authorization nonce: See `Identity::offchain_authorization_nonce`.
  pub nonce: AuthorizationNonce,
  /// Expire timestamp to limit how long the authorization is valid for.
  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),
}

/// Get ids from *Created events.
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(),
  )
}

/// Search transaction events for newly created identity id.
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
  }))
}

/// Search transaction events for VenueId.
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
  }))
}

/// Search transaction events for InstructionId.
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
  }))
}