Skip to content

Commit 438482b

Browse files
authored
Merge pull request #248 from starius/invoice-state
SubscribeSingleInvoice: provide full invoice
2 parents 5df32d6 + fcedbfa commit 438482b

File tree

1 file changed

+18
-28
lines changed

1 file changed

+18
-28
lines changed

invoices_client.go

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package lndclient
22

33
import (
44
"context"
5-
"errors"
65
"fmt"
76
"io"
87
"sync"
@@ -89,9 +88,18 @@ type InvoicesClient interface {
8988
handler InvoiceHtlcModifyHandler) error
9089
}
9190

92-
// InvoiceUpdate contains a state update for an invoice.
91+
// InvoiceUpdate embeds an Invoice to expose the complete invoice view along
92+
// with the legacy satoshis-paid field used by existing callers.
9393
type InvoiceUpdate struct {
94-
State invpkg.ContractState
94+
// Invoice holds the current state of the invoice.
95+
Invoice
96+
97+
// AmtPaid is the amount that was accepted for this invoice, in sats.
98+
// This will ONLY be set if this invoice has been settled or accepted.
99+
// We provide this field as if the invoice was created with a zero
100+
// value, then we need to record what amount was ultimately accepted.
101+
// Additionally, it's possible that the sender paid MORE that was
102+
// specified in the original invoice. So we'll record that here as well.
95103
AmtPaid btcutil.Amount
96104
}
97105

@@ -203,17 +211,19 @@ func (s *invoicesClient) SubscribeSingleInvoice(ctx context.Context,
203211
return
204212
}
205213

206-
state, err := fromRPCInvoiceState(invoice.State)
214+
clientInvoice, err := unmarshalInvoice(invoice)
207215
if err != nil {
208216
errChan <- err
209217
return
210218
}
211219

212-
select {
213-
case updateChan <- InvoiceUpdate{
214-
State: state,
220+
invoiceUpdate := InvoiceUpdate{
221+
Invoice: *clientInvoice,
215222
AmtPaid: btcutil.Amount(invoice.AmtPaidSat),
216-
}:
223+
}
224+
225+
select {
226+
case updateChan <- invoiceUpdate:
217227
case <-ctx.Done():
218228
return
219229
}
@@ -254,26 +264,6 @@ func (s *invoicesClient) AddHoldInvoice(ctx context.Context,
254264
return resp.PaymentRequest, nil
255265
}
256266

257-
func fromRPCInvoiceState(state lnrpc.Invoice_InvoiceState) (
258-
invpkg.ContractState, error) {
259-
260-
switch state {
261-
case lnrpc.Invoice_OPEN:
262-
return invpkg.ContractOpen, nil
263-
264-
case lnrpc.Invoice_ACCEPTED:
265-
return invpkg.ContractAccepted, nil
266-
267-
case lnrpc.Invoice_SETTLED:
268-
return invpkg.ContractSettled, nil
269-
270-
case lnrpc.Invoice_CANCELED:
271-
return invpkg.ContractCanceled, nil
272-
}
273-
274-
return 0, errors.New("unknown state")
275-
}
276-
277267
// HtlcModifier is a bidirectional streaming RPC that allows a client to
278268
// intercept and modify the HTLCs that attempt to settle the given invoice. The
279269
// server will send HTLCs of invoices to the client and the client can modify

0 commit comments

Comments
 (0)