Skip to content

Commit

Permalink
avoid unnecessary db calls
Browse files Browse the repository at this point in the history
  • Loading branch information
kiwiidb committed Dec 6, 2022
1 parent e63917a commit 47690db
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions lib/service/checkpayments.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,36 @@ func (svc *LndhubService) TrackOutgoingPaymentstatus(ctx context.Context, invoic
//ask lnd using TrackPaymentV2 by hash of payment
rawHash, err := hex.DecodeString(invoice.RHash)
if err != nil {
svc.Logger.Errorf("Error tracking payment %v: %s", invoice, err.Error())
svc.Logger.Errorf("Error tracking payment %s: %s", invoice.RHash, err.Error())
return
}
paymentTracker, err := svc.LndClient.SubscribePayment(ctx, &routerrpc.TrackPaymentRequest{
PaymentHash: rawHash,
NoInflightUpdates: true,
})
if err != nil {
svc.Logger.Errorf("Error tracking payment %v: %s", invoice, err.Error())
svc.Logger.Errorf("Error tracking payment %s: %s", invoice.RHash, err.Error())
return
}
//fetch the tx entry for the invoice
entry := models.TransactionEntry{}
err = svc.DB.NewSelect().Model(&entry).Where("invoice_id = ?", invoice.ID).Limit(1).Scan(ctx)
if err != nil {
svc.Logger.Errorf("Error tracking payment %v: %s", invoice, err.Error())
return

}
if entry.UserID != invoice.UserID {
svc.Logger.Errorf("User ID's don't match : entry %v, invoice %v", entry, invoice)
return
}
//call HandleFailedPayment or HandleSuccesfulPayment
for {
payment, err := paymentTracker.Recv()
if err != nil {
svc.Logger.Errorf("Error tracking payment with hash %s: %s", invoice.RHash, err.Error())
return
}
err = svc.DB.NewSelect().Model(&entry).Where("invoice_id = ?", invoice.ID).Limit(1).Scan(ctx)
if err != nil {
svc.Logger.Errorf("Error tracking payment %s: %s", invoice.RHash, err.Error())
return

}
if entry.UserID != invoice.UserID {
svc.Logger.Errorf("User ID's don't match : entry %v, invoice %v", entry, invoice)
return
}
if payment.Status == lnrpc.Payment_FAILED {
svc.Logger.Infof("Failed payment detected: hash %s, reason %s", payment.PaymentHash, payment.FailureReason)
err = svc.HandleFailedPayment(ctx, invoice, entry, fmt.Errorf(payment.FailureReason.String()))
Expand Down

0 comments on commit 47690db

Please sign in to comment.