multi: factor out sweep fee clamping function

This commit is contained in:
Boris Nagaev 2026-01-14 18:41:56 -05:00
parent 9ee7a36f37
commit 0b53296aa8
No known key found for this signature in database
6 changed files with 153 additions and 40 deletions

View file

@ -317,9 +317,16 @@ func (f *FSM) BuildHTLCAction(ctx context.Context,
return f.handleErrorAndUnlockReservations(ctx, err)
}
minRelayFee, err := f.cfg.Wallet.MinRelayFee(ctx)
if err != nil {
return f.handleErrorAndUnlockReservations(ctx, err)
}
// Now that our nonces are set, we can create and sign the htlc
// transaction.
htlcTx, err := f.InstantOut.createHtlcTransaction(f.cfg.Network)
htlcTx, err := f.InstantOut.createHtlcTransaction(
f.cfg.Network, minRelayFee,
)
if err != nil {
return f.handleErrorAndUnlockReservations(ctx, err)
}
@ -382,6 +389,11 @@ func (f *FSM) PushPreimageAction(ctx context.Context,
return f.handleErrorAndUnlockReservations(ctx, err)
}
minRelayFee, err := f.cfg.Wallet.MinRelayFee(ctx)
if err != nil {
return f.handleErrorAndUnlockReservations(ctx, err)
}
pushPreImageRes, err := f.cfg.InstantOutClient.PushPreimage(
ctx,
&swapserverrpc.PushPreimageRequest{
@ -400,7 +412,9 @@ func (f *FSM) PushPreimageAction(ctx context.Context,
// Now that we have the sweepless sweep signatures we can build and
// publish the sweepless sweep transaction.
sweepTx, err := f.InstantOut.createSweeplessSweepTx(feeRate)
sweepTx, err := f.InstantOut.createSweeplessSweepTx(
feeRate, minRelayFee,
)
if err != nil {
f.LastActionError = err
return OnErrorPublishHtlc

View file

@ -18,6 +18,7 @@ import (
"github.com/lightninglabs/loop/instantout/reservation"
"github.com/lightninglabs/loop/loopdb"
"github.com/lightninglabs/loop/swap"
"github.com/lightninglabs/loop/utils"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lntypes"
@ -145,8 +146,8 @@ func (i *InstantOut) getInputReservations() (InputReservations, error) {
}
// createHtlcTransaction creates the htlc transaction for the instant out.
func (i *InstantOut) createHtlcTransaction(network *chaincfg.Params) (
*wire.MsgTx, error) {
func (i *InstantOut) createHtlcTransaction(network *chaincfg.Params,
minRelayFeeRate chainfee.SatPerKWeight) (*wire.MsgTx, error) {
if network == nil {
return nil, errors.New("no network provided")
@ -170,7 +171,16 @@ func (i *InstantOut) createHtlcTransaction(network *chaincfg.Params) (
// Estimate the fee
weight := htlcWeight(len(inputReservations))
fee := i.htlcFeeRate.FeeForWeight(weight)
if fee > i.Value/5 {
// We cap the fee at 20% of the deposit value.
_, clamped, err := utils.ClampSweepFee(
fee, i.Value, utils.MaxFeeToAmountRatio, minRelayFeeRate,
weight,
)
if err != nil {
return nil, err
}
if clamped {
return nil, errors.New("fee is higher than 20% of " +
"sweep value")
}
@ -193,8 +203,8 @@ func (i *InstantOut) createHtlcTransaction(network *chaincfg.Params) (
// createSweeplessSweepTx creates the sweepless sweep transaction for the
// instant out.
func (i *InstantOut) createSweeplessSweepTx(feerate chainfee.SatPerKWeight) (
*wire.MsgTx, error) {
func (i *InstantOut) createSweeplessSweepTx(feerate,
minRelayFeeRate chainfee.SatPerKWeight) (*wire.MsgTx, error) {
inputReservations, err := i.getInputReservations()
if err != nil {
@ -214,7 +224,14 @@ func (i *InstantOut) createSweeplessSweepTx(feerate chainfee.SatPerKWeight) (
// Estimate the fee
weight := sweeplessSweepWeight(len(inputReservations))
fee := feerate.FeeForWeight(weight)
if fee > i.Value/5 {
_, clamped, err := utils.ClampSweepFee(
fee, i.Value, utils.MaxFeeToAmountRatio, minRelayFeeRate,
weight,
)
if err != nil {
return nil, err
}
if clamped {
return nil, errors.New("fee is higher than 20% of " +
"sweep value")
}