Skip to content

Commit

Permalink
simplify loans market's governance call (#1801)
Browse files Browse the repository at this point in the history
Signed-off-by: Cheng JIANG <[email protected]>
  • Loading branch information
GopherJ authored Jun 13, 2022
1 parent 4b5e93b commit 766f9e8
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 147 deletions.
1 change: 0 additions & 1 deletion pallets/crowdloans/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,6 @@ impl pallet_xcm_helper::Config for Test {
type AccountIdToMultiLocation = AccountIdToMultiLocation;
type RefundLocation = RefundLocation;
type BlockNumberProvider = frame_system::Pallet<Test>;
type XcmOrigin = EnsureRoot<AccountId>;
type WeightInfo = ();
type RelayCurrency = RelayCurrency;
}
Expand Down
1 change: 0 additions & 1 deletion pallets/liquid-staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,6 @@ impl pallet_xcm_helper::Config for Test {
type AccountIdToMultiLocation = AccountIdToMultiLocation;
type RefundLocation = RefundLocation;
type BlockNumberProvider = frame_system::Pallet<Test>;
type XcmOrigin = EnsureRoot<AccountId>;
type WeightInfo = ();
type RelayCurrency = StakingCurrency;
}
Expand Down
26 changes: 13 additions & 13 deletions pallets/loans/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,16 +218,16 @@ benchmarks! {
update_market {
assert_ok!(Loans::<T>::add_market(SystemOrigin::Root.into(), KSM, pending_market_mock::<T>(PKSM)));
}: _(
SystemOrigin::Root,
KSM,
Ratio::from_percent(50),
Ratio::from_percent(55),
Ratio::from_percent(50),
Ratio::from_percent(15),
Ratio::from_percent(3),
Rate::from_inner(Rate::DIV / 100 * 110),
1_000_000_000_000_000_000_000u128,
1_000_000_000_000_000_000_000u128
SystemOrigin::Root,
KSM,
Some(Ratio::from_percent(50)),
Some(Ratio::from_percent(55)),
Some(Ratio::from_percent(50)),
Some(Ratio::from_percent(15)),
Some(Ratio::from_percent(3)),
Some(Rate::from_inner(Rate::DIV / 100 * 110)),
Some(1_000_000_000_000_000_000_000u128),
Some(1_000_000_000_000_000_000_000u128)
)
verify {
let mut market = pending_market_mock::<T>(PKSM);
Expand Down Expand Up @@ -264,7 +264,7 @@ benchmarks! {
update_market_reward_speed {
assert_ok!(Loans::<T>::add_market(SystemOrigin::Root.into(), USDT, pending_market_mock::<T>(USDT)));
assert_ok!(Loans::<T>::activate_market(SystemOrigin::Root.into(), USDT));
}: _(SystemOrigin::Root, USDT, 1_000_000, 1_000_000)
}: _(SystemOrigin::Root, USDT, Some(1_000_000), Some(1_000_000))
verify {
assert_last_event::<T>(Event::<T>::MarketRewardSpeedUpdated(USDT, 1_000_000, 1_000_000).into());
}
Expand All @@ -276,7 +276,7 @@ benchmarks! {
assert_ok!(Loans::<T>::activate_market(SystemOrigin::Root.into(), USDT));
assert_ok!(Loans::<T>::mint(SystemOrigin::Signed(caller.clone()).into(), USDT, 100_000_000));
assert_ok!(Loans::<T>::add_reward(SystemOrigin::Signed(caller.clone()).into(), 1_000_000_000_000_u128));
assert_ok!(Loans::<T>::update_market_reward_speed(SystemOrigin::Root.into(), USDT, 1_000_000, 1_000_000));
assert_ok!(Loans::<T>::update_market_reward_speed(SystemOrigin::Root.into(), USDT, Some(1_000_000), Some(1_000_000)));
let target_height = frame_system::Pallet::<T>::block_number().saturating_add(One::one());
frame_system::Pallet::<T>::set_block_number(target_height);
}: _(SystemOrigin::Signed(caller.clone()))
Expand All @@ -291,7 +291,7 @@ benchmarks! {
assert_ok!(Loans::<T>::activate_market(SystemOrigin::Root.into(), USDT));
assert_ok!(Loans::<T>::mint(SystemOrigin::Signed(caller.clone()).into(), USDT, 100_000_000));
assert_ok!(Loans::<T>::add_reward(SystemOrigin::Signed(caller.clone()).into(), 1_000_000_000_000_u128));
assert_ok!(Loans::<T>::update_market_reward_speed(SystemOrigin::Root.into(), USDT, 1_000_000, 1_000_000));
assert_ok!(Loans::<T>::update_market_reward_speed(SystemOrigin::Root.into(), USDT, Some(1_000_000), Some(1_000_000)));
let target_height = frame_system::Pallet::<T>::block_number().saturating_add(One::one());
frame_system::Pallet::<T>::set_block_number(target_height);
}: _(SystemOrigin::Signed(caller.clone()), USDT)
Expand Down
36 changes: 26 additions & 10 deletions pallets/loans/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,17 +598,30 @@ pub mod pallet {
pub fn update_market(
origin: OriginFor<T>,
asset_id: AssetIdOf<T>,
collateral_factor: Ratio,
liquidation_threshold: Ratio,
reserve_factor: Ratio,
close_factor: Ratio,
liquidate_incentive_reserved_factor: Ratio,
liquidate_incentive: Rate,
#[pallet::compact] supply_cap: BalanceOf<T>,
#[pallet::compact] borrow_cap: BalanceOf<T>,
collateral_factor: Option<Ratio>,
liquidation_threshold: Option<Ratio>,
reserve_factor: Option<Ratio>,
close_factor: Option<Ratio>,
liquidate_incentive_reserved_factor: Option<Ratio>,
liquidate_incentive: Option<Rate>,
supply_cap: Option<BalanceOf<T>>,
borrow_cap: Option<BalanceOf<T>>,
) -> DispatchResultWithPostInfo {
T::UpdateOrigin::ensure_origin(origin)?;

let market = Self::market(asset_id)?;

let collateral_factor = collateral_factor.unwrap_or(market.collateral_factor);
let liquidation_threshold =
liquidation_threshold.unwrap_or(market.liquidation_threshold);
let reserve_factor = reserve_factor.unwrap_or(market.reserve_factor);
let close_factor = close_factor.unwrap_or(market.close_factor);
let liquidate_incentive_reserved_factor = liquidate_incentive_reserved_factor
.unwrap_or(market.liquidate_incentive_reserved_factor);
let liquidate_incentive = liquidate_incentive.unwrap_or(market.liquidate_incentive);
let supply_cap = supply_cap.unwrap_or(market.supply_cap);
let borrow_cap = borrow_cap.unwrap_or(market.borrow_cap);

ensure!(
collateral_factor >= Ratio::zero() && collateral_factor < Ratio::one(),
Error::<T>::InvalidFactor
Expand Down Expand Up @@ -736,15 +749,18 @@ pub mod pallet {
pub fn update_market_reward_speed(
origin: OriginFor<T>,
asset_id: AssetIdOf<T>,
supply_reward_per_block: BalanceOf<T>,
borrow_reward_per_block: BalanceOf<T>,
supply_reward_per_block: Option<BalanceOf<T>>,
borrow_reward_per_block: Option<BalanceOf<T>>,
) -> DispatchResultWithPostInfo {
T::UpdateOrigin::ensure_origin(origin)?;
Self::ensure_active_market(asset_id)?;

let current_supply_speed = RewardSupplySpeed::<T>::get(asset_id);
let current_borrow_speed = RewardBorrowSpeed::<T>::get(asset_id);

let supply_reward_per_block = supply_reward_per_block.unwrap_or(current_supply_speed);
let borrow_reward_per_block = borrow_reward_per_block.unwrap_or(current_borrow_speed);

if supply_reward_per_block != current_supply_speed {
Self::update_reward_supply_index(asset_id)?;
RewardSupplySpeed::<T>::try_mutate(asset_id, |current_speed| -> DispatchResult {
Expand Down
75 changes: 50 additions & 25 deletions pallets/loans/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -931,22 +931,27 @@ fn update_market_reward_speed_works() {
assert_ok!(Loans::update_market_reward_speed(
Origin::root(),
DOT,
unit(1),
unit(2),
Some(unit(1)),
Some(unit(2)),
));
assert_eq!(Loans::reward_supply_speed(DOT), unit(1));
assert_eq!(Loans::reward_borrow_speed(DOT), unit(2));

assert_ok!(Loans::update_market_reward_speed(
Origin::root(),
DOT,
unit(2),
0,
Some(unit(2)),
Some(0),
));
assert_eq!(Loans::reward_supply_speed(DOT), unit(2));
assert_eq!(Loans::reward_borrow_speed(DOT), unit(0));

assert_ok!(Loans::update_market_reward_speed(Origin::root(), DOT, 0, 0));
assert_ok!(Loans::update_market_reward_speed(
Origin::root(),
DOT,
Some(0),
Some(0)
));
assert_eq!(Loans::reward_supply_speed(DOT), unit(0));
assert_eq!(Loans::reward_borrow_speed(DOT), unit(0));
})
Expand All @@ -966,8 +971,8 @@ fn reward_calculation_one_palyer_in_multi_markets_works() {
assert_ok!(Loans::update_market_reward_speed(
Origin::root(),
DOT,
unit(1),
unit(2),
Some(unit(1)),
Some(unit(2)),
));

// check status
Expand All @@ -988,8 +993,8 @@ fn reward_calculation_one_palyer_in_multi_markets_works() {
assert_ok!(Loans::update_market_reward_speed(
Origin::root(),
KSM,
unit(1),
unit(1),
Some(unit(1)),
Some(unit(1)),
));

// check status
Expand All @@ -1005,7 +1010,12 @@ fn reward_calculation_one_palyer_in_multi_markets_works() {
assert_eq!(Loans::reward_accrued(ALICE), unit(10));

_run_to_block(30);
assert_ok!(Loans::update_market_reward_speed(Origin::root(), DOT, 0, 0));
assert_ok!(Loans::update_market_reward_speed(
Origin::root(),
DOT,
Some(0),
Some(0)
));
assert_ok!(Loans::redeem(Origin::signed(ALICE), DOT, unit(100)));
assert_ok!(Loans::borrow(Origin::signed(ALICE), DOT, unit(10)));
assert_ok!(Loans::mint(Origin::signed(ALICE), KSM, unit(100)));
Expand All @@ -1022,7 +1032,12 @@ fn reward_calculation_one_palyer_in_multi_markets_works() {
assert_eq!(almost_equal(Loans::reward_accrued(ALICE), unit(80)), true);

_run_to_block(40);
assert_ok!(Loans::update_market_reward_speed(Origin::root(), KSM, 0, 0));
assert_ok!(Loans::update_market_reward_speed(
Origin::root(),
KSM,
Some(0),
Some(0)
));
assert_ok!(Loans::mint(Origin::signed(ALICE), DOT, unit(100)));
assert_ok!(Loans::borrow(Origin::signed(ALICE), DOT, unit(10)));
assert_ok!(Loans::redeem(Origin::signed(ALICE), KSM, unit(100)));
Expand All @@ -1042,8 +1057,8 @@ fn reward_calculation_one_palyer_in_multi_markets_works() {
assert_ok!(Loans::update_market_reward_speed(
Origin::root(),
DOT,
unit(1),
unit(1),
Some(unit(1)),
Some(unit(1)),
));
assert_ok!(Loans::redeem(Origin::signed(ALICE), DOT, unit(100)));
assert_ok!(Loans::repay_borrow_all(Origin::signed(ALICE), DOT));
Expand All @@ -1064,8 +1079,8 @@ fn reward_calculation_one_palyer_in_multi_markets_works() {
assert_ok!(Loans::update_market_reward_speed(
Origin::root(),
KSM,
unit(1),
unit(1),
Some(unit(1)),
Some(unit(1)),
));
assert_ok!(Loans::mint(Origin::signed(ALICE), DOT, unit(100)));
assert_ok!(Loans::redeem(Origin::signed(ALICE), KSM, unit(100)));
Expand All @@ -1082,8 +1097,18 @@ fn reward_calculation_one_palyer_in_multi_markets_works() {
assert_eq!(almost_equal(Loans::reward_accrued(ALICE), unit(110)), true,);

_run_to_block(70);
assert_ok!(Loans::update_market_reward_speed(Origin::root(), DOT, 0, 0));
assert_ok!(Loans::update_market_reward_speed(Origin::root(), KSM, 0, 0));
assert_ok!(Loans::update_market_reward_speed(
Origin::root(),
DOT,
Some(0),
Some(0)
));
assert_ok!(Loans::update_market_reward_speed(
Origin::root(),
KSM,
Some(0),
Some(0)
));
assert_ok!(Loans::redeem(Origin::signed(ALICE), DOT, unit(100)));
assert_ok!(Loans::mint(Origin::signed(ALICE), KSM, unit(100)));

Expand Down Expand Up @@ -1115,8 +1140,8 @@ fn reward_calculation_one_palyer_in_multi_markets_works() {
assert_ok!(Loans::update_market_reward_speed(
Origin::root(),
DOT,
unit(1),
0,
Some(unit(1)),
Some(0),
));

// DOT supply:500 DOT supply reward: 50
Expand Down Expand Up @@ -1144,8 +1169,8 @@ fn reward_calculation_multi_player_in_one_market_works() {
assert_ok!(Loans::update_market_reward_speed(
Origin::root(),
DOT,
unit(1),
unit(1),
Some(unit(1)),
Some(unit(1)),
));
// Alice supply:10 supply reward: 0
// Alice borrow:0 borrow reward: 0
Expand Down Expand Up @@ -1249,14 +1274,14 @@ fn reward_calculation_after_liquidate_borrow_works() {
assert_ok!(Loans::update_market_reward_speed(
Origin::root(),
DOT,
unit(1),
unit(1),
Some(unit(1)),
Some(unit(1)),
));
assert_ok!(Loans::update_market_reward_speed(
Origin::root(),
KSM,
unit(1),
unit(1),
Some(unit(1)),
Some(unit(1)),
));

_run_to_block(20);
Expand Down
Loading

0 comments on commit 766f9e8

Please sign in to comment.