|
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" |
@@ -555,6 +556,13 @@ func staticAddressLoopIn(ctx context.Context, cmd *cli.Command) error { |
555 | 556 | return errors.New("no deposited outputs available") |
556 | 557 | } |
557 | 558 |
|
| 559 | + summary, err := client.GetStaticAddressSummary( |
| 560 | + ctx, &looprpc.StaticAddressSummaryRequest{}, |
| 561 | + ) |
| 562 | + if err != nil { |
| 563 | + return err |
| 564 | + } |
| 565 | + |
558 | 566 | var depositOutpoints []string |
559 | 567 | switch { |
560 | 568 | case isAllSelected && isUtxoSelected: |
@@ -609,6 +617,22 @@ func staticAddressLoopIn(ctx context.Context, cmd *cli.Command) error { |
609 | 617 | return err |
610 | 618 | } |
611 | 619 |
|
| 620 | + // Warn the user if any selected deposits have fewer than 6 |
| 621 | + // confirmations, as the swap payment won't be received immediately |
| 622 | + // for those. |
| 623 | + depositsToCheck := depositOutpoints |
| 624 | + if autoSelectDepositsForQuote { |
| 625 | + // When auto-selecting, any deposit could be chosen. |
| 626 | + depositsToCheck = depositsToOutpoints(allDeposits) |
| 627 | + } |
| 628 | + warning := lowConfDepositWarning( |
| 629 | + allDeposits, depositsToCheck, |
| 630 | + int64(summary.RelativeExpiryBlocks), |
| 631 | + ) |
| 632 | + if warning != "" { |
| 633 | + fmt.Println(warning) |
| 634 | + } |
| 635 | + |
612 | 636 | if !(cmd.Bool("force") || cmd.Bool("f")) { |
613 | 637 | err = displayInDetails(quoteReq, quote, cmd.Bool("verbose")) |
614 | 638 | if err != nil { |
@@ -664,6 +688,79 @@ func depositsToOutpoints(deposits []*looprpc.Deposit) []string { |
664 | 688 | return outpoints |
665 | 689 | } |
666 | 690 |
|
| 691 | +// minImmediateConfs is the minimum number of confirmations a deposit needs |
| 692 | +// for the swap payment to be executed immediately. |
| 693 | +const minImmediateConfs = 6 |
| 694 | + |
| 695 | +// lowConfDepositWarning checks the selected deposits for fewer than 6 |
| 696 | +// confirmations and returns a warning string if any are found. The swap |
| 697 | +// payment for such deposits won't be received immediately. |
| 698 | +func lowConfDepositWarning(allDeposits []*looprpc.Deposit, |
| 699 | + selectedOutpoints []string, csvExpiry int64) string { |
| 700 | + |
| 701 | + depositMap := make(map[string]*looprpc.Deposit, len(allDeposits)) |
| 702 | + for _, d := range allDeposits { |
| 703 | + depositMap[d.Outpoint] = d |
| 704 | + } |
| 705 | + |
| 706 | + var lowConfEntries []string |
| 707 | + for _, op := range selectedOutpoints { |
| 708 | + d, ok := depositMap[op] |
| 709 | + if !ok { |
| 710 | + continue |
| 711 | + } |
| 712 | + |
| 713 | + var confs int64 |
| 714 | + switch { |
| 715 | + case d.ConfirmationHeight <= 0: |
| 716 | + confs = 0 |
| 717 | + |
| 718 | + case csvExpiry > 0: |
| 719 | + // For confirmed deposits we can compute |
| 720 | + // confirmations as CSVExpiry - BlocksUntilExpiry + 1. |
| 721 | + confs = csvExpiry - d.BlocksUntilExpiry + 1 |
| 722 | + |
| 723 | + default: |
| 724 | + // Can't determine confirmations without the CSV expiry. |
| 725 | + continue |
| 726 | + } |
| 727 | + |
| 728 | + if confs >= minImmediateConfs { |
| 729 | + continue |
| 730 | + } |
| 731 | + |
| 732 | + if confs == 0 { |
| 733 | + lowConfEntries = append( |
| 734 | + lowConfEntries, |
| 735 | + fmt.Sprintf(" - %s (unconfirmed)", op), |
| 736 | + ) |
| 737 | + } else { |
| 738 | + lowConfEntries = append( |
| 739 | + lowConfEntries, |
| 740 | + fmt.Sprintf( |
| 741 | + " - %s (%d confirmations)", op, |
| 742 | + confs, |
| 743 | + ), |
| 744 | + ) |
| 745 | + } |
| 746 | + } |
| 747 | + |
| 748 | + if len(lowConfEntries) == 0 { |
| 749 | + return "" |
| 750 | + } |
| 751 | + |
| 752 | + return fmt.Sprintf( |
| 753 | + "\nWARNING: The following deposits have fewer than %d "+ |
| 754 | + "confirmations:\n%s\n"+ |
| 755 | + "The swap payment for these deposits may not be "+ |
| 756 | + "received immediately.\nOnly deposits with %d or "+ |
| 757 | + "more confirmations are executed immediately.\n", |
| 758 | + minImmediateConfs, |
| 759 | + strings.Join(lowConfEntries, "\n"), |
| 760 | + minImmediateConfs, |
| 761 | + ) |
| 762 | +} |
| 763 | + |
667 | 764 | func displayNewAddressWarning() error { |
668 | 765 | fmt.Printf("\nWARNING: Be aware that loosing your l402.token file in " + |
669 | 766 | ".loop under your home directory will take your ability to " + |
|
0 commit comments