Skip to content

Commit 8a7577a

Browse files
committed
improve error handling for approval
1 parent 02cff0d commit 8a7577a

3 files changed

Lines changed: 21 additions & 9 deletions

File tree

x/lending/keeper/approval.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,24 @@ func (k Keeper) HandleApproval(ctx sdk.Context, loan *types.Loan) error {
1414
return types.ErrInsufficientLiquidity
1515
}
1616

17+
// initiate signing request for repayment cet adaptor signatures from DCM
18+
if err := k.InitiateRepaymentCetSigningRequest(ctx, loan.VaultAddress); err != nil {
19+
return err
20+
}
21+
1722
amount := sdk.NewCoin(loan.BorrowAmount.Denom, loan.BorrowAmount.Amount.Sub(loan.OriginationFee))
1823
if err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, sdk.MustAccAddressFromBech32(loan.Borrower), sdk.NewCoins(amount)); err != nil {
19-
return err
24+
// unexpected error
25+
return types.ErrUnexpected
2026
}
2127

2228
if loan.OriginationFee.IsPositive() {
2329
if err := k.handleOriginationFee(ctx, loan); err != nil {
24-
return err
30+
// unexpected error
31+
return types.ErrUnexpected
2532
}
2633
}
2734

28-
// initiate signing request for repayment cet adaptor signatures from DCM
29-
if err := k.InitiateRepaymentCetSigningRequest(ctx, loan.VaultAddress); err != nil {
30-
return err
31-
}
32-
3335
// update pool
3436
k.AfterPoolBorrowed(ctx, loan.PoolId, loan.Maturity, loan.BorrowAmount)
3537

x/lending/module/abci.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package lending
22

33
import (
44
"encoding/hex"
5+
"errors"
56
"fmt"
67

78
sdkmath "cosmossdk.io/math"
@@ -24,7 +25,9 @@ func BeginBlocker(ctx sdk.Context, k keeper.Keeper) error {
2425
// EndBlocker called at the end of each block
2526
func EndBlocker(ctx sdk.Context, k keeper.Keeper) error {
2627
// handle pending loans
27-
handlePendingLoans(ctx, k)
28+
if err := handlePendingLoans(ctx, k); err != nil {
29+
return err
30+
}
2831

2932
// handle active loans
3033
handleActiveLoans(ctx, k)
@@ -37,7 +40,7 @@ func EndBlocker(ctx sdk.Context, k keeper.Keeper) error {
3740
}
3841

3942
// handleActiveLoans handles pending loans
40-
func handlePendingLoans(ctx sdk.Context, k keeper.Keeper) {
43+
func handlePendingLoans(ctx sdk.Context, k keeper.Keeper) error {
4144
// handler on loan rejected
4245
rejectHandler := func(loan *types.Loan, authorizationId uint64, reason error) {
4346
if authorizationId > 0 {
@@ -99,6 +102,10 @@ func handlePendingLoans(ctx sdk.Context, k keeper.Keeper) {
99102

100103
// approve loan
101104
if err := k.HandleApproval(ctx, loan); err != nil {
105+
if errors.Is(err, types.ErrUnexpected) {
106+
return err
107+
}
108+
102109
rejectHandler(loan, authorizationId, err)
103110
continue
104111
}
@@ -110,6 +117,8 @@ func handlePendingLoans(ctx sdk.Context, k keeper.Keeper) {
110117
}
111118
}
112119
}
120+
121+
return nil
113122
}
114123

115124
// handleActiveLoans handles active loans

x/lending/types/errors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
var (
1010
ErrInvalidAmount = errorsmod.Register(ModuleName, 1000, "invalid amount")
1111
ErrInvalidParams = errorsmod.Register(ModuleName, 1001, "invalid params")
12+
ErrUnexpected = errorsmod.Register(ModuleName, 1002, "unexpected error")
1213

1314
ErrInvalidPoolId = errorsmod.Register(ModuleName, 2000, "invalid pool id")
1415
ErrInvalidPoolConfig = errorsmod.Register(ModuleName, 2001, "invalid pool config")

0 commit comments

Comments
 (0)