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
use std::path::PathBuf;
use std::str::FromStr;
use async_trait::async_trait;
use url::Url;
use near_primitives::views::ExecutionStatusView;
use crate::network::Info;
use crate::network::{AllowDevAccountCreation, NetworkClient, NetworkInfo, TopLevelAccountCreator};
use crate::result::{Execution, ExecutionDetails, ExecutionFinalResult, ExecutionOutcome, Result};
use crate::rpc::{client::Client, tool};
use crate::types::{AccountId, InMemorySigner, SecretKey};
use crate::{Account, Contract, CryptoHash, Network, Worker};
const RPC_URL: &str = "https://rpc.testnet.near.org";
const HELPER_URL: &str = "https://helper.testnet.near.org";
const ARCHIVAL_URL: &str = "https://archival-rpc.testnet.near.org";
pub struct Testnet {
client: Client,
info: Info,
}
impl Testnet {
pub(crate) async fn new() -> Result<Self> {
let client = Client::new(RPC_URL);
client.wait_for_rpc().await?;
Ok(Self {
client,
info: Info {
name: "testnet".into(),
root_id: AccountId::from_str("testnet").unwrap(),
keystore_path: PathBuf::from(".near-credentials/testnet/"),
rpc_url: RPC_URL.into(),
},
})
}
pub(crate) async fn archival() -> Result<Self> {
let client = Client::new(ARCHIVAL_URL);
client.wait_for_rpc().await?;
Ok(Self {
client,
info: Info {
name: "testnet-archival".into(),
root_id: AccountId::from_str("testnet").unwrap(),
keystore_path: PathBuf::from(".near-credentials/testnet/"),
rpc_url: ARCHIVAL_URL.into(),
},
})
}
}
impl std::fmt::Debug for Testnet {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Testnet")
.field("root_id", &self.info.root_id)
.field("rpc_url", &self.info.rpc_url)
.finish()
}
}
impl AllowDevAccountCreation for Testnet {}
#[async_trait]
impl TopLevelAccountCreator for Testnet {
async fn create_tla(
&self,
worker: Worker<dyn Network>,
id: AccountId,
sk: SecretKey,
) -> Result<Execution<Account>> {
let url = Url::parse(HELPER_URL).unwrap();
tool::url_create_account(url, id.clone(), sk.public_key()).await?;
let signer = InMemorySigner::from_secret_key(id, sk);
Ok(Execution {
result: Account::new(signer, worker),
details: ExecutionFinalResult {
total_gas_burnt: 0,
status: near_primitives::views::FinalExecutionStatus::SuccessValue(Vec::new()),
details: ExecutionDetails {
transaction: ExecutionOutcome {
block_hash: CryptoHash::default(),
logs: Vec::new(),
receipt_ids: Vec::new(),
gas_burnt: 0,
tokens_burnt: 0,
executor_id: "testnet".parse().unwrap(),
status: ExecutionStatusView::SuccessValue(Vec::new()),
},
receipts: Vec::new(),
},
},
})
}
async fn create_tla_and_deploy(
&self,
worker: Worker<dyn Network>,
id: AccountId,
sk: SecretKey,
wasm: &[u8],
) -> Result<Execution<Contract>> {
let signer = InMemorySigner::from_secret_key(id.clone(), sk.clone());
let account = self.create_tla(worker, id.clone(), sk).await?;
let outcome = self.client().deploy(&signer, &id, wasm.into()).await?;
Ok(Execution {
result: Contract::account(account.into_result()?),
details: ExecutionFinalResult::from_view(outcome),
})
}
}
impl NetworkClient for Testnet {
fn client(&self) -> &Client {
&self.client
}
}
impl NetworkInfo for Testnet {
fn info(&self) -> &Info {
&self.info
}
}