-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.rs
141 lines (125 loc) · 3.93 KB
/
utils.rs
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 aurora_release_repository::id::{Id, Status};
use near_sdk::json_types::Base64VecU8;
use near_sdk::serde::Deserialize;
use near_sdk::ONE_YOCTO;
use serde_json::json;
use std::str::FromStr;
use workspaces::result::ExecutionFinalResult;
use workspaces::{AccountId, Contract};
#[derive(Debug, Clone, Deserialize, Eq, PartialEq)]
pub struct CustomId {
pub version: String,
pub checksum: String,
}
#[derive(Debug, Clone, Deserialize, Eq, PartialEq)]
pub struct CustomIdStatus {
pub id: CustomId,
pub status: Status,
}
impl CustomIdStatus {
pub fn new(id: Id, status: Option<Status>) -> Self {
Self {
id: CustomId {
version: id.version.to_string(),
checksum: id.checksum.to_string(),
},
status: status.unwrap_or(Status::Released),
}
}
}
pub struct TestContract {
pub contract: Contract,
}
impl TestContract {
pub async fn new(owner_id: Option<&str>) -> anyhow::Result<TestContract> {
let contract = Self::deploy_contract().await?;
let owner_id: AccountId = if let Some(owner_id) = owner_id {
AccountId::from_str(owner_id).unwrap()
} else {
contract.as_account().id().clone()
};
let res = contract
.call("new")
.args_json((owner_id,))
.max_gas()
.transact()
.await?;
assert!(res.is_success());
Ok(Self { contract })
}
pub async fn deploy_contract() -> anyhow::Result<Contract> {
let worker = workspaces::sandbox()
.await
.map_err(|err| anyhow::anyhow!("Failed init sandbox: {:?}", err))?;
// Explicitly read contract file
let contract_data =
std::fs::read("target/wasm32-unknown-unknown/release/aurora_release_repository.wasm")
.unwrap_or_else(|_| {
panic!(
"Failed read contract in path: {:?} file: bin/aurora-eth-connector-test.wasm",
std::env::current_dir().unwrap()
)
});
let contract = worker.dev_deploy(&contract_data).await?;
Ok(contract)
}
pub async fn push(
&self,
version: &str,
code: &Base64VecU8,
latest: bool,
// Terra Gas
tgas: u64,
) -> anyhow::Result<ExecutionFinalResult> {
Ok(self
.contract
.call("push")
.args_json(json!({
"version": version,
"code": code,
"latest": latest
}))
.gas(tgas * 1_000_000_000_000)
.deposit(ONE_YOCTO)
.transact()
.await?)
}
pub async fn pull(&self, id: &Id) -> anyhow::Result<ExecutionFinalResult> {
let res = self
.contract
.call("pull")
.args_json(json!({
"id": id.to_string(),
}))
.gas(6_000_000_000_000)
.deposit(ONE_YOCTO)
.transact()
.await?;
Ok(res)
}
pub async fn get_blob(&self, id: &Id) -> anyhow::Result<Base64VecU8> {
Ok(self
.contract
.view("get_blob")
.args_json(json!({"id": id.to_string()}))
.await?
.json()?)
}
pub async fn get_latest(&self) -> anyhow::Result<CustomId> {
Ok(self.contract.view("latest").await.unwrap().json()?)
}
pub async fn get_status(&self, id: &Id) -> anyhow::Result<CustomIdStatus> {
Ok(self
.contract
.view("get_status")
.args_json(json!({"id": id.to_string()}))
.await?
.json()?)
}
pub async fn list(&self) -> anyhow::Result<Vec<CustomIdStatus>> {
Ok(self.contract.view("list").await.unwrap().json()?)
}
pub async fn yank_list(&self) -> anyhow::Result<Vec<CustomId>> {
Ok(self.contract.view("yank_list").await.unwrap().json()?)
}
}