From 72560de7560084eb8ea8dcea3adee2ceb617b87e Mon Sep 17 00:00:00 2001 From: Denis Fadeev Date: Tue, 5 Oct 2021 14:13:09 +0500 Subject: [PATCH] fix: added checks and state change when liquidated * liquidation: add lender address check * liquidation: deadline line check throws an error * liquidation: add state change * errors: fix index --- x/loan/keeper/msg_server_liquidate_loan.go | 14 ++++++++++++-- x/loan/types/errors.go | 1 + 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/x/loan/keeper/msg_server_liquidate_loan.go b/x/loan/keeper/msg_server_liquidate_loan.go index 3e68f81..e08688f 100644 --- a/x/loan/keeper/msg_server_liquidate_loan.go +++ b/x/loan/keeper/msg_server_liquidate_loan.go @@ -18,6 +18,10 @@ func (k msgServer) LiquidateLoan(goCtx context.Context, msg *types.MsgLiquidateL return nil, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, fmt.Sprintf("key %d doesn't exist", msg.Id)) } + if loan.Lender != msg.Creator { + return nil, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "Cannot liquidate: not the lender") + } + if loan.State != "approved" { return nil, sdkerrors.Wrapf(types.ErrWrongLoanState, "%v", loan.State) } @@ -30,9 +34,15 @@ func (k msgServer) LiquidateLoan(goCtx context.Context, msg *types.MsgLiquidateL panic(err) } - if ctx.BlockHeight() > deadline { - k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, lender, amount) + if ctx.BlockHeight() < deadline { + return nil, sdkerrors.Wrap(types.ErrDeadline, "Cannot liquidate before deadline") } + k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, lender, amount) + + loan.State = "liquidated" + + k.SetLoan(ctx, loan) + return &types.MsgLiquidateLoanResponse{}, nil } diff --git a/x/loan/types/errors.go b/x/loan/types/errors.go index 83ebe61..97bca6e 100644 --- a/x/loan/types/errors.go +++ b/x/loan/types/errors.go @@ -9,4 +9,5 @@ import ( // x/loan module sentinel errors var ( ErrWrongLoanState = sdkerrors.Register(ModuleName, 1, "wrong loan state") + ErrDeadline = sdkerrors.Register(ModuleName, 2, "deadline") )