-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathcontracts.rs
More file actions
307 lines (267 loc) · 8.8 KB
/
Copy pathcontracts.rs
File metadata and controls
307 lines (267 loc) · 8.8 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
use aleph_client::{
contract::ContractInstance, AnyConnection, Balance, Connection, SignedConnection,
};
use anyhow::{Context, Result};
use sp_core::crypto::{AccountId32 as AccountId, Ss58Codec};
use crate::Config;
/// A wrapper around the simple dex contract.
///
/// The methods on this type match contract methods.
#[derive(Debug)]
pub(super) struct SimpleDexInstance {
contract: ContractInstance,
}
impl<'a> From<&'a SimpleDexInstance> for &'a ContractInstance {
fn from(dex: &'a SimpleDexInstance) -> Self {
&dex.contract
}
}
impl<'a> From<&'a SimpleDexInstance> for AccountId {
fn from(dex: &'a SimpleDexInstance) -> Self {
dex.contract.address().clone()
}
}
impl SimpleDexInstance {
pub fn new(config: &Config) -> Result<Self> {
let dex_address = config
.test_case_params
.simple_dex
.clone()
.context("Simple dex address not set.")?;
let dex_address = AccountId::from_string(&dex_address)?;
let metadata_path = config
.test_case_params
.simple_dex_metadata
.clone()
.context("Simple dex metadata not set")?;
Ok(Self {
contract: ContractInstance::new(dex_address, &metadata_path)?,
})
}
pub fn add_swap_pair(
&self,
conn: &SignedConnection,
from: AccountId,
to: AccountId,
) -> Result<()> {
self.contract
.contract_exec(conn, "add_swap_pair", &[&from.to_string(), &to.to_string()])
}
pub fn deposit(
&self,
conn: &SignedConnection,
amounts: &[(&PSP22TokenInstance, Balance)],
) -> Result<()> {
let deposits = amounts
.iter()
.map(|(token, amount)| {
let address: AccountId = (*token).try_into()?;
Ok(format!("deposits ({:}, {:})", address, amount))
})
.collect::<Result<Vec<String>>>()?;
self.contract
.contract_exec(conn, "deposit", &[format!("[{:}]", deposits.join(","))])
}
pub fn out_given_in<C: AnyConnection>(
&self,
conn: &C,
token_in: &PSP22TokenInstance,
token_out: &PSP22TokenInstance,
amount_token_in: Balance,
min_amount_token_out: Option<Balance>,
) -> Result<Balance> {
let token_in: AccountId = token_in.into();
let token_out: AccountId = token_out.into();
self.contract
.contract_read(
conn,
"out_given_in",
&[
token_in.to_string(),
token_out.to_string(),
amount_token_in.to_string(),
min_amount_token_out.map_or("None".to_string(), |x| format!("Some({:})", x)),
],
)?
.try_into()?
}
pub fn swap(
&self,
conn: &SignedConnection,
token_in: &PSP22TokenInstance,
amount_token_in: Balance,
token_out: &PSP22TokenInstance,
min_amount_token_out: Balance,
) -> Result<()> {
let token_in: AccountId = token_in.into();
let token_out: AccountId = token_out.into();
self.contract.contract_exec(
conn,
"swap",
&[
token_in.to_string(),
token_out.to_string(),
amount_token_in.to_string(),
min_amount_token_out.to_string(),
],
)
}
}
/// A wrapper around a button game contract.
///
/// The methods on this type match contract methods.
#[derive(Debug)]
pub(super) struct ButtonInstance {
contract: ContractInstance,
}
impl ButtonInstance {
pub fn new(config: &Config, button_address: &Option<String>) -> Result<Self> {
let button_address = button_address
.clone()
.context("Button game address not set.")?;
let button_address = AccountId::from_string(&button_address)?;
let metadata_path = config
.test_case_params
.button_game_metadata
.clone()
.context("Button game metadata path not set.")?;
Ok(Self {
contract: ContractInstance::new(button_address, &metadata_path)?,
})
}
pub fn deadline<C: AnyConnection>(&self, conn: &C) -> Result<u128> {
self.contract.contract_read0(conn, "deadline")?.try_into()
}
pub fn is_dead<C: AnyConnection>(&self, conn: &C) -> Result<bool> {
self.contract.contract_read0(conn, "is_dead")?.try_into()
}
pub fn ticket_token<C: AnyConnection>(&self, conn: &C) -> Result<AccountId> {
self.contract
.contract_read0(conn, "ticket_token")?
.try_into()
}
pub fn reward_token<C: AnyConnection>(&self, conn: &C) -> Result<AccountId> {
self.contract
.contract_read0(conn, "reward_token")?
.try_into()
}
pub fn marketplace<C: AnyConnection>(&self, conn: &C) -> Result<AccountId> {
self.contract
.contract_read0(conn, "marketplace")?
.try_into()
}
pub fn press(&self, conn: &SignedConnection) -> Result<()> {
self.contract.contract_exec0(conn, "press")
}
pub fn reset(&self, conn: &SignedConnection) -> Result<()> {
self.contract.contract_exec0(conn, "reset")
}
}
impl<'a> From<&'a ButtonInstance> for &'a ContractInstance {
fn from(button: &'a ButtonInstance) -> Self {
&button.contract
}
}
impl From<&ButtonInstance> for AccountId {
fn from(button: &ButtonInstance) -> Self {
button.contract.address().clone()
}
}
/// A wrapper around a PSP22 contract.
///
/// The methods on this type match contract methods.
#[derive(Debug)]
pub(super) struct PSP22TokenInstance {
contract: ContractInstance,
}
impl PSP22TokenInstance {
pub fn new(address: AccountId, metadata_path: &Option<String>) -> Result<Self> {
let metadata_path = metadata_path
.as_ref()
.context("PSP22Token metadata not set.")?;
Ok(Self {
contract: ContractInstance::new(address, metadata_path)?,
})
}
pub fn transfer(&self, conn: &SignedConnection, to: &AccountId, amount: Balance) -> Result<()> {
self.contract.contract_exec(
conn,
"PSP22::transfer",
&[to.to_string(), amount.to_string(), "0x00".to_string()],
)
}
pub fn mint(&self, conn: &SignedConnection, to: &AccountId, amount: Balance) -> Result<()> {
self.contract.contract_exec(
conn,
"PSP22Mintable::mint",
&[to.to_string(), amount.to_string()],
)
}
pub fn approve(
&self,
conn: &SignedConnection,
spender: &AccountId,
value: Balance,
) -> Result<()> {
self.contract.contract_exec(
conn,
"PSP22::approve",
&[spender.to_string(), value.to_string()],
)
}
pub fn balance_of(&self, conn: &Connection, account: &AccountId) -> Result<Balance> {
self.contract
.contract_read(conn, "PSP22::balance_of", &[account.to_string()])?
.try_into()
}
}
impl<'a> From<&'a PSP22TokenInstance> for &'a ContractInstance {
fn from(token: &'a PSP22TokenInstance) -> Self {
&token.contract
}
}
impl From<&PSP22TokenInstance> for AccountId {
fn from(token: &PSP22TokenInstance) -> AccountId {
token.contract.address().clone()
}
}
/// A wrapper around a marketplace contract instance.
///
/// The methods on this type match contract methods.
#[derive(Debug)]
pub(super) struct MarketplaceInstance {
contract: ContractInstance,
}
impl MarketplaceInstance {
pub fn new(address: AccountId, metadata_path: &Option<String>) -> Result<Self> {
Ok(Self {
contract: ContractInstance::new(
address,
metadata_path
.as_ref()
.context("Marketplace metadata not set.")?,
)?,
})
}
pub fn reset(&self, conn: &SignedConnection) -> Result<()> {
self.contract.contract_exec0(conn, "reset")
}
pub fn buy(&self, conn: &SignedConnection, max_price: Option<Balance>) -> Result<()> {
let max_price = max_price.map_or_else(|| "None".to_string(), |x| format!("Some({})", x));
self.contract
.contract_exec(conn, "buy", &[max_price.as_str()])
}
pub fn price<C: AnyConnection>(&self, conn: &C) -> Result<Balance> {
self.contract.contract_read0(conn, "price")?.try_into()
}
}
impl<'a> From<&'a MarketplaceInstance> for &'a ContractInstance {
fn from(marketplace: &'a MarketplaceInstance) -> Self {
&marketplace.contract
}
}
impl From<&MarketplaceInstance> for AccountId {
fn from(marketplace: &MarketplaceInstance) -> AccountId {
marketplace.contract.address().clone()
}
}