From 4cde3ba577f22a7a041afd5d078bfe4853ad80fa Mon Sep 17 00:00:00 2001 From: Kevin Davis Date: Wed, 15 Apr 2020 14:54:38 -0400 Subject: [PATCH] fix: prevent cdp sim from attempting to draw too much debt (#438) * fix: account for all fees when drawing more debt --- x/cdp/simulation/operations/msgs.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/x/cdp/simulation/operations/msgs.go b/x/cdp/simulation/operations/msgs.go index 7600d87b..dd1eb506 100644 --- a/x/cdp/simulation/operations/msgs.go +++ b/x/cdp/simulation/operations/msgs.go @@ -122,8 +122,10 @@ func SimulateMsgCdp(ak auth.AccountKeeper, k cdp.Keeper, pfk pricefeed.Keeper) s if shouldDraw(r) { collateralShifted := ShiftDec(sdk.NewDecFromInt(existingCDP.Collateral.AmountOf(randCollateralParam.Denom)), randCollateralParam.ConversionFactor.Neg()) collateralValue := collateralShifted.Mul(priceShifted) + newFeesAccumulated := k.CalculateFees(ctx, existingCDP.Principal, sdk.NewInt(ctx.BlockTime().Unix()-existingCDP.FeesUpdated.Unix()), randCollateralParam.Denom).AmountOf(randDebtParam.Denom) + totalFees := existingCDP.AccumulatedFees.AmountOf(randCollateralParam.Denom).Add(newFeesAccumulated) // given the current collateral value, calculate how much debt we could add while maintaining a valid liquidation ratio - debt := (existingCDP.Principal.Add(existingCDP.AccumulatedFees)).AmountOf(randDebtParam.Denom) + debt := existingCDP.Principal.AmountOf(randDebtParam.Denom).Add(totalFees) maxTotalDebt := collateralValue.Quo(randCollateralParam.LiquidationRatio) maxDebt := maxTotalDebt.Sub(sdk.NewDecFromInt(debt)).TruncateInt() if maxDebt.LTE(sdk.OneInt()) {