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
use crate::network::Info;
use crate::result::CallExecution;
use crate::rpc::client::Client;
use crate::types::{AccountId, KeyType, SecretKey};
use crate::{Account, Contract};
use async_trait::async_trait;
pub(crate) const DEV_ACCOUNT_SEED: &str = "testificate";
pub trait NetworkClient {
fn client(&self) -> &Client;
}
pub trait NetworkInfo {
fn info(&self) -> &Info;
}
#[async_trait]
pub trait TopLevelAccountCreator {
async fn create_tla(
&self,
id: AccountId,
sk: SecretKey,
) -> anyhow::Result<CallExecution<Account>>;
async fn create_tla_and_deploy(
&self,
id: AccountId,
sk: SecretKey,
wasm: &[u8],
) -> anyhow::Result<CallExecution<Contract>>;
}
pub trait AllowDevAccountCreation {}
#[async_trait]
pub trait DevAccountDeployer {
async fn dev_generate(&self) -> (AccountId, SecretKey);
async fn dev_create_account(&self) -> anyhow::Result<Account>;
async fn dev_deploy(&self, wasm: &[u8]) -> anyhow::Result<Contract>;
}
#[async_trait]
impl<T> DevAccountDeployer for T
where
T: TopLevelAccountCreator + NetworkInfo + AllowDevAccountCreation + Send + Sync,
{
async fn dev_generate(&self) -> (AccountId, SecretKey) {
let id = crate::rpc::tool::random_account_id();
let sk = SecretKey::from_seed(KeyType::ED25519, DEV_ACCOUNT_SEED);
(id, sk)
}
async fn dev_create_account(&self) -> anyhow::Result<Account> {
let (id, sk) = self.dev_generate().await;
let account = self.create_tla(id.clone(), sk).await?;
account.into()
}
async fn dev_deploy(&self, wasm: &[u8]) -> anyhow::Result<Contract> {
let (id, sk) = self.dev_generate().await;
let contract = self.create_tla_and_deploy(id.clone(), sk, wasm).await?;
contract.into()
}
}
pub trait Network: NetworkInfo + NetworkClient + Send + Sync {}
impl<T> Network for T where T: NetworkInfo + NetworkClient + Send + Sync {}
pub trait DevNetwork: TopLevelAccountCreator + AllowDevAccountCreation + Network {}
impl<T> DevNetwork for T where T: TopLevelAccountCreator + AllowDevAccountCreation + Network {}