@@ -17,10 +17,26 @@ func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
17
17
18
18
// handleLoans handles loans
19
19
func handleLoans (ctx sdk.Context , k keeper.Keeper ) {
20
- // get all valid loans
20
+ // get all active loans
21
21
loans := k .GetLoans (ctx , types .LoanStatus_Disburse )
22
22
23
23
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
+
24
40
liquidationPrice := types .GetLiquidationPrice (loan .CollateralAmount , loan .BorrowAmount .Amount , k .GetParams (ctx ).LiquidationThresholdPercent )
25
41
26
42
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) {
29
45
continue
30
46
}
31
47
32
- // liquidated
48
+ // check if the loan is to be liquidated
33
49
if price .LTE (liquidationPrice ) {
34
50
loan .Status = types .LoanStatus_Liquidate
35
51
k .SetLoan (ctx , * loan )
0 commit comments