Skip to content

Commit

Permalink
chore: finish reserves tests
Browse files Browse the repository at this point in the history
Signed-off-by: Urban Vidovič <[email protected]>
  • Loading branch information
pseudobun committed Aug 21, 2024
1 parent ee8e5f0 commit 610bbbf
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 4 deletions.
142 changes: 138 additions & 4 deletions contracts/market/tests/local_tests/scenarios/reserves.rs
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, &eth)
.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, &eth)
.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, &eth)
.await
.unwrap();
}
1 change: 1 addition & 0 deletions libs/market_sdk/src/market_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?)
}
Expand Down

0 comments on commit 610bbbf

Please sign in to comment.