From 610bbbf38cd40e2ac416e054453b627d7c73ba92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Urban=20Vidovic=CC=8C?= Date: Wed, 21 Aug 2024 16:46:55 +0200 Subject: [PATCH] chore: finish reserves tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Urban Vidovič --- .../tests/local_tests/scenarios/reserves.rs | 142 +++++++++++++++++- libs/market_sdk/src/market_utils.rs | 1 + 2 files changed, 139 insertions(+), 4 deletions(-) diff --git a/contracts/market/tests/local_tests/scenarios/reserves.rs b/contracts/market/tests/local_tests/scenarios/reserves.rs index db1143f4..82ba4d22 100644 --- a/contracts/market/tests/local_tests/scenarios/reserves.rs +++ b/contracts/market/tests/local_tests/scenarios/reserves.rs @@ -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: + 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(); +} diff --git a/libs/market_sdk/src/market_utils.rs b/libs/market_sdk/src/market_utils.rs index b7b4a1b1..dc10bd12 100644 --- a/libs/market_sdk/src/market_utils.rs +++ b/libs/market_sdk/src/market_utils.rs @@ -539,6 +539,7 @@ impl MarketContract { .methods() .withdraw_reserves(to, amount.into()) .with_tx_policies(tx_policies) + .with_variable_output_policy(VariableOutputPolicy::Exactly(2)) .call() .await?) }