loop/utils/fees.go
Boris Nagaev d554b42ef6
multi: migrate to btcd v2 modules
Update LND, Aperture, and Taproot Assets to revisions using the
btcd v2 modules, and update lndclient to v0.21.0-3. Migrate Loop
chain, transaction, and address types to their corresponding v2
packages.

The lndclient release includes the migration from:
https://github.com/lightninglabs/lndclient/pull/280

Taproot Assets is temporarily replaced with its btcd v2 revision
because the v0.8 release branch has not adopted the new modules.

This raises the minimum Go version to 1.26 and changes exported
address types.
2026-08-12 23:39:26 +00:00

44 lines
1.2 KiB
Go

package utils
import (
"fmt"
"github.com/btcsuite/btcd/btcutil/v2"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
)
const (
// MaxFeeToAmountRatio is the maximum fee to total amount ratio allowed
// for a sweep transaction.
MaxFeeToAmountRatio = 0.2
)
// ClampSweepFee caps a fee to a percentage of the provided total amount and
// verifies the resulting fee rate is not below the minimum relay fee. It
// returns the clamped fee, whether it was clamped, or an error if the clamped
// fee would fall below the minimum relay fee.
func ClampSweepFee(fee btcutil.Amount, totalAmount btcutil.Amount,
ratio float64, minRelayFeeRate chainfee.SatPerKWeight,
weight lntypes.WeightUnit) (btcutil.Amount, bool, error) {
maxFeeAmount := btcutil.Amount(float64(totalAmount) * ratio)
clampedFee := fee
clamped := false
if fee > maxFeeAmount {
clampedFee = maxFeeAmount
clamped = true
}
if minRelayFeeRate > 0 {
clampedFeeRate := chainfee.NewSatPerKWeight(clampedFee, weight)
if clampedFeeRate < minRelayFeeRate {
return 0, clamped, fmt.Errorf("clamped fee rate %v is "+
"less than minimum relay fee %v",
clampedFeeRate, minRelayFeeRate)
}
}
return clampedFee, clamped, nil
}