From 783247851d0749ad3790c4b424774ff48fd5f6e4 Mon Sep 17 00:00:00 2001 From: Kevin Davis Date: Thu, 16 Apr 2020 07:43:44 -0400 Subject: [PATCH] [R4R]: Avoid divide by zero when price is very small (#441) * fix: avoid divide by zero when price is very small * fix: typo --- x/cdp/keeper/fees.go | 7 +++++-- x/cdp/keeper/seize.go | 6 +++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/x/cdp/keeper/fees.go b/x/cdp/keeper/fees.go index 6f6693ed..936cfa74 100644 --- a/x/cdp/keeper/fees.go +++ b/x/cdp/keeper/fees.go @@ -41,9 +41,12 @@ func (k Keeper) UpdateFeesForRiskyCdps(ctx sdk.Context, collateralDenom string, } liquidationRatio := k.getLiquidationRatio(ctx, collateralDenom) - + priceDivLiqRatio := price.Price.Quo(liquidationRatio) + if priceDivLiqRatio.IsZero() { + priceDivLiqRatio = sdk.SmallestDec() + } // NOTE - we have a fixed cutoff at 110% - this may or may not be changed in the future - normalizedRatio := sdk.OneDec().Quo(price.Price.Quo(liquidationRatio)).Mul(sdk.MustNewDecFromStr("1.1")) + normalizedRatio := sdk.OneDec().Quo(priceDivLiqRatio).Mul(sdk.MustNewDecFromStr("1.1")) // now iterate over all the cdps based on collateral ratio k.IterateCdpsByCollateralRatio(ctx, collateralDenom, normalizedRatio, func(cdp types.CDP) bool { diff --git a/x/cdp/keeper/seize.go b/x/cdp/keeper/seize.go index 2a63b14f..6ef5d3e3 100644 --- a/x/cdp/keeper/seize.go +++ b/x/cdp/keeper/seize.go @@ -109,10 +109,14 @@ func (k Keeper) LiquidateCdps(ctx sdk.Context, marketID string, denom string, li if err != nil { return err } + priceDivLiqRatio := price.Price.Quo(liquidationRatio) + if priceDivLiqRatio.IsZero() { + priceDivLiqRatio = sdk.SmallestDec() + } // price = $0.5 // liquidation ratio = 1.5 // normalizedRatio = (1/(0.5/1.5)) = 3 - normalizedRatio := sdk.OneDec().Quo(price.Price.Quo(liquidationRatio)) + normalizedRatio := sdk.OneDec().Quo(priceDivLiqRatio) cdpsToLiquidate := k.GetAllCdpsByDenomAndRatio(ctx, denom, normalizedRatio) for _, c := range cdpsToLiquidate { err := k.SeizeCollateral(ctx, c)