From f5abd4f1d1457c1aa5560dc366261348dbffd359 Mon Sep 17 00:00:00 2001 From: ThomasV Date: Wed, 13 Jul 2022 10:14:34 +0200 Subject: [PATCH] add command line for rebalancing channels --- electrum/commands.py | 18 ++++++++++++++++++ electrum/lnworker.py | 11 ++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/electrum/commands.py b/electrum/commands.py index 329fbebae..5a9ac86a7 100644 --- a/electrum/commands.py +++ b/electrum/commands.py @@ -1232,6 +1232,24 @@ class Commands: """ return the local watchtower's ctn of channel. used in regtests """ return await self.network.local_watchtower.sweepstore.get_ctn(channel_point, None) + @command('wnl') + async def rebalance_channels(self, from_scid, dest_scid, amount, wallet: Abstract_Wallet = None): + """ + Rebalance channels. + If trampoline is used, channels must be with diferent trampolines. + """ + from .lnutil import ShortChannelID + from_scid = ShortChannelID.from_str(from_scid) + dest_scid = ShortChannelID.from_str(dest_scid) + from_channel = wallet.lnworker.get_channel_by_scid(from_scid) + dest_channel = wallet.lnworker.get_channel_by_scid(dest_scid) + amount_sat = satoshis(amount) + success, log = await wallet.lnworker.rebalance_channels(from_channel, dest_channel, amount_sat * 1000) + return { + 'success': success, + 'log': [x.formatted_tuple() for x in log] + } + @command('wnpl') async def normal_swap(self, onchain_amount, lightning_amount, password=None, wallet: Abstract_Wallet = None): """ diff --git a/electrum/lnworker.py b/electrum/lnworker.py index bfa9d8f3e..56c537fe5 100644 --- a/electrum/lnworker.py +++ b/electrum/lnworker.py @@ -693,6 +693,11 @@ class LNWallet(LNWorker): def get_channel_by_id(self, channel_id: bytes) -> Optional[Channel]: return self._channels.get(channel_id, None) + def get_channel_by_scid(self, scid: bytes) -> Optional[Channel]: + for chan in self._channels.values(): + if chan.short_channel_id == scid: + return chan + def diagnostic_name(self): return self.wallet.diagnostic_name() @@ -2299,6 +2304,10 @@ class LNWallet(LNWorker): return (chan, swap_recv_amount) async def rebalance_channels(self, chan1, chan2, amount_msat): + if chan1 == chan2: + raise Exception('Rebalance requires two different channels') + if not self.channel_db and chan1.node_id == chan2.node_id: + raise Exception('Rebalance requires channels from different trampolines') lnaddr, invoice = self.create_invoice( amount_msat=amount_msat, message='rebalance', @@ -2306,7 +2315,7 @@ class LNWallet(LNWorker): fallback_address=None, channels = [chan2] ) - await self.pay_invoice( + return await self.pay_invoice( invoice, channels=[chan1]) def num_sats_can_receive_no_mpp(self) -> Decimal: