|
4 | 4 | "context" |
5 | 5 | "errors" |
6 | 6 | "fmt" |
| 7 | + "strings" |
7 | 8 |
|
8 | 9 | "github.com/lightninglabs/loop/labels" |
9 | 10 | "github.com/lightninglabs/loop/looprpc" |
@@ -609,6 +610,19 @@ func staticAddressLoopIn(ctx context.Context, cmd *cli.Command) error { |
609 | 610 | return err |
610 | 611 | } |
611 | 612 |
|
| 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 | + |
612 | 626 | if !(cmd.Bool("force") || cmd.Bool("f")) { |
613 | 627 | err = displayInDetails(quoteReq, quote, cmd.Bool("verbose")) |
614 | 628 | if err != nil { |
@@ -664,6 +678,91 @@ func depositsToOutpoints(deposits []*looprpc.Deposit) []string { |
664 | 678 | return outpoints |
665 | 679 | } |
666 | 680 |
|
| 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 | + |
667 | 766 | func displayNewAddressWarning() error { |
668 | 767 | fmt.Printf("\nWARNING: Be aware that loosing your l402.token file in " + |
669 | 768 | ".loop under your home directory will take your ability to " + |
|
0 commit comments