Skip to content

Commit 67b2be4

Browse files
committed
cmd/loop: warn user about low-confirmation deposits before loop-in
Display a warning when selected deposits have fewer than 6 confirmations, since the swap payment for those won't be received immediately. Works for both manually selected and auto-selected deposits by deriving confirmation count from the CSV expiry and blocks-until-expiry fields.
1 parent 667778c commit 67b2be4

1 file changed

Lines changed: 99 additions & 0 deletions

File tree

cmd/loop/staticaddr.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"strings"
78

89
"github.com/lightninglabs/loop/labels"
910
"github.com/lightninglabs/loop/looprpc"
@@ -609,6 +610,19 @@ func staticAddressLoopIn(ctx context.Context, cmd *cli.Command) error {
609610
return err
610611
}
611612

613+
// Warn the user if any selected deposits have fewer than 6
614+
// confirmations, as the swap payment won't be received immediately
615+
// for those.
616+
depositsToCheck := depositOutpoints
617+
if autoSelectDepositsForQuote {
618+
// When auto-selecting, any deposit could be chosen.
619+
depositsToCheck = depositsToOutpoints(allDeposits)
620+
}
621+
warning := lowConfDepositWarning(allDeposits, depositsToCheck)
622+
if warning != "" {
623+
fmt.Println(warning)
624+
}
625+
612626
if !(cmd.Bool("force") || cmd.Bool("f")) {
613627
err = displayInDetails(quoteReq, quote, cmd.Bool("verbose"))
614628
if err != nil {
@@ -664,6 +678,91 @@ func depositsToOutpoints(deposits []*looprpc.Deposit) []string {
664678
return outpoints
665679
}
666680

681+
// minImmediateConfs is the minimum number of confirmations a deposit needs
682+
// for the swap payment to be executed immediately.
683+
const minImmediateConfs = 6
684+
685+
// lowConfDepositWarning checks the selected deposits for fewer than 6
686+
// confirmations and returns a warning string if any are found. The swap
687+
// payment for such deposits won't be received immediately.
688+
func lowConfDepositWarning(allDeposits []*looprpc.Deposit,
689+
selectedOutpoints []string) string {
690+
691+
depositMap := make(map[string]*looprpc.Deposit, len(allDeposits))
692+
for _, d := range allDeposits {
693+
depositMap[d.Outpoint] = d
694+
}
695+
696+
// Derive CSV expiry from any unconfirmed deposit. For unconfirmed
697+
// deposits, BlocksUntilExpiry equals the full CSV expiry value since
698+
// the timeout hasn't started counting yet.
699+
var csvExpiry int64
700+
for _, d := range allDeposits {
701+
if d.ConfirmationHeight <= 0 && d.BlocksUntilExpiry > 0 {
702+
csvExpiry = d.BlocksUntilExpiry
703+
break
704+
}
705+
}
706+
707+
var lowConfEntries []string
708+
for _, op := range selectedOutpoints {
709+
d, ok := depositMap[op]
710+
if !ok {
711+
continue
712+
}
713+
714+
var confs int64
715+
switch {
716+
case d.ConfirmationHeight <= 0:
717+
confs = 0
718+
719+
case csvExpiry > 0:
720+
// For confirmed deposits we can compute
721+
// confirmations as CSVExpiry - BlocksUntilExpiry + 1.
722+
confs = csvExpiry - d.BlocksUntilExpiry + 1
723+
724+
default:
725+
// Can't determine confirmations without a reference
726+
// unconfirmed deposit to derive CSVExpiry.
727+
continue
728+
}
729+
730+
if confs >= minImmediateConfs {
731+
continue
732+
}
733+
734+
if confs == 0 {
735+
lowConfEntries = append(
736+
lowConfEntries,
737+
fmt.Sprintf(" - %s (unconfirmed)", op),
738+
)
739+
} else {
740+
lowConfEntries = append(
741+
lowConfEntries,
742+
fmt.Sprintf(
743+
" - %s (%d confirmations)", op,
744+
confs,
745+
),
746+
)
747+
}
748+
}
749+
750+
if len(lowConfEntries) == 0 {
751+
return ""
752+
}
753+
754+
return fmt.Sprintf(
755+
"\nWARNING: The following deposits have fewer than %d "+
756+
"confirmations:\n%s\n"+
757+
"The swap payment for these deposits may not be "+
758+
"received immediately.\nOnly deposits with %d or "+
759+
"more confirmations are executed immediately.\n",
760+
minImmediateConfs,
761+
strings.Join(lowConfEntries, "\n"),
762+
minImmediateConfs,
763+
)
764+
}
765+
667766
func displayNewAddressWarning() error {
668767
fmt.Printf("\nWARNING: Be aware that loosing your l402.token file in " +
669768
".loop under your home directory will take your ability to " +

0 commit comments

Comments
 (0)