From bea0ffdf81f2697eb0c2b9d84462edb62085fa5a Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Wed, 26 Jul 2023 10:48:00 +0200 Subject: [PATCH] chainfee: add new unit `SatPerVByte` --- lnwallet/chainfee/rates.go | 18 ++++++++++++++++++ lnwallet/chainfee/rates_test.go | 22 ++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 lnwallet/chainfee/rates_test.go diff --git a/lnwallet/chainfee/rates.go b/lnwallet/chainfee/rates.go index f9b606f74..34da55374 100644 --- a/lnwallet/chainfee/rates.go +++ b/lnwallet/chainfee/rates.go @@ -18,6 +18,24 @@ const ( AbsoluteFeePerKwFloor SatPerKWeight = 250 ) +// SatPerVByte represents a fee rate in sat/vbyte. +type SatPerVByte btcutil.Amount + +// FeePerKWeight converts the current fee rate from sat/vb to sat/kw. +func (s SatPerVByte) FeePerKWeight() SatPerKWeight { + return SatPerKWeight(s * 1000 / blockchain.WitnessScaleFactor) +} + +// FeePerKVByte converts the current fee rate from sat/vb to sat/kvb. +func (s SatPerVByte) FeePerKVByte() SatPerKVByte { + return SatPerKVByte(s * 1000) +} + +// String returns a human-readable string of the fee rate. +func (s SatPerVByte) String() string { + return fmt.Sprintf("%v sat/vb", int64(s)) +} + // SatPerKVByte represents a fee rate in sat/kb. type SatPerKVByte btcutil.Amount diff --git a/lnwallet/chainfee/rates_test.go b/lnwallet/chainfee/rates_test.go new file mode 100644 index 000000000..11aa35adf --- /dev/null +++ b/lnwallet/chainfee/rates_test.go @@ -0,0 +1,22 @@ +package chainfee + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +// TestSatPerVByteConversion checks that the conversion from sat/vb to either +// sat/kw or sat/kvb is correct. +func TestSatPerVByteConversion(t *testing.T) { + t.Parallel() + + // Create a test fee rate of 1 sat/vb. + rate := SatPerVByte(1) + + // 1 sat/vb should be equal to 1000 sat/kvb. + require.Equal(t, SatPerKVByte(1000), rate.FeePerKVByte()) + + // 1 sat/vb should be equal to 250 sat/kw. + require.Equal(t, SatPerKWeight(250), rate.FeePerKWeight()) +}