-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathlndwrap.go
133 lines (107 loc) · 3.56 KB
/
lndwrap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Package lndwrap wraps various calls to lndclient for convenience. It offers
// wrapping for paginated queries that will obtain all entries from a desired
// index onwards.
package lndwrap
import (
"context"
"fmt"
"time"
"github.com/lightninglabs/faraday/paginater"
"github.com/lightninglabs/lndclient"
)
// ListInvoices makes paginated calls to lnd to get our full set of
// invoices.
func ListInvoices(ctx context.Context, startOffset, maxInvoices uint64,
lnd lndclient.LightningClient) ([]lndclient.Invoice, error) {
var invoices []lndclient.Invoice
query := func(offset, maxInvoices uint64) (uint64, uint64, error) {
resp, err := lnd.ListInvoices(
ctx, lndclient.ListInvoicesRequest{
Offset: offset,
MaxInvoices: maxInvoices,
},
)
if err != nil {
return 0, 0, err
}
invoices = append(invoices, resp.Invoices...)
return resp.LastIndexOffset, uint64(len(resp.Invoices)), nil
}
// Make paginated calls to the invoices API, starting at offset 0 and
// querying our max number of invoices each time.
if err := paginater.QueryPaginated(
ctx, query, startOffset, maxInvoices,
); err != nil {
return nil, fmt.Errorf("ListInvoices failed: %w", err)
}
return invoices, nil
}
// ListPayments makes a set of paginated calls to lnd to get our full set
// of payments.
func ListPayments(ctx context.Context, startOffset, maxPayments uint64,
lnd lndclient.LightningClient) ([]lndclient.Payment, error) {
var payments []lndclient.Payment
query := func(offset, maxEvents uint64) (uint64, uint64, error) {
resp, err := lnd.ListPayments(
ctx, lndclient.ListPaymentsRequest{
Offset: offset,
MaxPayments: maxEvents,
},
)
if err != nil {
return 0, 0, err
}
payments = append(payments, resp.Payments...)
return resp.LastIndexOffset, uint64(len(resp.Payments)), nil
}
// Make paginated calls to the payments API, starting at offset 0 and
// querying our max number of payments each time.
if err := paginater.QueryPaginated(
ctx, query, startOffset, maxPayments,
); err != nil {
return nil, fmt.Errorf("ListPayments failed: %w", err)
}
return payments, nil
}
// ListForwards makes paginated calls to our forwarding events api.
func ListForwards(ctx context.Context, maxForwards uint64, startTime,
endTime time.Time, lnd lndclient.LightningClient) (
[]lndclient.ForwardingEvent, error) {
var forwards []lndclient.ForwardingEvent
query := func(offset, maxEvents uint64) (uint64, uint64, error) {
resp, err := lnd.ForwardingHistory(
ctx, lndclient.ForwardingHistoryRequest{
StartTime: startTime,
EndTime: endTime,
Offset: uint32(offset),
MaxEvents: uint32(maxEvents),
},
)
if err != nil {
return 0, 0, err
}
forwards = append(forwards, resp.Events...)
return uint64(resp.LastIndexOffset),
uint64(len(resp.Events)), nil
}
// Make paginated calls to the forwards API, starting at offset 0 and
// querying our max number of payments each time.
if err := paginater.QueryPaginated(
ctx, query, 0, maxForwards,
); err != nil {
return nil, fmt.Errorf("ListForwards failed: %w", err)
}
return forwards, nil
}
// ListChannels wraps the listchannels call to lnd, with a publicOnly bool
// that can be used to toggle whether private channels are included.
func ListChannels(ctx context.Context, lnd lndclient.LightningClient,
publicOnly bool) func() ([]lndclient.ChannelInfo, error) {
return func() ([]lndclient.ChannelInfo, error) {
resp, err := lnd.ListChannels(ctx, false, publicOnly)
if err != nil {
return nil, fmt.Errorf("ListChannels failed: %w", err)
}
return resp, nil
}
}