Skip to content

Commit e635d63

Browse files
committed
cmd: quote for static address loop-ins
1 parent faf3fd9 commit e635d63

File tree

1 file changed

+65
-11
lines changed

1 file changed

+65
-11
lines changed

cmd/loop/quote.go

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77
"time"
88

9+
"github.com/btcsuite/btcd/btcutil"
910
"github.com/lightninglabs/loop"
1011
"github.com/lightninglabs/loop/looprpc"
1112
"github.com/lightningnetwork/lnd/routing/route"
@@ -19,10 +20,11 @@ var quoteCommand = cli.Command{
1920
}
2021

2122
var quoteInCommand = cli.Command{
22-
Name: "in",
23-
Usage: "get a quote for the cost of a loop in swap",
24-
ArgsUsage: "amt",
25-
Description: "Allows to determine the cost of a swap up front",
23+
Name: "in",
24+
Usage: "get a quote for the cost of a loop in swap",
25+
ArgsUsage: "amt",
26+
Description: "Allows to determine the cost of a swap up front." +
27+
"Either specify an amount or deposit outpoints.",
2628
Flags: []cli.Flag{
2729
cli.StringFlag{
2830
Name: lastHopFlag.Name,
@@ -33,20 +35,38 @@ var quoteInCommand = cli.Command{
3335
verboseFlag,
3436
privateFlag,
3537
routeHintsFlag,
38+
cli.StringSliceFlag{
39+
Name: "deposit_outpoint",
40+
Usage: "one or more static address deposit outpoints " +
41+
"to quote for. Deposit outpoints are not to " +
42+
"be used in combination with an amount. Each" +
43+
"additional outpoint can be added by " +
44+
"specifying --deposit_outpoint tx_id:idx",
45+
},
3646
},
3747
Action: quoteIn,
3848
}
3949

4050
func quoteIn(ctx *cli.Context) error {
4151
// Show command help if the incorrect number arguments was provided.
42-
if ctx.NArg() != 1 {
52+
if ctx.NArg() != 1 && !ctx.IsSet("deposit_outpoint") {
4353
return cli.ShowCommandHelp(ctx, "in")
4454
}
4555

46-
args := ctx.Args()
47-
amt, err := parseAmt(args[0])
48-
if err != nil {
49-
return err
56+
var (
57+
manualAmt btcutil.Amount
58+
depositAmt btcutil.Amount
59+
depositOutpoints []string
60+
err error
61+
ctxb = context.Background()
62+
)
63+
64+
if ctx.NArg() == 1 {
65+
args := ctx.Args()
66+
manualAmt, err = parseAmt(args[0])
67+
if err != nil {
68+
return err
69+
}
5070
}
5171

5272
client, cleanup, err := getClient(ctx)
@@ -62,11 +82,20 @@ func quoteIn(ctx *cli.Context) error {
6282
return err
6383
}
6484

85+
if ctx.IsSet("deposit_outpoint") {
86+
depositOutpoints = ctx.StringSlice("deposit_outpoint")
87+
depositAmt, err = depositAmount(ctxb, client, depositOutpoints)
88+
if err != nil {
89+
return err
90+
}
91+
}
92+
6593
quoteReq := &looprpc.QuoteRequest{
66-
Amt: int64(amt),
94+
Amt: int64(manualAmt),
6795
ConfTarget: int32(ctx.Uint64("conf_target")),
6896
LoopInRouteHints: hints,
6997
Private: ctx.Bool(privateFlag.Name),
98+
DepositOutpoints: depositOutpoints,
7099
}
71100

72101
if ctx.IsSet(lastHopFlag.Name) {
@@ -80,7 +109,6 @@ func quoteIn(ctx *cli.Context) error {
80109
quoteReq.LoopInLastHop = lastHopVertex[:]
81110
}
82111

83-
ctxb := context.Background()
84112
quoteResp, err := client.GetLoopInQuote(ctxb, quoteReq)
85113
if err != nil {
86114
return err
@@ -98,10 +126,36 @@ func quoteIn(ctx *cli.Context) error {
98126
"amount.\n")
99127
}
100128

129+
// If the user specified static address deposits, we quoted for their
130+
// total value and need to display that value instead of the manually
131+
// selected one.
132+
if manualAmt == 0 {
133+
quoteReq.Amt = int64(depositAmt)
134+
}
101135
printQuoteInResp(quoteReq, quoteResp, ctx.Bool("verbose"))
102136
return nil
103137
}
104138

139+
func depositAmount(ctx context.Context, client looprpc.SwapClientClient,
140+
depositOutpoints []string) (btcutil.Amount, error) {
141+
142+
addressSummary, err := client.GetStaticAddressSummary(
143+
ctx, &looprpc.StaticAddressSummaryRequest{
144+
Outpoints: depositOutpoints,
145+
},
146+
)
147+
if err != nil {
148+
return 0, err
149+
}
150+
151+
var depositAmt btcutil.Amount
152+
for _, deposit := range addressSummary.FilteredDeposits {
153+
depositAmt += btcutil.Amount(deposit.Value)
154+
}
155+
156+
return depositAmt, nil
157+
}
158+
105159
var quoteOutCommand = cli.Command{
106160
Name: "out",
107161
Usage: "get a quote for the cost of a loop out swap",

0 commit comments

Comments
 (0)