Skip to content

Commit 139f2c5

Browse files
committed
check defaulted loans
1 parent 4afa6c7 commit 139f2c5

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

x/lending/module/abci.go

+18-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,26 @@ func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
1717

1818
// handleLoans handles loans
1919
func handleLoans(ctx sdk.Context, k keeper.Keeper) {
20-
// get all valid loans
20+
// get all active loans
2121
loans := k.GetLoans(ctx, types.LoanStatus_Disburse)
2222

2323
for _, loan := range loans {
24+
// check if the loan has defaulted
25+
if ctx.BlockTime().Unix() >= loan.MaturityTime {
26+
loan.Status = types.LoanStatus_Default
27+
k.SetLoan(ctx, *loan)
28+
29+
// emit event
30+
ctx.EventManager().EmitEvent(
31+
sdk.NewEvent(
32+
types.EventTypeDefault,
33+
sdk.NewAttribute(types.AttributeKeyLoanId, loan.VaultAddress),
34+
),
35+
)
36+
37+
continue
38+
}
39+
2440
liquidationPrice := types.GetLiquidationPrice(loan.CollateralAmount, loan.BorrowAmount.Amount, k.GetParams(ctx).LiquidationThresholdPercent)
2541

2642
price, err := k.OracleKeeper().GetPrice(ctx, fmt.Sprintf("BTC-%s", loan.BorrowAmount.Denom))
@@ -29,7 +45,7 @@ func handleLoans(ctx sdk.Context, k keeper.Keeper) {
2945
continue
3046
}
3147

32-
// liquidated
48+
// check if the loan is to be liquidated
3349
if price.LTE(liquidationPrice) {
3450
loan.Status = types.LoanStatus_Liquidate
3551
k.SetLoan(ctx, *loan)

x/lending/types/events.go

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package types
33
// Lending module event types
44
const (
55
EventTypeRepay = "repay"
6+
EventTypeDefault = "default"
67
EventTypeLiquidate = "liquidate"
78

89
AttributeKeyLoanId = "loan_id"

0 commit comments

Comments
 (0)