Merge pull request #9226 from sputn1ck/sendcoins_selectutxo_fix_amt

cmd/sendcoins: fix display amount if select utxo and sweepall is set
This commit is contained in:
Oliver Gugger 2024-10-28 11:34:12 +01:00 committed by GitHub
commit c37baa68d8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

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