Skip to content

Commit ab8ae54

Browse files
committed
chainfee: method to round up the fee for a given transaction weight
1 parent 4335d9c commit ab8ae54

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

lnwallet/chainfee/rates.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff 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).
7684
func (s SatPerKWeight) FeeForVByte(vb lntypes.VByte) btcutil.Amount {

lnwallet/chainfee/rates_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package chainfee
33
import (
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+
}

0 commit comments

Comments
 (0)