From b7836e6e2df402ac8956c0992651432e72cb72a8 Mon Sep 17 00:00:00 2001 From: 0xTitan Date: Thu, 8 Feb 2024 22:57:42 +0100 Subject: [PATCH] feat:vessel_manager --- src/utils/shisui_base.cairo | 2 +- .../apply_pending_rewards.tree | 2 +- .../test_apply_pending_rewards.cairo | 2 +- .../check_recovery_mode.tree | 5 + .../test_check_recovery_mode.cairo | 112 +++++++++++++++++ .../get_borrowing_fee/get_borrowing_fee.tree | 5 + .../test_get_borrowing_fee.cairo | 69 +++++++++++ .../get_borrowing_rate.tree | 5 + .../test_get_borrowing_rate.cairo | 67 +++++++++++ .../get_current_icr/get_current_icr.tree | 5 +- .../test_get_current_icr.cairo | 27 +++++ .../get_nominal_icr/get_nominal_icr.tree | 5 +- .../test_get_nominal_icr.cairo | 29 ++++- .../test_get_redemption_fee.cairo | 11 +- .../test_get_redemption_fee_with_decay.cairo | 8 ++ .../test_get_redemption_rate.cairo | 19 +-- .../pools/vessel_manager/get_tcr/get_tcr.tree | 5 + .../vessel_manager/get_tcr/test_get_tcr.cairo | 113 ++++++++++++++++++ tests/lib.cairo | 14 ++- 19 files changed, 488 insertions(+), 17 deletions(-) create mode 100644 tests/integration/pools/vessel_manager/check_recovery_mode/check_recovery_mode.tree create mode 100644 tests/integration/pools/vessel_manager/check_recovery_mode/test_check_recovery_mode.cairo create mode 100644 tests/integration/pools/vessel_manager/get_borrowing_fee/get_borrowing_fee.tree create mode 100644 tests/integration/pools/vessel_manager/get_borrowing_fee/test_get_borrowing_fee.cairo create mode 100644 tests/integration/pools/vessel_manager/get_borrowing_rate/get_borrowing_rate.tree create mode 100644 tests/integration/pools/vessel_manager/get_borrowing_rate/test_get_borrowing_rate.cairo create mode 100644 tests/integration/pools/vessel_manager/get_tcr/get_tcr.tree create mode 100644 tests/integration/pools/vessel_manager/get_tcr/test_get_tcr.cairo diff --git a/src/utils/shisui_base.cairo b/src/utils/shisui_base.cairo index 01d5f97..0c93a94 100644 --- a/src/utils/shisui_base.cairo +++ b/src/utils/shisui_base.cairo @@ -11,6 +11,7 @@ use shisui::pools::{ active_pool::{IActivePoolDispatcher, IActivePoolDispatcherTrait}, default_pool::{IDefaultPoolDispatcher, IDefaultPoolDispatcherTrait} }; +use snforge_std::{PrintTrait}; #[derive(Drop, Clone, starknet::Store, Serde)] struct Colls { @@ -82,7 +83,6 @@ fn check_recovery_mode( addres_provider: IAddressProviderDispatcher, asset: ContractAddress, price: u256 ) -> bool { let tcr = get_TCR(addres_provider, asset, price); - return tcr < IAdminContractDispatcher { contract_address: addres_provider.get_address(AddressesKey::admin_contract) } diff --git a/tests/integration/pools/vessel_manager/apply_pending_rewards/apply_pending_rewards.tree b/tests/integration/pools/vessel_manager/apply_pending_rewards/apply_pending_rewards.tree index 275c2d1..f9adeed 100644 --- a/tests/integration/pools/vessel_manager/apply_pending_rewards/apply_pending_rewards.tree +++ b/tests/integration/pools/vessel_manager/apply_pending_rewards/apply_pending_rewards.tree @@ -1,6 +1,6 @@ test_apply_pending_rewards.cairo ├── when caller is not borrower or vessel manager operation -│ └── it should revert +│ └── it should revert with message Only vessel mngr op or borrower ├── when caller is vessel manager operation and vessel has no pending rewards │ ├── it should not update vessel │ ├── it should not raised VesselSnapshotsUpdated event diff --git a/tests/integration/pools/vessel_manager/apply_pending_rewards/test_apply_pending_rewards.cairo b/tests/integration/pools/vessel_manager/apply_pending_rewards/test_apply_pending_rewards.cairo index c6a27c8..aa518d5 100644 --- a/tests/integration/pools/vessel_manager/apply_pending_rewards/test_apply_pending_rewards.cairo +++ b/tests/integration/pools/vessel_manager/apply_pending_rewards/test_apply_pending_rewards.cairo @@ -47,7 +47,7 @@ fn when_caller_is_not_borrower_operation_it_should_revert() { } #[test] -fn when_vessel_has_no_pending_reward_should_return() { +fn when_vessel_has_no_pending_reward_it_should_not_update_vessel() { let ( borrower_operations, vessel_manager, diff --git a/tests/integration/pools/vessel_manager/check_recovery_mode/check_recovery_mode.tree b/tests/integration/pools/vessel_manager/check_recovery_mode/check_recovery_mode.tree new file mode 100644 index 0000000..d0d28e7 --- /dev/null +++ b/tests/integration/pools/vessel_manager/check_recovery_mode/check_recovery_mode.tree @@ -0,0 +1,5 @@ +test_check_recovery_mode.cairo +├── When TCR is greater or equals than CCR +│ └── it should return false +└── When TCR is lower than CCR + ├── it should return true \ No newline at end of file diff --git a/tests/integration/pools/vessel_manager/check_recovery_mode/test_check_recovery_mode.cairo b/tests/integration/pools/vessel_manager/check_recovery_mode/test_check_recovery_mode.cairo new file mode 100644 index 0000000..d13ccdf --- /dev/null +++ b/tests/integration/pools/vessel_manager/check_recovery_mode/test_check_recovery_mode.cairo @@ -0,0 +1,112 @@ +use tests::tests_lib::{deploy_main_contracts}; +use super::super::setup::open_vessel; +use shisui::core::{ + address_provider::{IAddressProviderDispatcher, IAddressProviderDispatcherTrait, AddressesKey}, + admin_contract::{IAdminContractDispatcher, IAdminContractDispatcherTrait}, + fee_collector::{IFeeCollectorDispatcher, IFeeCollectorDispatcherTrait}, + debt_token::{IDebtTokenDispatcher, IDebtTokenDispatcherTrait}, + price_feed::{IPriceFeedDispatcher, IPriceFeedDispatcherTrait}, +}; +use shisui::pools::{ + borrower_operations::{IBorrowerOperationsDispatcher, IBorrowerOperationsDispatcherTrait}, + vessel_manager::{IVesselManagerDispatcher, IVesselManagerDispatcherTrait}, +}; +use snforge_std::{ + start_prank, stop_prank, store, map_entry_address, CheatTarget, spy_events, SpyOn, EventSpy, + EventAssertions, start_mock_call, PrintTrait +}; +use starknet::{ContractAddress, contract_address_const, get_caller_address}; + + +#[test] +fn when_tcr_is_greater_or_equals_than_ccr_should_return_false() { + let ( + borrower_operations, + vessel_manager, + adress_provider, + admin_contract, + fee_collector, + debt_token, + price_feed, + pragma_mock, + active_pool, + default_pool, + asset, + vessel_manager_operations_address + ) = + deploy_main_contracts(); + + let mut asset_price: u256 = 1600_000000000000000000; + let deposit_amount: u256 = 1_890000000000000000; + let debt_token_amount: u256 = 2000_000000000000000000; + + let borrower = open_vessel( + asset, + price_feed, + admin_contract, + active_pool, + default_pool, + debt_token, + borrower_operations, + vessel_manager, + pragma_mock, + asset_price, + deposit_amount, + debt_token_amount + ); + + start_mock_call(active_pool.contract_address, 'get_asset_balance', 0_u256); + start_mock_call(default_pool.contract_address, 'get_asset_balance', 0_u256); + start_mock_call(active_pool.contract_address, 'get_debt_token_balance', 0_u256); + start_mock_call(default_pool.contract_address, 'get_debt_token_balance', 0_u256); + + let is_recovery = vessel_manager.check_recovery_mode(asset.contract_address, asset_price); + assert(!is_recovery, 'Wrong recovery mode'); +} + +#[test] +fn when_tcr_is_lower_than_ccr_should_return_true() { + let ( + borrower_operations, + vessel_manager, + adress_provider, + admin_contract, + fee_collector, + debt_token, + price_feed, + pragma_mock, + active_pool, + default_pool, + asset, + vessel_manager_operations_address + ) = + deploy_main_contracts(); + + let mut asset_price: u256 = 1600_000000000000000000; + let deposit_amount: u256 = 1_890000000000000000; + let debt_token_amount: u256 = 2000_000000000000000000; + + let borrower = open_vessel( + asset, + price_feed, + admin_contract, + active_pool, + default_pool, + debt_token, + borrower_operations, + vessel_manager, + pragma_mock, + asset_price, + deposit_amount, + debt_token_amount + ); + + start_mock_call(active_pool.contract_address, 'get_asset_balance', deposit_amount); + start_mock_call(default_pool.contract_address, 'get_asset_balance', 0_u256); + start_mock_call(active_pool.contract_address, 'get_debt_token_balance', debt_token_amount * 2); + start_mock_call(default_pool.contract_address, 'get_debt_token_balance', 0_u256); + + let is_recovery = vessel_manager.check_recovery_mode(asset.contract_address, asset_price); + assert(is_recovery, 'Wrong recovery mode'); +} + diff --git a/tests/integration/pools/vessel_manager/get_borrowing_fee/get_borrowing_fee.tree b/tests/integration/pools/vessel_manager/get_borrowing_fee/get_borrowing_fee.tree new file mode 100644 index 0000000..0b536ed --- /dev/null +++ b/tests/integration/pools/vessel_manager/get_borrowing_fee/get_borrowing_fee.tree @@ -0,0 +1,5 @@ +test_get_borrowing_fee.cairo +├── When borrowing fee is defined for asset +│ └── it should return borrowing fee based on debt +└── When borrowing fee is not defined for asset + └── it should return 0 \ No newline at end of file diff --git a/tests/integration/pools/vessel_manager/get_borrowing_fee/test_get_borrowing_fee.cairo b/tests/integration/pools/vessel_manager/get_borrowing_fee/test_get_borrowing_fee.cairo new file mode 100644 index 0000000..d2ef07c --- /dev/null +++ b/tests/integration/pools/vessel_manager/get_borrowing_fee/test_get_borrowing_fee.cairo @@ -0,0 +1,69 @@ +use starknet::{ContractAddress, contract_address_const, get_caller_address}; +use super::super::setup::open_vessel; +use shisui::core::{ + address_provider::{IAddressProviderDispatcher, IAddressProviderDispatcherTrait, AddressesKey}, + admin_contract::{IAdminContractDispatcher, IAdminContractDispatcherTrait}, + fee_collector::{IFeeCollectorDispatcher, IFeeCollectorDispatcherTrait}, + debt_token::{IDebtTokenDispatcher, IDebtTokenDispatcherTrait}, + price_feed::{IPriceFeedDispatcher, IPriceFeedDispatcherTrait}, +}; +use shisui::pools::{ + borrower_operations::{IBorrowerOperationsDispatcher, IBorrowerOperationsDispatcherTrait}, + vessel_manager::{IVesselManagerDispatcher, IVesselManagerDispatcherTrait}, +}; +use tests::tests_lib::{deploy_main_contracts}; +use snforge_std::{ + start_prank, stop_prank, CheatTarget, spy_events, SpyOn, EventSpy, EventAssertions, + start_mock_call, PrintTrait +}; + + +#[test] +fn when_borrowing_fee_is_defined_for_asset_should_return_fee_based_on_debt() { + let ( + borrower_operations, + vessel_manager, + adress_provider, + admin_contract, + fee_collector, + debt_token, + price_feed, + pragma_mock, + active_pool, + default_pool, + asset, + vessel_manager_operations_address + ) = + deploy_main_contracts(); + + let debt_token_amount: u256 = 2000_000000000000000000; + + admin_contract.add_new_collateral(asset.contract_address, 1000, 18); + + let fee = vessel_manager.get_borrowing_fee(asset.contract_address, debt_token_amount); + assert(fee == 1_0000000000000000000, 'Wrong fee'); +} + +#[test] +fn when_borrowing_fee_is_not_defined_for_asset_should_return_0() { + let ( + borrower_operations, + vessel_manager, + adress_provider, + admin_contract, + fee_collector, + debt_token, + price_feed, + pragma_mock, + active_pool, + default_pool, + asset, + vessel_manager_operations_address + ) = + deploy_main_contracts(); + + let debt_token_amount: u256 = 2000_000000000000000000; + + let fee = vessel_manager.get_borrowing_fee(asset.contract_address, debt_token_amount); + assert(fee == 0, 'Wrong fee'); +} diff --git a/tests/integration/pools/vessel_manager/get_borrowing_rate/get_borrowing_rate.tree b/tests/integration/pools/vessel_manager/get_borrowing_rate/get_borrowing_rate.tree new file mode 100644 index 0000000..60c0676 --- /dev/null +++ b/tests/integration/pools/vessel_manager/get_borrowing_rate/get_borrowing_rate.tree @@ -0,0 +1,5 @@ +test_get_borrowing_fee.cairo +├── When borrowing fee is defined for asset +│ └── it should return borrowing fee +└── When borrowing fee is not defined for asset + └── it should return 0 \ No newline at end of file diff --git a/tests/integration/pools/vessel_manager/get_borrowing_rate/test_get_borrowing_rate.cairo b/tests/integration/pools/vessel_manager/get_borrowing_rate/test_get_borrowing_rate.cairo new file mode 100644 index 0000000..d402e66 --- /dev/null +++ b/tests/integration/pools/vessel_manager/get_borrowing_rate/test_get_borrowing_rate.cairo @@ -0,0 +1,67 @@ +use starknet::{ContractAddress, contract_address_const, get_caller_address}; +use super::super::setup::open_vessel; +use shisui::core::{ + address_provider::{IAddressProviderDispatcher, IAddressProviderDispatcherTrait, AddressesKey}, + admin_contract::{IAdminContractDispatcher, IAdminContractDispatcherTrait}, + fee_collector::{IFeeCollectorDispatcher, IFeeCollectorDispatcherTrait}, + debt_token::{IDebtTokenDispatcher, IDebtTokenDispatcherTrait}, + price_feed::{IPriceFeedDispatcher, IPriceFeedDispatcherTrait}, +}; +use shisui::pools::{ + borrower_operations::{IBorrowerOperationsDispatcher, IBorrowerOperationsDispatcherTrait}, + vessel_manager::{IVesselManagerDispatcher, IVesselManagerDispatcherTrait}, +}; +use tests::tests_lib::{deploy_main_contracts}; +use snforge_std::{ + start_prank, stop_prank, CheatTarget, spy_events, SpyOn, EventSpy, EventAssertions, + start_mock_call, PrintTrait +}; + + +#[test] +fn when_borrowing_fee_is_defined_for_asset_should_return_fee_based_on_debt() { + let ( + borrower_operations, + vessel_manager, + adress_provider, + admin_contract, + fee_collector, + debt_token, + price_feed, + pragma_mock, + active_pool, + default_pool, + asset, + vessel_manager_operations_address + ) = + deploy_main_contracts(); + + admin_contract.add_new_collateral(asset.contract_address, 1000, 18); + + let rate = vessel_manager.get_borrowing_rate(asset.contract_address); + assert(rate == 5000000000000000, 'Wrong rate'); +} + +#[test] +fn when_borrowing_fee_is_not_defined_for_asset_should_return_0() { + let ( + borrower_operations, + vessel_manager, + adress_provider, + admin_contract, + fee_collector, + debt_token, + price_feed, + pragma_mock, + active_pool, + default_pool, + asset, + vessel_manager_operations_address + ) = + deploy_main_contracts(); + + let debt_token_amount: u256 = 2000_000000000000000000; + + let rate = vessel_manager.get_borrowing_rate(asset.contract_address); + assert(rate == 0, 'Wrong fee'); +} diff --git a/tests/integration/pools/vessel_manager/get_current_icr/get_current_icr.tree b/tests/integration/pools/vessel_manager/get_current_icr/get_current_icr.tree index 613c7e3..6660e44 100644 --- a/tests/integration/pools/vessel_manager/get_current_icr/get_current_icr.tree +++ b/tests/integration/pools/vessel_manager/get_current_icr/get_current_icr.tree @@ -1,2 +1,5 @@ test_get_current_icr.cairo -└── When vessel exist icr is correctly returned \ No newline at end of file +├── When vessel exist +├── it should return icr correctly calculated +├── When vessel not exist +└── it should return max value \ No newline at end of file diff --git a/tests/integration/pools/vessel_manager/get_current_icr/test_get_current_icr.cairo b/tests/integration/pools/vessel_manager/get_current_icr/test_get_current_icr.cairo index fe22c01..1870f6d 100644 --- a/tests/integration/pools/vessel_manager/get_current_icr/test_get_current_icr.cairo +++ b/tests/integration/pools/vessel_manager/get_current_icr/test_get_current_icr.cairo @@ -1,4 +1,5 @@ use starknet::{ContractAddress, contract_address_const, get_caller_address}; +use core::integer::BoundedU256; use super::super::setup::open_vessel; use shisui::core::{ address_provider::{IAddressProviderDispatcher, IAddressProviderDispatcherTrait, AddressesKey}, @@ -63,3 +64,29 @@ fn when_vessel_exists_current_icr_is_correctly_calculated() { let new_icr = vessel_manager.get_current_icr(asset.contract_address, caller, asset_price); assert(new_icr >= 1_880000000000000000 && new_icr <= 1_881000000000000000, 'Wrong icr'); } + +#[test] +fn when_vessel_not_exists_it_should_return_max_value() { + let ( + borrower_operations, + vessel_manager, + adress_provider, + admin_contract, + fee_collector, + debt_token, + price_feed, + pragma_mock, + active_pool, + default_pool, + asset, + vessel_manager_operations_address + ) = + deploy_main_contracts(); + + let mut asset_price: u256 = 1600_000000000000000000; + let deposit_amount: u256 = 1_890000000000000000; + let debt_token_amount: u256 = 2000_000000000000000000; + let caller = contract_address_const::<'caller'>(); + let icr = vessel_manager.get_current_icr(asset.contract_address, caller, asset_price); + assert(icr == BoundedU256::max(), 'Wrong icr'); +} diff --git a/tests/integration/pools/vessel_manager/get_nominal_icr/get_nominal_icr.tree b/tests/integration/pools/vessel_manager/get_nominal_icr/get_nominal_icr.tree index c0a786d..8d80449 100644 --- a/tests/integration/pools/vessel_manager/get_nominal_icr/get_nominal_icr.tree +++ b/tests/integration/pools/vessel_manager/get_nominal_icr/get_nominal_icr.tree @@ -1,2 +1,5 @@ test_get_nominal_icr.cairo -└── When vessel exist nominal icr is correctly returned \ No newline at end of file +├── When vessel exist +├── it should return nominal icr correctly calculated +├── When vessel not exist +└── it should return max value \ No newline at end of file diff --git a/tests/integration/pools/vessel_manager/get_nominal_icr/test_get_nominal_icr.cairo b/tests/integration/pools/vessel_manager/get_nominal_icr/test_get_nominal_icr.cairo index fc01d72..247bf77 100644 --- a/tests/integration/pools/vessel_manager/get_nominal_icr/test_get_nominal_icr.cairo +++ b/tests/integration/pools/vessel_manager/get_nominal_icr/test_get_nominal_icr.cairo @@ -1,4 +1,5 @@ use tests::tests_lib::{deploy_main_contracts}; +use core::integer::BoundedU256; use super::super::setup::open_vessel; use shisui::core::{ address_provider::{IAddressProviderDispatcher, IAddressProviderDispatcherTrait, AddressesKey}, @@ -59,5 +60,31 @@ fn when_vessel_exists_nominal_icr_is_correctly_calculated() { assert( nicr >= 9_4000000000000000 && nicr <= 9_5000000000000000, 'Wrong nicr' - ); //9.4e16 && 9.5e16 + ); //9.4e16 && 9.5e16 = 1.89 * NCIR_PRECISION / 2000 +} + +#[test] +fn when_vessel_not_exist_it_should_return_max_value() { + let ( + borrower_operations, + vessel_manager, + adress_provider, + admin_contract, + fee_collector, + debt_token, + price_feed, + pragma_mock, + active_pool, + default_pool, + asset, + vessel_manager_operations_address + ) = + deploy_main_contracts(); + + let mut asset_price: u256 = 1600_000000000000000000; + let deposit_amount: u256 = 1_890000000000000000; + let debt_token_amount: u256 = 2000_000000000000000000; + let caller = contract_address_const::<'caller'>(); + let nicr = vessel_manager.get_nominal_icr(asset.contract_address, caller); + assert(nicr == BoundedU256::max(), 'Wrong nicr'); } diff --git a/tests/integration/pools/vessel_manager/get_redemption_fee/test_get_redemption_fee.cairo b/tests/integration/pools/vessel_manager/get_redemption_fee/test_get_redemption_fee.cairo index 10020b6..ab80b50 100644 --- a/tests/integration/pools/vessel_manager/get_redemption_fee/test_get_redemption_fee.cairo +++ b/tests/integration/pools/vessel_manager/get_redemption_fee/test_get_redemption_fee.cairo @@ -40,16 +40,19 @@ fn when_base_rate_is_defined_should_return_redemption_fee() { admin_contract.add_new_collateral(asset.contract_address, 1000, 18); + start_prank( + CheatTarget::One(vessel_manager.contract_address), borrower_operations.contract_address + ); vessel_manager.set_base_rate(asset.contract_address, 100000000000000000); // 1e17 + stop_prank(CheatTarget::One(vessel_manager.contract_address)); let fee = vessel_manager.get_redemption_fee(asset.contract_address, 10); - assert(fee == 1, 'Wrong redemption fee'); } #[test] #[should_panic(expected: ('Fee bigger than assert draw',))] -fn when_base_rate_is_defined_redemption_fee_is_below_asset_draw_should_revert() { +fn when_base_rate_is_defined_and_redemption_fee_is_below_asset_draw_should_revert() { let ( borrower_operations, vessel_manager, @@ -68,7 +71,11 @@ fn when_base_rate_is_defined_redemption_fee_is_below_asset_draw_should_revert() admin_contract.add_new_collateral(asset.contract_address, 1000, 18); + start_prank( + CheatTarget::One(vessel_manager.contract_address), borrower_operations.contract_address + ); vessel_manager.set_base_rate(asset.contract_address, 1000000000000000000); // 1e18 + stop_prank(CheatTarget::One(vessel_manager.contract_address)); vessel_manager.get_redemption_fee_with_decay(asset.contract_address, 10); } diff --git a/tests/integration/pools/vessel_manager/get_redemption_fee_with_decay/test_get_redemption_fee_with_decay.cairo b/tests/integration/pools/vessel_manager/get_redemption_fee_with_decay/test_get_redemption_fee_with_decay.cairo index 0130b26..a391010 100644 --- a/tests/integration/pools/vessel_manager/get_redemption_fee_with_decay/test_get_redemption_fee_with_decay.cairo +++ b/tests/integration/pools/vessel_manager/get_redemption_fee_with_decay/test_get_redemption_fee_with_decay.cairo @@ -40,7 +40,11 @@ fn when_base_rate_is_defined_should_return_redemption_fee() { admin_contract.add_new_collateral(asset.contract_address, 1000, 18); + start_prank( + CheatTarget::One(vessel_manager.contract_address), borrower_operations.contract_address + ); vessel_manager.set_base_rate(asset.contract_address, 100000000000000000); // 1e17 + stop_prank(CheatTarget::One(vessel_manager.contract_address)); start_warp(CheatTarget::One(vessel_manager.contract_address), 20); @@ -78,7 +82,11 @@ fn when_base_rate_is_defined_redemption_fee_is_below_asset_draw_should_revert() admin_contract.add_new_collateral(asset.contract_address, 1000, 18); + start_prank( + CheatTarget::One(vessel_manager.contract_address), borrower_operations.contract_address + ); vessel_manager.set_base_rate(asset.contract_address, 1000000000000000000); // 1e18 + stop_prank(CheatTarget::One(vessel_manager.contract_address)); start_warp(CheatTarget::One(vessel_manager.contract_address), 20); diff --git a/tests/integration/pools/vessel_manager/get_redemption_rate/test_get_redemption_rate.cairo b/tests/integration/pools/vessel_manager/get_redemption_rate/test_get_redemption_rate.cairo index 628cce6..bd6e2b8 100644 --- a/tests/integration/pools/vessel_manager/get_redemption_rate/test_get_redemption_rate.cairo +++ b/tests/integration/pools/vessel_manager/get_redemption_rate/test_get_redemption_rate.cairo @@ -14,9 +14,7 @@ use shisui::pools::{ VesselManagerOperation, Status }, }; -use shisui::utils::{ - constants::{DECIMAL_PRECISION, ONE} - }; +use shisui::utils::{constants::{DECIMAL_PRECISION, ONE}}; use snforge_std::{ start_prank, stop_prank, start_warp, store, map_entry_address, CheatTarget, spy_events, SpyOn, EventSpy, EventAssertions, start_mock_call, PrintTrait @@ -42,12 +40,14 @@ fn when_redemption_fee_floor_plus_base_rate_is_below_decimal_precision_should_re deploy_main_contracts(); admin_contract.add_new_collateral(asset.contract_address, 1000, 18); - + start_prank( + CheatTarget::One(vessel_manager.contract_address), borrower_operations.contract_address + ); vessel_manager.set_base_rate(asset.contract_address, 100000000000000000); // 1e17 + stop_prank(CheatTarget::One(vessel_manager.contract_address)); let rate = vessel_manager.get_redemption_rate(asset.contract_address); - - assert(rate == 5000000000000000+100000000000000000, 'Wrong redemption rate'); + assert(rate == 5000000000000000 + 100000000000000000, 'Wrong redemption rate'); } #[test] @@ -70,10 +70,13 @@ fn when_redemption_fee_floor_plus_base_rate_is_below_decimal_precision_should_re admin_contract.add_new_collateral(asset.contract_address, 1000, 18); + start_prank( + CheatTarget::One(vessel_manager.contract_address), borrower_operations.contract_address + ); vessel_manager.set_base_rate(asset.contract_address, 1000000000000000000); // 1e18 + stop_prank(CheatTarget::One(vessel_manager.contract_address)); - let rate = vessel_manager.get_redemption_rate(asset.contract_address); - + let rate = vessel_manager.get_redemption_rate(asset.contract_address); assert(rate == DECIMAL_PRECISION, 'Wrong redemption rate'); } diff --git a/tests/integration/pools/vessel_manager/get_tcr/get_tcr.tree b/tests/integration/pools/vessel_manager/get_tcr/get_tcr.tree new file mode 100644 index 0000000..b8ae87e --- /dev/null +++ b/tests/integration/pools/vessel_manager/get_tcr/get_tcr.tree @@ -0,0 +1,5 @@ +test_get_tcr.cairo +├── When asset exists as collateral +├── it should return tcr correctly calculated +├── When asset is not existing as collateral +└── it should return max value \ No newline at end of file diff --git a/tests/integration/pools/vessel_manager/get_tcr/test_get_tcr.cairo b/tests/integration/pools/vessel_manager/get_tcr/test_get_tcr.cairo new file mode 100644 index 0000000..e87dfa7 --- /dev/null +++ b/tests/integration/pools/vessel_manager/get_tcr/test_get_tcr.cairo @@ -0,0 +1,113 @@ +use tests::tests_lib::{deploy_main_contracts}; +use core::integer::BoundedU256; +use super::super::setup::open_vessel; +use shisui::core::{ + address_provider::{IAddressProviderDispatcher, IAddressProviderDispatcherTrait, AddressesKey}, + admin_contract::{IAdminContractDispatcher, IAdminContractDispatcherTrait}, + fee_collector::{IFeeCollectorDispatcher, IFeeCollectorDispatcherTrait}, + debt_token::{IDebtTokenDispatcher, IDebtTokenDispatcherTrait}, + price_feed::{IPriceFeedDispatcher, IPriceFeedDispatcherTrait}, +}; +use shisui::pools::{ + borrower_operations::{IBorrowerOperationsDispatcher, IBorrowerOperationsDispatcherTrait}, + vessel_manager::{IVesselManagerDispatcher, IVesselManagerDispatcherTrait}, +}; +use snforge_std::{ + start_prank, stop_prank, store, map_entry_address, CheatTarget, spy_events, SpyOn, EventSpy, + EventAssertions, start_mock_call, PrintTrait +}; +use starknet::{ContractAddress, contract_address_const, get_caller_address}; + + +#[test] +fn when_asset_exists_as_collateral_tcr_is_correctly_calculated() { + let ( + borrower_operations, + vessel_manager, + adress_provider, + admin_contract, + fee_collector, + debt_token, + price_feed, + pragma_mock, + active_pool, + default_pool, + asset, + vessel_manager_operations_address + ) = + deploy_main_contracts(); + + let mut asset_price: u256 = 1600_000000000000000000; + let deposit_amount: u256 = 1_890000000000000000; + let debt_token_amount: u256 = 2000_000000000000000000; + + let borrower = open_vessel( + asset, + price_feed, + admin_contract, + active_pool, + default_pool, + debt_token, + borrower_operations, + vessel_manager, + pragma_mock, + asset_price, + deposit_amount, + debt_token_amount + ); + + start_mock_call(active_pool.contract_address, 'get_asset_balance', deposit_amount); + start_mock_call(default_pool.contract_address, 'get_asset_balance', 0_u256); + start_mock_call(active_pool.contract_address, 'get_debt_token_balance', debt_token_amount); + start_mock_call(default_pool.contract_address, 'get_debt_token_balance', 0_u256); + + let tcr = vessel_manager.get_tcr(asset.contract_address, asset_price); + assert(tcr == 1_512000000000000000, 'Wrong tcr'); +} + +#[test] +fn when_asset_is_not_existing_as_collateral_it_should_return_max_value() { + let ( + borrower_operations, + vessel_manager, + adress_provider, + admin_contract, + fee_collector, + debt_token, + price_feed, + pragma_mock, + active_pool, + default_pool, + asset, + vessel_manager_operations_address + ) = + deploy_main_contracts(); + + let mut asset_price: u256 = 1600_000000000000000000; + let deposit_amount: u256 = 1_890000000000000000; + let debt_token_amount: u256 = 2000_000000000000000000; + + // let borrower = open_vessel( + // asset, + // price_feed, + // admin_contract, + // active_pool, + // default_pool, + // debt_token, + // borrower_operations, + // vessel_manager, + // pragma_mock, + // asset_price, + // deposit_amount, + // debt_token_amount + // ); + + start_mock_call(active_pool.contract_address, 'get_asset_balance', 0_u256); + start_mock_call(default_pool.contract_address, 'get_asset_balance', 0_u256); + start_mock_call(active_pool.contract_address, 'get_debt_token_balance', 0_u256); + start_mock_call(default_pool.contract_address, 'get_debt_token_balance', 0_u256); + + let tcr = vessel_manager.get_tcr(asset.contract_address, asset_price); + assert(tcr == BoundedU256::max(), 'Wrong tcr'); +} + diff --git a/tests/lib.cairo b/tests/lib.cairo index f6da4c3..425a9ee 100644 --- a/tests/lib.cairo +++ b/tests/lib.cairo @@ -249,9 +249,21 @@ mod integration { mod get_redemption_fee_with_decay { mod test_get_redemption_fee_with_decay; } - mod get_redemption_rate { + mod get_redemption_rate { mod test_get_redemption_rate; } + mod get_borrowing_fee { + mod test_get_borrowing_fee; + } + mod get_borrowing_rate { + mod test_get_borrowing_rate; + } + mod check_recovery_mode { + mod test_check_recovery_mode; + } + mod get_tcr { + mod test_get_tcr; + } } mod borrower_operations { mod setup;