def available_to_spend(self, subject: HTLCOwner) -> int:
        """The usable balance of 'subject' in msat, after taking reserve and fees (and anchors) into
        consideration. Note that fees (and hence the result) fluctuate even without user interaction.
        """
        assert type(subject) is HTLCOwner
        sender = subject
        receiver = subject.inverted()
        initiator = LOCAL if self.constraints.is_initiator else REMOTE  # the initiator/funder pays on-chain fees

        def consider_ctx(*, ctx_owner: HTLCOwner, is_htlc_dust: bool) -> int:
            ctn = self.get_next_ctn(ctx_owner)
            sender_balance_msat = self.balance_minus_outgoing_htlcs(whose=sender, ctx_owner=ctx_owner, ctn=ctn)
            receiver_balance_msat = self.balance_minus_outgoing_htlcs(whose=receiver, ctx_owner=ctx_owner, ctn=ctn)
            sender_reserve_msat = self.config[receiver].reserve_sat * 1000
            receiver_reserve_msat = self.config[sender].reserve_sat * 1000
            num_htlcs_in_ctx = len(self.included_htlcs(ctx_owner, SENT, ctn=ctn) + self.included_htlcs(ctx_owner, RECEIVED, ctn=ctn))
            feerate = self.get_feerate(ctx_owner, ctn=ctn)
            ctx_fees_msat = calc_fees_for_commitment_tx(
                num_htlcs=num_htlcs_in_ctx,
                feerate=feerate,
                is_local_initiator=self.constraints.is_initiator,
                round_to_sat=False,
                has_anchors=self.has_anchors()
            )
            htlc_fee_msat = fee_for_htlc_output(feerate=feerate)
            htlc_trim_func = received_htlc_trim_threshold_sat if ctx_owner == receiver else offered_htlc_trim_threshold_sat
            htlc_trim_threshold_msat = htlc_trim_func(dust_limit_sat=self.config[ctx_owner].dust_limit_sat, feerate=feerate, has_anchors=self.has_anchors()) * 1000

            # the sender cannot spend below its reserve
            max_send_msat = sender_balance_msat - sender_reserve_msat

            # reserve a fee spike buffer
            # see https://github.com/lightningnetwork/lightning-rfc/pull/740
            if sender == initiator == LOCAL:
                fee_spike_buffer = calc_fees_for_commitment_tx(
                    num_htlcs=num_htlcs_in_ctx + int(not is_htlc_dust) + 1,
                    feerate=2 * feerate,
                    is_local_initiator=self.constraints.is_initiator,
                    round_to_sat=False,
                    has_anchors=self.has_anchors())[sender]
                max_send_msat -= fee_spike_buffer
            # we can't enforce the fee spike buffer on the remote party
            elif sender == initiator == REMOTE:
                max_send_msat -= ctx_fees_msat[sender]

            # initiator pays for anchor outputs
            if sender == initiator and self.has_anchors():
                max_send_msat -= 2 * FIXED_ANCHOR_SAT * 1000

            # handle the transaction fees for the HTLC transaction
            if is_htlc_dust:
                # nobody pays additional HTLC transaction fees
                return min(max_send_msat, htlc_trim_threshold_msat - 1)
            else:
                # somebody has to pay for the additional HTLC transaction fees
                if sender == initiator:
                    return max_send_msat - htlc_fee_msat
                else:
                    # check if the receiver can afford to pay for the HTLC transaction fees
                    new_receiver_balance = receiver_balance_msat - receiver_reserve_msat - ctx_fees_msat[receiver] - htlc_fee_msat
                    if self.has_anchors():
                        new_receiver_balance -= 2 * FIXED_ANCHOR_SAT * 1000
                    if new_receiver_balance < 0:
                        return 0
                    return max_send_msat

        max_send_msat = min(
            max(
                consider_ctx(ctx_owner=receiver, is_htlc_dust=True),
                consider_ctx(ctx_owner=receiver, is_htlc_dust=False),
            ),
            max(
                consider_ctx(ctx_owner=sender, is_htlc_dust=True),
                consider_ctx(ctx_owner=sender, is_htlc_dust=False),
            ),
        )

        max_send_msat = min(max_send_msat, self.remaining_max_inflight(receiver, strict=True))
        if self.htlc_slots_left(sender) == 0:
            max_send_msat = 0

        max_send_msat = max(max_send_msat, 0)
        return max_send_msat
The usable balance of 'subject' in msat, after taking reserve and fees (and anchors) into
        consideration. Note that fees (and hence the result) fluctuate even without user interaction.
        
electrum/lnchannel.py