| | | 1 | | namespace NLightning.Domain.Bitcoin.Transactions.Factories; |
| | | 2 | | |
| | | 3 | | using Bitcoin.Enums; |
| | | 4 | | using Channels.Models; |
| | | 5 | | using Constants; |
| | | 6 | | using Interfaces; |
| | | 7 | | using Models; |
| | | 8 | | using Money; |
| | | 9 | | using Wallet.Models; |
| | | 10 | | |
| | | 11 | | public class FundingTransactionModelFactory : IFundingTransactionModelFactory |
| | | 12 | | { |
| | | 13 | | public FundingTransactionModel Create(ChannelModel channel, List<UtxoModel> utxos, |
| | | 14 | | WalletAddressModel? changeAddress) |
| | | 15 | | { |
| | 0 | 16 | | if (utxos.Count == 0) |
| | 0 | 17 | | throw new ArgumentException("UTXO list cannot be empty", nameof(utxos)); |
| | | 18 | | |
| | 0 | 19 | | var fundingOutput = channel.FundingOutput ?? |
| | 0 | 20 | | throw new NullReferenceException($"{nameof(channel.FundingOutput)} cannot be null"); |
| | | 21 | | |
| | | 22 | | // Calculate the total input amount |
| | 0 | 23 | | 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 |
| | 0 | 27 | | var weight = WeightConstants.TransactionBaseWeight; |
| | | 28 | | |
| | | 29 | | // Add weight for each input (assuming P2WPKH for now, which is most common) |
| | 0 | 30 | | foreach (var utxo in utxos) |
| | | 31 | | { |
| | 0 | 32 | | if (utxo.AddressType == AddressType.P2Wpkh) |
| | | 33 | | { |
| | 0 | 34 | | weight += WeightConstants.P2WpkhInputWeight * 4 |
| | 0 | 35 | | + WeightConstants.SingleSigWitnessWeight; |
| | | 36 | | } |
| | 0 | 37 | | else if (utxo.AddressType == AddressType.P2Tr) |
| | | 38 | | { |
| | 0 | 39 | | weight += WeightConstants.P2TrInputWeight * 4 |
| | 0 | 40 | | + WeightConstants.TaprootSigWitnessWeight; |
| | | 41 | | } |
| | | 42 | | else |
| | | 43 | | { |
| | 0 | 44 | | throw new NotSupportedException($"Unsupported utxo type {utxo.AddressType}"); |
| | | 45 | | } |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | // Add weight for the funding output (P2WSH) |
| | 0 | 49 | | weight += WeightConstants.P2WshOutputWeight; |
| | | 50 | | |
| | | 51 | | // Calculate fee based on the channel's fee rate |
| | 0 | 52 | | var fee = LightningMoney.MilliSatoshis(weight * channel.ChannelConfig.FeeRateAmountPerKw.Satoshi); |
| | | 53 | | |
| | | 54 | | // Calculate what's left after funding output and fee |
| | 0 | 55 | | var fundingAmount = fundingOutput.Amount; |
| | 0 | 56 | | var remainingAmount = totalInputAmount - fundingAmount - fee; |
| | | 57 | | |
| | | 58 | | // Create the funding transaction model |
| | 0 | 59 | | var fundingTransactionModel = new FundingTransactionModel(utxos, fundingOutput, fee); |
| | | 60 | | |
| | | 61 | | // If there's a remaining amount, we need a change output |
| | 0 | 62 | | if (remainingAmount.Satoshi <= 0) |
| | 0 | 63 | | return fundingTransactionModel; |
| | | 64 | | |
| | | 65 | | // Add change output weight to recalculate fee |
| | 0 | 66 | | weight += WeightConstants.P2WpkhOutputWeight; |
| | 0 | 67 | | fee = LightningMoney.MilliSatoshis(weight * channel.ChannelConfig.FeeRateAmountPerKw.Satoshi); |
| | | 68 | | |
| | | 69 | | // Recalculate the remaining amount with updated fee |
| | 0 | 70 | | fundingTransactionModel.ChangeAmount = totalInputAmount - fundingAmount - fee; |
| | 0 | 71 | | fundingTransactionModel.ChangeAddress = changeAddress ?? |
| | 0 | 72 | | throw new ArgumentNullException( |
| | 0 | 73 | | nameof(changeAddress), |
| | 0 | 74 | | "We need a change address but none was provided."); |
| | | 75 | | |
| | 0 | 76 | | return fundingTransactionModel; |
| | | 77 | | } |
| | | 78 | | } |