File tree Expand file tree Collapse file tree 2 files changed +19
-0
lines changed Expand file tree Collapse file tree 2 files changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -71,6 +71,14 @@ func (s SatPerKWeight) FeeForWeight(wu lntypes.WeightUnit) btcutil.Amount {
7171 return btcutil .Amount (s ) * btcutil .Amount (wu ) / 1000
7272}
7373
74+ // FeeForWeightRoundUp calculates the fee resulting from this fee rate and the
75+ // given weight in weight units (wu), rounding up to the nearest satoshi.
76+ func (s SatPerKWeight ) FeeForWeightRoundUp (
77+ wu lntypes.WeightUnit ) btcutil.Amount {
78+
79+ return (btcutil .Amount (s )* btcutil .Amount (wu ) + 999 ) / 1000
80+ }
81+
7482// FeeForVByte calculates the fee resulting from this fee rate and the given
7583// size in vbytes (vb).
7684func (s SatPerKWeight ) FeeForVByte (vb lntypes.VByte ) btcutil.Amount {
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package chainfee
33import (
44 "testing"
55
6+ "github.com/lightningnetwork/lnd/lntypes"
67 "github.com/stretchr/testify/require"
78)
89
@@ -20,3 +21,13 @@ func TestSatPerVByteConversion(t *testing.T) {
2021 // 1 sat/vb should be equal to 250 sat/kw.
2122 require .Equal (t , SatPerKWeight (250 ), rate .FeePerKWeight ())
2223}
24+
25+ // TestFeeForWeightRoundUp checks that the FeeForWeightRoundUp method correctly
26+ // rounds up the fee for a given weight.
27+ func TestFeeForWeightRoundUp (t * testing.T ) {
28+ feeRate := SatPerVByte (1 ).FeePerKWeight ()
29+ txWeight := lntypes .WeightUnit (674 ) // 674 weight units is 168.5 vb.
30+
31+ require .EqualValues (t , 168 , feeRate .FeeForWeight (txWeight ))
32+ require .EqualValues (t , 169 , feeRate .FeeForWeightRoundUp (txWeight ))
33+ }
You can’t perform that action at this time.
0 commit comments