Skip to content

Commit

Permalink
fix calculation and add check (#145)
Browse files Browse the repository at this point in the history
* fix calculation and add check

* modify
  • Loading branch information
wangjj9219 authored Mar 4, 2020
1 parent accebf3 commit d6f2bca
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
4 changes: 3 additions & 1 deletion modules/cdp_engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,9 +608,11 @@ impl<T: Trait> Module<T> {

// 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
Expand Down
10 changes: 10 additions & 0 deletions modules/dex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,12 @@ impl<T: Trait> Module<T> {
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))
Expand Down Expand Up @@ -480,6 +482,14 @@ impl<T: Trait> Module<T> {
}

impl<T: Trait> DexManager<T::AccountId, CurrencyIdOf<T>, BalanceOf<T>> for Module<T> {
fn get_target_amount(
supply_currency_id: CurrencyIdOf<T>,
target_currency_id: CurrencyIdOf<T>,
supply_currency_amount: BalanceOf<T>,
) -> BalanceOf<T> {
Self::get_target_amount_available(supply_currency_id, target_currency_id, supply_currency_amount)
}

fn get_supply_amount(
supply_currency_id: CurrencyIdOf<T>,
target_currency_id: CurrencyIdOf<T>,
Expand Down
16 changes: 15 additions & 1 deletion modules/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ pub trait AuctionManagerExtended<AccountId>: AuctionManager<AccountId> {
}

pub trait DexManager<AccountId, CurrencyId, Balance> {
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,
Expand All @@ -72,12 +78,20 @@ impl<AccountId, CurrencyId, Balance> DexManager<AccountId, CurrencyId, Balance>
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(
Expand Down

0 comments on commit d6f2bca

Please sign in to comment.