Skip to content

Commit fb720c1

Browse files
authored
Merge pull request #9989 from hieblmi/round-up
chainfee: method to round up the fee for a given transaction weight
2 parents 33e6f28 + 45ba301 commit fb720c1

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

docs/release-notes/release-notes-0.19.2.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333

3434
## Functional Enhancements
3535

36+
- [Adds](https://github.com/lightningnetwork/lnd/pull/9989) a method
37+
`FeeForWeightRoundUp` to the `chainfee` package which rounds up a calculated
38+
fee value to the nearest satoshi.
39+
3640
## RPC Additions
3741

3842
## lncli Additions
@@ -78,4 +82,5 @@ much more slowly.
7882
## Tooling and Documentation
7983

8084
# Contributors (Alphabetical Order)
85+
* hieblmi
8186
* Yong Yu

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)