Skip to content

Commit 16ada8c

Browse files
committed
optimize liquidation check
1 parent 1e33ccd commit 16ada8c

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

x/lending/module/abci.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func handlePendingLoans(ctx sdk.Context, k keeper.Keeper) {
7272
}
7373

7474
// check if liquidation price reached
75-
if currentPrice.LTE(loan.LiquidationPrice) {
75+
if types.ToBeLiquidated(currentPrice, loan.LiquidationPrice, pool.Config.CollateralAsset.IsBasePriceAsset) {
7676
rejectHandler(loan, authorizationId, types.ErrLiquidationPriceReached)
7777
continue
7878
}
@@ -151,7 +151,7 @@ func handleActiveLoans(ctx sdk.Context, k keeper.Keeper) {
151151
)
152152
} else if !currentPrice.IsZero() {
153153
// check if the loan is to be liquidated
154-
if currentPrice.LTE(loan.LiquidationPrice) {
154+
if types.ToBeLiquidated(currentPrice, loan.LiquidationPrice, pool.Config.CollateralAsset.IsBasePriceAsset) {
155155
liquidationInterest = k.GetCurrentInterest(ctx, loan).Amount
156156
loan.Status = types.LoanStatus_Liquidated
157157

x/lending/types/lending.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ func GetMaturityTime(originMaturityTime int64) int64 {
8888
return time.Unix(originMaturityTime, 0).Truncate(24 * time.Hour).Add(24 * time.Hour).Unix()
8989
}
9090

91+
// ToBeLiquidated returns true if the given price satisfies the liquidation price, false otherwise
92+
func ToBeLiquidated(price sdkmath.LegacyDec, liquidationPrice sdkmath.LegacyDec, collateralIsBaseAsset bool) bool {
93+
if collateralIsBaseAsset {
94+
return price.LTE(liquidationPrice)
95+
}
96+
97+
return price.GTE(liquidationPrice)
98+
}
99+
91100
// CheckLTV returns true if the collateral amount and borrow amount satisfy the max LTV limitation by the given price, false otherwise
92101
func CheckLTV(collateralAmount sdkmath.Int, collateralAssetDecimals int, borrowAmount sdkmath.Int, borrowAssetDecimals int, maxLTV uint32, price sdkmath.LegacyDec, collateralIsBaseAsset bool) bool {
93102
if collateralIsBaseAsset {

0 commit comments

Comments
 (0)