@@ -35,6 +35,37 @@ use std::fmt;
35
35
36
36
//TODO(stevenroose) consider using a Time type
37
37
38
+ /// A representation of a fee rate. Bitcoin Core uses different units in different
39
+ /// versions. To avoid burdening the user with using the correct unit, this struct
40
+ /// provides an umambiguous way to represent the fee rate, and the lib will perform
41
+ /// the necessary conversions.
42
+ #[ derive( Copy , Clone , PartialEq , Eq , Debug , Default ) ]
43
+ pub struct FeeRate ( Amount ) ;
44
+
45
+ impl FeeRate {
46
+ /// Construct FeeRate from the amount per vbyte
47
+ pub fn per_vbyte ( amount_per_vbyte : Amount ) -> Self {
48
+ // internal representation is amount per vbyte
49
+ Self ( amount_per_vbyte)
50
+ }
51
+
52
+ /// Construct FeeRate from the amount per kilo-vbyte
53
+ pub fn per_kvbyte ( amount_per_kvbyte : Amount ) -> Self {
54
+ // internal representation is amount per vbyte, so divide by 1000
55
+ Self :: per_vbyte ( amount_per_kvbyte / 1000 )
56
+ }
57
+
58
+ pub fn as_sat_per_vbyte ( & self ) -> f64 {
59
+ // multiply by the number of decimals to get sat
60
+ self . 0 . as_sat ( ) as f64
61
+ }
62
+
63
+ pub fn as_btc_per_kvbyte ( & self ) -> f64 {
64
+ // divide by 10^8 to get btc/vbyte, then multiply by 10^3 to get btc/kbyte
65
+ self . 0 . as_sat ( ) as f64 / 100_000.0
66
+ }
67
+ }
68
+
38
69
/// A module used for serde serialization of bytes in hexadecimal format.
39
70
///
40
71
/// The module is compatible with the serde attribute.
@@ -1827,9 +1858,9 @@ impl BumpFeeOptions {
1827
1858
pub fn to_serializable ( & self , version : usize ) -> SerializableBumpFeeOptions {
1828
1859
let fee_rate = self . fee_rate . map ( |x| {
1829
1860
if version < 210000 {
1830
- x. btc_per_kvbyte ( )
1861
+ x. as_btc_per_kvbyte ( )
1831
1862
} else {
1832
- x. sat_per_vbyte ( )
1863
+ x. as_sat_per_vbyte ( )
1833
1864
}
1834
1865
} ) ;
1835
1866
@@ -1842,23 +1873,6 @@ impl BumpFeeOptions {
1842
1873
}
1843
1874
}
1844
1875
1845
- #[ derive( Copy , Clone , PartialEq , Eq , Debug , Default ) ]
1846
- pub struct FeeRate ( Amount ) ;
1847
-
1848
- impl FeeRate {
1849
- pub fn new ( amount_per_vbyte : Amount ) -> Self {
1850
- Self ( amount_per_vbyte)
1851
- }
1852
- pub fn sat_per_vbyte ( & self ) -> f64 {
1853
- // multiply by the number of decimals to get sat
1854
- self . 0 . as_sat ( ) as f64
1855
- }
1856
- pub fn btc_per_kvbyte ( & self ) -> f64 {
1857
- // divide by 10^8 to get btc/vbyte, then multiply by 10^3 to get btc/kbyte
1858
- self . 0 . as_sat ( ) as f64 / 100_000.0
1859
- }
1860
- }
1861
-
1862
1876
#[ derive( Serialize , Clone , PartialEq , Debug , Default ) ]
1863
1877
#[ serde( rename_all = "camelCase" ) ]
1864
1878
pub struct SerializableBumpFeeOptions {
@@ -2072,3 +2086,18 @@ where
2072
2086
}
2073
2087
Ok ( Some ( res) )
2074
2088
}
2089
+
2090
+ #[ cfg( test) ]
2091
+ mod tests {
2092
+ use super :: * ;
2093
+
2094
+ #[ test]
2095
+ fn test_fee_rate_conversion ( ) {
2096
+ let rate_1 = FeeRate :: per_kvbyte ( Amount :: from_sat ( 10_000 ) ) ;
2097
+ let rate_2 = FeeRate :: per_vbyte ( Amount :: from_sat ( 10 ) ) ;
2098
+ assert_eq ! ( rate_1, rate_2) ;
2099
+
2100
+ assert_eq ! ( rate_1. as_sat_per_vbyte( ) , 10.0 ) ;
2101
+ assert_eq ! ( rate_1. as_btc_per_kvbyte( ) , 10.0 * 1e3 / 1e8 ) ;
2102
+ }
2103
+ }
0 commit comments