@@ -16,8 +16,6 @@ use super::{
16
16
OutOfRangeError , ParseAmountError , ParseError , SignedAmount ,
17
17
} ;
18
18
19
- use crate :: { FeeRate , Weight } ;
20
-
21
19
mod encapsulate {
22
20
use super :: OutOfRangeError ;
23
21
@@ -236,7 +234,7 @@ impl Amount {
236
234
#[ cfg( feature = "alloc" ) ]
237
235
pub fn to_btc ( self ) -> f64 { self . to_float_in ( Denomination :: Bitcoin ) }
238
236
239
- /// Computes the value of an output accounting for the cost of spending it .
237
+ /// Computes the effective value of an [`Amount`] .
240
238
///
241
239
/// The effective value is the value of an output value minus the amount to spend it. That is, the
242
240
/// effective_value can be calculated as: value - (fee_rate * weight).
@@ -251,11 +249,17 @@ impl Amount {
251
249
/// * `input_weight_prediction` - the predicted input weight.
252
250
pub fn to_effective_value (
253
251
& self ,
254
- fee_rate : FeeRate ,
255
- weight : Weight ,
256
- ) -> Option < SignedAmount > {
257
- let signed_input_fee = fee_rate. to_fee ( weight) ?. to_signed ( ) ;
258
- self . to_signed ( ) . checked_sub ( signed_input_fee)
252
+ fee_rate : crate :: FeeRate ,
253
+ weight : crate :: Weight
254
+ ) -> Result < SignedAmount , OutOfRangeError > {
255
+ let signed_input_fee = fee_rate. to_fee ( weight) . ok_or (
256
+ OutOfRangeError { is_signed : false , is_greater_than_max : true }
257
+ ) ?. to_signed ( ) ;
258
+
259
+ // it's not possible to overflow when subtracted by the smallest givem:
260
+ // Amount::MIN - SignedAmount::MAX = SignedAmount::MIN
261
+ let eff_val = ( self . to_signed ( ) - signed_input_fee) . unwrap ( ) ; //unwrap ok
262
+ Ok ( eff_val)
259
263
}
260
264
261
265
/// Converts this [`Amount`] in floating-point notation in the given [`Denomination`].
0 commit comments