6
6
"os"
7
7
"time"
8
8
9
+ "github.com/btcsuite/btcd/btcutil"
9
10
"github.com/lightninglabs/loop"
10
11
"github.com/lightninglabs/loop/looprpc"
11
12
"github.com/lightningnetwork/lnd/routing/route"
@@ -19,10 +20,11 @@ var quoteCommand = cli.Command{
19
20
}
20
21
21
22
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." ,
26
28
Flags : []cli.Flag {
27
29
cli.StringFlag {
28
30
Name : lastHopFlag .Name ,
@@ -33,20 +35,38 @@ var quoteInCommand = cli.Command{
33
35
verboseFlag ,
34
36
privateFlag ,
35
37
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
+ },
36
46
},
37
47
Action : quoteIn ,
38
48
}
39
49
40
50
func quoteIn (ctx * cli.Context ) error {
41
51
// Show command help if the incorrect number arguments was provided.
42
- if ctx .NArg () != 1 {
52
+ if ctx .NArg () != 1 && ! ctx . IsSet ( "deposit_outpoint" ) {
43
53
return cli .ShowCommandHelp (ctx , "in" )
44
54
}
45
55
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
+ }
50
70
}
51
71
52
72
client , cleanup , err := getClient (ctx )
@@ -62,11 +82,20 @@ func quoteIn(ctx *cli.Context) error {
62
82
return err
63
83
}
64
84
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
+
65
93
quoteReq := & looprpc.QuoteRequest {
66
- Amt : int64 (amt ),
94
+ Amt : int64 (manualAmt ),
67
95
ConfTarget : int32 (ctx .Uint64 ("conf_target" )),
68
96
LoopInRouteHints : hints ,
69
97
Private : ctx .Bool (privateFlag .Name ),
98
+ DepositOutpoints : depositOutpoints ,
70
99
}
71
100
72
101
if ctx .IsSet (lastHopFlag .Name ) {
@@ -80,7 +109,6 @@ func quoteIn(ctx *cli.Context) error {
80
109
quoteReq .LoopInLastHop = lastHopVertex [:]
81
110
}
82
111
83
- ctxb := context .Background ()
84
112
quoteResp , err := client .GetLoopInQuote (ctxb , quoteReq )
85
113
if err != nil {
86
114
return err
@@ -98,10 +126,36 @@ func quoteIn(ctx *cli.Context) error {
98
126
"amount.\n " )
99
127
}
100
128
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
+ }
101
135
printQuoteInResp (quoteReq , quoteResp , ctx .Bool ("verbose" ))
102
136
return nil
103
137
}
104
138
139
+ func depositAmount (ctx context.Context , client looprpc.SwapClientClient ,
140
+ depositOutpoints []string ) (btcutil.Amount , error ) {
141
+
142
+ addressSummary , err := client .ListStaticAddressDeposits (
143
+ ctx , & looprpc.ListStaticAddressDepositsRequest {
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
+
105
159
var quoteOutCommand = cli.Command {
106
160
Name : "out" ,
107
161
Usage : "get a quote for the cost of a loop out swap" ,
@@ -174,7 +228,11 @@ func printQuoteInResp(req *looprpc.QuoteRequest,
174
228
175
229
totalFee := resp .HtlcPublishFeeSat + resp .SwapFeeSat
176
230
177
- fmt .Printf (satAmtFmt , "Send on-chain:" , req .Amt )
231
+ if req .DepositOutpoints != nil {
232
+ fmt .Printf (satAmtFmt , "Previously deposited on-chain:" , req .Amt )
233
+ } else {
234
+ fmt .Printf (satAmtFmt , "Send on-chain:" , req .Amt )
235
+ }
178
236
fmt .Printf (satAmtFmt , "Receive off-chain:" , req .Amt - totalFee )
179
237
180
238
switch {
0 commit comments