From d6f2bca6525bdbe2e764dbaf262c7eb74a2798e5 Mon Sep 17 00:00:00 2001 From: wangjj9219 <183318287@qq.com> Date: Wed, 4 Mar 2020 17:52:59 +0800 Subject: [PATCH] fix calculation and add check (#145) * fix calculation and add check * modify --- modules/cdp_engine/src/lib.rs | 4 +++- modules/dex/src/lib.rs | 10 ++++++++++ modules/support/src/lib.rs | 16 +++++++++++++++- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/modules/cdp_engine/src/lib.rs b/modules/cdp_engine/src/lib.rs index 8c21c42243..2bd55dced9 100644 --- a/modules/cdp_engine/src/lib.rs +++ b/modules/cdp_engine/src/lib.rs @@ -608,9 +608,11 @@ impl Module { // forth: handle bad debt and collateral if !supply_amount.is_zero() // supply_amount must not be zero - && collateral_balance >= supply_amount // make sure supply can afford target debit amount + && collateral_balance >= supply_amount // ensure have sufficient collateral && slippage_limit > Ratio::from_natural(0) // slippage_limit must be greater than zero && slippage.map_or(false, |s| s <= slippage_limit) + && T::Dex::get_target_amount(currency_id, stable_currency_id, supply_amount) >= target + // ensure supply can afford target { // directly exchange with DEX // deposit supply_amount collateral to cdp treasury diff --git a/modules/dex/src/lib.rs b/modules/dex/src/lib.rs index eb13581c70..0b0ca85362 100644 --- a/modules/dex/src/lib.rs +++ b/modules/dex/src/lib.rs @@ -242,10 +242,12 @@ impl Module { Rate::from_natural(1) .checked_sub(&T::GetExchangeFee::get()) .and_then(|n| Ratio::from_natural(1).checked_div(&n)) + .and_then(|n| Ratio::from_parts(1).checked_add(&n)) // add Ratio::from_parts(1) to correct the possible losses caused by discarding the remainder in inner division .and_then(|n| n.checked_mul_int(&target_amount)) .and_then(|n| n.checked_add(&1.into())) // add 1 to correct the possible losses caused by discarding the remainder in division .and_then(|n| target_pool.checked_sub(&n)) .and_then(|n| Some(Ratio::from_rational(supply_pool, n))) + .and_then(|n| Ratio::from_parts(1).checked_add(&n)) // add Ratio::from_parts(1) to correct the possible losses caused by discarding the remainder in inner division .and_then(|n| n.checked_mul_int(&target_pool)) .and_then(|n| n.checked_add(&1.into())) // add 1 to correct the possible losses caused by discarding the remainder in division .and_then(|n| n.checked_sub(&supply_pool)) @@ -480,6 +482,14 @@ impl Module { } impl DexManager, BalanceOf> for Module { + fn get_target_amount( + supply_currency_id: CurrencyIdOf, + target_currency_id: CurrencyIdOf, + supply_currency_amount: BalanceOf, + ) -> BalanceOf { + Self::get_target_amount_available(supply_currency_id, target_currency_id, supply_currency_amount) + } + fn get_supply_amount( supply_currency_id: CurrencyIdOf, target_currency_id: CurrencyIdOf, diff --git a/modules/support/src/lib.rs b/modules/support/src/lib.rs index d9e5a5d1b6..80b8c54ce9 100644 --- a/modules/support/src/lib.rs +++ b/modules/support/src/lib.rs @@ -49,6 +49,12 @@ pub trait AuctionManagerExtended: AuctionManager { } pub trait DexManager { + fn get_target_amount( + supply_currency_id: CurrencyId, + target_currency_id: CurrencyId, + supply_currency_amount: Balance, + ) -> Balance; + fn get_supply_amount( supply_currency_id: CurrencyId, target_currency_id: CurrencyId, @@ -72,12 +78,20 @@ impl DexManager where Balance: Default, { + fn get_target_amount( + _supply_currency_id: CurrencyId, + _target_currency_id: CurrencyId, + _supply_currency_amount: Balance, + ) -> Balance { + Default::default() + } + fn get_supply_amount( _supply_currency_id: CurrencyId, _target_currency_id: CurrencyId, _target_currency_amount: Balance, ) -> Balance { - Balance::default() + Default::default() } fn exchange_currency(