@@ -21,8 +21,10 @@ use crate::sign::{P2TR_KEY_PATH_WITNESS_WEIGHT, P2WPKH_WITNESS_WEIGHT};
2121/// The components of a splice's funding transaction that are contributed by one party.
2222#[ derive( Debug , Clone ) ]
2323pub struct SpliceContribution {
24- /// The amount to contribute to the splice.
25- value : SignedAmount ,
24+ /// The amount from [`inputs`] to contribute to the splice.
25+ ///
26+ /// [`inputs`]: Self::inputs
27+ value_added : Amount ,
2628
2729 /// The inputs included in the splice's funding transaction to meet the contributed amount
2830 /// plus fees. Any excess amount will be sent to a change output.
@@ -42,23 +44,14 @@ pub struct SpliceContribution {
4244impl SpliceContribution {
4345 /// Creates a contribution for when funds are only added to a channel.
4446 pub fn splice_in (
45- value : Amount , inputs : Vec < FundingTxInput > , change_script : Option < ScriptBuf > ,
47+ value_added : Amount , inputs : Vec < FundingTxInput > , change_script : Option < ScriptBuf > ,
4648 ) -> Self {
47- let value_added = value. to_signed ( ) . unwrap_or ( SignedAmount :: MAX ) ;
48-
49- Self { value : value_added, inputs, outputs : vec ! [ ] , change_script }
49+ Self { value_added, inputs, outputs : vec ! [ ] , change_script }
5050 }
5151
5252 /// Creates a contribution for when funds are only removed from a channel.
5353 pub fn splice_out ( outputs : Vec < TxOut > ) -> Self {
54- let value_removed = outputs
55- . iter ( )
56- . map ( |txout| txout. value )
57- . sum :: < Amount > ( )
58- . to_signed ( )
59- . unwrap_or ( SignedAmount :: MAX ) ;
60-
61- Self { value : -value_removed, inputs : vec ! [ ] , outputs, change_script : None }
54+ Self { value_added : Amount :: ZERO , inputs : vec ! [ ] , outputs, change_script : None }
6255 }
6356
6457 /// Creates a contribution for when funds are both added to and removed from a channel.
@@ -70,29 +63,26 @@ impl SpliceContribution {
7063 value_added : Amount , inputs : Vec < FundingTxInput > , outputs : Vec < TxOut > ,
7164 change_script : Option < ScriptBuf > ,
7265 ) -> Self {
73- let splice_in = Self :: splice_in ( value_added, inputs, change_script) ;
74- let splice_out = Self :: splice_out ( outputs) ;
75-
76- Self {
77- value : splice_in. value + splice_out. value ,
78- inputs : splice_in. inputs ,
79- outputs : splice_out. outputs ,
80- change_script : splice_in. change_script ,
81- }
66+ Self { value_added, inputs, outputs, change_script }
8267 }
8368
8469 /// The net value contributed to a channel by the splice. If negative, more value will be
8570 /// spliced out than spliced in.
8671 pub fn net_value ( & self ) -> SignedAmount {
87- self . value
88- }
72+ let value_added = self . value_added . to_signed ( ) . unwrap_or ( SignedAmount :: MAX ) ;
73+ let value_removed = self
74+ . outputs
75+ . iter ( )
76+ . map ( |txout| txout. value )
77+ . sum :: < Amount > ( )
78+ . to_signed ( )
79+ . unwrap_or ( SignedAmount :: MAX ) ;
8980
90- pub ( super ) fn input_value ( & self ) -> Amount {
91- ( self . net_value ( ) + self . output_value ( ) . to_signed ( ) . expect ( "" ) ) . to_unsigned ( ) . expect ( "" )
81+ value_added - value_removed
9282 }
9383
94- pub ( super ) fn output_value ( & self ) -> Amount {
95- self . outputs . iter ( ) . map ( |txout| txout . value ) . sum :: < Amount > ( )
84+ pub ( super ) fn value_added ( & self ) -> Amount {
85+ self . value_added
9686 }
9787
9888 pub ( super ) fn inputs ( & self ) -> & [ FundingTxInput ] {
@@ -104,7 +94,7 @@ impl SpliceContribution {
10494 }
10595
10696 pub ( super ) fn into_tx_parts ( self ) -> ( Vec < FundingTxInput > , Vec < TxOut > , Option < ScriptBuf > ) {
107- let SpliceContribution { value : _, inputs, outputs, change_script } = self ;
97+ let SpliceContribution { value_added : _, inputs, outputs, change_script } = self ;
10898 ( inputs, outputs, change_script)
10999 }
110100}
0 commit comments