Skip to content

Commit

Permalink
Merge pull request #9226 from sputn1ck/sendcoins_selectutxo_fix_amt
Browse files Browse the repository at this point in the history
cmd/sendcoins: fix display amount if select utxo and sweepall is set
  • Loading branch information
guggero authored Oct 28, 2024
2 parents acbb33b + 6be3e5b commit c37baa6
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion cmd/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit c37baa6

Please sign in to comment.