URI: 
       tlnchan: add available_to_spend() - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
   DIR commit ac68c8f53119855ea3293da07205084b0777e7ad
   DIR parent d317bdbd9b8f0ad6a8a17b9e841e4e0d4cfbcafa
  HTML Author: Janus <ysangkok@gmail.com>
       Date:   Wed, 17 Oct 2018 20:01:45 +0200
       
       lnchan: add available_to_spend()
       
       Diffstat:
         M electrum/gui/qt/channels_list.py    |      13 +++++++++++--
         M electrum/lnchan.py                  |       9 ++++++---
         M electrum/tests/test_lnchan.py       |       1 +
       
       3 files changed, 18 insertions(+), 5 deletions(-)
       ---
   DIR diff --git a/electrum/gui/qt/channels_list.py b/electrum/gui/qt/channels_list.py
       t@@ -23,10 +23,19 @@ class ChannelsList(MyTreeWidget):
                self.status = QLabel('')
        
            def format_fields(self, chan):
       +        labels = {}
       +        for subject in (REMOTE, LOCAL):
       +            available = chan.available_to_spend(subject)//1000
       +            label = self.parent.format_amount(available)
       +            bal_other = chan.balance(-subject)//1000
       +            available_other = chan.available_to_spend(-subject)//1000
       +            if bal_other != available_other:
       +                label += ' (+' + self.parent.format_amount(bal_other - available_other) + ')'
       +            labels[subject] = label
                return [
                    bh2u(chan.node_id),
       -            self.parent.format_amount(chan.balance(LOCAL)//1000),
       -            self.parent.format_amount(chan.balance(REMOTE)//1000),
       +            labels[LOCAL],
       +            labels[REMOTE],
                    chan.get_state()
                ]
        
   DIR diff --git a/electrum/lnchan.py b/electrum/lnchan.py
       t@@ -155,11 +155,9 @@ class Channel(PrintError):
                return self._state
        
            def check_can_pay(self, amount_msat):
       -        # FIXME what about channel_reserve_satoshis? will the remote fail the channel if we go below? test.
       -        # FIXME what about tx fees
                if self.get_state() != 'OPEN':
                    raise PaymentFailure('Channel not open')
       -        if self.balance(LOCAL) < amount_msat:
       +        if self.available_to_spend(LOCAL) < amount_msat:
                    raise PaymentFailure('Not enough local balance')
                if len(self.htlcs(LOCAL, only_pending=True)) + 1 > self.config[REMOTE].max_accepted_htlcs:
                    raise PaymentFailure('Too many HTLCs already in channel')
       t@@ -461,6 +459,11 @@ class Channel(PrintError):
                assert initial == self.config[subject].amount_msat
                return initial
        
       +    def available_to_spend(self, subject):
       +        # FIXME what about channel_reserve_satoshis? will the remote fail the channel if we go below? test.
       +        # FIXME what about tx fees
       +        return self.balance(subject) - htlcsum(self.htlcs(subject, only_pending=True))
       +
            def amounts(self):
                remote_settled= htlcsum(self.htlcs(REMOTE, False))
                local_settled= htlcsum(self.htlcs(LOCAL, False))
   DIR diff --git a/electrum/tests/test_lnchan.py b/electrum/tests/test_lnchan.py
       t@@ -339,6 +339,7 @@ class TestDust(unittest.TestCase):
                aliceHtlcIndex = alice_channel.add_htlc(htlc)
                bobHtlcIndex = bob_channel.receive_htlc(htlc)
                force_state_transition(alice_channel, bob_channel)
       +        self.assertEqual(alice_channel.available_to_spend(LOCAL), alice_channel.balance(LOCAL) - htlc['amount_msat'])
                self.assertEqual(len(alice_channel.local_commitment.outputs()), 3)
                self.assertEqual(len(bob_channel.local_commitment.outputs()), 2)
                default_fee = calc_static_fee(0)