From fc90bc9b0f2b4238ea28a33eeb5b63394454701b Mon Sep 17 00:00:00 2001 From: feelancer21 <2828397+feelancer21@users.noreply.github.com> Date: Sun, 5 May 2024 23:19:32 +0200 Subject: [PATCH] lncli: new command `wallet estimatefeerate` `lncli wallet estimatefeerate` returns the fee rate estimate for on-chain transactions in sat/kw and sat/vb to achieve a given confirmation target. --- cmd/lncli/walletrpc_active.go | 52 ++++++++++++++++++++++ docs/release-notes/release-notes-0.18.1.md | 4 ++ 2 files changed, 56 insertions(+) diff --git a/cmd/lncli/walletrpc_active.go b/cmd/lncli/walletrpc_active.go index bb808a7cd8..30d6e56e72 100644 --- a/cmd/lncli/walletrpc_active.go +++ b/cmd/lncli/walletrpc_active.go @@ -11,6 +11,7 @@ import ( "encoding/json" "errors" "fmt" + "math" "sort" "strconv" "strings" @@ -22,6 +23,7 @@ import ( "github.com/btcsuite/btcd/wire" "github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnrpc/walletrpc" + "github.com/lightningnetwork/lnd/lnwallet/chainfee" "github.com/lightningnetwork/lnd/lnwallet/chanfunding" "github.com/urfave/cli" ) @@ -77,6 +79,7 @@ func walletCommands() []cli.Command { Usage: "Interact with the wallet.", Description: "", Subcommands: []cli.Command{ + estimateFeeRateCommand, pendingSweepsCommand, bumpFeeCommand, bumpCloseFeeCommand, @@ -124,6 +127,55 @@ func getWalletClient(ctx *cli.Context) (walletrpc.WalletKitClient, func()) { return walletrpc.NewWalletKitClient(conn), cleanUp } +var estimateFeeRateCommand = cli.Command{ + Name: "estimatefeerate", + Usage: "Estimates the on-chain fee rate to achieve a confirmation " + + "target.", + ArgsUsage: "conf_target", + Description: ` + Returns the fee rate estimate for on-chain transactions in sat/kw and + sat/vb to achieve a given confirmation target. The source of the fee + rate depends on the configuration and is either the on-chain backend or + alternatively an external URL. + `, + Action: actionDecorator(estimateFeeRate), +} + +func estimateFeeRate(ctx *cli.Context) error { + ctxc := getContext() + client, cleanUp := getWalletClient(ctx) + defer cleanUp() + + confTarget, err := strconv.ParseInt(ctx.Args().First(), 10, 64) + if err != nil { + return cli.ShowCommandHelp(ctx, "estimatefeerate") + } + + if confTarget <= 0 || confTarget > math.MaxInt32 { + return errors.New("conf_target out of range") + } + + resp, err := client.EstimateFee(ctxc, &walletrpc.EstimateFeeRequest{ + ConfTarget: int32(confTarget), + }) + if err != nil { + return err + } + + rateKW := chainfee.SatPerKWeight(resp.SatPerKw) + rateVB := rateKW.FeePerVByte() + + printJSON(struct { + SatPerKw int64 `json:"sat_per_kw"` + SatPerVByte int64 `json:"sat_per_vbyte"` + }{ + SatPerKw: int64(rateKW), + SatPerVByte: int64(rateVB), + }) + + return nil +} + var pendingSweepsCommand = cli.Command{ Name: "pendingsweeps", Usage: "List all outputs that are pending to be swept within lnd.", diff --git a/docs/release-notes/release-notes-0.18.1.md b/docs/release-notes/release-notes-0.18.1.md index cf5410a2c5..2ac8928f6e 100644 --- a/docs/release-notes/release-notes-0.18.1.md +++ b/docs/release-notes/release-notes-0.18.1.md @@ -32,6 +32,10 @@ argument to `addinvoice` and `addholdinvoice`, allowing users to set the `min_final_cltv_expiry_delta` +* The [`lncli wallet estimatefeerate`](https://github.com/lightningnetwork/lnd/pull/8730) + command returns the fee rate estimate for on-chain transactions in sat/kw and + sat/vb to achieve a given confirmation target. + # Improvements ## Functional Updates ## RPC Updates