Skip to content

Commit

Permalink
added more documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
ardevd committed Feb 7, 2024
1 parent 0ba695d commit 02bf5f1
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions internal/tui/pay_invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/lightningnetwork/lnd/routing/route"
)

// Model
type PayInvoiceModel struct {
styles *Styles
lndService *lndclient.GrpcLndServices
Expand Down Expand Up @@ -77,17 +78,20 @@ func (m *PayInvoiceModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

case tea.KeyMsg:
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 {
m.invoiceState = PaymentStateSending
return m, paymentCreatedMsg
}
}
// Payment has been decoded and issued.
case paymentCreated:
cmds = append(cmds, m.payInvoice)
// Payment failed
case paymentError:
m.invoiceState = PaymentStateNone
// Payment has been settled
case paymentSettled:
m.invoiceState = PaymentStateSettled
}
Expand All @@ -96,7 +100,7 @@ func (m *PayInvoiceModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.spinner, cmd = m.spinner.Update(msg)
cmds = append(cmds, cmd)

// Process the form
// Process the invoice form
if m.form != nil {
form, cmd := m.form.Update(msg)
if f, ok := form.(*huh.Form); ok {
Expand All @@ -108,6 +112,7 @@ func (m *PayInvoiceModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Batch(cmds...)
}

// Get the invoice payment form
func getInvoicePaymentForm() *huh.Form {
form := huh.NewForm(
huh.NewGroup(huh.NewNote().
Expand All @@ -122,10 +127,12 @@ func getInvoicePaymentForm() *huh.Form {
return form
}

// Init
func (m PayInvoiceModel) Init() tea.Cmd {
return m.spinner.Tick
}

// Model view logic
func (m PayInvoiceModel) View() string {
s := m.styles
v := strings.TrimSuffix(m.form.View(), "\n")
Expand All @@ -141,19 +148,22 @@ func (m PayInvoiceModel) View() string {
return lipgloss.JoinVertical(lipgloss.Left, form)
}

// Get the Payment pending view
func (m PayInvoiceModel) getPaymentPendingView() string {

return m.styles.HeaderText.Render("Invoice in flight") + "\n\n" +
fmt.Sprintf("\n\n %s Sending payment\n\n", m.spinner.View())
}

// Get the Payment settled view
func (m PayInvoiceModel) getPaymentSettledView() string {
s := m.styles
return s.HeaderText.Render("Invoice settled") + "\n\n" +
s.PositiveString("The invoice was successfully settled") + "\n" +
"Press Esc to return"
}

// Get node name for a given public key
func (m PayInvoiceModel) getNodeName(pubkey route.Vertex) string {
nodeInfo, err := m.lndService.Client.GetNodeInfo(m.ctx, pubkey, false)
if err != nil {
Expand All @@ -163,6 +173,7 @@ func (m PayInvoiceModel) getNodeName(pubkey route.Vertex) string {
return nodeInfo.Alias
}

// Decode an invoice string
func (m PayInvoiceModel) decodeInvoice() string {
// Decode the invoice string
invoiceString = lnd.SantizeBoltInvoice(invoiceString)
Expand All @@ -181,6 +192,7 @@ func (m PayInvoiceModel) decodeInvoice() string {
s.SubKeyword("Press Enter to accept, Esc to cancel")
}

// Pay the invoice
func (m *PayInvoiceModel) payInvoice() tea.Msg {
result := m.lndService.Client.PayInvoice(m.ctx, invoiceString, btcutil.Amount(10), nil)
defer close(result)
Expand Down

0 comments on commit 02bf5f1

Please sign in to comment.