Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 18 additions & 28 deletions invoices_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package lndclient

import (
"context"
"errors"
"fmt"
"io"
"sync"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down