@@ -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"
@@ -627,3 +629,39 @@ func (s *SQLStore) QueryPayments(ctx context.Context,
627629 TotalCount : uint64 (totalCount ),
628630 }, nil
629631}
632+
633+ // FetchPayment fetches the payment corresponding to the given payment
634+ // hash.
635+ //
636+ // This is part of the DB interface.
637+ func (s * SQLStore ) FetchPayment (paymentHash lntypes.Hash ) (* MPPayment , error ) {
638+ ctx := context .TODO ()
639+
640+ var mpPayment * MPPayment
641+
642+ err := s .db .ExecTx (ctx , sqldb .ReadTxOpt (), func (db SQLQueries ) error {
643+ dbPayment , err := db .FetchPayment (ctx , paymentHash [:])
644+ if err != nil && ! errors .Is (err , sql .ErrNoRows ) {
645+ return fmt .Errorf ("failed to fetch payment: %w" , err )
646+ }
647+
648+ if errors .Is (err , sql .ErrNoRows ) {
649+ return ErrPaymentNotInitiated
650+ }
651+
652+ mpPayment , err = s .fetchPaymentWithCompleteData (
653+ ctx , db , dbPayment ,
654+ )
655+ if err != nil {
656+ return fmt .Errorf ("failed to fetch payment with " +
657+ "complete data: %w" , err )
658+ }
659+
660+ return nil
661+ }, sqldb .NoOpReset )
662+ if err != nil {
663+ return nil , err
664+ }
665+
666+ return mpPayment , nil
667+ }
0 commit comments