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.
This commit is contained in:
sputn1ck 2024-10-28 10:01:50 +01:00
parent acbb33bb7b
commit 6be3e5b609
No known key found for this signature in database
GPG Key ID: 671103D881A5F0E4

View File

@ -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",