lnrpc: move CalculateFeeRate from rpcserver.go to rpc_utils.go

This refactor aims to house the CalculateFeeRate function in a more
"shareable" location. It used to be a non-exported function inside
of rpcserver.go. However, other routines, such as FundPsbt inside
of walletkit_server.go, could also rely on the same fee calculation
functionality. So we move the function to rpc_utils.go and export it,
and we will reuse it in the FundPsbt workflow in a future PR.
This commit is contained in:
shaurya947 2023-04-27 19:33:21 -04:00
parent 25097f0993
commit 85ab96cd09
No known key found for this signature in database
2 changed files with 45 additions and 42 deletions

View File

@ -3,10 +3,13 @@ package lnrpc
import (
"encoding/hex"
"errors"
"fmt"
"sort"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
"github.com/lightningnetwork/lnd/sweep"
"google.golang.org/protobuf/encoding/protojson"
)
@ -193,3 +196,41 @@ func GetChanPointFundingTxid(chanPoint *ChannelPoint) (*chainhash.Hash, error) {
return chainhash.NewHash(txid)
}
// CalculateFeeRate uses either satPerByte or satPerVByte, but not both, from a
// request to calculate the fee rate. It provides compatibility for the
// deprecated field, satPerByte. Once the field is safe to be removed, the
// check can then be deleted.
func CalculateFeeRate(satPerByte, satPerVByte uint64, targetConf uint32,
estimator chainfee.Estimator) (chainfee.SatPerKWeight, error) {
var feeRate chainfee.SatPerKWeight
// We only allow using either the deprecated field or the new field.
if satPerByte != 0 && satPerVByte != 0 {
return feeRate, fmt.Errorf("either SatPerByte or " +
"SatPerVByte should be set, but not both")
}
// Default to satPerVByte, and overwrite it if satPerByte is set.
satPerKw := chainfee.SatPerKVByte(satPerVByte * 1000).FeePerKWeight()
if satPerByte != 0 {
satPerKw = chainfee.SatPerKVByte(
satPerByte * 1000,
).FeePerKWeight()
}
// Based on the passed fee related parameters, we'll determine an
// appropriate fee rate for this transaction.
feeRate, err := sweep.DetermineFeePerKw(
estimator, sweep.FeePreference{
ConfTarget: targetConf,
FeeRate: satPerKw,
},
)
if err != nil {
return feeRate, err
}
return feeRate, nil
}

View File

@ -222,44 +222,6 @@ func stringInSlice(a string, slice []string) bool {
return false
}
// calculateFeeRate uses either satPerByte or satPerVByte, but not both, from a
// request to calculate the fee rate. It provides compatibility for the
// deprecated field, satPerByte. Once the field is safe to be removed, the
// check can then be deleted.
func calculateFeeRate(satPerByte, satPerVByte uint64, targetConf uint32,
estimator chainfee.Estimator) (chainfee.SatPerKWeight, error) {
var feeRate chainfee.SatPerKWeight
// We only allow using either the deprecated field or the new field.
if satPerByte != 0 && satPerVByte != 0 {
return feeRate, fmt.Errorf("either SatPerByte or " +
"SatPerVByte should be set, but not both")
}
// Default to satPerVByte, and overwrite it if satPerByte is set.
satPerKw := chainfee.SatPerKVByte(satPerVByte * 1000).FeePerKWeight()
if satPerByte != 0 {
satPerKw = chainfee.SatPerKVByte(
satPerByte * 1000,
).FeePerKWeight()
}
// Based on the passed fee related parameters, we'll determine an
// appropriate fee rate for this transaction.
feeRate, err := sweep.DetermineFeePerKw(
estimator, sweep.FeePreference{
ConfTarget: targetConf,
FeeRate: satPerKw,
},
)
if err != nil {
return feeRate, err
}
return feeRate, nil
}
// GetAllPermissions returns all the permissions required to interact with lnd.
func GetAllPermissions() []bakery.Op {
allPerms := make([]bakery.Op, 0)
@ -1239,7 +1201,7 @@ func (r *rpcServer) SendCoins(ctx context.Context,
in *lnrpc.SendCoinsRequest) (*lnrpc.SendCoinsResponse, error) {
// Calculate an appropriate fee rate for this transaction.
feePerKw, err := calculateFeeRate(
feePerKw, err := lnrpc.CalculateFeeRate(
uint64(in.SatPerByte), in.SatPerVbyte, // nolint:staticcheck
uint32(in.TargetConf), r.server.cc.FeeEstimator,
)
@ -1452,7 +1414,7 @@ func (r *rpcServer) SendMany(ctx context.Context,
in *lnrpc.SendManyRequest) (*lnrpc.SendManyResponse, error) {
// Calculate an appropriate fee rate for this transaction.
feePerKw, err := calculateFeeRate(
feePerKw, err := lnrpc.CalculateFeeRate(
uint64(in.SatPerByte), in.SatPerVbyte, // nolint:staticcheck
uint32(in.TargetConf), r.server.cc.FeeEstimator,
)
@ -2093,7 +2055,7 @@ func (r *rpcServer) parseOpenChannelReq(in *lnrpc.OpenChannelRequest,
}
// Calculate an appropriate fee rate for this transaction.
feeRate, err := calculateFeeRate(
feeRate, err := lnrpc.CalculateFeeRate(
uint64(in.SatPerByte), in.SatPerVbyte, // nolint:staticcheck
uint32(in.TargetConf), r.server.cc.FeeEstimator,
)
@ -2574,7 +2536,7 @@ func (r *rpcServer) CloseChannel(in *lnrpc.CloseChannelRequest,
// Based on the passed fee related parameters, we'll determine
// an appropriate fee rate for the cooperative closure
// transaction.
feeRate, err := calculateFeeRate(
feeRate, err := lnrpc.CalculateFeeRate(
uint64(in.SatPerByte), in.SatPerVbyte, // nolint:staticcheck
uint32(in.TargetConf), r.server.cc.FeeEstimator,
)