-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Urban Vidovič <[email protected]>
- Loading branch information
Showing
2 changed files
with
139 additions
and
4 deletions.
There are no files selected for viewing
142 changes: 138 additions & 4 deletions
142
contracts/market/tests/local_tests/scenarios/reserves.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,141 @@ | ||
// **Scenario #5 - Reserves** | ||
use crate::utils::{setup, TestData}; | ||
use market::PriceDataUpdate; | ||
use market_sdk::{convert_i256_to_i128, parse_units}; | ||
const AMOUNT_COEFFICIENT: u64 = 10u64.pow(0); | ||
const SCALE_6: f64 = 10u64.pow(6) as f64; | ||
const SCALE_9: f64 = 10u64.pow(9) as f64; | ||
|
||
// Description: Repeat scenario #4 multiple times and check the reserve is receiving base asset. Verify that governor can withdraw reserves. | ||
#[tokio::test] | ||
async fn reserves_test() { | ||
let TestData { | ||
wallets, | ||
bob, | ||
bob_address, | ||
alice, | ||
alice_address, | ||
market, | ||
assets, | ||
usdc, | ||
usdc_contract, | ||
oracle, | ||
price_feed_ids, | ||
publish_time, | ||
prices, | ||
eth, | ||
admin, | ||
admin_address, | ||
.. | ||
} = setup().await; | ||
|
||
// Code: <insert link to the test file> | ||
let price_data_update = PriceDataUpdate { | ||
update_fee: 0, | ||
price_feed_ids, | ||
publish_times: vec![publish_time; assets.len()], | ||
update_data: oracle.create_update_data(&prices).await.unwrap(), | ||
}; | ||
|
||
// Steps: | ||
for _ in 0..3 { | ||
// Scenario #4 Steps | ||
|
||
// Step 0: Alice supplies 5000 USDC | ||
let alice_supply_amount = parse_units(4000 * AMOUNT_COEFFICIENT, usdc.decimals); | ||
usdc_contract | ||
.mint(alice_address, alice_supply_amount) | ||
.await | ||
.unwrap(); | ||
let res = market | ||
.with_account(&alice) | ||
.await | ||
.unwrap() | ||
.supply_base(usdc.asset_id, alice_supply_amount) | ||
.await; | ||
assert!(res.is_ok()); | ||
market.debug_increment_timestamp().await.unwrap(); | ||
|
||
// Step 1: Bob supplies 2 ETH | ||
let bob_supply_amount = parse_units(2 * AMOUNT_COEFFICIENT, eth.decimals); | ||
let res = market | ||
.with_account(&bob) | ||
.await | ||
.unwrap() | ||
.supply_collateral(eth.asset_id, bob_supply_amount) | ||
.await; | ||
assert!(res.is_ok()); | ||
market.debug_increment_timestamp().await.unwrap(); | ||
|
||
// Step 2: Bob borrows 1000 USDC | ||
let borrow_amount = parse_units(4000 * AMOUNT_COEFFICIENT, usdc.decimals); | ||
let res = market | ||
.with_account(&bob) | ||
.await | ||
.unwrap() | ||
.withdraw_base(&[&oracle.instance], borrow_amount, &price_data_update) | ||
.await; | ||
assert!(res.is_ok()); | ||
|
||
// Simulate time passing to accrue interest | ||
market.debug_increment_timestamp().await.unwrap(); | ||
|
||
// Step 3: Bob repays 1000 USDC | ||
let repay_amount = parse_units(10030 * AMOUNT_COEFFICIENT, usdc.decimals); | ||
usdc_contract.mint(bob_address, repay_amount).await.unwrap(); | ||
let res = market | ||
.with_account(&bob) | ||
.await | ||
.unwrap() | ||
.supply_base(usdc.asset_id, repay_amount) | ||
.await; | ||
assert!(res.is_ok()); | ||
market.debug_increment_timestamp().await.unwrap(); | ||
|
||
// Step 4: Bob withdraws 2 ETH | ||
let res = market | ||
.with_account(&bob) | ||
.await | ||
.unwrap() | ||
.withdraw_collateral( | ||
&[&oracle.instance], | ||
eth.bits256, | ||
bob_supply_amount, | ||
&price_data_update, | ||
) | ||
.await; | ||
assert!(res.is_ok()); | ||
market.debug_increment_timestamp().await.unwrap(); | ||
|
||
market | ||
.print_debug_state(&wallets, &usdc, ð) | ||
.await | ||
.unwrap(); | ||
} | ||
|
||
// Check reserves | ||
let reserves = market.get_reserves().await.unwrap().value; | ||
let normalized_reserves: u64 = convert_i256_to_i128(reserves).try_into().unwrap(); | ||
assert!(normalized_reserves > 0); | ||
println!("Reserves: {:?}", normalized_reserves); | ||
|
||
// Governor withdraws reserves; | ||
let res = market | ||
.with_account(&admin) | ||
.await | ||
.unwrap() | ||
.withdraw_reserves(admin_address, normalized_reserves) | ||
.await; | ||
assert!(res.is_ok()); | ||
|
||
market | ||
.print_debug_state(&wallets, &usdc, ð) | ||
.await | ||
.unwrap(); | ||
|
||
// Check if reserves are withdrawn | ||
let new_reserves = market.get_reserves().await.unwrap().value; | ||
let normalized_reserves: u64 = convert_i256_to_i128(new_reserves).try_into().unwrap(); | ||
assert!(normalized_reserves == 0); | ||
|
||
market | ||
.print_debug_state(&wallets, &usdc, ð) | ||
.await | ||
.unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters