Skip to content

Commit

Permalink
feat:vessel_manager
Browse files Browse the repository at this point in the history
  • Loading branch information
0xTitan committed Feb 8, 2024
1 parent d09f6f5 commit b7836e6
Show file tree
Hide file tree
Showing 19 changed files with 488 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/utils/shisui_base.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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');
}

Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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');
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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');
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
test_get_current_icr.cairo
└── When vessel exist icr is correctly returned
├── When vessel exist
├── it should return icr correctly calculated
├── When vessel not exist
└── it should return max value
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -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');
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
test_get_nominal_icr.cairo
└── When vessel exist nominal icr is correctly returned
├── When vessel exist
├── it should return nominal icr correctly calculated
├── When vessel not exist
└── it should return max value
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -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');
}
Loading

0 comments on commit b7836e6

Please sign in to comment.