mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-30 15:40:59 +02:00
sweep+cnct+nursery+rpc: extract DetermineFeePerKw to func, add FeePreference
In this commit, we extract the existing determineFeePerKw method on the RPC server into a new file in the sweep package. Along the way, we consolidate code by introducing a new FeePreference struct, which allows the caller to express their fee preference either in blocks to confirmation, or a direct fee rate. This move takes a small step to father decoupling calls in the main RPC server.
This commit is contained in:
@@ -750,10 +750,10 @@ func (s *UtxoSweeper) waitForSpend(outpoint wire.OutPoint,
|
||||
// - Make handling re-orgs easier.
|
||||
// - Thwart future possible fee sniping attempts.
|
||||
// - Make us blend in with the bitcoind wallet.
|
||||
func (s *UtxoSweeper) CreateSweepTx(inputs []Input, confTarget uint32,
|
||||
func (s *UtxoSweeper) CreateSweepTx(inputs []Input, feePref FeePreference,
|
||||
currentBlockHeight uint32) (*wire.MsgTx, error) {
|
||||
|
||||
feePerKw, err := s.cfg.FeeEstimator.EstimateFeePerKW(confTarget)
|
||||
feePerKw, err := DetermineFeePerKw(s.cfg.FeeEstimator, feePref)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
68
sweep/walletsweep.go
Normal file
68
sweep/walletsweep.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package sweep
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/lightningnetwork/lnd/lnwallet"
|
||||
)
|
||||
|
||||
// FeePreference allows callers to express their time value for inclusion of a
|
||||
// transaction into a block via either a confirmation target, or a fee rate.
|
||||
type FeePreference struct {
|
||||
// ConfTarget if non-zero, signals a fee preference expressed in the
|
||||
// number of desired blocks between first broadcast, and confirmation.
|
||||
ConfTarget uint32
|
||||
|
||||
// FeeRate if non-zero, signals a fee pre fence expressed in the fee
|
||||
// rate expressed in sat/kw for a particular transaction.
|
||||
FeeRate lnwallet.SatPerKWeight
|
||||
}
|
||||
|
||||
// DetermineFeePerKw will determine the fee in sat/kw that should be paid given
|
||||
// an estimator, a confirmation target, and a manual value for sat/byte. A
|
||||
// value is chosen based on the two free parameters as one, or both of them can
|
||||
// be zero.
|
||||
func DetermineFeePerKw(feeEstimator lnwallet.FeeEstimator,
|
||||
feePref FeePreference) (lnwallet.SatPerKWeight, error) {
|
||||
|
||||
switch {
|
||||
// If the target number of confirmations is set, then we'll use that to
|
||||
// consult our fee estimator for an adequate fee.
|
||||
case feePref.ConfTarget != 0:
|
||||
feePerKw, err := feeEstimator.EstimateFeePerKW(
|
||||
uint32(feePref.ConfTarget),
|
||||
)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("unable to query fee "+
|
||||
"estimator: %v", err)
|
||||
}
|
||||
|
||||
return feePerKw, nil
|
||||
|
||||
// If a manual sat/byte fee rate is set, then we'll use that directly.
|
||||
// We'll need to convert it to sat/kw as this is what we use
|
||||
// internally.
|
||||
case feePref.FeeRate != 0:
|
||||
feePerKW := feePref.FeeRate
|
||||
if feePerKW < lnwallet.FeePerKwFloor {
|
||||
log.Infof("Manual fee rate input of %d sat/kw is "+
|
||||
"too low, using %d sat/kw instead", feePerKW,
|
||||
lnwallet.FeePerKwFloor)
|
||||
|
||||
feePerKW = lnwallet.FeePerKwFloor
|
||||
}
|
||||
|
||||
return feePerKW, nil
|
||||
|
||||
// Otherwise, we'll attempt a relaxed confirmation target for the
|
||||
// transaction
|
||||
default:
|
||||
feePerKw, err := feeEstimator.EstimateFeePerKW(6)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("unable to query fee estimator: "+
|
||||
"%v", err)
|
||||
}
|
||||
|
||||
return feePerKw, nil
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user