Skip to content

Commit ef0dcf4

Browse files
committed
loopd: fractional static address swap amount
1 parent 64a3f74 commit ef0dcf4

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
@@ -478,6 +478,13 @@ var staticAddressLoopInCommand = cli.Command{
478478
"The client can retry the swap with adjusted " +
479479
"parameters after the payment timed out.",
480480
},
481+
cli.IntFlag{
482+
Name: "amount",
483+
Usage: "the number of satoshis that should be " +
484+
"swapped from the selected deposits. If there" +
485+
"is change it is sent back to the static " +
486+
"address.",
487+
},
481488
lastHopFlag,
482489
labelFlag,
483490
routeHintsFlag,
@@ -500,13 +507,15 @@ func staticAddressLoopIn(ctx *cli.Context) error {
500507
defer cleanup()
501508

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

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

545-
if len(depositList.FilteredDeposits) == 0 {
554+
allDeposits := depositList.FilteredDeposits
555+
556+
if len(allDeposits) == 0 {
546557
errString := fmt.Sprintf("no confirmed deposits available, "+
547558
"deposits need at least %v confirmations",
548559
deposit.MinConfs)
@@ -552,17 +563,18 @@ func staticAddressLoopIn(ctx *cli.Context) error {
552563

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

558569
case isAllSelected:
559-
depositOutpoints = depositsToOutpoints(
560-
depositList.FilteredDeposits,
561-
)
570+
depositOutpoints = depositsToOutpoints(allDeposits)
562571

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

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

586+
if len(depositOutpoints) == 0 && selectedAmount > 0 {
587+
selectDepositsForQuote = true
588+
}
589+
574590
quoteReq := &looprpc.QuoteRequest{
591+
Amt: selectedAmount,
575592
LoopInRouteHints: hints,
576593
LoopInLastHop: lastHop,
577594
Private: ctx.Bool(privateFlag.Name),
578595
DepositOutpoints: depositOutpoints,
596+
SelectDeposits: selectDepositsForQuote,
579597
}
580598
quote, err := client.GetLoopInQuote(ctxb, quoteReq)
581599
if err != nil {
@@ -584,15 +602,6 @@ func staticAddressLoopIn(ctx *cli.Context) error {
584602

585603
limits := getInLimits(quote)
586604

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

607616
req := &looprpc.StaticAddressLoopInRequest{
617+
Amount: quoteReq.Amt,
608618
Outpoints: depositOutpoints,
609619
MaxSwapFeeSatoshis: int64(limits.maxSwapFee),
610620
LastHop: lastHop,
@@ -637,26 +647,6 @@ func containsDuplicates(outpoints []string) bool {
637647
return false
638648
}
639649

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

0 commit comments

Comments
 (0)