mirror of
https://github.com/0glabs/0g-chain.git
synced 2024-12-24 15:25:18 +00:00
[R4R]: Avoid divide by zero when price is very small (#441)
* fix: avoid divide by zero when price is very small * fix: typo
This commit is contained in:
parent
4cde3ba577
commit
783247851d
@ -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 {
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user