Skip to content

Commit

Permalink
tapcli+rpcserver: switch manual fee unit to sat/vB
Browse files Browse the repository at this point in the history
  • Loading branch information
jharveyb committed Jan 9, 2024
1 parent b8624ed commit 9a47eb8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
20 changes: 15 additions & 5 deletions cmd/tapcli/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/lightninglabs/taproot-assets/tapcfg"
"github.com/lightninglabs/taproot-assets/taprpc"
"github.com/lightninglabs/taproot-assets/taprpc/mintrpc"
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
"github.com/urfave/cli"
)

Expand Down Expand Up @@ -50,7 +51,7 @@ var (
groupByGroupName = "by_group"
assetIDName = "asset_id"
shortResponseName = "short"
feeRateName = "fee_rate"
feeRateName = "sat_per_vbyte"
assetAmountName = "amount"
burnOverrideConfirmationName = "override_confirmation_destroy_assets"
)
Expand Down Expand Up @@ -261,7 +262,7 @@ var finalizeBatchCommand = cli.Command{
},
cli.Uint64Flag{
Name: feeRateName,
Usage: "if set, the fee rate in sat/kw to use for " +
Usage: "if set, the fee rate in sat/vB to use for " +
"the minting transaction",
},
},
Expand All @@ -270,11 +271,20 @@ var finalizeBatchCommand = cli.Command{

func parseFeeRate(ctx *cli.Context) (uint32, error) {
if ctx.IsSet(feeRateName) {
feeRate := ctx.Uint64(feeRateName)
if feeRate > math.MaxUint32 {
userFeeRate := ctx.Uint64(feeRateName)
if userFeeRate > math.MaxUint32 {
return 0, fmt.Errorf("fee rate exceeds 2^32")
}

// Convert from sat/vB to sat/kw. Round up to the fee floor if
// the specified feerate is too low.
feeRate := chainfee.SatPerKVByte(userFeeRate * 1000).
FeePerKWeight()

if feeRate < chainfee.FeePerKwFloor {
feeRate = chainfee.FeePerKwFloor
}

return uint32(feeRate), nil
}

Expand Down Expand Up @@ -531,7 +541,7 @@ var sendAssetsCommand = cli.Command{
},
cli.Uint64Flag{
Name: feeRateName,
Usage: "if set, the fee rate in sat/kw to use for " +
Usage: "if set, the fee rate in sat/vB to use for " +
"the anchor transaction",
},
// TODO(roasbeef): add arg for file name to write sender proof
Expand Down
20 changes: 9 additions & 11 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,26 +491,24 @@ func (r *rpcServer) MintAsset(ctx context.Context,
}
}

// checkFeeRateSanity ensures that the provided fee rate is above the same
// minimum fee used as a floor in the fee estimator.
// checkFeeRateSanity ensures that the provided fee rate, in sat/kw, is above
// the same minimum fee used as a floor in the fee estimator.
func checkFeeRateSanity(rpcFeeRate uint32) (*chainfee.SatPerKWeight, error) {
var feeRate *chainfee.SatPerKWeight
feeFloor := uint32(chainfee.FeePerKwFloor)
switch {
// No manual fee rate was set, which is the default.
case rpcFeeRate == 0:
return nil, nil

// A manual fee was set but is below a reasonable floor.
case rpcFeeRate < uint32(chainfee.FeePerKwFloor):
return nil, fmt.Errorf("manual fee rate %d below floor of %d",
rpcFeeRate, uint32(chainfee.FeePerKwFloor))
case rpcFeeRate < feeFloor:
return nil, fmt.Errorf("manual fee rate below floor: "+
"(fee_rate=%d, floor=%d sat/kw)", rpcFeeRate, feeFloor)

// Set the fee rate for this transaction.
default:
// Set the fee rate for this transaction.
manualFeeRate := chainfee.SatPerKWeight(rpcFeeRate)
feeRate = &manualFeeRate
return fn.Ptr(chainfee.SatPerKWeight(rpcFeeRate)), nil
}

return feeRate, nil
}

// FinalizeBatch attempts to finalize the current pending batch.
Expand Down

0 comments on commit 9a47eb8

Please sign in to comment.