private Transaction BuildHtlcTransaction(Htlc htlc)
        {
            bool incoming      = htlc.Direction == Direction.Incoming;
            var  fee           = TransactionFee.CalculateFee(FeeratePerKw, incoming ? TransactionFee.HtlcSuccessWeight : TransactionFee.HtlcTimeoutWeight);
            var  witnessScript = CreateHtlcWitnessScript(htlc);
            var  outputScript  = OutputScripts.ToLocal(CommitmentTxParams.RevocationPublicKey.ToPubKey(), CommitmentTxParams.DelayedPaymentPublicKey.ToPubKey(), ChannelParameters.ToSelfDelay);
            var  outputIndex   = _htlcOutputIndexMap[htlc];
            var  amount        = htlc.AmountMsat.MSatToSatoshi() - fee;

            if (amount < ChannelParameters.DustLimitSatoshis)
            {
                throw new InvalidOperationException("HTLC amount below dust limit");
            }

            Transaction tx   = Transaction.Create(Network);
            TxIn        txIn = new TxIn(new OutPoint(_commitmentTransaction, outputIndex));

            txIn.Sequence  = 0;
            txIn.ScriptSig = witnessScript.WitHash.ScriptPubKey;
            tx.Inputs.Add(txIn);

            TxOut txOut = new TxOut(Money.Satoshis(amount), outputScript.WitHash.ScriptPubKey);

            tx.Outputs.Add(txOut);

            tx.Version  = 2;
            tx.LockTime = incoming ? 0 : htlc.Expiry;

            return(tx);
        }
        private List <Htlc> GetTrimmedOfferedHtlcs()
        {
            var htlcTimeoutFee = TransactionFee.CalculateFee(FeeratePerKw, TransactionFee.HtlcTimeoutWeight);

            return(Htlcs.Where(h => h.Direction == Direction.Outgoing &&
                               h.AmountMsat >= (ChannelParameters.DustLimitSatoshis + htlcTimeoutFee) * 1000).ToList());
        }
        private List <Htlc> GetTrimmedReceivedHtlcs()
        {
            var htlcSuccessFee = TransactionFee.CalculateFee(FeeratePerKw, TransactionFee.HtlcSuccessWeight);

            return(Htlcs.Where(h => h.Direction == Direction.Incoming &&
                               h.AmountMsat >= (ChannelParameters.DustLimitSatoshis + htlcSuccessFee) * 1000).ToList());
        }
        private ulong CalculateFee(List <Htlc> trimmedOfferedHtlcs, List <Htlc> trimmedReceivedHtlcs)
        {
            var weight = TransactionFee.CommitWeight + 172 * ((ulong)trimmedOfferedHtlcs.Count + (ulong)trimmedReceivedHtlcs.Count);

            return(TransactionFee.CalculateFee(FeeratePerKw, weight));
        }