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