• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 17245860019

26 Aug 2025 05:34PM UTC coverage: 66.189% (+8.9%) from 57.321%
17245860019

Pull #9147

github

web-flow
Merge c65984741 into 0c2f045f5
Pull Request #9147: [Part 1|3] Introduce SQL Payment schema into LND

129 of 1880 new or added lines in 13 files covered. (6.86%)

24 existing lines in 9 files now uncovered.

136011 of 205489 relevant lines covered (66.19%)

21294.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/sqldb/sqlc/payments.sql.go
1
// Code generated by sqlc. DO NOT EDIT.
2
// versions:
3
//   sqlc v1.29.0
4
// source: payments.sql
5

6
package sqlc
7

8
import (
9
        "context"
10
        "database/sql"
11
        "strings"
12
        "time"
13
)
14

15
const countPayments = `-- name: CountPayments :one
16
SELECT COUNT(*) FROM payments
17
`
18

NEW
19
func (q *Queries) CountPayments(ctx context.Context) (int64, error) {
×
NEW
20
        row := q.db.QueryRowContext(ctx, countPayments)
×
NEW
21
        var count int64
×
NEW
22
        err := row.Scan(&count)
×
NEW
23
        return count, err
×
NEW
24
}
×
25

26
const deleteFailedAttempts = `-- name: DeleteFailedAttempts :exec
27
DELETE FROM payment_htlc_attempts WHERE payment_id = $1 AND htlc_fail_reason IS NOT NULL
28
`
29

30
// TODO(ziggie): Is the htlc_fail_reason always set for a failed attempt?
NEW
31
func (q *Queries) DeleteFailedAttempts(ctx context.Context, paymentID int64) error {
×
NEW
32
        _, err := q.db.ExecContext(ctx, deleteFailedAttempts, paymentID)
×
NEW
33
        return err
×
NEW
34
}
×
35

36
const deleteFailedAttemptsByAttemptIndices = `-- name: DeleteFailedAttemptsByAttemptIndices :exec
37
DELETE FROM payment_htlc_attempts WHERE attempt_index IN (/*SLICE:attempt_indices*/?)
38
`
39

NEW
40
func (q *Queries) DeleteFailedAttemptsByAttemptIndices(ctx context.Context, attemptIndices []int64) error {
×
NEW
41
        query := deleteFailedAttemptsByAttemptIndices
×
NEW
42
        var queryParams []interface{}
×
NEW
43
        if len(attemptIndices) > 0 {
×
NEW
44
                for _, v := range attemptIndices {
×
NEW
45
                        queryParams = append(queryParams, v)
×
NEW
46
                }
×
NEW
47
                query = strings.Replace(query, "/*SLICE:attempt_indices*/?", makeQueryParams(len(queryParams), len(attemptIndices)), 1)
×
NEW
48
        } else {
×
NEW
49
                query = strings.Replace(query, "/*SLICE:attempt_indices*/?", "NULL", 1)
×
NEW
50
        }
×
NEW
51
        _, err := q.db.ExecContext(ctx, query, queryParams...)
×
NEW
52
        return err
×
53
}
54

55
const deletePayment = `-- name: DeletePayment :exec
56
/* ─────────────────────────────────────────────
57
   Delete queries
58
   ─────────────────────────────────────────────
59
*/
60

61
DELETE FROM payments WHERE payment_hash = $1
62
`
63

NEW
64
func (q *Queries) DeletePayment(ctx context.Context, paymentHash []byte) error {
×
NEW
65
        _, err := q.db.ExecContext(ctx, deletePayment, paymentHash)
×
NEW
66
        return err
×
NEW
67
}
×
68

69
const deletePayments = `-- name: DeletePayments :exec
70
DELETE FROM payments WHERE id IN (/*SLICE:payment_ids*/?)
71
`
72

NEW
73
func (q *Queries) DeletePayments(ctx context.Context, paymentIds []int64) error {
×
NEW
74
        query := deletePayments
×
NEW
75
        var queryParams []interface{}
×
NEW
76
        if len(paymentIds) > 0 {
×
NEW
77
                for _, v := range paymentIds {
×
NEW
78
                        queryParams = append(queryParams, v)
×
NEW
79
                }
×
NEW
80
                query = strings.Replace(query, "/*SLICE:payment_ids*/?", makeQueryParams(len(queryParams), len(paymentIds)), 1)
×
NEW
81
        } else {
×
NEW
82
                query = strings.Replace(query, "/*SLICE:payment_ids*/?", "NULL", 1)
×
NEW
83
        }
×
NEW
84
        _, err := q.db.ExecContext(ctx, query, queryParams...)
×
NEW
85
        return err
×
86
}
87

88
const fetchAllInflightAttempts = `-- name: FetchAllInflightAttempts :many
89
SELECT id, attempt_index, payment_id, session_key, attempt_time, payment_hash, first_hop_amount_msat, route_total_time_lock, route_total_amount, route_source_key, failure_source_index, htlc_fail_reason, failure_msg, fail_time, settle_preimage, settle_time FROM payment_htlc_attempts ha
90
WHERE ha.settle_preimage IS NULL AND ha.htlc_fail_reason IS NULL
91
`
92

93
// Fetch all inflight attempts across all payments
NEW
94
func (q *Queries) FetchAllInflightAttempts(ctx context.Context) ([]PaymentHtlcAttempt, error) {
×
NEW
95
        rows, err := q.db.QueryContext(ctx, fetchAllInflightAttempts)
×
NEW
96
        if err != nil {
×
NEW
97
                return nil, err
×
NEW
98
        }
×
NEW
99
        defer rows.Close()
×
NEW
100
        var items []PaymentHtlcAttempt
×
NEW
101
        for rows.Next() {
×
NEW
102
                var i PaymentHtlcAttempt
×
NEW
103
                if err := rows.Scan(
×
NEW
104
                        &i.ID,
×
NEW
105
                        &i.AttemptIndex,
×
NEW
106
                        &i.PaymentID,
×
NEW
107
                        &i.SessionKey,
×
NEW
108
                        &i.AttemptTime,
×
NEW
109
                        &i.PaymentHash,
×
NEW
110
                        &i.FirstHopAmountMsat,
×
NEW
111
                        &i.RouteTotalTimeLock,
×
NEW
112
                        &i.RouteTotalAmount,
×
NEW
113
                        &i.RouteSourceKey,
×
NEW
114
                        &i.FailureSourceIndex,
×
NEW
115
                        &i.HtlcFailReason,
×
NEW
116
                        &i.FailureMsg,
×
NEW
117
                        &i.FailTime,
×
NEW
118
                        &i.SettlePreimage,
×
NEW
119
                        &i.SettleTime,
×
NEW
120
                ); err != nil {
×
NEW
121
                        return nil, err
×
NEW
122
                }
×
NEW
123
                items = append(items, i)
×
124
        }
NEW
125
        if err := rows.Close(); err != nil {
×
NEW
126
                return nil, err
×
NEW
127
        }
×
NEW
128
        if err := rows.Err(); err != nil {
×
NEW
129
                return nil, err
×
NEW
130
        }
×
NEW
131
        return items, nil
×
132
}
133

134
const fetchCustomRecordsForAttempts = `-- name: FetchCustomRecordsForAttempts :many
135
SELECT id, key, value, htlc_attempt_index FROM payment_htlc_attempt_custom_records
136
WHERE htlc_attempt_index IN (/*SLICE:htlc_attempt_indices*/?)
137
`
138

NEW
139
func (q *Queries) FetchCustomRecordsForAttempts(ctx context.Context, htlcAttemptIndices []int64) ([]PaymentHtlcAttemptCustomRecord, error) {
×
NEW
140
        query := fetchCustomRecordsForAttempts
×
NEW
141
        var queryParams []interface{}
×
NEW
142
        if len(htlcAttemptIndices) > 0 {
×
NEW
143
                for _, v := range htlcAttemptIndices {
×
NEW
144
                        queryParams = append(queryParams, v)
×
NEW
145
                }
×
NEW
146
                query = strings.Replace(query, "/*SLICE:htlc_attempt_indices*/?", makeQueryParams(len(queryParams), len(htlcAttemptIndices)), 1)
×
NEW
147
        } else {
×
NEW
148
                query = strings.Replace(query, "/*SLICE:htlc_attempt_indices*/?", "NULL", 1)
×
NEW
149
        }
×
NEW
150
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
NEW
151
        if err != nil {
×
NEW
152
                return nil, err
×
NEW
153
        }
×
NEW
154
        defer rows.Close()
×
NEW
155
        var items []PaymentHtlcAttemptCustomRecord
×
NEW
156
        for rows.Next() {
×
NEW
157
                var i PaymentHtlcAttemptCustomRecord
×
NEW
158
                if err := rows.Scan(
×
NEW
159
                        &i.ID,
×
NEW
160
                        &i.Key,
×
NEW
161
                        &i.Value,
×
NEW
162
                        &i.HtlcAttemptIndex,
×
NEW
163
                ); err != nil {
×
NEW
164
                        return nil, err
×
NEW
165
                }
×
NEW
166
                items = append(items, i)
×
167
        }
NEW
168
        if err := rows.Close(); err != nil {
×
NEW
169
                return nil, err
×
NEW
170
        }
×
NEW
171
        if err := rows.Err(); err != nil {
×
NEW
172
                return nil, err
×
NEW
173
        }
×
NEW
174
        return items, nil
×
175
}
176

177
const fetchCustomRecordsForHops = `-- name: FetchCustomRecordsForHops :many
178
SELECT id, key, value, hop_id FROM payment_route_hop_custom_records
179
WHERE hop_id IN (/*SLICE:hop_ids*/?)
180
`
181

NEW
182
func (q *Queries) FetchCustomRecordsForHops(ctx context.Context, hopIds []int64) ([]PaymentRouteHopCustomRecord, error) {
×
NEW
183
        query := fetchCustomRecordsForHops
×
NEW
184
        var queryParams []interface{}
×
NEW
185
        if len(hopIds) > 0 {
×
NEW
186
                for _, v := range hopIds {
×
NEW
187
                        queryParams = append(queryParams, v)
×
NEW
188
                }
×
NEW
189
                query = strings.Replace(query, "/*SLICE:hop_ids*/?", makeQueryParams(len(queryParams), len(hopIds)), 1)
×
NEW
190
        } else {
×
NEW
191
                query = strings.Replace(query, "/*SLICE:hop_ids*/?", "NULL", 1)
×
NEW
192
        }
×
NEW
193
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
NEW
194
        if err != nil {
×
NEW
195
                return nil, err
×
NEW
196
        }
×
NEW
197
        defer rows.Close()
×
NEW
198
        var items []PaymentRouteHopCustomRecord
×
NEW
199
        for rows.Next() {
×
NEW
200
                var i PaymentRouteHopCustomRecord
×
NEW
201
                if err := rows.Scan(
×
NEW
202
                        &i.ID,
×
NEW
203
                        &i.Key,
×
NEW
204
                        &i.Value,
×
NEW
205
                        &i.HopID,
×
NEW
206
                ); err != nil {
×
NEW
207
                        return nil, err
×
NEW
208
                }
×
NEW
209
                items = append(items, i)
×
210
        }
NEW
211
        if err := rows.Close(); err != nil {
×
NEW
212
                return nil, err
×
NEW
213
        }
×
NEW
214
        if err := rows.Err(); err != nil {
×
NEW
215
                return nil, err
×
NEW
216
        }
×
NEW
217
        return items, nil
×
218
}
219

220
const fetchFirstHopCustomRecords = `-- name: FetchFirstHopCustomRecords :many
221
SELECT id, key, value, payment_id FROM payment_first_hop_custom_records WHERE payment_id = $1
222
`
223

NEW
224
func (q *Queries) FetchFirstHopCustomRecords(ctx context.Context, paymentID int64) ([]PaymentFirstHopCustomRecord, error) {
×
NEW
225
        rows, err := q.db.QueryContext(ctx, fetchFirstHopCustomRecords, paymentID)
×
NEW
226
        if err != nil {
×
NEW
227
                return nil, err
×
NEW
228
        }
×
NEW
229
        defer rows.Close()
×
NEW
230
        var items []PaymentFirstHopCustomRecord
×
NEW
231
        for rows.Next() {
×
NEW
232
                var i PaymentFirstHopCustomRecord
×
NEW
233
                if err := rows.Scan(
×
NEW
234
                        &i.ID,
×
NEW
235
                        &i.Key,
×
NEW
236
                        &i.Value,
×
NEW
237
                        &i.PaymentID,
×
NEW
238
                ); err != nil {
×
NEW
239
                        return nil, err
×
NEW
240
                }
×
NEW
241
                items = append(items, i)
×
242
        }
NEW
243
        if err := rows.Close(); err != nil {
×
NEW
244
                return nil, err
×
NEW
245
        }
×
NEW
246
        if err := rows.Err(); err != nil {
×
NEW
247
                return nil, err
×
NEW
248
        }
×
NEW
249
        return items, nil
×
250
}
251

252
const fetchHopsForAttempt = `-- name: FetchHopsForAttempt :many
253
SELECT id, htlc_attempt_index, hop_index, pub_key, scid, outgoing_time_lock, amt_to_forward, meta_data, legacy_payload, mpp_payment_addr, mpp_total_msat, amp_root_share, amp_set_id, amp_child_index, encrypted_data, blinding_point, blinded_path_total_amt FROM payment_route_hops h
254
WHERE h.htlc_attempt_index = $1
255
ORDER BY h.hop_index ASC
256
`
257

NEW
258
func (q *Queries) FetchHopsForAttempt(ctx context.Context, htlcAttemptIndex int64) ([]PaymentRouteHop, error) {
×
NEW
259
        rows, err := q.db.QueryContext(ctx, fetchHopsForAttempt, htlcAttemptIndex)
×
NEW
260
        if err != nil {
×
NEW
261
                return nil, err
×
NEW
262
        }
×
NEW
263
        defer rows.Close()
×
NEW
264
        var items []PaymentRouteHop
×
NEW
265
        for rows.Next() {
×
NEW
266
                var i PaymentRouteHop
×
NEW
267
                if err := rows.Scan(
×
NEW
268
                        &i.ID,
×
NEW
269
                        &i.HtlcAttemptIndex,
×
NEW
270
                        &i.HopIndex,
×
NEW
271
                        &i.PubKey,
×
NEW
272
                        &i.Scid,
×
NEW
273
                        &i.OutgoingTimeLock,
×
NEW
274
                        &i.AmtToForward,
×
NEW
275
                        &i.MetaData,
×
NEW
276
                        &i.LegacyPayload,
×
NEW
277
                        &i.MppPaymentAddr,
×
NEW
278
                        &i.MppTotalMsat,
×
NEW
279
                        &i.AmpRootShare,
×
NEW
280
                        &i.AmpSetID,
×
NEW
281
                        &i.AmpChildIndex,
×
NEW
282
                        &i.EncryptedData,
×
NEW
283
                        &i.BlindingPoint,
×
NEW
284
                        &i.BlindedPathTotalAmt,
×
NEW
285
                ); err != nil {
×
NEW
286
                        return nil, err
×
NEW
287
                }
×
NEW
288
                items = append(items, i)
×
289
        }
NEW
290
        if err := rows.Close(); err != nil {
×
NEW
291
                return nil, err
×
NEW
292
        }
×
NEW
293
        if err := rows.Err(); err != nil {
×
NEW
294
                return nil, err
×
NEW
295
        }
×
NEW
296
        return items, nil
×
297
}
298

299
const fetchHopsForAttempts = `-- name: FetchHopsForAttempts :many
300
SELECT id, htlc_attempt_index, hop_index, pub_key, scid, outgoing_time_lock, amt_to_forward, meta_data, legacy_payload, mpp_payment_addr, mpp_total_msat, amp_root_share, amp_set_id, amp_child_index, encrypted_data, blinding_point, blinded_path_total_amt FROM payment_route_hops
301
WHERE htlc_attempt_index IN (/*SLICE:htlc_attempt_indices*/?)
302
`
303

NEW
304
func (q *Queries) FetchHopsForAttempts(ctx context.Context, htlcAttemptIndices []int64) ([]PaymentRouteHop, error) {
×
NEW
305
        query := fetchHopsForAttempts
×
NEW
306
        var queryParams []interface{}
×
NEW
307
        if len(htlcAttemptIndices) > 0 {
×
NEW
308
                for _, v := range htlcAttemptIndices {
×
NEW
309
                        queryParams = append(queryParams, v)
×
NEW
310
                }
×
NEW
311
                query = strings.Replace(query, "/*SLICE:htlc_attempt_indices*/?", makeQueryParams(len(queryParams), len(htlcAttemptIndices)), 1)
×
NEW
312
        } else {
×
NEW
313
                query = strings.Replace(query, "/*SLICE:htlc_attempt_indices*/?", "NULL", 1)
×
NEW
314
        }
×
NEW
315
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
NEW
316
        if err != nil {
×
NEW
317
                return nil, err
×
NEW
318
        }
×
NEW
319
        defer rows.Close()
×
NEW
320
        var items []PaymentRouteHop
×
NEW
321
        for rows.Next() {
×
NEW
322
                var i PaymentRouteHop
×
NEW
323
                if err := rows.Scan(
×
NEW
324
                        &i.ID,
×
NEW
325
                        &i.HtlcAttemptIndex,
×
NEW
326
                        &i.HopIndex,
×
NEW
327
                        &i.PubKey,
×
NEW
328
                        &i.Scid,
×
NEW
329
                        &i.OutgoingTimeLock,
×
NEW
330
                        &i.AmtToForward,
×
NEW
331
                        &i.MetaData,
×
NEW
332
                        &i.LegacyPayload,
×
NEW
333
                        &i.MppPaymentAddr,
×
NEW
334
                        &i.MppTotalMsat,
×
NEW
335
                        &i.AmpRootShare,
×
NEW
336
                        &i.AmpSetID,
×
NEW
337
                        &i.AmpChildIndex,
×
NEW
338
                        &i.EncryptedData,
×
NEW
339
                        &i.BlindingPoint,
×
NEW
340
                        &i.BlindedPathTotalAmt,
×
NEW
341
                ); err != nil {
×
NEW
342
                        return nil, err
×
NEW
343
                }
×
NEW
344
                items = append(items, i)
×
345
        }
NEW
346
        if err := rows.Close(); err != nil {
×
NEW
347
                return nil, err
×
NEW
348
        }
×
NEW
349
        if err := rows.Err(); err != nil {
×
NEW
350
                return nil, err
×
NEW
351
        }
×
NEW
352
        return items, nil
×
353
}
354

355
const fetchHtlcAttempts = `-- name: FetchHtlcAttempts :many
356
SELECT id, attempt_index, payment_id, session_key, attempt_time, payment_hash, first_hop_amount_msat, route_total_time_lock, route_total_amount, route_source_key, failure_source_index, htlc_fail_reason, failure_msg, fail_time, settle_preimage, settle_time FROM payment_htlc_attempts ha
357
WHERE ha.payment_id = $1
358
    AND (
359
        ($2 = true AND ha.settle_preimage IS NULL AND ha.htlc_fail_reason IS NULL)
360
        OR
361
        ($2 = false OR $2 IS NULL)
362
    )
363
ORDER BY
364
    CASE WHEN $3 = false OR $3 IS NULL THEN ha.attempt_time END ASC,
365
    CASE WHEN $3 = true THEN ha.attempt_time END DESC
366
`
367

368
type FetchHtlcAttemptsParams struct {
369
        PaymentID    int64
370
        InFlightOnly interface{}
371
        Reverse      interface{}
372
}
373

374
// This fetches all htlc attempts for a payment.
NEW
375
func (q *Queries) FetchHtlcAttempts(ctx context.Context, arg FetchHtlcAttemptsParams) ([]PaymentHtlcAttempt, error) {
×
NEW
376
        rows, err := q.db.QueryContext(ctx, fetchHtlcAttempts, arg.PaymentID, arg.InFlightOnly, arg.Reverse)
×
NEW
377
        if err != nil {
×
NEW
378
                return nil, err
×
NEW
379
        }
×
NEW
380
        defer rows.Close()
×
NEW
381
        var items []PaymentHtlcAttempt
×
NEW
382
        for rows.Next() {
×
NEW
383
                var i PaymentHtlcAttempt
×
NEW
384
                if err := rows.Scan(
×
NEW
385
                        &i.ID,
×
NEW
386
                        &i.AttemptIndex,
×
NEW
387
                        &i.PaymentID,
×
NEW
388
                        &i.SessionKey,
×
NEW
389
                        &i.AttemptTime,
×
NEW
390
                        &i.PaymentHash,
×
NEW
391
                        &i.FirstHopAmountMsat,
×
NEW
392
                        &i.RouteTotalTimeLock,
×
NEW
393
                        &i.RouteTotalAmount,
×
NEW
394
                        &i.RouteSourceKey,
×
NEW
395
                        &i.FailureSourceIndex,
×
NEW
396
                        &i.HtlcFailReason,
×
NEW
397
                        &i.FailureMsg,
×
NEW
398
                        &i.FailTime,
×
NEW
399
                        &i.SettlePreimage,
×
NEW
400
                        &i.SettleTime,
×
NEW
401
                ); err != nil {
×
NEW
402
                        return nil, err
×
NEW
403
                }
×
NEW
404
                items = append(items, i)
×
405
        }
NEW
406
        if err := rows.Close(); err != nil {
×
NEW
407
                return nil, err
×
NEW
408
        }
×
NEW
409
        if err := rows.Err(); err != nil {
×
NEW
410
                return nil, err
×
NEW
411
        }
×
NEW
412
        return items, nil
×
413
}
414

415
const fetchPayment = `-- name: FetchPayment :one
416
SELECT id, payment_request, amount_msat, created_at, payment_hash, fail_reason FROM payments WHERE payment_hash = $1
417
`
418

NEW
419
func (q *Queries) FetchPayment(ctx context.Context, paymentHash []byte) (Payment, error) {
×
NEW
420
        row := q.db.QueryRowContext(ctx, fetchPayment, paymentHash)
×
NEW
421
        var i Payment
×
NEW
422
        err := row.Scan(
×
NEW
423
                &i.ID,
×
NEW
424
                &i.PaymentRequest,
×
NEW
425
                &i.AmountMsat,
×
NEW
426
                &i.CreatedAt,
×
NEW
427
                &i.PaymentHash,
×
NEW
428
                &i.FailReason,
×
NEW
429
        )
×
NEW
430
        return i, err
×
NEW
431
}
×
432

433
const fetchPayments = `-- name: FetchPayments :many
434
SELECT id, payment_request, amount_msat, created_at, payment_hash, fail_reason FROM payments WHERE payment_hash IN (/*SLICE:payment_hashes*/?)
435
`
436

NEW
437
func (q *Queries) FetchPayments(ctx context.Context, paymentHashes [][]byte) ([]Payment, error) {
×
NEW
438
        query := fetchPayments
×
NEW
439
        var queryParams []interface{}
×
NEW
440
        if len(paymentHashes) > 0 {
×
NEW
441
                for _, v := range paymentHashes {
×
NEW
442
                        queryParams = append(queryParams, v)
×
NEW
443
                }
×
NEW
444
                query = strings.Replace(query, "/*SLICE:payment_hashes*/?", makeQueryParams(len(queryParams), len(paymentHashes)), 1)
×
NEW
445
        } else {
×
NEW
446
                query = strings.Replace(query, "/*SLICE:payment_hashes*/?", "NULL", 1)
×
NEW
447
        }
×
NEW
448
        rows, err := q.db.QueryContext(ctx, query, queryParams...)
×
NEW
449
        if err != nil {
×
NEW
450
                return nil, err
×
NEW
451
        }
×
NEW
452
        defer rows.Close()
×
NEW
453
        var items []Payment
×
NEW
454
        for rows.Next() {
×
NEW
455
                var i Payment
×
NEW
456
                if err := rows.Scan(
×
NEW
457
                        &i.ID,
×
NEW
458
                        &i.PaymentRequest,
×
NEW
459
                        &i.AmountMsat,
×
NEW
460
                        &i.CreatedAt,
×
NEW
461
                        &i.PaymentHash,
×
NEW
462
                        &i.FailReason,
×
NEW
463
                ); err != nil {
×
NEW
464
                        return nil, err
×
NEW
465
                }
×
NEW
466
                items = append(items, i)
×
467
        }
NEW
468
        if err := rows.Close(); err != nil {
×
NEW
469
                return nil, err
×
NEW
470
        }
×
NEW
471
        if err := rows.Err(); err != nil {
×
NEW
472
                return nil, err
×
NEW
473
        }
×
NEW
474
        return items, nil
×
475
}
476

477
const filterPayments = `-- name: FilterPayments :many
478
/* ─────────────────────────────────────────────
479
   fetch queries
480
   ─────────────────────────────────────────────
481
*/
482

483
SELECT id, payment_request, amount_msat, created_at, payment_hash, fail_reason FROM payments
484
WHERE (
485
    -- This will exclude payments which have the failed reason set. These 
486
    -- payments might not be final yet meaning that they still can be inflight
487
    -- but they will transition to failed when all corresponding HTLCs are
488
    -- resolved.     
489
    ($1 = true AND fail_reason IS NULL) OR
490
    $1 = false OR $1 IS NULL
491
)
492
 AND (
493
    id > $2 OR
494
    $2 IS NULL 
495

496
) AND (
497
    id < $3 OR
498
    $3 IS NULL
499
) AND (
500
    created_at >= $4 OR
501
    $4 IS NULL
502
) AND (
503
    created_at <= $5 OR
504
    $5 IS NULL
505
)
506
ORDER BY 
507
    CASE WHEN $6 = false OR $6 IS NULL THEN id END ASC,
508
    CASE WHEN $6 = true THEN id END DESC
509
LIMIT $7
510
`
511

512
type FilterPaymentsParams struct {
513
        ExcludeFailed  interface{}
514
        IndexOffsetGet sql.NullInt64
515
        IndexOffsetLet sql.NullInt64
516
        CreatedAfter   sql.NullTime
517
        CreatedBefore  sql.NullTime
518
        Reverse        interface{}
519
        NumLimit       int32
520
}
521

NEW
522
func (q *Queries) FilterPayments(ctx context.Context, arg FilterPaymentsParams) ([]Payment, error) {
×
NEW
523
        rows, err := q.db.QueryContext(ctx, filterPayments,
×
NEW
524
                arg.ExcludeFailed,
×
NEW
525
                arg.IndexOffsetGet,
×
NEW
526
                arg.IndexOffsetLet,
×
NEW
527
                arg.CreatedAfter,
×
NEW
528
                arg.CreatedBefore,
×
NEW
529
                arg.Reverse,
×
NEW
530
                arg.NumLimit,
×
NEW
531
        )
×
NEW
532
        if err != nil {
×
NEW
533
                return nil, err
×
NEW
534
        }
×
NEW
535
        defer rows.Close()
×
NEW
536
        var items []Payment
×
NEW
537
        for rows.Next() {
×
NEW
538
                var i Payment
×
NEW
539
                if err := rows.Scan(
×
NEW
540
                        &i.ID,
×
NEW
541
                        &i.PaymentRequest,
×
NEW
542
                        &i.AmountMsat,
×
NEW
543
                        &i.CreatedAt,
×
NEW
544
                        &i.PaymentHash,
×
NEW
545
                        &i.FailReason,
×
NEW
546
                ); err != nil {
×
NEW
547
                        return nil, err
×
NEW
548
                }
×
NEW
549
                items = append(items, i)
×
550
        }
NEW
551
        if err := rows.Close(); err != nil {
×
NEW
552
                return nil, err
×
NEW
553
        }
×
NEW
554
        if err := rows.Err(); err != nil {
×
NEW
555
                return nil, err
×
NEW
556
        }
×
NEW
557
        return items, nil
×
558
}
559

560
const insertFirstHopCustomRecord = `-- name: InsertFirstHopCustomRecord :exec
561
INSERT INTO payment_first_hop_custom_records (
562
    payment_id,
563
    key,
564
    value
565
) VALUES (
566
    $1, $2, $3
567
)
568
`
569

570
type InsertFirstHopCustomRecordParams struct {
571
        PaymentID int64
572
        Key       int64
573
        Value     []byte
574
}
575

NEW
576
func (q *Queries) InsertFirstHopCustomRecord(ctx context.Context, arg InsertFirstHopCustomRecordParams) error {
×
NEW
577
        _, err := q.db.ExecContext(ctx, insertFirstHopCustomRecord, arg.PaymentID, arg.Key, arg.Value)
×
NEW
578
        return err
×
NEW
579
}
×
580

581
const insertHop = `-- name: InsertHop :one
582
INSERT INTO payment_route_hops (
583
    htlc_attempt_index,
584
    hop_index,
585
    pub_key,
586
    scid,
587
    outgoing_time_lock,
588
    amt_to_forward,
589
    meta_data,
590
    legacy_payload,
591
    mpp_payment_addr,
592
    mpp_total_msat,
593
    amp_root_share,
594
    amp_set_id,
595
    amp_child_index,
596
    blinding_point,
597
    encrypted_data,
598
    blinded_path_total_amt
599
) VALUES (
600
        $1,
601
        $2,
602
        $3,
603
        $4,
604
        $5,
605
        $6,
606
        $7,
607
        $8,
608
        $9,
609
        $10,
610
        $11,
611
        $12,
612
        $13,
613
        $14,
614
        $15,
615
        $16
616
) RETURNING id
617
`
618

619
type InsertHopParams struct {
620
        HtlcAttemptIndex    int64
621
        HopIndex            int32
622
        PubKey              []byte
623
        Scid                string
624
        OutgoingTimeLock    int32
625
        AmtToForward        int64
626
        MetaData            []byte
627
        LegacyPayload       bool
628
        MppPaymentAddr      []byte
629
        MppTotalMsat        sql.NullInt64
630
        AmpRootShare        []byte
631
        AmpSetID            []byte
632
        AmpChildIndex       sql.NullInt32
633
        BlindingPoint       []byte
634
        EncryptedData       []byte
635
        BlindedPathTotalAmt sql.NullInt64
636
}
637

NEW
638
func (q *Queries) InsertHop(ctx context.Context, arg InsertHopParams) (int64, error) {
×
NEW
639
        row := q.db.QueryRowContext(ctx, insertHop,
×
NEW
640
                arg.HtlcAttemptIndex,
×
NEW
641
                arg.HopIndex,
×
NEW
642
                arg.PubKey,
×
NEW
643
                arg.Scid,
×
NEW
644
                arg.OutgoingTimeLock,
×
NEW
645
                arg.AmtToForward,
×
NEW
646
                arg.MetaData,
×
NEW
647
                arg.LegacyPayload,
×
NEW
648
                arg.MppPaymentAddr,
×
NEW
649
                arg.MppTotalMsat,
×
NEW
650
                arg.AmpRootShare,
×
NEW
651
                arg.AmpSetID,
×
NEW
652
                arg.AmpChildIndex,
×
NEW
653
                arg.BlindingPoint,
×
NEW
654
                arg.EncryptedData,
×
NEW
655
                arg.BlindedPathTotalAmt,
×
NEW
656
        )
×
NEW
657
        var id int64
×
NEW
658
        err := row.Scan(&id)
×
NEW
659
        return id, err
×
NEW
660
}
×
661

662
const insertHopCustomRecord = `-- name: InsertHopCustomRecord :exec
663
INSERT INTO payment_route_hop_custom_records (
664
    hop_id,
665
    key,
666
    value
667
) VALUES (
668
    $1,
669
    $2,
670
    $3
671
)
672
`
673

674
type InsertHopCustomRecordParams struct {
675
        HopID int64
676
        Key   int64
677
        Value []byte
678
}
679

NEW
680
func (q *Queries) InsertHopCustomRecord(ctx context.Context, arg InsertHopCustomRecordParams) error {
×
NEW
681
        _, err := q.db.ExecContext(ctx, insertHopCustomRecord, arg.HopID, arg.Key, arg.Value)
×
NEW
682
        return err
×
NEW
683
}
×
684

685
const insertHtlAttemptFirstHopCustomRecord = `-- name: InsertHtlAttemptFirstHopCustomRecord :exec
686
INSERT INTO payment_htlc_attempt_custom_records (
687
    htlc_attempt_index,
688
    key,
689
    value
690
) VALUES (
691
    $1,
692
    $2,
693
    $3
694
)
695
`
696

697
type InsertHtlAttemptFirstHopCustomRecordParams struct {
698
        HtlcAttemptIndex int64
699
        Key              int64
700
        Value            []byte
701
}
702

NEW
703
func (q *Queries) InsertHtlAttemptFirstHopCustomRecord(ctx context.Context, arg InsertHtlAttemptFirstHopCustomRecordParams) error {
×
NEW
704
        _, err := q.db.ExecContext(ctx, insertHtlAttemptFirstHopCustomRecord, arg.HtlcAttemptIndex, arg.Key, arg.Value)
×
NEW
705
        return err
×
NEW
706
}
×
707

708
const insertHtlcAttempt = `-- name: InsertHtlcAttempt :one
709
INSERT INTO payment_htlc_attempts (
710
    attempt_index,
711
    payment_id,
712
    session_key,
713
    attempt_time,
714
    payment_hash,
715
    route_total_time_lock,
716
    route_total_amount,
717
    route_source_key,
718
    first_hop_amount_msat
719
) VALUES (
720
    $1,
721
    $2,
722
    $3,
723
    $4,
724
    $5,
725
    $6,
726
    $7,
727
    $8,
728
    $9
729
) RETURNING id
730
`
731

732
type InsertHtlcAttemptParams struct {
733
        AttemptIndex       int64
734
        PaymentID          int64
735
        SessionKey         []byte
736
        AttemptTime        time.Time
737
        PaymentHash        []byte
738
        RouteTotalTimeLock int32
739
        RouteTotalAmount   int64
740
        RouteSourceKey     []byte
741
        FirstHopAmountMsat int64
742
}
743

NEW
744
func (q *Queries) InsertHtlcAttempt(ctx context.Context, arg InsertHtlcAttemptParams) (int64, error) {
×
NEW
745
        row := q.db.QueryRowContext(ctx, insertHtlcAttempt,
×
NEW
746
                arg.AttemptIndex,
×
NEW
747
                arg.PaymentID,
×
NEW
748
                arg.SessionKey,
×
NEW
749
                arg.AttemptTime,
×
NEW
750
                arg.PaymentHash,
×
NEW
751
                arg.RouteTotalTimeLock,
×
NEW
752
                arg.RouteTotalAmount,
×
NEW
753
                arg.RouteSourceKey,
×
NEW
754
                arg.FirstHopAmountMsat,
×
NEW
755
        )
×
NEW
756
        var id int64
×
NEW
757
        err := row.Scan(&id)
×
NEW
758
        return id, err
×
NEW
759
}
×
760

761
const insertPayment = `-- name: InsertPayment :one
762
/* ─────────────────────────────────────────────
763
   Insert queries
764
   ─────────────────────────────────────────────
765
*/
766

767
INSERT INTO payments (
768
    payment_request,
769
    amount_msat,
770
    created_at,
771
    payment_hash
772
) VALUES (
773
    $1,
774
    $2,
775
    $3,
776
    $4
777
) RETURNING id
778
`
779

780
type InsertPaymentParams struct {
781
        PaymentRequest []byte
782
        AmountMsat     int64
783
        CreatedAt      time.Time
784
        PaymentHash    []byte
785
}
786

NEW
787
func (q *Queries) InsertPayment(ctx context.Context, arg InsertPaymentParams) (int64, error) {
×
NEW
788
        row := q.db.QueryRowContext(ctx, insertPayment,
×
NEW
789
                arg.PaymentRequest,
×
NEW
790
                arg.AmountMsat,
×
NEW
791
                arg.CreatedAt,
×
NEW
792
                arg.PaymentHash,
×
NEW
793
        )
×
NEW
794
        var id int64
×
NEW
795
        err := row.Scan(&id)
×
NEW
796
        return id, err
×
NEW
797
}
×
798

799
const updateHtlcAttemptFailInfo = `-- name: UpdateHtlcAttemptFailInfo :one
800
UPDATE payment_htlc_attempts
801
SET htlc_fail_reason = $1, fail_time = $2, failure_source_index = $3, failure_msg = $4
802
WHERE attempt_index = $5
803
RETURNING id
804
`
805

806
type UpdateHtlcAttemptFailInfoParams struct {
807
        HtlcFailReason     sql.NullInt32
808
        FailTime           sql.NullTime
809
        FailureSourceIndex sql.NullInt32
810
        FailureMsg         []byte
811
        AttemptIndex       int64
812
}
813

NEW
814
func (q *Queries) UpdateHtlcAttemptFailInfo(ctx context.Context, arg UpdateHtlcAttemptFailInfoParams) (int64, error) {
×
NEW
815
        row := q.db.QueryRowContext(ctx, updateHtlcAttemptFailInfo,
×
NEW
816
                arg.HtlcFailReason,
×
NEW
817
                arg.FailTime,
×
NEW
818
                arg.FailureSourceIndex,
×
NEW
819
                arg.FailureMsg,
×
NEW
820
                arg.AttemptIndex,
×
NEW
821
        )
×
NEW
822
        var id int64
×
NEW
823
        err := row.Scan(&id)
×
NEW
824
        return id, err
×
NEW
825
}
×
826

827
const updateHtlcAttemptSettleInfo = `-- name: UpdateHtlcAttemptSettleInfo :one
828
/* ─────────────────────────────────────────────
829
   Update queries
830
   ─────────────────────────────────────────────
831
*/
832

833
UPDATE payment_htlc_attempts
834
SET settle_preimage = $1, settle_time = $2
835
WHERE attempt_index = $3
836
RETURNING id
837
`
838

839
type UpdateHtlcAttemptSettleInfoParams struct {
840
        SettlePreimage []byte
841
        SettleTime     sql.NullTime
842
        AttemptIndex   int64
843
}
844

NEW
845
func (q *Queries) UpdateHtlcAttemptSettleInfo(ctx context.Context, arg UpdateHtlcAttemptSettleInfoParams) (int64, error) {
×
NEW
846
        row := q.db.QueryRowContext(ctx, updateHtlcAttemptSettleInfo, arg.SettlePreimage, arg.SettleTime, arg.AttemptIndex)
×
NEW
847
        var id int64
×
NEW
848
        err := row.Scan(&id)
×
NEW
849
        return id, err
×
NEW
850
}
×
851

852
const updatePaymentFailReason = `-- name: UpdatePaymentFailReason :one
853
UPDATE payments
854
SET fail_reason = $1
855
WHERE payment_hash = $2
856
RETURNING id
857
`
858

859
type UpdatePaymentFailReasonParams struct {
860
        FailReason  sql.NullInt32
861
        PaymentHash []byte
862
}
863

NEW
864
func (q *Queries) UpdatePaymentFailReason(ctx context.Context, arg UpdatePaymentFailReasonParams) (int64, error) {
×
NEW
865
        row := q.db.QueryRowContext(ctx, updatePaymentFailReason, arg.FailReason, arg.PaymentHash)
×
NEW
866
        var id int64
×
NEW
867
        err := row.Scan(&id)
×
NEW
868
        return id, err
×
NEW
869
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc