< Summary - Combined Code Coverage

Information
Class: NLightning.Domain.Bitcoin.Transactions.Factories.FundingTransactionModelFactory
Assembly: NLightning.Domain
File(s): /home/runner/work/NLightning/NLightning/src/NLightning.Domain/Bitcoin/Transactions/Factories/FundingTransactionModelFactory.cs
Tag: 57_24045730253
Line coverage
0%
Covered lines: 0
Uncovered lines: 29
Coverable lines: 29
Total lines: 78
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 14
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Create(...)0%210140%

File(s)

/home/runner/work/NLightning/NLightning/src/NLightning.Domain/Bitcoin/Transactions/Factories/FundingTransactionModelFactory.cs

#LineLine coverage
 1namespace NLightning.Domain.Bitcoin.Transactions.Factories;
 2
 3using Bitcoin.Enums;
 4using Channels.Models;
 5using Constants;
 6using Interfaces;
 7using Models;
 8using Money;
 9using Wallet.Models;
 10
 11public class FundingTransactionModelFactory : IFundingTransactionModelFactory
 12{
 13    public FundingTransactionModel Create(ChannelModel channel, List<UtxoModel> utxos,
 14                                          WalletAddressModel? changeAddress)
 15    {
 016        if (utxos.Count == 0)
 017            throw new ArgumentException("UTXO list cannot be empty", nameof(utxos));
 18
 019        var fundingOutput = channel.FundingOutput ??
 020                            throw new NullReferenceException($"{nameof(channel.FundingOutput)} cannot be null");
 21
 22        // Calculate the total input amount
 023        var totalInputAmount = LightningMoney.Satoshis(utxos.Sum(u => u.Amount.Satoshi));
 24
 25        // Calculate the weight based on the input types
 26        // Starting with base transaction weight
 027        var weight = WeightConstants.TransactionBaseWeight;
 28
 29        // Add weight for each input (assuming P2WPKH for now, which is most common)
 030        foreach (var utxo in utxos)
 31        {
 032            if (utxo.AddressType == AddressType.P2Wpkh)
 33            {
 034                weight += WeightConstants.P2WpkhInputWeight * 4
 035                        + WeightConstants.SingleSigWitnessWeight;
 36            }
 037            else if (utxo.AddressType == AddressType.P2Tr)
 38            {
 039                weight += WeightConstants.P2TrInputWeight * 4
 040                        + WeightConstants.TaprootSigWitnessWeight;
 41            }
 42            else
 43            {
 044                throw new NotSupportedException($"Unsupported utxo type {utxo.AddressType}");
 45            }
 46        }
 47
 48        // Add weight for the funding output (P2WSH)
 049        weight += WeightConstants.P2WshOutputWeight;
 50
 51        // Calculate fee based on the channel's fee rate
 052        var fee = LightningMoney.MilliSatoshis(weight * channel.ChannelConfig.FeeRateAmountPerKw.Satoshi);
 53
 54        // Calculate what's left after funding output and fee
 055        var fundingAmount = fundingOutput.Amount;
 056        var remainingAmount = totalInputAmount - fundingAmount - fee;
 57
 58        // Create the funding transaction model
 059        var fundingTransactionModel = new FundingTransactionModel(utxos, fundingOutput, fee);
 60
 61        // If there's a remaining amount, we need a change output
 062        if (remainingAmount.Satoshi <= 0)
 063            return fundingTransactionModel;
 64
 65        // Add change output weight to recalculate fee
 066        weight += WeightConstants.P2WpkhOutputWeight;
 067        fee = LightningMoney.MilliSatoshis(weight * channel.ChannelConfig.FeeRateAmountPerKw.Satoshi);
 68
 69        // Recalculate the remaining amount with updated fee
 070        fundingTransactionModel.ChangeAmount = totalInputAmount - fundingAmount - fee;
 071        fundingTransactionModel.ChangeAddress = changeAddress ??
 072                                                throw new ArgumentNullException(
 073                                                    nameof(changeAddress),
 074                                                    "We need a change address but none was provided.");
 75
 076        return fundingTransactionModel;
 77    }
 78}