accounts: implement ListAccountPayments in SQLStore

Add sqlc queries to select account payment hashes from the database.
Implement the ListAccountPayments method in the SQLStore, allowing
retrieval of stored payment hashes for SQLite and Postgres backends.

Also define the AccountPaymentEntry helper struct in
accounts/interface.go to wrap payment hashes and details.
This commit is contained in:
cyberguru1 2026-07-07 11:21:54 -05:00
parent a132fca1ba
commit 5632998030
No known key found for this signature in database
GPG key ID: F0FB5ECF1A8786E6
2 changed files with 82 additions and 0 deletions

View file

@ -107,6 +107,12 @@ type AccountInvoices map[lntypes.Hash]struct{}
// AccountPayments is the set of payments that are associated with an account.
type AccountPayments map[lntypes.Hash]*PaymentEntry
// AccountPaymentEntry wraps a payment hash with its entry details.
type AccountPaymentEntry struct {
Hash lntypes.Hash
*PaymentEntry
}
// OffChainBalanceAccount holds all information that is needed to keep track of
// a user's off-chain account balance. This balance can only be spent by paying
// invoices.

View file

@ -46,6 +46,8 @@ type SQLQueries interface {
ListAllAccountPayments(ctx context.Context) ([]sqlc.AccountPayment, error)
ListAccountInvoices(ctx context.Context, id int64) ([]sqlc.AccountInvoice, error)
ListAccountPayments(ctx context.Context, id int64) ([]sqlc.AccountPayment, error)
AccountPaymentsPaginated(ctx context.Context, arg sqlc.AccountPaymentsPaginatedParams) ([]sqlc.AccountPayment, error)
CountAccountPayments(ctx context.Context, accountID int64) (int64, error)
ListAllAccounts(ctx context.Context) ([]sqlc.Account, error)
SetAccountIndex(ctx context.Context, arg sqlc.SetAccountIndexParams) error
UpdateAccountBalance(ctx context.Context, arg sqlc.UpdateAccountBalanceParams) (int64, error)
@ -779,6 +781,80 @@ func (s *SQLStore) DeleteAccountPayment(ctx context.Context, alias AccountID,
}, sqldb.NoOpReset)
}
// ListAccountPayments returns a paginated list of payments
// associated with the given account, sorted in ascending lexicographical
// order of their payment hash.
func (s *SQLStore) ListAccountPayments(ctx context.Context, alias AccountID,
offset, limit int32) ([]*AccountPaymentEntry, error) {
var (
readTxOpts = db.NewQueryReadTx()
payments []*AccountPaymentEntry
)
err := s.db.ExecTx(ctx, &readTxOpts, func(db SQLQueries) error {
id, err := getAccountIDByAlias(ctx, db, alias)
if err != nil {
return err
}
var dbPayments []sqlc.AccountPayment
dbPayments, err = db.AccountPaymentsPaginated(
ctx, sqlc.AccountPaymentsPaginatedParams{
AccountID: id,
Limit: limit,
Offset: offset,
},
)
if err != nil {
return err
}
payments = make([]*AccountPaymentEntry, len(dbPayments))
for i, p := range dbPayments {
var hash lntypes.Hash
copy(hash[:], p.Hash)
payments[i] = &AccountPaymentEntry{
Hash: hash,
PaymentEntry: &PaymentEntry{
Status: lnrpc.Payment_PaymentStatus(
p.Status,
),
FullAmount: lnwire.MilliSatoshi(
p.FullAmountMsat,
),
},
}
}
return nil
}, sqldb.NoOpReset)
return payments, err
}
// CountAccountPayments returns the total number of payments associated with
// the given account.
func (s *SQLStore) CountAccountPayments(ctx context.Context,
alias AccountID) (uint64, error) {
var (
readTxOpts = db.NewQueryReadTx()
count int64
)
err := s.db.ExecTx(ctx, &readTxOpts, func(db SQLQueries) error {
id, err := getAccountIDByAlias(ctx, db, alias)
if err != nil {
return err
}
count, err = db.CountAccountPayments(ctx, id)
return err
}, sqldb.NoOpReset)
return uint64(count), err
}
// LastIndexes returns the last invoice add and settle index or
// ErrNoInvoiceIndexKnown if no indexes are known yet.
//