@@ -35,6 +35,37 @@ use std::fmt;
3535
3636//TODO(stevenroose) consider using a Time type
3737
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+
3869/// A module used for serde serialization of bytes in hexadecimal format.
3970///
4071/// The module is compatible with the serde attribute.
@@ -1827,9 +1858,9 @@ impl BumpFeeOptions {
18271858 pub fn to_serializable ( & self , version : usize ) -> SerializableBumpFeeOptions {
18281859 let fee_rate = self . fee_rate . map ( |x| {
18291860 if version < 210000 {
1830- x. btc_per_kvbyte ( )
1861+ x. as_btc_per_kvbyte ( )
18311862 } else {
1832- x. sat_per_vbyte ( )
1863+ x. as_sat_per_vbyte ( )
18331864 }
18341865 } ) ;
18351866
@@ -1842,23 +1873,6 @@ impl BumpFeeOptions {
18421873 }
18431874}
18441875
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-
18621876#[ derive( Serialize , Clone , PartialEq , Debug , Default ) ]
18631877#[ serde( rename_all = "camelCase" ) ]
18641878pub struct SerializableBumpFeeOptions {
@@ -2072,3 +2086,18 @@ where
20722086 }
20732087 Ok ( Some ( res) )
20742088}
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