Skip to content

Commit

Permalink
fixed channel management
Browse files Browse the repository at this point in the history
  • Loading branch information
ardevd committed Jan 10, 2024
1 parent db885eb commit e712262
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions internal/tui/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type ChannelModel struct {
help help.Model
keys keyMap
form *huh.Form
updateChan chan lndclient.CloseChannelUpdate
errorsChan chan error
}

// ChannelState indicates the state of the selected Channel model
Expand Down Expand Up @@ -102,12 +104,11 @@ func (m *ChannelModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.help.ShowAll = !m.help.ShowAll
case key.Matches(msg, Keymap.ForceClose):
// Force close channel
//m.closeChannel(true)
m.form = m.getChannelOperationForm("Force Close Channel", "Are you sure??")
m.form = m.getChannelOperationForm("Force Close Channel", "The latest commitment transaction will be broadcasted on-chain. Are you sure?")
m.state = ChannelStateWantForceClose
case key.Matches(msg, Keymap.Close):
// Close channel
//m.closeChannel(false)
m.form = m.getChannelOperationForm("Close Channel", "A cooperative close will be issued. Are you sure?")
m.state = ChannelStateWantClose
}

Expand Down Expand Up @@ -271,7 +272,6 @@ func (m ChannelModel) getChannelBalanceView() string {
}

func (m ChannelModel) closeChannel(force bool) {

//address, err := m.lndService.WalletKit.NextAddr(m.ctx, "default", walletrpc.AddressType_TAPROOT_PUBKEY, true)

outPoint, err := lndclient.NewOutpointFromStr(m.channel.Info.ChannelPoint)
Expand All @@ -284,26 +284,33 @@ func (m ChannelModel) closeChannel(force bool) {
// A force close can't include custom fee
targetBlocks = 0
}
closeUpdates, closeErrors, err := m.lndService.Client.CloseChannel(m.ctx, outPoint, force, targetBlocks, nil)

m.updateChan, m.errorsChan, err = m.lndService.Client.CloseChannel(m.ctx, outPoint, force, targetBlocks, nil)

if err != nil {
fmt.Println("Unable to close channel", err)
}

// Start goroutine to listen for close updates
go func() {
defer close(closeUpdates)
defer close(closeErrors)

for {
select {
case update := <-closeUpdates:
case update, ok := <-m.updateChan:
if !ok {
// Channel closed
fmt.Println("Channel closed!")
return
}
// Handle close updates received from the channel
fmt.Println("Received close update:", update)
case errorUpdate := <-closeErrors:
fmt.Println("Closing transaction: ", update.CloseTxid().String())
case errorUpdate, ok := <-m.errorsChan:
if !ok {
fmt.Println("Error channel closed")
return
}
// The closing process is complete
fmt.Println("Recieved close error:", errorUpdate)
return
fmt.Println("Error:", errorUpdate)
}
}
}()
Expand Down Expand Up @@ -356,6 +363,10 @@ func (m ChannelModel) View() string {
v := strings.TrimSuffix(m.form.View(), "\n\n")
form := lipgloss.DefaultRenderer().NewStyle().Margin(1, 0).Render(v)
return lipgloss.JoinVertical(lipgloss.Left, form)
} else if m.state == ChannelStateWantClose {
v := strings.TrimSuffix(m.form.View(), "\n\n")
form := lipgloss.DefaultRenderer().NewStyle().Margin(1, 0).Render(v)
return lipgloss.JoinVertical(lipgloss.Left, form)
}

return ""
Expand Down

0 comments on commit e712262

Please sign in to comment.