fix: prevent cdp sim from attempting to draw too much debt (#438)

* fix: account for all fees when drawing more debt
This commit is contained in:
Kevin Davis 2020-04-15 14:54:38 -04:00 committed by GitHub
parent 55b73e36ee
commit 4cde3ba577
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -122,8 +122,10 @@ func SimulateMsgCdp(ak auth.AccountKeeper, k cdp.Keeper, pfk pricefeed.Keeper) s
if shouldDraw(r) { if shouldDraw(r) {
collateralShifted := ShiftDec(sdk.NewDecFromInt(existingCDP.Collateral.AmountOf(randCollateralParam.Denom)), randCollateralParam.ConversionFactor.Neg()) collateralShifted := ShiftDec(sdk.NewDecFromInt(existingCDP.Collateral.AmountOf(randCollateralParam.Denom)), randCollateralParam.ConversionFactor.Neg())
collateralValue := collateralShifted.Mul(priceShifted) 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 // 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) maxTotalDebt := collateralValue.Quo(randCollateralParam.LiquidationRatio)
maxDebt := maxTotalDebt.Sub(sdk.NewDecFromInt(debt)).TruncateInt() maxDebt := maxTotalDebt.Sub(sdk.NewDecFromInt(debt)).TruncateInt()
if maxDebt.LTE(sdk.OneInt()) { if maxDebt.LTE(sdk.OneInt()) {