@@ -2,11 +2,13 @@ package paymentsdb
22
33import (
44 "context"
5+ "database/sql"
56 "errors"
67 "fmt"
78 "math"
89 "time"
910
11+ "github.com/lightningnetwork/lnd/lntypes"
1012 "github.com/lightningnetwork/lnd/lnwire"
1113 "github.com/lightningnetwork/lnd/sqldb"
1214 "github.com/lightningnetwork/lnd/sqldb/sqlc"
@@ -474,3 +476,40 @@ func (s *SQLStore) QueryPayments(ctx context.Context,
474476 TotalCount : uint64 (totalCount ),
475477 }, nil
476478}
479+
480+ // FetchPayment fetches the payment corresponding to the given payment
481+ // hash.
482+ //
483+ // This is part of the DB interface.
484+ func (s * SQLStore ) FetchPayment (paymentHash lntypes.Hash ) (* MPPayment , error ) {
485+ ctx := context .TODO ()
486+
487+ var mpPayment * MPPayment
488+
489+ err := s .db .ExecTx (ctx , sqldb .ReadTxOpt (), func (db SQLQueries ) error {
490+ dbPayment , err := db .FetchPayment (ctx , paymentHash [:])
491+ if err != nil && ! errors .Is (err , sql .ErrNoRows ) {
492+ return fmt .Errorf ("failed to fetch payment: %w" , err )
493+ }
494+
495+ if errors .Is (err , sql .ErrNoRows ) {
496+ return ErrPaymentNotInitiated
497+ }
498+
499+ mpPayment , err = s .fetchPaymentWithCompleteData (
500+ ctx , db , dbPayment ,
501+ )
502+ if err != nil {
503+ return fmt .Errorf ("failed to fetch payment with " +
504+ "complete data: %w" , err )
505+ }
506+
507+ return nil
508+ }, func () {
509+ })
510+ if err != nil {
511+ return nil , fmt .Errorf ("failed to fetch payment: %w" , err )
512+ }
513+
514+ return mpPayment , nil
515+ }
0 commit comments