Skip to content

Commit 374d41e

Browse files
committed
loopd: fractional static address swap amount
1 parent d837a66 commit 374d41e

File tree

1 file changed

+32
-42
lines changed

1 file changed

+32
-42
lines changed

cmd/loop/staticaddr.go

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,13 @@ var staticAddressLoopInCommand = cli.Command{
477477
"The client can retry the swap with adjusted " +
478478
"parameters after the payment timed out.",
479479
},
480+
cli.IntFlag{
481+
Name: "amount",
482+
Usage: "the number of satoshis that should be " +
483+
"swapped from the selected deposits. If there" +
484+
"is change it is sent back to the static " +
485+
"address.",
486+
},
480487
lastHopFlag,
481488
labelFlag,
482489
routeHintsFlag,
@@ -499,13 +506,15 @@ func staticAddressLoopIn(ctx *cli.Context) error {
499506
defer cleanup()
500507

501508
var (
502-
ctxb = context.Background()
503-
isAllSelected = ctx.IsSet("all")
504-
isUtxoSelected = ctx.IsSet("utxo")
505-
label = ctx.String("static-loop-in")
506-
hints []*swapserverrpc.RouteHint
507-
lastHop []byte
508-
paymentTimeoutSeconds = uint32(loopin.DefaultPaymentTimeoutSeconds)
509+
ctxb = context.Background()
510+
isAllSelected = ctx.IsSet("all")
511+
isUtxoSelected = ctx.IsSet("utxo")
512+
selectedAmount = ctx.Int64("amount")
513+
selectDepositsForQuote bool
514+
label = ctx.String("static-loop-in")
515+
hints []*swapserverrpc.RouteHint
516+
lastHop []byte
517+
paymentTimeoutSeconds = uint32(loopin.DefaultPaymentTimeoutSeconds)
509518
)
510519

511520
// Validate our label early so that we can fail before getting a quote.
@@ -541,7 +550,9 @@ func staticAddressLoopIn(ctx *cli.Context) error {
541550
return err
542551
}
543552

544-
if len(depositList.FilteredDeposits) == 0 {
553+
allDeposits := depositList.FilteredDeposits
554+
555+
if len(allDeposits) == 0 {
545556
errString := fmt.Sprintf("no confirmed deposits available, "+
546557
"deposits need at least %v confirmations",
547558
deposit.MinConfs)
@@ -551,17 +562,18 @@ func staticAddressLoopIn(ctx *cli.Context) error {
551562

552563
var depositOutpoints []string
553564
switch {
554-
case isAllSelected == isUtxoSelected:
555-
return errors.New("must select either all or some utxos")
565+
case isAllSelected && isUtxoSelected:
566+
return errors.New("cannot select all and specific utxos")
556567

557568
case isAllSelected:
558-
depositOutpoints = depositsToOutpoints(
559-
depositList.FilteredDeposits,
560-
)
569+
depositOutpoints = depositsToOutpoints(allDeposits)
561570

562571
case isUtxoSelected:
563572
depositOutpoints = ctx.StringSlice("utxo")
564573

574+
case selectedAmount > 0:
575+
// If only an amount is selected we will trigger coin selection.
576+
565577
default:
566578
return fmt.Errorf("unknown quote request")
567579
}
@@ -570,11 +582,17 @@ func staticAddressLoopIn(ctx *cli.Context) error {
570582
return errors.New("duplicate outpoints detected")
571583
}
572584

585+
if len(depositOutpoints) == 0 && selectedAmount > 0 {
586+
selectDepositsForQuote = true
587+
}
588+
573589
quoteReq := &looprpc.QuoteRequest{
590+
Amt: selectedAmount,
574591
LoopInRouteHints: hints,
575592
LoopInLastHop: lastHop,
576593
Private: ctx.Bool(privateFlag.Name),
577594
DepositOutpoints: depositOutpoints,
595+
SelectDeposits: selectDepositsForQuote,
578596
}
579597
quote, err := client.GetLoopInQuote(ctxb, quoteReq)
580598
if err != nil {
@@ -583,15 +601,6 @@ func staticAddressLoopIn(ctx *cli.Context) error {
583601

584602
limits := getInLimits(quote)
585603

586-
// populate the quote request with the sum of selected deposits and
587-
// prompt the user for acceptance.
588-
quoteReq.Amt, err = sumDeposits(
589-
depositOutpoints, depositList.FilteredDeposits,
590-
)
591-
if err != nil {
592-
return err
593-
}
594-
595604
if !(ctx.Bool("force") || ctx.Bool("f")) {
596605
err = displayInDetails(quoteReq, quote, ctx.Bool("verbose"))
597606
if err != nil {
@@ -604,6 +613,7 @@ func staticAddressLoopIn(ctx *cli.Context) error {
604613
}
605614

606615
req := &looprpc.StaticAddressLoopInRequest{
616+
Amount: quoteReq.Amt,
607617
Outpoints: depositOutpoints,
608618
MaxSwapFeeSatoshis: int64(limits.maxSwapFee),
609619
LastHop: lastHop,
@@ -636,26 +646,6 @@ func containsDuplicates(outpoints []string) bool {
636646
return false
637647
}
638648

639-
func sumDeposits(outpoints []string, deposits []*looprpc.Deposit) (int64,
640-
error) {
641-
642-
var sum int64
643-
depositMap := make(map[string]*looprpc.Deposit)
644-
for _, deposit := range deposits {
645-
depositMap[deposit.Outpoint] = deposit
646-
}
647-
648-
for _, outpoint := range outpoints {
649-
if _, ok := depositMap[outpoint]; !ok {
650-
return 0, fmt.Errorf("deposit %v not found", outpoint)
651-
}
652-
653-
sum += depositMap[outpoint].Value
654-
}
655-
656-
return sum, nil
657-
}
658-
659649
func depositsToOutpoints(deposits []*looprpc.Deposit) []string {
660650
outpoints := make([]string, 0, len(deposits))
661651
for _, deposit := range deposits {

0 commit comments

Comments
 (0)