Skip to content

Commit 74902a2

Browse files
committed
optimize repayment
1 parent 37b917c commit 74902a2

File tree

4 files changed

+43
-10
lines changed

4 files changed

+43
-10
lines changed

x/lending/keeper/msg_server_loan.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"encoding/hex"
77
"fmt"
8+
"strings"
89

910
"cosmossdk.io/math"
1011
"github.com/btcsuite/btcd/btcutil/psbt"
@@ -240,22 +241,20 @@ func (m msgServer) Repay(goCtx context.Context, msg *types.MsgRepay) (*types.Msg
240241

241242
ctx := sdk.UnwrapSDKContext(goCtx)
242243

243-
borrower, err := sdk.AccAddressFromBech32(msg.Borrower)
244-
if err != nil {
245-
return nil, types.ErrInvalidSender
246-
}
247-
248244
if !m.HasLoan(ctx, msg.LoanId) {
249245
return nil, types.ErrLoanDoesNotExist
250246
}
251247

252248
loan := m.GetLoan(ctx, msg.LoanId)
249+
if loan.Status != types.LoanStatus_Disburse {
250+
return nil, types.ErrInvalidLoanStatus
251+
}
253252

254253
amount := loan.BorrowAmount.Amount.Add(loan.Interests).Add(loan.Fees)
255254

256255
// send repayment to escrow account
257-
if e := m.bankKeeper.SendCoinsFromAccountToModule(ctx, borrower, types.RepaymentEscrowAccount, sdk.NewCoins(sdk.NewCoin(loan.BorrowAmount.Denom, amount))); e != nil {
258-
return nil, e
256+
if err := m.bankKeeper.SendCoinsFromAccountToModule(ctx, sdk.MustAccAddressFromBech32(msg.Borrower), types.RepaymentEscrowAccount, sdk.NewCoins(sdk.NewCoin(loan.BorrowAmount.Denom, amount))); err != nil {
257+
return nil, err
259258
}
260259

261260
loan.Status = types.LoanStatus_Repay
@@ -310,11 +309,19 @@ func (m msgServer) Repay(goCtx context.Context, msg *types.MsgRepay) (*types.Msg
310309

311310
m.SetRepayment(ctx, repayment)
312311

312+
// get sig hashes
313+
sigHashes, err := types.GetRepaymentTxSigHashes(repaymentTx, dlcMeta.RepaymentScript)
314+
if err != nil {
315+
return nil, err
316+
}
317+
313318
ctx.EventManager().EmitEvent(
314319
sdk.NewEvent(
315320
types.EventTypeRepay,
316321
sdk.NewAttribute(types.AttributeKeyLoanId, loan.VaultAddress),
317322
sdk.NewAttribute(types.AttributeKeyAdaptorPoint, msg.AdaptorPoint),
323+
sdk.NewAttribute(types.AttributeKeyAgencyPubKey, loan.Agency),
324+
sdk.NewAttribute(types.AttributeKeySigHashes, strings.Join(sigHashes, types.AttributeValueSeparator)),
318325
),
319326
)
320327

x/lending/types/dlc.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,32 @@ func GetLiquidationCetSigHashes(dlcMeta *DLCMeta) ([]string, error) {
358358
return sigHashes, nil
359359
}
360360

361+
// GetRepaymentTxSigHashes gets the sig hashes of the repayment tx
362+
func GetRepaymentTxSigHashes(repaymentTx string, repaymentScript string) ([]string, error) {
363+
p, err := psbt.NewFromRawBytes(bytes.NewReader([]byte(repaymentTx)), true)
364+
if err != nil {
365+
return nil, err
366+
}
367+
368+
script, err := hex.DecodeString(repaymentScript)
369+
if err != nil {
370+
return nil, err
371+
}
372+
373+
sigHashes := []string{}
374+
375+
for i, input := range p.Inputs {
376+
sigHash, err := CalcTapscriptSigHash(p, i, input.SighashType, script)
377+
if err != nil {
378+
return nil, err
379+
}
380+
381+
sigHashes = append(sigHashes, base64.StdEncoding.EncodeToString(sigHash))
382+
}
383+
384+
return sigHashes, nil
385+
}
386+
361387
// GetDLCTapscripts gets the tap scripts from the given dlc meta
362388
// Assume that the dlc meta is valid
363389
func GetDLCTapscripts(dlcMeta *DLCMeta) [][]byte {

x/lending/types/errors.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,6 @@ var (
5555
ErrLiquidationSignaturesAlreadyExist = errorsmod.Register(ModuleName, 7002, "agency liquidation signatures already exist")
5656
ErrInvalidLiquidationSignatures = errorsmod.Register(ModuleName, 7003, "invalid agency liquidation signatures")
5757

58-
ErrInvalidSignature = errorsmod.Register(ModuleName, 8001, "invalid signature")
58+
ErrInvalidLoanStatus = errorsmod.Register(ModuleName, 8001, "invalid loan status")
59+
ErrInvalidSignature = errorsmod.Register(ModuleName, 8002, "invalid signature")
5960
)

x/lending/types/events.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ const (
1212
AttributeKeyAdaptorPoint = "adaptor_point"
1313

1414
AttributeKeyAgencyPubKey = "agency_pub_key"
15-
16-
AttributeKeySigHashes = "sig_hashes"
15+
AttributeKeySigHashes = "sig_hashes"
1716
)
1817

1918
const (

0 commit comments

Comments
 (0)