polymesh_api_tester/
lib.rs

1pub use polymesh_api::{
2  client::{AccountId, AssetId, IdentityId, Signer},
3  polymesh::types::{
4    polymesh_primitives::{
5      secondary_key::{KeyRecord, Permissions, SecondaryKey},
6      ticker::Ticker,
7    },
8    runtime::{events::*, RuntimeEvent},
9  },
10  Api,
11};
12pub use polymesh_api_client_extras as extras;
13
14mod error;
15use error::*;
16
17mod db;
18pub use db::*;
19
20mod account;
21pub use account::*;
22
23mod tester;
24pub use tester::*;
25
26pub async fn client_api() -> Result<Api> {
27  let url = std::env::var("POLYMESH_URL").unwrap_or_else(|_| "ws://localhost:9944".into());
28  Ok(Api::new(&url).await?)
29}
30
31#[derive(Clone)]
32pub struct User {
33  pub api: Api,
34  /// Primary key signer.
35  pub primary_key: AccountSigner,
36  /// User's secondary keys.
37  pub secondary_keys: Vec<AccountSigner>,
38  /// User's identity if they have been onboarded.
39  pub did: Option<IdentityId>,
40}
41
42#[async_trait::async_trait]
43impl Signer for User {
44  fn account(&self) -> AccountId {
45    self.primary_key.account()
46  }
47
48  async fn nonce(&self) -> Option<u32> {
49    self.primary_key.nonce().await
50  }
51
52  async fn set_nonce(&mut self, nonce: u32) {
53    self.primary_key.set_nonce(nonce).await
54  }
55
56  async fn sign(&self, msg: &[u8]) -> polymesh_api::client::Result<sp_runtime::MultiSignature> {
57    Ok(self.primary_key.sign(msg).await?)
58  }
59
60  async fn lock(&self) -> Option<Box<dyn Signer>> {
61    self.primary_key.lock().await
62  }
63}
64
65impl User {
66  pub fn new(api: &Api, primary_key: AccountSigner) -> Self {
67    Self {
68      api: api.clone(),
69      primary_key,
70      secondary_keys: Vec::new(),
71      did: None,
72    }
73  }
74
75  pub fn account(&self) -> AccountId {
76    self.primary_key.account()
77  }
78}