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
mod account;
mod info;
mod mainnet;
mod result;
mod sandbox;
mod server;
mod testnet;
use async_trait::async_trait;
use near_jsonrpc_client::methods::sandbox_patch_state::RpcSandboxPatchStateRequest;
use near_primitives::state_record::StateRecord;
pub(crate) use crate::network::info::Info;
use crate::rpc::client::Client;
use crate::rpc::patch::ImportContractBuilder;
use crate::types::{AccountId, KeyType, SecretKey};
use crate::Worker;
pub use crate::network::account::{Account, Contract};
pub use crate::network::mainnet::Mainnet;
pub use crate::network::result::{CallExecution, CallExecutionDetails, ViewResultDetails};
pub use crate::network::sandbox::Sandbox;
pub use crate::network::testnet::Testnet;
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: Vec<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: Vec<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);
let mut savepath = self.info().keystore_path.clone();
std::fs::create_dir_all(savepath.clone()).unwrap();
savepath = savepath.join(id.to_string());
savepath.set_extension("json");
crate::rpc::tool::write_cred_to_file(&savepath, id.clone(), sk.clone());
(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: Vec<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 AllowStatePatching {}
#[async_trait]
pub trait StatePatcher {
async fn patch_state(
&self,
contract_id: AccountId,
key: String,
value: Vec<u8>,
) -> anyhow::Result<()>;
fn import_contract<'a, 'b>(
&'b self,
id: AccountId,
worker: &'a Worker<impl Network>,
) -> ImportContractBuilder<'a, 'b>;
}
#[async_trait]
impl<T> StatePatcher for T
where
T: AllowStatePatching + NetworkClient + Send + Sync,
{
async fn patch_state(
&self,
contract_id: AccountId,
key: String,
value: Vec<u8>,
) -> anyhow::Result<()> {
let state = StateRecord::Data {
account_id: contract_id,
data_key: key.into(),
value,
};
let records = vec![state];
let _patch_resp = self
.client()
.query(&RpcSandboxPatchStateRequest { records })
.await
.map_err(|err| anyhow::anyhow!("Failed to patch state: {:?}", err))?;
Ok(())
}
fn import_contract<'a, 'b>(
&'b self,
id: AccountId,
worker: &'a Worker<impl Network>,
) -> ImportContractBuilder<'a, 'b> {
ImportContractBuilder::new(id, worker.client(), self.client())
}
}
pub trait Network: TopLevelAccountCreator + NetworkInfo + NetworkClient + Send + Sync {}
impl<T> Network for T where T: TopLevelAccountCreator + NetworkInfo + NetworkClient + Send + Sync {}
pub trait DevNetwork: AllowDevAccountCreation + Network {}
impl<T> DevNetwork for T where T: AllowDevAccountCreation + Network {}