Skip to content

Commit 475c7f2

Browse files
committed
lndclient: add listpayments endpoint
1 parent 5ca0186 commit 475c7f2

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

lndclient/lightning_client.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ type LightningClient interface {
6262
ListInvoices(ctx context.Context, req ListInvoicesRequest) (
6363
*ListInvoicesResponse, error)
6464

65+
// ListPayments makes a paginated call to our list payments endpoint.
66+
ListPayments(ctx context.Context,
67+
req ListPaymentsRequest) (*ListPaymentsResponse, error)
68+
6569
// ChannelBackup retrieves the backup for a particular channel. The
6670
// backup is returned as an encrypted chanbackup.Single payload.
6771
ChannelBackup(context.Context, wire.OutPoint) ([]byte, error)
@@ -1022,6 +1026,121 @@ func (s *lightningClient) ListInvoices(ctx context.Context,
10221026
}, nil
10231027
}
10241028

1029+
// Payment represents a payment made by our node.
1030+
type Payment struct {
1031+
// Hash is the payment hash used.
1032+
Hash lntypes.Hash
1033+
1034+
// Preimage is the preimage of the payment. It will have a non-nil value
1035+
// if the payment is settled.
1036+
Preimage *lntypes.Preimage
1037+
1038+
// Amount is the amount in millisatoshis of the payment.
1039+
Amount lnwire.MilliSatoshi
1040+
1041+
// Fee is the amount in millisatoshis that was paid in fees.
1042+
Fee lnwire.MilliSatoshi
1043+
1044+
// Status describes the state of a payment.
1045+
Status *PaymentStatus
1046+
1047+
// Htlcs is the set of htlc attempts made by the payment.
1048+
Htlcs []*lnrpc.HTLCAttempt
1049+
1050+
// SequenceNumber is a unique id for each payment.
1051+
SequenceNumber uint64
1052+
}
1053+
1054+
// ListPaymentsRequest contains the request parameters for a paginated
1055+
// list payments call.
1056+
type ListPaymentsRequest struct {
1057+
// MaxPayments is the maximum number of payments to return.
1058+
MaxPayments uint64
1059+
1060+
// Offset is the index from which to start querying.
1061+
Offset uint64
1062+
1063+
// Reversed is set to query our payments backwards.
1064+
Reversed bool
1065+
1066+
// IncludeIncomplete is set if we want to include incomplete payments.
1067+
IncludeIncomplete bool
1068+
}
1069+
1070+
// ListPaymentsResponse contains the response to a list payments query,
1071+
// including the index offsets required for paginated queries.
1072+
type ListPaymentsResponse struct {
1073+
// FirstIndexOffset is the index offset of the first item in our set.
1074+
FirstIndexOffset uint64
1075+
1076+
// LastIndexOffset is the index offset of the last item in our set.
1077+
LastIndexOffset uint64
1078+
1079+
// Payments is the set of invoices that were returned.
1080+
Payments []Payment
1081+
}
1082+
1083+
// ListPayments makes a paginated call to our listpayments endpoint.
1084+
func (s *lightningClient) ListPayments(ctx context.Context,
1085+
req ListPaymentsRequest) (*ListPaymentsResponse, error) {
1086+
1087+
rpcCtx, cancel := context.WithTimeout(ctx, rpcTimeout)
1088+
defer cancel()
1089+
1090+
resp, err := s.client.ListPayments(
1091+
s.adminMac.WithMacaroonAuth(rpcCtx),
1092+
&lnrpc.ListPaymentsRequest{
1093+
IncludeIncomplete: req.IncludeIncomplete,
1094+
IndexOffset: req.Offset,
1095+
MaxPayments: req.MaxPayments,
1096+
Reversed: req.Reversed,
1097+
})
1098+
if err != nil {
1099+
return nil, err
1100+
}
1101+
1102+
payments := make([]Payment, len(resp.Payments))
1103+
for i, payment := range resp.Payments {
1104+
hash, err := lntypes.MakeHashFromStr(payment.PaymentHash)
1105+
if err != nil {
1106+
return nil, err
1107+
}
1108+
1109+
status, err := unmarshallPaymentStatus(payment)
1110+
if err != nil {
1111+
return nil, err
1112+
}
1113+
1114+
pmt := Payment{
1115+
Hash: hash,
1116+
Status: status,
1117+
Htlcs: payment.Htlcs,
1118+
Amount: lnwire.MilliSatoshi(payment.ValueMsat),
1119+
Fee: lnwire.MilliSatoshi(payment.FeeMsat),
1120+
SequenceNumber: payment.PaymentIndex,
1121+
}
1122+
1123+
// Add our preimage if it is known.
1124+
if payment.PaymentPreimage != "" {
1125+
preimage, err := lntypes.MakePreimageFromStr(
1126+
payment.PaymentPreimage,
1127+
)
1128+
if err != nil {
1129+
return nil, err
1130+
}
1131+
pmt.Preimage = &preimage
1132+
}
1133+
1134+
payments[i] = pmt
1135+
}
1136+
1137+
return &ListPaymentsResponse{
1138+
FirstIndexOffset: resp.FirstIndexOffset,
1139+
LastIndexOffset: resp.LastIndexOffset,
1140+
Payments: payments,
1141+
}, nil
1142+
}
1143+
10251144
// ChannelBackup retrieves the backup for a particular channel. The backup is
10261145
// returned as an encrypted chanbackup.Single payload.
10271146
func (s *lightningClient) ChannelBackup(ctx context.Context,

test/lightning_client_mock.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,16 @@ func (h *mockLightningClient) ListInvoices(_ context.Context,
208208
}, nil
209209
}
210210

211+
// ListPayments makes a paginated call to our list payments endpoint.
212+
func (h *mockLightningClient) ListPayments(_ context.Context,
213+
_ lndclient.ListPaymentsRequest) (*lndclient.ListPaymentsResponse,
214+
error) {
215+
216+
return &lndclient.ListPaymentsResponse{
217+
Payments: h.lnd.Payments,
218+
}, nil
219+
}
220+
211221
// ChannelBackup retrieves the backup for a particular channel. The
212222
// backup is returned as an encrypted chanbackup.Single payload.
213223
func (h *mockLightningClient) ChannelBackup(context.Context, wire.OutPoint) ([]byte, error) {

test/lnd_services_mock.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ type LndMockServices struct {
166166
Channels []lndclient.ChannelInfo
167167
ClosedChannels []lndclient.ClosedChannel
168168
ForwardingEvents []lndclient.ForwardingEvent
169+
Payments []lndclient.Payment
169170

170171
WaitForFinished func()
171172

0 commit comments

Comments
 (0)