From 6be3e5b60931030ca2d6e146bb5a5aaa72216841 Mon Sep 17 00:00:00 2001 From: sputn1ck Date: Mon, 28 Oct 2024 10:01:50 +0100 Subject: [PATCH] cmd/sendcoins: fix amt if select utxo This commit fixes the display of the amount when selecting utxos for the sendcoins command and combining it with the `-sweepall` flag. Prior this would show the full balance of the wallet. Now it shows the total amount of the selected utxos. --- cmd/commands/commands.go | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/cmd/commands/commands.go b/cmd/commands/commands.go index 0af6c14aac..d24ce63651 100644 --- a/cmd/commands/commands.go +++ b/cmd/commands/commands.go @@ -461,7 +461,7 @@ func sendCoins(ctx *cli.Context) error { // In case that the user has specified the sweepall flag, we'll // calculate the amount to send based on the current wallet balance. displayAmt := amt - if ctx.Bool("sweepall") { + if ctx.Bool("sweepall") && !ctx.IsSet("utxo") { balanceResponse, err := client.WalletBalance( ctxc, &lnrpc.WalletBalanceRequest{ MinConfs: minConfs, @@ -481,6 +481,32 @@ func sendCoins(ctx *cli.Context) error { if err != nil { return fmt.Errorf("unable to decode utxos: %w", err) } + + if ctx.Bool("sweepall") { + displayAmt = 0 + // If we're sweeping all funds of the utxos, we'll need + // to set the display amount to the total amount of the + // utxos. + unspents, err := client.ListUnspent( + ctxc, &lnrpc.ListUnspentRequest{ + MinConfs: 0, + MaxConfs: math.MaxInt32, + }, + ) + if err != nil { + return err + } + + for _, utxo := range outpoints { + for _, unspent := range unspents.Utxos { + unspentUtxo := unspent.Outpoint + if isSameOutpoint(utxo, unspentUtxo) { + displayAmt += unspent.AmountSat + break + } + } + } + } } // Ask for confirmation if we're on an actual terminal and the output is @@ -517,6 +543,10 @@ func sendCoins(ctx *cli.Context) error { return nil } +func isSameOutpoint(a, b *lnrpc.OutPoint) bool { + return a.TxidStr == b.TxidStr && a.OutputIndex == b.OutputIndex +} + var listUnspentCommand = cli.Command{ Name: "listunspent", Category: "On-chain",