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()) +}