Skip to content

Commit 83fada4

Browse files
committed
improve repayment cet signing
1 parent 2d950be commit 83fada4

File tree

5 files changed

+23
-14
lines changed

5 files changed

+23
-14
lines changed

x/lending/keeper/approval.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ 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-
2217
amount := sdk.NewCoin(loan.BorrowAmount.Denom, loan.BorrowAmount.Amount.Sub(loan.OriginationFee))
2318
if err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, sdk.MustAccAddressFromBech32(loan.Borrower), sdk.NewCoins(amount)); err != nil {
2419
// unexpected error

x/lending/keeper/msg_server_loan.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ func (m msgServer) SubmitCets(goCtx context.Context, msg *types.MsgSubmitCets) (
214214
return nil, err
215215
}
216216

217+
// initiate signing request for repayment cet adaptor signatures from DCM
218+
if err := m.InitiateRepaymentCetSigningRequest(ctx, loan.VaultAddress); err != nil {
219+
return nil, err
220+
}
221+
217222
// create authorization
218223
authorization := m.CreateAuthorization(ctx, msg.LoanId, depositTxHashes)
219224

x/lending/keeper/repayment.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ func (k Keeper) HandleRepaymentAdaptorSignatures(ctx sdk.Context, loanId string,
4747
}
4848

4949
loan := k.GetLoan(ctx, loanId)
50-
if loan.Status != types.LoanStatus_Open && loan.Status != types.LoanStatus_Repaid {
51-
return errorsmod.Wrap(types.ErrInvalidLoanStatus, "loan neither open nor repaid")
50+
if types.LoanDisbursed(loan) {
51+
return errorsmod.Wrap(types.ErrInvalidLoanStatus, "loan has been disbursed")
5252
}
5353

5454
dlcMeta := k.GetDLCMeta(ctx, loanId)
@@ -125,3 +125,10 @@ func (k Keeper) GetRepaymentCetAdaptorPoint(ctx sdk.Context, loanId string) ([]b
125125

126126
return dlctypes.GetSignaturePointFromEvent(dlcEvent, types.RepaidOutcomeIndex)
127127
}
128+
129+
// RepaymentCetSigned returns true if the repayment cet adaptor signatures already exist, false otherwise
130+
func (k Keeper) RepaymentCetSigned(ctx sdk.Context, loanId string) bool {
131+
dlcMeta := k.GetDLCMeta(ctx, loanId)
132+
133+
return len(dlcMeta.RepaymentCet.DCMAdaptorSignatures) > 0
134+
}

x/lending/module/abci.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ func handlePendingLoans(ctx sdk.Context, k keeper.Keeper) error {
8686
continue
8787
}
8888

89-
// try to approve loan if all deposit txs verified
90-
if k.DepositsVerified(ctx, k.GetAuthorization(ctx, loan.VaultAddress, authorizationId)) {
89+
// try to approve loan if the repayment cet signed by DCM and all deposit txs verified
90+
if k.RepaymentCetSigned(ctx, loan.VaultAddress) && k.DepositsVerified(ctx, k.GetAuthorization(ctx, loan.VaultAddress, authorizationId)) {
9191
// check LTV
9292
if !types.CheckLTV(loan.CollateralAmount, int(pool.Config.CollateralAsset.Decimals), loan.BorrowAmount.Amount, int(pool.Config.LendingAsset.Decimals), pool.Config.MaxLtv, currentPrice, pool.Config.CollateralAsset.IsBasePriceAsset) {
9393
rejectHandler(loan, authorizationId, types.ErrInsufficientCollateral)
@@ -314,13 +314,10 @@ func handleRepayments(ctx sdk.Context, k keeper.Keeper) error {
314314
continue
315315
}
316316

317+
// get dlc meta
317318
dlcMeta := k.GetDLCMeta(ctx, loan.VaultAddress)
318319

319-
// check if the DCM adaptor signatures have been submitted
320-
if len(dlcMeta.RepaymentCet.DCMAdaptorSignatures) == 0 {
321-
continue
322-
}
323-
320+
// check if the DCM adapted signatures already exist
324321
if len(dlcMeta.RepaymentCet.DCMAdaptedSignatures) == 0 {
325322
// check if the event attestation has been submitted
326323
attestation := k.DLCKeeper().GetAttestationByEvent(ctx, loan.DlcEventId)

x/lending/types/lending.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@ func CheckBorrowAmountLimit(pool *LendingPool, borrowAmount sdkmath.Int) error {
235235
return nil
236236
}
237237

238+
// LoanDisbursed returns true if the loan has been disbursed, false otherwise
239+
func LoanDisbursed(loan *Loan) bool {
240+
return loan.Status >= LoanStatus_Open
241+
}
242+
238243
// CollateralRedeemable returns true if the collateral is redeemable, false otherwise
239244
func CollateralRedeemable(loan *Loan) bool {
240245
if len(loan.Authorizations) > 0 {

0 commit comments

Comments
 (0)