| | 1 | | using NBitcoin; |
| | 2 | |
|
| | 3 | | namespace NLightning.Infrastructure.Bitcoin.Transactions; |
| | 4 | |
|
| | 5 | | public class ClosingTransaction : Transaction |
| | 6 | | { |
| 0 | 7 | | public ulong CloserOutputAmountSatoshis { get; } |
| 0 | 8 | | public ulong CloseeOutputAmountSatoshis { get; } |
| | 9 | |
|
| 0 | 10 | | public ClosingTransaction(OutPoint outPoint, ulong closerAmountSatoshis, ulong closeeAmountSatoshis, Script closerSc |
| | 11 | | { |
| 0 | 12 | | Version = 2; |
| 0 | 13 | | LockTime = 0; // TODO: Find out correct lockTime |
| | 14 | |
|
| 0 | 15 | | Inputs.Add(new TxIn |
| 0 | 16 | | { |
| 0 | 17 | | PrevOut = outPoint, |
| 0 | 18 | | Sequence = 0xFFFFFFFD |
| 0 | 19 | | }); |
| | 20 | |
|
| 0 | 21 | | CloserOutputAmountSatoshis = CalculateOutputAmount(closerAmountSatoshis, closerScriptPubKey, feeSatoshis); |
| 0 | 22 | | Outputs.Add(new TxOut((Money)CloserOutputAmountSatoshis, closerScriptPubKey)); |
| | 23 | |
|
| 0 | 24 | | if (closeeAmountSatoshis == 0) |
| | 25 | | { |
| 0 | 26 | | return; |
| | 27 | | } |
| | 28 | |
|
| 0 | 29 | | CloseeOutputAmountSatoshis = CalculateOutputAmount(closeeAmountSatoshis, closeeScriptPubKey, 0); |
| 0 | 30 | | if (CloseeOutputAmountSatoshis > 0) |
| | 31 | | { |
| 0 | 32 | | Outputs.Add(new TxOut((Money)CloseeOutputAmountSatoshis, closeeScriptPubKey)); |
| | 33 | | } |
| 0 | 34 | | } |
| | 35 | |
|
| | 36 | | private static ulong CalculateOutputAmount(ulong amountSatoshis, Script scriptPubKey, ulong feeSatoshis) |
| | 37 | | { |
| 0 | 38 | | if (scriptPubKey.ToBytes()[0] == (byte)OpcodeType.OP_RETURN) |
| | 39 | | { |
| 0 | 40 | | return 0; |
| | 41 | | } |
| | 42 | |
|
| 0 | 43 | | var finalAmount = amountSatoshis - feeSatoshis; |
| 0 | 44 | | return finalAmount > 0 ? finalAmount : 0; |
| | 45 | | } |
| | 46 | | } |