From fcedbfab8473dbf33b1dc0376c2e0678a991eb5f Mon Sep 17 00:00:00 2001 From: Boris Nagaev Date: Fri, 10 Oct 2025 13:16:55 -0300 Subject: [PATCH] SubscribeSingleInvoice: provide full invoice --- invoices_client.go | 46 ++++++++++++++++++---------------------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/invoices_client.go b/invoices_client.go index 452e1ef..5946419 100644 --- a/invoices_client.go +++ b/invoices_client.go @@ -2,7 +2,6 @@ package lndclient import ( "context" - "errors" "fmt" "io" "sync" @@ -89,9 +88,18 @@ type InvoicesClient interface { handler InvoiceHtlcModifyHandler) error } -// InvoiceUpdate contains a state update for an invoice. +// InvoiceUpdate embeds an Invoice to expose the complete invoice view along +// with the legacy satoshis-paid field used by existing callers. type InvoiceUpdate struct { - State invpkg.ContractState + // Invoice holds the current state of the invoice. + Invoice + + // AmtPaid is the amount that was accepted for this invoice, in sats. + // This will ONLY be set if this invoice has been settled or accepted. + // We provide this field as if the invoice was created with a zero + // value, then we need to record what amount was ultimately accepted. + // Additionally, it's possible that the sender paid MORE that was + // specified in the original invoice. So we'll record that here as well. AmtPaid btcutil.Amount } @@ -203,17 +211,19 @@ func (s *invoicesClient) SubscribeSingleInvoice(ctx context.Context, return } - state, err := fromRPCInvoiceState(invoice.State) + clientInvoice, err := unmarshalInvoice(invoice) if err != nil { errChan <- err return } - select { - case updateChan <- InvoiceUpdate{ - State: state, + invoiceUpdate := InvoiceUpdate{ + Invoice: *clientInvoice, AmtPaid: btcutil.Amount(invoice.AmtPaidSat), - }: + } + + select { + case updateChan <- invoiceUpdate: case <-ctx.Done(): return } @@ -254,26 +264,6 @@ func (s *invoicesClient) AddHoldInvoice(ctx context.Context, return resp.PaymentRequest, nil } -func fromRPCInvoiceState(state lnrpc.Invoice_InvoiceState) ( - invpkg.ContractState, error) { - - switch state { - case lnrpc.Invoice_OPEN: - return invpkg.ContractOpen, nil - - case lnrpc.Invoice_ACCEPTED: - return invpkg.ContractAccepted, nil - - case lnrpc.Invoice_SETTLED: - return invpkg.ContractSettled, nil - - case lnrpc.Invoice_CANCELED: - return invpkg.ContractCanceled, nil - } - - return 0, errors.New("unknown state") -} - // HtlcModifier is a bidirectional streaming RPC that allows a client to // intercept and modify the HTLCs that attempt to settle the given invoice. The // server will send HTLCs of invoices to the client and the client can modify