From f36df9d201acc1c6c01d1f09266f617dbd59b35a Mon Sep 17 00:00:00 2001 From: Bryan Chen Date: Wed, 18 Dec 2019 21:51:04 +0800 Subject: [PATCH] cleanup code a bit --- modules/auction_manager/src/lib.rs | 4 +- .../src/debit_exchange_rate_convertor.rs | 10 ++-- modules/cdp_engine/src/lib.rs | 55 ++++++------------- modules/honzon/src/lib.rs | 2 +- modules/support/src/lib.rs | 2 +- modules/vaults/src/lib.rs | 18 +++--- orml | 2 +- 7 files changed, 36 insertions(+), 57 deletions(-) diff --git a/modules/auction_manager/src/lib.rs b/modules/auction_manager/src/lib.rs index f2bc1d3a24..c9e5c57245 100644 --- a/modules/auction_manager/src/lib.rs +++ b/modules/auction_manager/src/lib.rs @@ -161,7 +161,7 @@ impl AuctionHandler= deduct_amount { @@ -215,7 +215,7 @@ impl AuctionManager for Module { type Balance = T::Balance; fn new_collateral_auction( - who: T::AccountId, + who: &T::AccountId, currency_id: Self::CurrencyId, amount: Self::Balance, target: Self::Balance, diff --git a/modules/cdp_engine/src/debit_exchange_rate_convertor.rs b/modules/cdp_engine/src/debit_exchange_rate_convertor.rs index 0d156c9464..c501eef884 100644 --- a/modules/cdp_engine/src/debit_exchange_rate_convertor.rs +++ b/modules/cdp_engine/src/debit_exchange_rate_convertor.rs @@ -8,11 +8,11 @@ where T: Trait, { fn convert(a: (CurrencyIdOf, DebitBalanceOf)) -> BalanceOf { - let balance = TryInto::>::try_into(TryInto::::try_into(a.1).unwrap_or(u128::max_value())) - .unwrap_or(BalanceOf::::max_value()); - >::debit_exchange_rate(a.0) + let (currency_id, balance) = a; + let balance: u128 = balance.unique_saturated_into(); + let balance: BalanceOf = balance.unique_saturated_into(); + >::debit_exchange_rate(currency_id) .unwrap_or(T::DefaulDebitExchangeRate::get()) - .checked_mul_int(&balance) - .unwrap_or(BalanceOf::::max_value()) + .saturating_mul_int(&balance) } } diff --git a/modules/cdp_engine/src/lib.rs b/modules/cdp_engine/src/lib.rs index edf2beda99..2ec4276211 100644 --- a/modules/cdp_engine/src/lib.rs +++ b/modules/cdp_engine/src/lib.rs @@ -4,7 +4,7 @@ use frame_support::{decl_error, decl_event, decl_module, decl_storage, ensure, t use orml_traits::{arithmetic::Signed, MultiCurrency, MultiCurrencyExtended, PriceProvider}; use orml_utilities::FixedU128; use rstd::{convert::TryInto, marker, prelude::*, result}; -use sp_runtime::traits::{Bounded, CheckedAdd, CheckedSub, Convert}; +use sp_runtime::traits::{CheckedAdd, CheckedSub, Convert, Saturating, UniqueSaturatedInto, Zero}; use support::{AuctionManager, CDPTreasury, ExchangeRate, Price, Rate, Ratio, RiskManager}; use system::ensure_root; @@ -131,18 +131,20 @@ decl_module! { // handle all kinds of collateral type for currency_id in T::CollateralCurrencyIds::get() { let debit_exchange_rate = Self::debit_exchange_rate(currency_id).unwrap_or(T::DefaulDebitExchangeRate::get()); - let stability_fee_rate = Self::stability_fee(currency_id).unwrap_or(Rate::from_parts(0)).checked_add(&global_stability_fee).unwrap_or(Rate::max_value()); + let stability_fee_rate = Self::stability_fee(currency_id) + .unwrap_or_default() + .saturating_add(global_stability_fee); let total_debits = >::total_debits(currency_id); - if stability_fee_rate > Rate::from_parts(0) && total_debits > 0.into() { - let debit_exchange_rate_increment = debit_exchange_rate.checked_mul(&stability_fee_rate).unwrap_or(ExchangeRate::max_value()); + if !stability_fee_rate.is_zero() && !total_debits.is_zero() { + let debit_exchange_rate_increment = debit_exchange_rate.saturating_mul(stability_fee_rate); // update exchange rate - let new_debit_exchange_rate = debit_exchange_rate.checked_add(&debit_exchange_rate_increment).unwrap_or(ExchangeRate::max_value()); + let new_debit_exchange_rate = debit_exchange_rate.saturating_add(debit_exchange_rate_increment); >::insert(currency_id, new_debit_exchange_rate); // issue stablecoin to surplus pool let total_debit_value = DebitExchangeRateConvertor::::convert((currency_id, total_debits)); - let issued_stable_coin_balance = debit_exchange_rate_increment.checked_mul_int(&total_debit_value).unwrap_or(BalanceOf::::max_value()); + let issued_stable_coin_balance = debit_exchange_rate_increment.saturating_mul_int(&total_debit_value); T::Treasury::on_surplus(issued_stable_coin_balance); } } @@ -157,15 +159,8 @@ impl Module { debit_balance: DebitBalanceOf, price: Price, ) -> Ratio { - let locked_collateral_value = TryInto::::try_into( - price - .checked_mul_int(&collateral_balance) - .unwrap_or(BalanceOf::::max_value()), - ) - .unwrap_or(u128::max_value()); - let debit_value = - TryInto::::try_into(DebitExchangeRateConvertor::::convert((currency_id, debit_balance))) - .unwrap_or(u128::max_value()); + let locked_collateral_value = price.saturating_mul_int(&collateral_balance); + let debit_value = DebitExchangeRateConvertor::::convert((currency_id, debit_balance)); Ratio::from_rational(locked_collateral_value, debit_value) } @@ -177,7 +172,7 @@ impl Module { } pub fn update_position( - who: T::AccountId, + who: &T::AccountId, currency_id: CurrencyIdOf, collateral_adjustment: AmountOf, debit_adjustment: DebitAmountOf, @@ -195,18 +190,14 @@ impl Module { // TODO: how to trigger cdp liquidation pub fn liquidate_unsafe_cdp(who: T::AccountId, currency_id: CurrencyIdOf) -> result::Result<(), Error> { let debit_balance = >::debits(&who, currency_id); - let collateral_balance: BalanceOf = >::collaterals(&who, currency_id); + let collateral_balance = >::collaterals(&who, currency_id); // ensure the cdp is unsafe - let feed_price = ::PriceSource::get_price(T::GetStableCurrencyId::get(), currency_id) - .ok_or(Error::InvalidFeedPrice)?; + let feed_price = + T::PriceSource::get_price(T::GetStableCurrencyId::get(), currency_id).ok_or(Error::InvalidFeedPrice)?; let collateral_ratio = Self::calculate_collateral_ratio(currency_id, collateral_balance, debit_balance, feed_price); - let liquidation_ratio = if let Some(ratio) = Self::liquidation_ratio(currency_id) { - ratio - } else { - T::DefaultLiquidationRatio::get() - }; + let liquidation_ratio = Self::liquidation_ratio(currency_id).unwrap_or_else(T::DefaultLiquidationRatio::get); ensure!(collateral_ratio < liquidation_ratio, Error::CollateralRatioStillSafe); // grab collaterals and debits from unsafe cdp @@ -221,21 +212,9 @@ impl Module { let bad_debt = DebitExchangeRateConvertor::::convert((currency_id, debit_balance)); let mut target = bad_debt; if let Some(penalty_ratio) = Self::liquidation_penalty(currency_id) { - target = target - .checked_add( - &penalty_ratio - .checked_mul_int(&target) - .unwrap_or(BalanceOf::::max_value()), - ) - .unwrap_or(BalanceOf::::max_value()); + target = target.saturating_add(penalty_ratio.saturating_mul_int(&target)); } - T::AuctionManagerHandler::new_collateral_auction( - who.clone(), - currency_id, - collateral_balance, - target, - bad_debt, - ); + T::AuctionManagerHandler::new_collateral_auction(&who, currency_id, collateral_balance, target, bad_debt); Self::deposit_event(RawEvent::LiquidateUnsafeCdp( currency_id, who, diff --git a/modules/honzon/src/lib.rs b/modules/honzon/src/lib.rs index a3ab6b59f9..66cd01b4aa 100644 --- a/modules/honzon/src/lib.rs +++ b/modules/honzon/src/lib.rs @@ -76,7 +76,7 @@ decl_module! { ) { let who = ensure_signed(origin).map_err(|_| Error::AccountUnSigned)?; - >::update_position(who.clone(), currency_id, collateral, debit).map_err(|_| Error::UpdatePositionFailed)?; + >::update_position(&who, currency_id, collateral, debit).map_err(|_| Error::UpdatePositionFailed)?; Self::deposit_event(RawEvent::UpdateVault(who, currency_id, collateral, debit)); } diff --git a/modules/support/src/lib.rs b/modules/support/src/lib.rs index 4a388587a4..c4118b4b5b 100644 --- a/modules/support/src/lib.rs +++ b/modules/support/src/lib.rs @@ -25,7 +25,7 @@ pub trait AuctionManager { type Balance; fn new_collateral_auction( - who: AccountId, + who: &AccountId, currency_id: Self::CurrencyId, amount: Self::Balance, target: Self::Balance, diff --git a/modules/vaults/src/lib.rs b/modules/vaults/src/lib.rs index 4f5fa04189..6b1a03abfa 100644 --- a/modules/vaults/src/lib.rs +++ b/modules/vaults/src/lib.rs @@ -103,46 +103,46 @@ impl Module { // mulate collaterals and debits and then mulate stable coin pub fn update_position( - who: T::AccountId, + who: &T::AccountId, currency_id: CurrencyIdOf, collaterals: AmountOf, debits: DebitAmountOf, ) -> result::Result<(), Error> { // ensure mutate safe - Self::check_add_and_sub(&who, currency_id, collaterals, debits)?; + Self::check_add_and_sub(who, currency_id, collaterals, debits)?; // ensure debits cap T::RiskManager::check_debit_cap(currency_id, debits).map_err(|_| Error::ExceedDebitValueHardCap)?; // ensure cdp safe - T::RiskManager::check_position_adjustment(&who, currency_id, collaterals, debits) + T::RiskManager::check_position_adjustment(who, currency_id, collaterals, debits) .map_err(|_| Error::PositionWillUnsafe)?; // ensure account has sufficient balance - Self::check_balance(&who, currency_id, collaterals)?; + Self::check_balance(who, currency_id, collaterals)?; // amount -> balance let collateral_balance = TryInto::>::try_into(collaterals.abs()).map_err(|_| Error::AmountIntoBalanceFailed)?; // update stable coin - T::DebitCurrency::update_balance(currency_id, &who, debits).map_err(|_| Error::UpdateStableCoinFailed)?; + T::DebitCurrency::update_balance(currency_id, who, debits).map_err(|_| Error::UpdateStableCoinFailed)?; let module_account = Self::account_id(); // update collateral asset if collaterals.is_positive() { - T::Currency::transfer(currency_id, &who, &module_account, collateral_balance) + T::Currency::transfer(currency_id, who, &module_account, collateral_balance) .expect("Will never fail ensured by check_balance"); } else { - T::Currency::transfer(currency_id, &module_account, &who, collateral_balance) + T::Currency::transfer(currency_id, &module_account, who, collateral_balance) .expect("Will never fail ensured by check_balance"); } // mutate collaterals and debits - Self::update_vault(&who, currency_id, collaterals, debits) + Self::update_vault(who, currency_id, collaterals, debits) .expect("Will never fail ensured by check_add_and_sub"); - Self::deposit_event(RawEvent::UpdatePosition(who, currency_id, collaterals, debits)); + Self::deposit_event(RawEvent::UpdatePosition(who.clone(), currency_id, collaterals, debits)); Ok(()) } diff --git a/orml b/orml index 30bfc6a9f6..cf54a9138b 160000 --- a/orml +++ b/orml @@ -1 +1 @@ -Subproject commit 30bfc6a9f61ae939c65ebd8d5bf97861cdb74545 +Subproject commit cf54a9138b672c373ce7769ad1f18518a8c17254