unc_workspaces/network/
variants.rs1use crate::network::Info;
2use crate::result::{Execution, Result};
3use crate::rpc::client::Client;
4use crate::types::{AccountId, KeyType, SecretKey};
5use crate::{Account, Contract, Worker};
6use async_trait::async_trait;
7
8pub(crate) const DEV_ACCOUNT_SEED: &str = "testificate";
9
10pub trait NetworkClient {
11 fn client(&self) -> &Client;
12}
13
14pub trait NetworkInfo {
15 fn info(&self) -> &Info;
16}
17
18#[async_trait]
19pub trait TopLevelAccountCreator {
20 async fn create_tla(
21 &self,
22 worker: Worker<dyn Network>,
23 id: AccountId,
24 sk: SecretKey,
25 ) -> Result<Execution<Account>>;
26
27 async fn create_account_and_deploy(
28 &self,
29 worker: Worker<dyn Network>,
30 id: AccountId,
31 sk: SecretKey,
32 wasm: &[u8],
33 ) -> Result<Execution<Contract>>;
34}
35
36pub trait AllowDevAccountCreation {}
39
40impl<T> Worker<T>
41where
42 T: DevNetwork + TopLevelAccountCreator + 'static,
43{
44 pub async fn create_tla(&self, id: AccountId, sk: SecretKey) -> Result<Execution<Account>> {
45 let res = self
46 .workspace
47 .create_tla(self.clone().coerce(), id, sk)
48 .await?;
49
50 for callback in self.tx_callbacks.iter() {
51 callback(res.details.total_gas_burnt)?;
52 }
53
54 Ok(res)
55 }
56
57 pub async fn create_account_and_deploy(
58 &self,
59 id: AccountId,
60 sk: SecretKey,
61 wasm: &[u8],
62 ) -> Result<Execution<Contract>> {
63 let res = self
64 .workspace
65 .create_account_and_deploy(self.clone().coerce(), id, sk, wasm)
66 .await?;
67
68 for callback in self.tx_callbacks.iter() {
69 callback(res.details.total_gas_burnt)?;
70 }
71
72 Ok(res)
73 }
74
75 pub async fn dev_generate(&self) -> (AccountId, SecretKey) {
76 let id = crate::rpc::tool::random_account_id();
77 let sk = SecretKey::from_seed(KeyType::ED25519, DEV_ACCOUNT_SEED);
78 (id, sk)
79 }
80
81 pub async fn dev_create_account(&self) -> Result<Account> {
82 let (id, sk) = self.dev_generate().await;
83 let account = self.create_tla(id.clone(), sk).await?;
84 Ok(account.into_result()?)
85 }
86
87 pub async fn dev_deploy(&self, wasm: &[u8]) -> Result<Contract> {
88 let (id, sk) = self.dev_generate().await;
89 let contract = self.create_account_and_deploy(id.clone(), sk, wasm).await?;
90 Ok(contract.into_result()?)
91 }
92}
93
94pub trait Network: NetworkInfo + NetworkClient + Send + Sync {}
97
98impl<T> Network for T where T: NetworkInfo + NetworkClient + Send + Sync {}
99
100pub trait DevNetwork: TopLevelAccountCreator + AllowDevAccountCreation + Network + 'static {}
102
103impl<T> DevNetwork for T where
104 T: TopLevelAccountCreator + AllowDevAccountCreation + Network + 'static
105{
106}