Skip to content

Commit

Permalink
fix: added checks and state change when liquidated
Browse files Browse the repository at this point in the history
* liquidation: add lender address check

* liquidation: deadline line check throws an error

* liquidation: add state change

* errors: fix index
  • Loading branch information
fadeev authored Oct 5, 2021
1 parent 567b7f1 commit 72560de
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
14 changes: 12 additions & 2 deletions x/loan/keeper/msg_server_liquidate_loan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
}
1 change: 1 addition & 0 deletions x/loan/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)

0 comments on commit 72560de

Please sign in to comment.