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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use core::ops::{Deref, DerefMut};

use polymesh_api::{
  client::{AccountId, IdentityId, DefaultSigner, Signer, Result, dev},
  polymesh::types::{
    frame_system::AccountInfo,
    polymesh_primitives::secondary_key::KeyRecord,
    polymesh_common_utilities::traits::balances::AccountData,
  },
  Api,
};

use crate::*;

#[derive(Clone)]
pub struct PolymeshHelper {
  api: Api,
  pub init_polyx: u128,
  pub cdd: DefaultSigner,
}

impl PolymeshHelper {
  pub async fn new(url: &str) -> Result<Self> {
    let api = Api::new(url).await?;
    Ok(Self {
      api,
      init_polyx: 100_000 * ONE_POLYX,
      cdd: dev::alice(),
    })
  }

  async fn batch_load_did_balance(&self, users: &mut [PolymeshUser]) -> Result<Vec<u128>> {
    let mut balances = Vec::with_capacity(users.len());
    for users in users.chunks_mut(200) {
      let mut tasks = Vec::with_capacity(200);
      for user in users.iter() {
        let account = user.account();
        let need_did = user.did.is_none();
        let helper = self.clone();
        let task = tokio::spawn(async move {
          helper.get_did_and_balance(account, need_did).await
        });
        tasks.push(task);
      }
      for (idx, task) in tasks.into_iter().enumerate() {
        let (did, balance) = task.await.unwrap()?;
        if did.is_some() {
          users[idx].did = did;
        }
        balances.push(balance);
      }
    }
    Ok(balances)
  }

  async fn batch_load_dids(&self, users: &mut [PolymeshUser]) -> Result<()> {
    for users in users.chunks_mut(200) {
      let mut tasks = Vec::with_capacity(200);
      for (idx, user) in users.iter().enumerate() {
        let account = user.account();
        let helper = self.clone();
        if user.did.is_none() {
          let task = tokio::spawn(async move {
            helper.get_did(account).await
          });
          tasks.push((idx, task));
        }
      }
      for (idx, task) in tasks {
        let did = task.await.unwrap()?;
        if did.is_some() {
          users[idx].did = did;
        }
      }
    }
    Ok(())
  }

  /// Generate new users from names.
  ///
  /// The users will be onboarded with the CDD provider, if they don't have an identity.
  /// Also they will be funded with `init_polyx` POLYX.
  pub async fn generate_named_users(&mut self, names: &[&str]) -> Result<Vec<PolymeshUser>> {
    let mut users = Vec::with_capacity(names.len());
    for name in names {
      // Get or create user.
      users.push(PolymeshUser::new(name)?);
    }
    self.onboard_users(&mut users).await?;
    Ok(users)
  }

  /// Generate new users from prefix.
  ///
  /// The users will be onboarded with the CDD provider, if they don't have an identity.
  /// Also they will be funded with `init_polyx` POLYX.
  pub async fn generate_prefix_users(&mut self, prefix: &str, count: usize) -> Result<Vec<PolymeshUser>> {
    let mut users = Vec::with_capacity(count);
    for idx in 0..count {
      // Get or create user.
      users.push(PolymeshUser::new(&format!("{prefix}_{idx}"))?);
    }
    self.onboard_users(&mut users).await?;
    Ok(users)
  }

  async fn onboard_users(&mut self, users: &mut [PolymeshUser]) -> Result<()> {
    let mut batches = Vec::new();
    for users in users.chunks_mut(200) {
      let balances = self.batch_load_did_balance(users).await?;
      // Calls for registering users and funding them.
      let mut did_calls = Vec::new();
      let mut fund_calls = Vec::new();
      for (idx, user) in users.iter_mut().enumerate() {
        let account = user.account();
        // If the user doesn't have an identity, then register them.
        if user.did.is_none() {
          // User needs an identity.
          did_calls.push(self
              .api
              .call()
              .identity()
              .cdd_register_did_with_cdd(account, vec![], None)?
              .into()
          );
        }
        // Add calls to fund the users.
        let balance = balances[idx];
        if balance < self.init_polyx {
          // Transfer some funds to the user.
          fund_calls.push(
            self
              .api
              .call()
              .balances()
              .set_balance(account.into(), self.init_polyx, 0)?
              .into(),
          );
        }
      }
      if did_calls.len() > 0 {
        let res = self
          .api
          .call()
          .utility()
          .batch(did_calls)?
          .submit_and_watch(&mut self.cdd)
          .await?;
        batches.push(res);
      }
      if fund_calls.len() > 0 {
        let res = self
          .api
          .call()
          .sudo()
          .sudo(self.api.call().utility().batch(fund_calls)?.into())?
          .submit_and_watch(&mut self.cdd)
          .await?;
        batches.push(res);
      }
    }
    // Check if we have any batches to process.
    if batches.len() == 0 {
      return Ok(());
    }
    // Wait for all batches to be finalized.
    for mut res in batches {
      res.wait_finalized().await?;
    }
    // Get new identities.
    self.batch_load_dids(users).await?;
    Ok(())
  }

  pub async fn key_records(&self, account: AccountId) -> Result<Option<KeyRecord<AccountId>>> {
    Ok(self.api.query().identity().key_records(account).await?)
  }

  pub async fn get_did(&self, account: AccountId) -> Result<Option<IdentityId>> {
    let did = match self.key_records(account).await? {
      Some(KeyRecord::PrimaryKey(did)) => Some(did),
      Some(KeyRecord::SecondaryKey(did, _)) => Some(did),
      _ => None,
    };
    Ok(did)
  }

  pub async fn get_account_info(&self, account: AccountId) -> Result<AccountInfo<u32, AccountData>> {
    Ok(self.api.query().system().account(account).await?)
  }

  pub async fn get_account_balance(&self, account: AccountId) -> Result<u128> {
    self.get_account_info(account).await.map(|info| info.data.free)
  }

  async fn get_did_and_balance(&self, account: AccountId, need_did: bool) -> Result<(Option<IdentityId>, u128)> {
    let did: Option<IdentityId> = if need_did {
      self.get_did(account).await?
    } else {
      None
    };
    let balance = self.get_account_balance(account).await?;
    Ok((did, balance))
  }

  pub async fn register_and_fund(&mut self, account: AccountId) -> Result<IdentityId> {
    let did = match self.get_did(account).await? {
      Some(did) => did,
      None => {
        // `account` is not linked to an identity.
        // Create a new identity with `account` as the primary key.
        let mut res = self
          .api
          .call()
          .utility()
          .batch(vec![
            self
              .api
              .call()
              .identity()
              .cdd_register_did_with_cdd(account, vec![], None)?
              .into(),
            self
              .api
              .call()
              .balances()
              .transfer(account.into(), self.init_polyx)?
              .into(),
          ])?
          .execute(&mut self.cdd)
          .await?;
        get_identity_id(&mut res).await?.unwrap()
      }
    };
    Ok(did)
  }
}

#[derive(Clone)]
pub struct PolymeshUser {
  pub name: String,
  signer: DefaultSigner,
  pub did: Option<IdentityId>,
}

impl Deref for PolymeshUser {
  type Target = DefaultSigner;

  fn deref(&self) -> &Self::Target {
      &self.signer
  }
}

impl DerefMut for PolymeshUser {
  fn deref_mut(&mut self) -> &mut Self::Target {
      &mut self.signer
  }
}

impl PolymeshUser {
  pub fn new(name: &str) -> Result<Self> {
    Ok(Self {
      name: name.to_string(),
      signer: DefaultSigner::from_string(&format!("//{name}"), None)?,
      did: None
    })
  }

  pub fn from_signer(name: &str, signer: DefaultSigner) -> Self {
    Self {
      name: name.to_string(),
      signer,
      did: None
    }
  }
}