From 2937837fc0869134535363cb96fb29598478649e Mon Sep 17 00:00:00 2001 From: Slyghtning Date: Tue, 24 Jun 2025 22:13:55 +0200 Subject: [PATCH 1/2] chainfee: method to round up the fee for a given transaction weight --- lnwallet/chainfee/rates.go | 8 ++++++++ lnwallet/chainfee/rates_test.go | 11 +++++++++++ 2 files changed, 19 insertions(+) diff --git a/lnwallet/chainfee/rates.go b/lnwallet/chainfee/rates.go index 20f4f1e1d..735cbd20c 100644 --- a/lnwallet/chainfee/rates.go +++ b/lnwallet/chainfee/rates.go @@ -71,6 +71,14 @@ func (s SatPerKWeight) FeeForWeight(wu lntypes.WeightUnit) btcutil.Amount { return btcutil.Amount(s) * btcutil.Amount(wu) / 1000 } +// FeeForWeightRoundUp calculates the fee resulting from this fee rate and the +// given weight in weight units (wu), rounding up to the nearest satoshi. +func (s SatPerKWeight) FeeForWeightRoundUp( + wu lntypes.WeightUnit) btcutil.Amount { + + return (btcutil.Amount(s)*btcutil.Amount(wu) + 999) / 1000 +} + // FeeForVByte calculates the fee resulting from this fee rate and the given // size in vbytes (vb). func (s SatPerKWeight) FeeForVByte(vb lntypes.VByte) btcutil.Amount { diff --git a/lnwallet/chainfee/rates_test.go b/lnwallet/chainfee/rates_test.go index 11aa35adf..d3e4740f3 100644 --- a/lnwallet/chainfee/rates_test.go +++ b/lnwallet/chainfee/rates_test.go @@ -3,6 +3,7 @@ package chainfee import ( "testing" + "github.com/lightningnetwork/lnd/lntypes" "github.com/stretchr/testify/require" ) @@ -20,3 +21,13 @@ func TestSatPerVByteConversion(t *testing.T) { // 1 sat/vb should be equal to 250 sat/kw. require.Equal(t, SatPerKWeight(250), rate.FeePerKWeight()) } + +// TestFeeForWeightRoundUp checks that the FeeForWeightRoundUp method correctly +// rounds up the fee for a given weight. +func TestFeeForWeightRoundUp(t *testing.T) { + feeRate := SatPerVByte(1).FeePerKWeight() + txWeight := lntypes.WeightUnit(674) // 674 weight units is 168.5 vb. + + require.EqualValues(t, 168, feeRate.FeeForWeight(txWeight)) + require.EqualValues(t, 169, feeRate.FeeForWeightRoundUp(txWeight)) +} From 45b26cf2d4c5ca0e5f56479428e1fdb14726e575 Mon Sep 17 00:00:00 2001 From: Slyghtning Date: Wed, 25 Jun 2025 10:36:57 +0200 Subject: [PATCH 2/2] docs: update release notes --- docs/release-notes/release-notes-0.19.2.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/release-notes/release-notes-0.19.2.md b/docs/release-notes/release-notes-0.19.2.md index 37ff73ea9..a1e25ad03 100644 --- a/docs/release-notes/release-notes-0.19.2.md +++ b/docs/release-notes/release-notes-0.19.2.md @@ -36,6 +36,10 @@ ## Functional Enhancements +- [Adds](https://github.com/lightningnetwork/lnd/pull/9989) a method + `FeeForWeightRoundUp` to the `chainfee` package which rounds up a calculated + fee value to the nearest satoshi. + ## RPC Additions * When querying @@ -89,6 +93,7 @@ much more slowly. * Abdulkbk * djkazic +* hieblmi * Olaoluwa Osuntokun * Yong Yu * Ziggie