lightning: fix kivy channel close

This commit is contained in:
Janus 2018-03-27 17:40:17 +02:00 committed by ThomasV
parent d19e8e7f9b
commit 1ab03e8b2a

View file

@ -1,17 +1,21 @@
import binascii
from kivy.lang import Builder from kivy.lang import Builder
from kivy.factory import Factory from kivy.factory import Factory
from kivy.clock import Clock from kivy.clock import Clock
import electrum.lightning as lightning import electrum.lightning as lightning
from electrum_gui.kivy.uix.context_menu import ContextMenu
Builder.load_string(''' Builder.load_string('''
<LightningChannelItem@CardItem> <LightningChannelItem@CardItem>
channelId: '<channelId not set>' active: False
channelPoint: '<channelPoint not set>'
Label: Label:
text: root.channelId text: root.channelPoint
<LightningChannelsDialog@Popup>: <LightningChannelsDialog@Popup>:
name: 'lightning_channels' name: 'lightning_channels'
BoxLayout: BoxLayout:
id: box
orientation: 'vertical' orientation: 'vertical'
spacing: '1dp' spacing: '1dp'
ScrollView: ScrollView:
@ -29,16 +33,36 @@ class LightningChannelsDialog(Factory.Popup):
super(LightningChannelsDialog, self).__init__() super(LightningChannelsDialog, self).__init__()
self.clocks = [] self.clocks = []
self.app = app self.app = app
self.context_menu = None
def close_channel(self, obj):
print("asked to close channel", obj.channelPoint)
lightning.lightningCall(self.app.wallet.network.lightningrpc, "closechannel")(obj.channelPoint + (" --force" if not obj.active else ""))
def show_menu(self, obj):
self.hide_menu()
self.context_menu = ContextMenu(obj, [("Close", self.close_channel)])
self.ids.box.add_widget(self.context_menu)
def hide_menu(self):
if self.context_menu is not None:
self.ids.box.remove_widget(self.context_menu)
self.context_menu = None
def open(self, *args, **kwargs): def open(self, *args, **kwargs):
super(LightningChannelsDialog, self).open(*args, **kwargs) super(LightningChannelsDialog, self).open(*args, **kwargs)
for i in self.clocks: i.cancel() for i in self.clocks: i.cancel()
self.clocks.append(Clock.schedule_interval(self.fetch_channels, 10)) self.clocks.append(Clock.schedule_interval(self.fetch_channels, 10))
self.app.wallet.network.lightningrpc.subscribe(self.rpc_result_handler) self.app.wallet.network.lightningrpc.subscribe(self.rpc_result_handler)
def dismiss(self, *args, **kwargs): def dismiss(self, *args, **kwargs):
self.hide_menu()
super(LightningChannelsDialog, self).dismiss(*args, **kwargs) super(LightningChannelsDialog, self).dismiss(*args, **kwargs)
self.app.wallet.network.lightningrpc.clearSubscribers() self.app.wallet.network.lightningrpc.clearSubscribers()
def fetch_channels(self, dw): def fetch_channels(self, dw):
lightning.lightningCall(self.app.wallet.network.lightningrpc, "listchannels")() lightning.lightningCall(self.app.wallet.network.lightningrpc, "listchannels")()
def rpc_result_handler(self, methodName, res): def rpc_result_handler(self, methodName, res):
print("got result", methodName) print("got result", methodName)
if isinstance(res, Exception): if isinstance(res, Exception):
@ -49,5 +73,6 @@ class LightningChannelsDialog(Factory.Popup):
item = Factory.LightningChannelItem() item = Factory.LightningChannelItem()
item.screen = self item.screen = self
print(i) print(i)
item.channelId = i["chan_id"] item.channelPoint = binascii.hexlify(bytes(reversed(bytes(bytearray.fromhex(i["channel_point"].split(":")[0]))))).decode("ascii")
item.active = i["active"]
channel_cards.add_widget(item) channel_cards.add_widget(item)