/
main_test_uni.rs
454 lines (365 loc) · 16.7 KB
/
main_test_uni.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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
use std::str::FromStr;
use fuels::prelude::ViewOnlyAccount;
use fuels::types::Address;
use src20_sdk::token_factory_abi_calls;
use crate::utils::contracts_utils::market_utils::{
deploy_market, get_market_config, market_abi_calls,
};
use crate::utils::contracts_utils::oracle_utils::{deploy_oracle, oracle_abi_calls};
use crate::utils::contracts_utils::token_utils::deploy_tokens;
use crate::utils::number_utils::parse_units;
use crate::utils::{debug_state, init_wallets, print_case_title, print_title};
// Multiplies all values by this number
// It is necessary in order to test how the protocol works with large amounts
const AMOUNT_COEFFICIENT: u64 = 10u64.pow(0);
#[tokio::test]
async fn main_test() {
let scale_6 = 10u64.pow(6) as f64;
let scale_9 = 10u64.pow(9) as f64;
print_title("Main test with UNI");
//--------------- WALLETS ---------------
let wallets = init_wallets().await;
let admin = &wallets[0];
let alice = &wallets[1];
let bob = &wallets[2];
let chad = &wallets[3];
let alice_address = Address::from(alice.address());
let bob_address = Address::from(bob.address());
let chad_address = Address::from(chad.address());
println!("alice_address = {alice_address}");
println!("bob_address = {bob_address}");
println!("chad_address = {chad_address}");
//--------------- ORACLE ---------------
let oracle = deploy_oracle(&admin).await;
//--------------- TOKENS ---------------
let (assets, asset_configs, factory) = deploy_tokens(&admin, oracle.contract_id().into()).await;
let usdc = assets.get("USDC").unwrap();
let uni = assets.get("UNI").unwrap();
//--------------- MARKET ---------------
let market_config = get_market_config(
admin.address().into(),
admin.address().into(),
usdc.bits256,
usdc.decimals,
oracle.contract_id().into(),
// assets.get("SWAY").unwrap().bits256,
);
// debug step
let step: Option<u64> = Option::Some(10000);
let market = deploy_market(&admin, market_config, step).await;
let sway_bits256 = market_abi_calls::get_reward_token_asset_id(&market).await;
// println!("SWAY Address = {:?}", AssetId::from(sway_bits256.0));
//--------------- SETUP COLLATERALS ---------------
for config in &asset_configs {
let mut config = config.clone();
// replace swaylend token into reward token
if config.asset_id == assets.get("SWAY").unwrap().bits256 {
config.asset_id = sway_bits256
}
market_abi_calls::add_collateral_asset(&market, &config)
.await
.unwrap();
}
// ==================== Set oracle prices ====================
for asset in &assets {
// replace swaylend token into reward token
let asset_id = if asset.1.symbol == "SWAY" {
sway_bits256
} else {
asset.1.bits256
};
let price = asset.1.default_price * 10u64.pow(9);
oracle_abi_calls::set_price(&oracle, asset_id, price).await;
}
// =================================================
// ==================== Step #0 ====================
// 👛 Wallet: Bob 🧛
// 🤙 Call: supply_base
// 💰 Amount: 100.00 USDC
let amount = parse_units(100 * AMOUNT_COEFFICIENT, usdc.decimals);
let log_amount = format!("{} USDC", amount as f64 / scale_6);
print_case_title(0, "Bob", "supply_base", log_amount.as_str());
println!("💸 Bob + {log_amount}");
// Transfer of 100 USDC to the Bob's wallet
token_factory_abi_calls::mint(&factory, bob_address, &usdc.symbol, amount)
.await
.unwrap();
let balance = bob.get_asset_balance(&usdc.asset_id).await.unwrap();
assert!(balance == amount);
// Bob calls supply_base
let inst = market.with_account(bob.clone()).unwrap();
market_abi_calls::supply_base(&inst, usdc.asset_id, amount)
.await
.unwrap();
// Сheck supply balance equal to 400 USDC
let (supply_balance, _) = market_abi_calls::get_user_supply_borrow(&inst, bob_address).await;
assert!(supply_balance == amount);
debug_state(&market, &wallets, usdc, uni).await;
market_abi_calls::debug_increment_timestamp(&market).await;
// =================================================
// ==================== Step #1 ====================
// 👛 Wallet: Alice 🦹
// 🤙 Call: supply_collateral
// 💰 Amount: 40.00 UNI ~ $200.00
let amount = parse_units(40 * AMOUNT_COEFFICIENT, uni.decimals);
let log_amount = format!("{} UNI", amount as f64 / scale_9);
print_case_title(1, "Alice", "supply_collateral", log_amount.as_str());
println!("💸 Alice + {log_amount}");
// Transfer of 40 UNI to the Alice's wallet
token_factory_abi_calls::mint(&factory, alice_address, &uni.symbol, amount)
.await
.unwrap();
let balance = alice.get_asset_balance(&uni.asset_id).await.unwrap();
assert!(balance == amount);
// Alice calls supply_collateral
let inst = market.with_account(alice.clone()).unwrap();
market_abi_calls::supply_collateral(&inst, uni.asset_id, amount)
.await
.unwrap();
// Сheck supply balance equal to 40 UNI
let res = market_abi_calls::get_user_collateral(&inst, alice_address, uni.bits256).await;
assert!(res == amount);
debug_state(&market, &wallets, usdc, uni).await;
market_abi_calls::debug_increment_timestamp(&market).await;
// =================================================
// ==================== Step #2 ====================
// 👛 Wallet: Alice 🦹
// 🤙 Call: withdraw_base
// 💰 Amount: 50.00 USDC
let amount = parse_units(50 * AMOUNT_COEFFICIENT, usdc.decimals);
let log_amount = format!("{} USDC", amount as f64 / scale_6);
print_case_title(2, "Alice", "withdraw_base", log_amount.as_str());
// Alice calls withdraw_base
let inst = market.with_account(alice.clone()).unwrap();
market_abi_calls::withdraw_base(&inst, &[&oracle], amount)
.await
.unwrap();
// USDC balance check
let balance = alice.get_asset_balance(&usdc.asset_id).await.unwrap();
assert!(balance == amount);
debug_state(&market, &wallets, usdc, uni).await;
market_abi_calls::debug_increment_timestamp(&market).await;
// =================================================
// ==================== Step #3 ====================
// 👛 Wallet: Chad 🤵
// 🤙 Call: supply_collateral
// 💰 Amount: 60.00 UNI ~ $300.00
let amount = parse_units(60 * AMOUNT_COEFFICIENT, uni.decimals);
let log_amount = format!("{} UNI", amount as f64 / scale_9);
print_case_title(3, "Chad", "supply_collateral", log_amount.as_str());
println!("💸 Chad + {log_amount}");
// Transfer of 60 UNI to the Chad's wallet
token_factory_abi_calls::mint(&factory, chad_address, &uni.symbol, amount)
.await
.unwrap();
let balance = chad.get_asset_balance(&uni.asset_id).await.unwrap();
assert!(balance == amount);
//Chad calls supply_collateral
let inst = market.with_account(chad.clone()).unwrap();
market_abi_calls::supply_collateral(&inst, uni.asset_id, amount)
.await
.unwrap();
//Сheck supply balance equal to 60 UNI
let res = market_abi_calls::get_user_collateral(&inst, chad_address, uni.bits256).await;
assert!(res == amount);
debug_state(&market, &wallets, usdc, uni).await;
market_abi_calls::debug_increment_timestamp(&market).await;
// =================================================
// ==================== Step #4 ====================
// 👛 Wallet: Chad 🤵
// 🤙 Call: supply_base
// 💰 Amount: 200.00 USDC
let amount = parse_units(200 * AMOUNT_COEFFICIENT, usdc.decimals);
let log_amount = format!("{} USDC", amount as f64 / scale_6);
print_case_title(4, "Chad", "supply_base", log_amount.as_str());
println!("💸 Chad + {log_amount}");
// Transfer of 200 USDC to the Chad's wallet
token_factory_abi_calls::mint(&factory, chad_address, &usdc.symbol, amount)
.await
.unwrap();
let balance = chad.get_asset_balance(&usdc.asset_id).await.unwrap();
assert!(balance == amount);
// Chad calls supply_base
let inst = market.with_account(chad.clone()).unwrap();
market_abi_calls::supply_base(&inst, usdc.asset_id, amount)
.await
.unwrap();
//Сheck supply balance equal to 200 USDC
let (supply_balance, _) = market_abi_calls::get_user_supply_borrow(&inst, chad_address).await;
assert!(amount - 5 < supply_balance);
debug_state(&market, &wallets, usdc, uni).await;
market_abi_calls::debug_increment_timestamp(&market).await;
// =================================================
// ==================== Step #5 ====================
// 👛 Wallet: Alice 🦹
// 🤙 Call: withdraw_base
// 💰 Amount: ~99.96 USDC (available_to_borrow)
let amount = market_abi_calls::available_to_borrow(&market, &[&oracle], alice_address).await;
let log_amount = format!("{} USDC", amount as f64 / scale_6);
print_case_title(5, "Alice", "withdraw_base", log_amount.as_str());
//Alice calls withdraw_base
let inst = market.with_account(alice.clone()).unwrap();
market_abi_calls::withdraw_base(&inst, &[&oracle], amount - 1)
.await
.unwrap();
//available_to_borrow should be 0 and we cannout do withdraw_base more
let res = market_abi_calls::available_to_borrow(&market, &[&oracle], alice_address).await;
assert!(res == 1);
let res = market_abi_calls::withdraw_base(&inst, &[&oracle], 2)
.await
.is_err();
assert!(res);
// USDC balance should be amount + 50 USDC from case #2
let balance = alice.get_asset_balance(&usdc.asset_id).await.unwrap();
assert!(balance == amount - 1 + parse_units(50 * AMOUNT_COEFFICIENT, usdc.decimals));
debug_state(&market, &wallets, usdc, uni).await;
market_abi_calls::debug_increment_timestamp(&market).await;
// =================================================
// ==================== Step #6 ====================
// 👛 Wallet: Admin 🗿
// 🤙 Drop of collateral price
// 💰 Amount: -10%
print_case_title(6, "Admin", "Drop of collateral price", "-10%");
let res = oracle_abi_calls::get_price(&oracle, uni.bits256).await;
let new_price = (res.price as f64 * 0.9) as u64;
println!(
"🔻 UNI price drops: ${} -> ${}",
res.price as f64 / scale_9,
new_price as f64 / scale_9
);
oracle_abi_calls::set_price(&oracle, uni.bits256, new_price).await;
let res = oracle_abi_calls::get_price(&oracle, uni.bits256).await;
assert!(new_price == res.price);
debug_state(&market, &wallets, usdc, uni).await;
market_abi_calls::debug_increment_timestamp(&market).await;
// =================================================
// ==================== Step #7 ====================
// 👛 Wallet: Bob 🦹
// 🤙 Call: absorb
// 🔥 Target: Alice
print_case_title(7, "Bob", "absorb", "Alice");
assert!(market_abi_calls::is_liquidatable(&market, &[&oracle], alice_address).await);
let inst = market.with_account(bob.clone()).unwrap();
market_abi_calls::absorb(&inst, &[&oracle], vec![alice_address])
.await
.unwrap();
//Check if absorb was ok
let (_, borrow) = market_abi_calls::get_user_supply_borrow(&market, alice_address).await;
assert!(borrow == 0);
let amount = market_abi_calls::get_user_collateral(&market, alice_address, uni.bits256).await;
assert!(amount == 0);
debug_state(&market, &wallets, usdc, uni).await;
market_abi_calls::debug_increment_timestamp(&market).await;
// =================================================
// ==================== Step #8 ====================
// 👛 Wallet: Bob 🤵
// 🤙 Call: buy_collateral
// 💰 Amount: 172.44 USDC
let inst = market.with_account(bob.clone()).unwrap();
let reservs = market_abi_calls::get_collateral_reserves(&market, uni.bits256).await;
assert!(!reservs.negative);
let reservs = reservs.value;
println!("reserves = {:?}", reservs);
let amount =
market_abi_calls::collateral_value_to_sell(&market, &[&oracle], uni.bits256, reservs).await;
let log_amount = format!("{} USDC", amount as f64 / scale_6);
print_case_title(8, "Bob", "buy_collateral", log_amount.as_str());
// Transfer of amount to the wallet
token_factory_abi_calls::mint(&factory, bob_address, &usdc.symbol, amount)
.await
.unwrap();
//Сheck balance
let balance = bob.get_asset_balance(&usdc.asset_id).await.unwrap();
assert!(balance == amount);
// Bob calls buy_collateral
let addr = bob_address;
market_abi_calls::buy_collateral(
&inst,
&[&oracle],
usdc.asset_id,
amount,
uni.bits256,
1,
addr,
)
.await
.unwrap();
//Check
let balance = bob.get_asset_balance(&uni.asset_id).await.unwrap();
assert!(balance == 40_000_000_000 * AMOUNT_COEFFICIENT);
debug_state(&market, &wallets, usdc, uni).await;
market_abi_calls::debug_increment_timestamp(&market).await;
// =================================================
// ==================== Step #9 ====================
// 👛 Wallet: Bob 🧛
// 🤙 Call: withdraw_base
// 💰 Amount: 100.021671 USDC
let (amount, _) = market_abi_calls::get_user_supply_borrow(&market, bob_address).await;
let log_amount = format!("{} USDC", amount as f64 / scale_6);
print_case_title(9, "Bob", "withdraw_base", log_amount.as_str());
//Bob calls withdraw_base
let inst = market.with_account(bob.clone()).unwrap();
market_abi_calls::withdraw_base(&inst, &[&oracle], amount)
.await
.unwrap();
// USDC balance check
let (supplied, _) = market_abi_calls::get_user_supply_borrow(&market, bob_address).await;
assert!(supplied == 0);
assert!(bob.get_asset_balance(&usdc.asset_id).await.unwrap() == amount);
debug_state(&market, &wallets, usdc, uni).await;
market_abi_calls::debug_increment_timestamp(&market).await;
// =================================================
// ==================== Step #10 ====================
// 👛 Wallet: Chad 🧛
// 🤙 Call: withdraw_base
// 💰 Amount: 200.0233392 USDC
let (amount, _) = market_abi_calls::get_user_supply_borrow(&market, chad_address).await;
let log_amount = format!("{} USDC", amount as f64 / scale_6);
print_case_title(10, "Chad", "withdraw_base", log_amount.as_str());
//Chad calls withdraw_base
let inst = market.with_account(chad.clone()).unwrap();
market_abi_calls::withdraw_base(&inst, &[&oracle], amount)
.await
.unwrap();
// USDC balance check
let (supplied, _) = market_abi_calls::get_user_supply_borrow(&market, chad_address).await;
assert!(supplied == 0);
assert!(chad.get_asset_balance(&usdc.asset_id).await.unwrap() == amount);
debug_state(&market, &wallets, usdc, uni).await;
market_abi_calls::debug_increment_timestamp(&market).await;
// =================================================
// ==================== Step #11 ====================
// 👛 Wallet: Alice 🧛
// 🤙 Call: withdraw_base
// 💰 Amount: 17.276598 USDC
let (amount, _) = market_abi_calls::get_user_supply_borrow(&market, alice_address).await;
let log_amount = format!("{} USDC", amount as f64 / scale_6);
print_case_title(11, "Alice", "withdraw_base", log_amount.as_str());
//Alice calls withdraw_base
let inst = market.with_account(alice.clone()).unwrap();
market_abi_calls::withdraw_base(&inst, &[&oracle], amount)
.await
.unwrap();
// USDC balance check
let (supplied, _) = market_abi_calls::get_user_supply_borrow(&market, alice_address).await;
assert!(supplied == 0);
debug_state(&market, &wallets, usdc, uni).await;
market_abi_calls::debug_increment_timestamp(&market).await;
// =================================================
// ==================== Step #12 ====================
// 👛 Wallet: Chad 🤵
// 🤙 Call: withdraw_collateral
// 💰 Amount: 270 UNI
let amount = market_abi_calls::get_user_collateral(&market, chad_address, uni.bits256).await;
let log_amount = format!("{} UNI", amount as f64 / scale_9);
print_case_title(12, "Chad", "withdraw_collateral", log_amount.as_str());
//Chad calls withdraw_base
let inst = market.with_account(chad.clone()).unwrap();
market_abi_calls::withdraw_collateral(&inst, &[&oracle], uni.bits256, amount)
.await
.unwrap();
// UNI balance check
let balance = chad.get_asset_balance(&uni.asset_id).await.unwrap();
assert!(balance == amount);
debug_state(&market, &wallets, usdc, uni).await;
}