Skip to content

Commit

Permalink
refactor: improved invoice payment flow. (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
ardevd authored Feb 8, 2024
1 parent 3c27242 commit 67ab69e
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions internal/tui/pay_invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ const (
// PaymentStateDecoded is when the invoice has been and parsed.
PaymentStateDecoded

// PaymentStateDecodeError is when the invoice is invalid
PaymentStateDecodeError

// PaymentStateSending is when the invoice payment is sending
PaymentStateSending

Expand Down Expand Up @@ -80,7 +83,7 @@ func (m *PayInvoiceModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch {
// Enter will pay the invoice is model is in appropriate state
case key.Matches(msg, Keymap.Enter):
if m.form.State == huh.StateCompleted && m.invoiceState == PaymentStateNone {
if m.invoiceState == PaymentStateDecoded {
m.invoiceState = PaymentStateSending
return m, paymentCreatedMsg
}
Expand Down Expand Up @@ -109,6 +112,16 @@ func (m *PayInvoiceModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
}

if m.form.State == huh.StateCompleted && m.invoiceState == PaymentStateNone {
// Form is ready, decode the invoice
_, err := m.lndService.Client.DecodePaymentRequest(m.ctx, invoiceString)
if err == nil {
m.invoiceState = PaymentStateDecoded
} else {
m.invoiceState = PaymentStateDecodeError
}
}

return m, tea.Batch(cmds...)
}

Expand Down Expand Up @@ -137,14 +150,19 @@ func (m PayInvoiceModel) View() string {
s := m.styles
v := strings.TrimSuffix(m.form.View(), "\n")
form := lipgloss.DefaultRenderer().NewStyle().Margin(1, 0).Render(v)
if m.invoiceState == PaymentStateSending {
switch m.invoiceState {
case PaymentStateSending:
return lipgloss.JoinVertical(lipgloss.Left, s.BorderedStyle.Render(m.getPaymentPendingView()))
} else if m.invoiceState == PaymentStateSettled {
case PaymentStateSettled:
return lipgloss.JoinVertical(lipgloss.Left, s.BorderedStyle.Render(m.getPaymentSettledView()))
} else if m.form.State == huh.StateCompleted {
case PaymentStateDecoded:
return lipgloss.JoinVertical(lipgloss.Left, s.BorderedStyle.Render(fmt.Sprintf("\n%s\n", s.HeaderText.Render("Pay Invoice?"))+
"\n"+m.decodeInvoice()))
"\n"+m.getDecodeInvoiceView()))
case PaymentStateDecodeError:
return lipgloss.JoinVertical(lipgloss.Left, s.BorderedStyle.Render(fmt.Sprintf("\n%s\n", s.HeaderText.Render("Invalid Invoice"))+
"\n"+m.getDecodeInvoiceView()))
}

return lipgloss.JoinVertical(lipgloss.Left, form)
}

Expand All @@ -163,7 +181,7 @@ func (m PayInvoiceModel) getPaymentSettledView() string {
"Press Esc to return"
}

// Get node name for a given public key
// Get node name for a given public key. Returns empty string if we can't find a match
func (m PayInvoiceModel) getNodeName(pubkey route.Vertex) string {
nodeInfo, err := m.lndService.Client.GetNodeInfo(m.ctx, pubkey, false)
if err != nil {
Expand All @@ -174,14 +192,13 @@ func (m PayInvoiceModel) getNodeName(pubkey route.Vertex) string {
}

// Decode an invoice string
func (m PayInvoiceModel) decodeInvoice() string {
func (m PayInvoiceModel) getDecodeInvoiceView() string {
// Decode the invoice string
invoiceString = lnd.SantizeBoltInvoice(invoiceString)
decodedInvoice, err := m.lndService.Client.DecodePaymentRequest(m.ctx, invoiceString)
if err != nil {
return "Error decoding invoice: " + err.Error()
}

amountInSats := decodedInvoice.Value.ToSatoshis()

s := m.styles
Expand Down

0 comments on commit 67ab69e

Please sign in to comment.