|
1 |
| -#[cfg(test)] |
2 | 1 | mod farm;
|
3 |
| - |
4 |
| -#[cfg(test)] |
5 | 2 | mod psp22;
|
6 |
| - |
7 | 3 | #[cfg(test)]
|
8 |
| -mod tests { |
9 |
| - use crate::farm::{self, Farm as _, Instance as Farm}; |
10 |
| - use crate::psp22::{self, Instance as PSP22, PSP22 as _}; |
11 |
| - |
12 |
| - use anyhow::Result; |
13 |
| - use assert2::assert; |
14 |
| - use drink::{runtime::MinimalRuntime, session::Session, AccountId32}; |
15 |
| - use ink_primitives::AccountId; |
16 |
| - use ink_wrapper_types::{util::ToAccountId, Connection}; |
17 |
| - |
18 |
| - const ALICE: drink::AccountId32 = AccountId32::new([2u8; 32]); |
19 |
| - const BOB: drink::AccountId32 = AccountId32::new([1u8; 32]); |
20 |
| - |
21 |
| - fn alice() -> ink_primitives::AccountId { |
22 |
| - AsRef::<[u8; 32]>::as_ref(&ALICE).clone().into() |
23 |
| - } |
24 |
| - |
25 |
| - fn bob() -> ink_primitives::AccountId { |
26 |
| - AsRef::<[u8; 32]>::as_ref(&BOB).clone().into() |
27 |
| - } |
28 |
| - |
29 |
| - /// Uploads and creates a PSP22 instance with 1B*10^18 issuance and given names. |
30 |
| - /// Returns its AccountId casted to PSP22 interface. |
31 |
| - fn setup_psp22( |
32 |
| - session: &mut Session<MinimalRuntime>, |
33 |
| - name: String, |
34 |
| - symbol: String, |
35 |
| - caller: drink::AccountId32, |
36 |
| - ) -> PSP22 { |
37 |
| - let _code_hash = session.upload_code(psp22::upload()).unwrap(); |
38 |
| - |
39 |
| - let _ = session.set_actor(caller); |
40 |
| - |
41 |
| - let instance = PSP22::new( |
42 |
| - 1_000_000_000u128 * 10u128.pow(18), |
43 |
| - Some(name), |
44 |
| - Some(symbol), |
45 |
| - 18, |
46 |
| - ); |
47 |
| - |
48 |
| - session |
49 |
| - .instantiate(instance) |
50 |
| - .unwrap() |
51 |
| - .result |
52 |
| - .to_account_id() |
53 |
| - .into() |
54 |
| - } |
55 |
| - |
56 |
| - /// Uploads and creates a Farm instance with given pool_id and rewards. |
57 |
| - /// Returns its AccountId casted to Farm interface. |
58 |
| - fn setup_farm( |
59 |
| - session: &mut Session<MinimalRuntime>, |
60 |
| - pool_id: AccountId, |
61 |
| - rewards: Vec<AccountId>, |
62 |
| - caller: drink::AccountId32, |
63 |
| - ) -> Farm { |
64 |
| - let _code_hash = session.upload_code(farm::upload()).unwrap(); |
65 |
| - |
66 |
| - let _ = session.set_actor(caller); |
67 |
| - |
68 |
| - let instance = Farm::new(pool_id, rewards); |
69 |
| - |
70 |
| - session |
71 |
| - .instantiate(instance) |
72 |
| - .unwrap() |
73 |
| - .result |
74 |
| - .to_account_id() |
75 |
| - .into() |
76 |
| - } |
77 |
| - |
78 |
| - /// Increases allowance of given token to given spender by given amount. |
79 |
| - fn increase_allowance( |
80 |
| - session: &mut Session<MinimalRuntime>, |
81 |
| - token: AccountId, |
82 |
| - spender: AccountId, |
83 |
| - amount: u128, |
84 |
| - caller: drink::AccountId32, |
85 |
| - ) -> Result<()> { |
86 |
| - let _ = session.set_actor(caller); |
87 |
| - |
88 |
| - session |
89 |
| - .execute(PSP22::increase_allowance(&token.into(), spender, amount)) |
90 |
| - .unwrap() |
91 |
| - .result |
92 |
| - .unwrap() |
93 |
| - .unwrap(); |
94 |
| - |
95 |
| - Ok(()) |
96 |
| - } |
97 |
| - |
98 |
| - fn balance_of( |
99 |
| - session: &mut Session<MinimalRuntime>, |
100 |
| - token: AccountId, |
101 |
| - account: AccountId, |
102 |
| - ) -> u128 { |
103 |
| - session |
104 |
| - .query(PSP22::balance_of(&token.into(), account)) |
105 |
| - .unwrap() |
106 |
| - .result |
107 |
| - .unwrap() |
108 |
| - } |
109 |
| - |
110 |
| - fn get_timestamp(session: &mut Session<MinimalRuntime>) -> u64 { |
111 |
| - session.sandbox().get_timestamp() |
112 |
| - } |
113 |
| - |
114 |
| - fn set_timestamp(session: &mut Session<MinimalRuntime>, timestamp: u64) { |
115 |
| - session.sandbox().set_timestamp(timestamp); |
116 |
| - } |
117 |
| - |
118 |
| - /// Returns farm details. |
119 |
| - fn get_farm_details(session: &mut Session<MinimalRuntime>, farm: &Farm) -> farm::FarmDetails { |
120 |
| - session |
121 |
| - .query(farm.view_farm_details()) |
122 |
| - .unwrap() |
123 |
| - .result |
124 |
| - .unwrap() |
125 |
| - } |
126 |
| - |
127 |
| - /// Starts a farm with given start and end timestamps and rewards. |
128 |
| - fn setup_farm_start( |
129 |
| - session: &mut Session<MinimalRuntime>, |
130 |
| - farm: &Farm, |
131 |
| - start: u64, |
132 |
| - end: u64, |
133 |
| - rewards: Vec<u128>, |
134 |
| - caller: drink::AccountId32, |
135 |
| - ) -> Result<()> { |
136 |
| - let _ = session.set_actor(caller); |
137 |
| - |
138 |
| - session |
139 |
| - .execute(farm.owner_start_new_farm(start, end, rewards)) |
140 |
| - .unwrap() |
141 |
| - .result |
142 |
| - .unwrap() |
143 |
| - .unwrap(); |
144 |
| - |
145 |
| - Ok(()) |
146 |
| - } |
147 |
| - |
148 |
| - const ICE: &str = "ICE"; |
149 |
| - const WOOD: &str = "WOOD"; |
150 |
| - const SAND: &str = "SAND"; |
151 |
| - |
152 |
| - #[test] |
153 |
| - fn farm_start() { |
154 |
| - let mut session: Session<MinimalRuntime> = Session::new().expect("Init new Session"); |
155 |
| - |
156 |
| - let ice = setup_psp22(&mut session, ICE.to_string(), ICE.to_string(), BOB); |
157 |
| - |
158 |
| - let wood = { setup_psp22(&mut session, WOOD.to_string(), WOOD.to_string(), BOB) }; |
159 |
| - |
160 |
| - let sand = { setup_psp22(&mut session, SAND.to_string(), SAND.to_string(), BOB) }; |
161 |
| - |
162 |
| - let farm = setup_farm( |
163 |
| - &mut session, |
164 |
| - ice.into(), |
165 |
| - vec![wood.into(), sand.into()], |
166 |
| - BOB, |
167 |
| - ); |
168 |
| - |
169 |
| - let farm_details: farm::FarmDetails = get_farm_details(&mut session, &farm); |
170 |
| - |
171 |
| - let expected_details = farm::FarmDetails { |
172 |
| - pool_id: ice.into(), |
173 |
| - reward_tokens: vec![wood.into(), sand.into()], |
174 |
| - reward_rates: vec![0, 0], |
175 |
| - start: 0, |
176 |
| - end: 0, |
177 |
| - }; |
178 |
| - |
179 |
| - assert!(farm_details == expected_details); |
180 |
| - |
181 |
| - let now = get_timestamp(&mut session); |
182 |
| - set_timestamp(&mut session, now); |
183 |
| - let now_plus_100 = now + 100; |
184 |
| - let farm_start = now_plus_100; |
185 |
| - let farm_end = farm_start + 100; |
186 |
| - |
187 |
| - let call_result = session |
188 |
| - .query(farm.owner_start_new_farm(farm_start, farm_end, vec![100, 100])) |
189 |
| - .unwrap() |
190 |
| - .result |
191 |
| - .unwrap(); |
192 |
| - |
193 |
| - let insufficient_allowance = |
194 |
| - farm::FarmError::PSP22Error(farm::PSP22Error::InsufficientAllowance()); |
195 |
| - |
196 |
| - assert!( |
197 |
| - call_result == Err(insufficient_allowance), |
198 |
| - "Caller hasn't increased allowance to spend reward tokens for the farm" |
199 |
| - ); |
200 |
| - |
201 |
| - let rewards_amount = 100; |
202 |
| - |
203 |
| - increase_allowance(&mut session, wood.into(), farm.into(), rewards_amount, BOB).unwrap(); |
204 |
| - increase_allowance(&mut session, sand.into(), farm.into(), rewards_amount, BOB).unwrap(); |
205 |
| - |
206 |
| - let bob_wood_balance_before = balance_of(&mut session, wood.into(), bob()); |
207 |
| - let bob_sand_balance_before = balance_of(&mut session, sand.into(), bob()); |
208 |
| - let farm_wood_balance_before = balance_of(&mut session, wood.into(), farm.into()); |
209 |
| - let farm_sand_balance_before = balance_of(&mut session, sand.into(), farm.into()); |
210 |
| - |
211 |
| - let call_result = setup_farm_start( |
212 |
| - &mut session, |
213 |
| - &farm, |
214 |
| - farm_start, |
215 |
| - farm_end, |
216 |
| - vec![rewards_amount, rewards_amount], |
217 |
| - BOB, |
218 |
| - ); |
219 |
| - |
220 |
| - assert!(call_result.is_ok()); |
221 |
| - |
222 |
| - let expected_details = farm::FarmDetails { |
223 |
| - pool_id: ice.into(), |
224 |
| - reward_tokens: vec![wood.into(), sand.into()], |
225 |
| - reward_rates: vec![1, 1], |
226 |
| - start: farm_start, |
227 |
| - end: farm_end, |
228 |
| - }; |
229 |
| - |
230 |
| - let farm_details: farm::FarmDetails = get_farm_details(&mut session, &farm); |
231 |
| - |
232 |
| - assert!(farm_details == expected_details); |
233 |
| - |
234 |
| - let bob_wood_balance_after = balance_of(&mut session, wood.into(), bob()); |
235 |
| - let bob_sand_balance_after = balance_of(&mut session, sand.into(), bob()); |
236 |
| - assert!( |
237 |
| - bob_wood_balance_after == bob_wood_balance_before - rewards_amount, |
238 |
| - "Farm start must deduct rewards from the caller" |
239 |
| - ); |
240 |
| - assert!( |
241 |
| - bob_sand_balance_after == bob_sand_balance_before - rewards_amount, |
242 |
| - "Farm start must deduct rewards from the caller" |
243 |
| - ); |
244 |
| - let farm_wood_balance_after = balance_of(&mut session, wood.into(), farm.into()); |
245 |
| - let farm_sand_balance_after = balance_of(&mut session, sand.into(), farm.into()); |
246 |
| - assert!( |
247 |
| - farm_wood_balance_after == farm_wood_balance_before + rewards_amount, |
248 |
| - "Farm start must transfer rewards to the farm" |
249 |
| - ); |
250 |
| - assert!( |
251 |
| - farm_sand_balance_after == farm_sand_balance_before + rewards_amount, |
252 |
| - "Farm start must transfer rewards to the farm" |
253 |
| - ); |
254 |
| - } |
255 |
| -} |
| 4 | +mod tests; |
| 5 | +#[cfg(test)] |
| 6 | +mod utils; |
0 commit comments