Skip to content

Commit a6539b6

Browse files
authored
Merge pull request #249 from joostjager/split-client-messages
looprpc: split client messages
2 parents 57374ef + 8d1ec50 commit a6539b6

File tree

9 files changed

+520
-310
lines changed

9 files changed

+520
-310
lines changed

cmd/loop/loopin.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"github.com/btcsuite/btcutil"
88
"github.com/lightninglabs/loop"
99
"github.com/lightninglabs/loop/looprpc"
10-
"github.com/lightninglabs/loop/swap"
1110
"github.com/lightningnetwork/lnd/routing/route"
1211
"github.com/urfave/cli"
1312
)
@@ -111,17 +110,18 @@ func loopIn(ctx *cli.Context) error {
111110
// HTLC. If the wallet doesn't have enough funds to create this TX, we
112111
// know it won't have enough to pay the real transaction either. It
113112
// makes sense to abort the loop in this case.
114-
if !external && quote.MinerFee == int64(loop.MinerFeeEstimationFailed) {
113+
if !external && quote.HtlcPublishFeeSat ==
114+
int64(loop.MinerFeeEstimationFailed) {
115+
115116
return fmt.Errorf("miner fee estimation not " +
116117
"possible, lnd has insufficient funds to " +
117118
"create a sample transaction for selected " +
118119
"amount")
119120
}
120121

121-
limits := getInLimits(amt, quote)
122-
err = displayLimits(
123-
swap.TypeIn, amt, btcutil.Amount(quote.MinerFee), limits,
124-
external, "",
122+
limits := getInLimits(quote)
123+
err = displayInLimits(
124+
amt, btcutil.Amount(quote.HtlcPublishFeeSat), limits, external,
125125
)
126126
if err != nil {
127127
return err
@@ -165,12 +165,3 @@ func loopIn(ctx *cli.Context) error {
165165

166166
return nil
167167
}
168-
169-
func getInLimits(amt btcutil.Amount, quote *looprpc.QuoteResponse) *limits {
170-
return &limits{
171-
// Apply a multiplier to the estimated miner fee, to not get
172-
// the swap canceled because fees increased in the mean time.
173-
maxMinerFee: btcutil.Amount(quote.MinerFee) * 3,
174-
maxSwapFee: btcutil.Amount(quote.SwapFee),
175-
}
176-
}

cmd/loop/loopout.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/btcsuite/btcutil"
1111
"github.com/lightninglabs/loop"
1212
"github.com/lightninglabs/loop/looprpc"
13-
"github.com/lightninglabs/loop/swap"
1413
"github.com/urfave/cli"
1514
)
1615

@@ -148,16 +147,15 @@ func loopOut(ctx *cli.Context) error {
148147
defaultSwapWaitTime)
149148
}
150149

151-
limits := getLimits(amt, quote)
150+
limits := getOutLimits(amt, quote)
152151
// If configured, use the specified maximum swap routing fee.
153152
if ctx.IsSet("max_swap_routing_fee") {
154-
*limits.maxSwapRoutingFee = btcutil.Amount(
153+
limits.maxSwapRoutingFee = btcutil.Amount(
155154
ctx.Int64("max_swap_routing_fee"),
156155
)
157156
}
158-
err = displayLimits(
159-
swap.TypeOut, amt, btcutil.Amount(quote.MinerFee), limits,
160-
false, warning,
157+
err = displayOutLimits(
158+
amt, btcutil.Amount(quote.HtlcSweepFeeSat), limits, warning,
161159
)
162160
if err != nil {
163161
return err
@@ -167,10 +165,10 @@ func loopOut(ctx *cli.Context) error {
167165
Amt: int64(amt),
168166
Dest: destAddr,
169167
MaxMinerFee: int64(limits.maxMinerFee),
170-
MaxPrepayAmt: int64(*limits.maxPrepayAmt),
168+
MaxPrepayAmt: int64(limits.maxPrepayAmt),
171169
MaxSwapFee: int64(limits.maxSwapFee),
172-
MaxPrepayRoutingFee: int64(*limits.maxPrepayRoutingFee),
173-
MaxSwapRoutingFee: int64(*limits.maxSwapRoutingFee),
170+
MaxPrepayRoutingFee: int64(limits.maxPrepayRoutingFee),
171+
MaxSwapRoutingFee: int64(limits.maxSwapRoutingFee),
174172
OutgoingChanSet: outgoingChanSet,
175173
SweepConfTarget: sweepConfTarget,
176174
SwapPublicationDeadline: uint64(swapDeadline.Unix()),

cmd/loop/main.go

Lines changed: 77 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -136,59 +136,65 @@ func getMaxRoutingFee(amt btcutil.Amount) btcutil.Amount {
136136
return swap.CalcFee(amt, maxRoutingFeeBase, maxRoutingFeeRate)
137137
}
138138

139-
type limits struct {
140-
maxSwapRoutingFee *btcutil.Amount
141-
maxPrepayRoutingFee *btcutil.Amount
139+
type inLimits struct {
140+
maxMinerFee btcutil.Amount
141+
maxSwapFee btcutil.Amount
142+
}
143+
144+
func getInLimits(quote *looprpc.InQuoteResponse) *inLimits {
145+
return &inLimits{
146+
// Apply a multiplier to the estimated miner fee, to not get
147+
// the swap canceled because fees increased in the mean time.
148+
maxMinerFee: btcutil.Amount(quote.HtlcPublishFeeSat) * 3,
149+
maxSwapFee: btcutil.Amount(quote.SwapFeeSat),
150+
}
151+
}
152+
153+
type outLimits struct {
154+
maxSwapRoutingFee btcutil.Amount
155+
maxPrepayRoutingFee btcutil.Amount
142156
maxMinerFee btcutil.Amount
143157
maxSwapFee btcutil.Amount
144-
maxPrepayAmt *btcutil.Amount
158+
maxPrepayAmt btcutil.Amount
145159
}
146160

147-
func getLimits(amt btcutil.Amount, quote *looprpc.QuoteResponse) *limits {
161+
func getOutLimits(amt btcutil.Amount,
162+
quote *looprpc.OutQuoteResponse) *outLimits {
163+
148164
maxSwapRoutingFee := getMaxRoutingFee(amt)
149165
maxPrepayRoutingFee := getMaxRoutingFee(btcutil.Amount(
150-
quote.PrepayAmt,
166+
quote.PrepayAmtSat,
151167
))
152-
maxPrepayAmt := btcutil.Amount(quote.PrepayAmt)
168+
maxPrepayAmt := btcutil.Amount(quote.PrepayAmtSat)
153169

154-
return &limits{
155-
maxSwapRoutingFee: &maxSwapRoutingFee,
156-
maxPrepayRoutingFee: &maxPrepayRoutingFee,
170+
return &outLimits{
171+
maxSwapRoutingFee: maxSwapRoutingFee,
172+
maxPrepayRoutingFee: maxPrepayRoutingFee,
157173

158174
// Apply a multiplier to the estimated miner fee, to not get
159175
// the swap canceled because fees increased in the mean time.
160-
maxMinerFee: btcutil.Amount(quote.MinerFee) * 100,
176+
maxMinerFee: btcutil.Amount(quote.HtlcSweepFeeSat) * 100,
161177

162-
maxSwapFee: btcutil.Amount(quote.SwapFee),
163-
maxPrepayAmt: &maxPrepayAmt,
178+
maxSwapFee: btcutil.Amount(quote.SwapFeeSat),
179+
maxPrepayAmt: maxPrepayAmt,
164180
}
165181
}
166182

167-
func displayLimits(swapType swap.Type, amt, minerFees btcutil.Amount, l *limits,
168-
externalHtlc bool, warning string) error {
183+
func displayInLimits(amt, minerFees btcutil.Amount, l *inLimits,
184+
externalHtlc bool) error {
169185

170186
totalSuccessMax := l.maxMinerFee + l.maxSwapFee
171-
if l.maxSwapRoutingFee != nil {
172-
totalSuccessMax += *l.maxSwapRoutingFee
173-
}
174-
if l.maxPrepayRoutingFee != nil {
175-
totalSuccessMax += *l.maxPrepayRoutingFee
176-
}
177187

178-
if swapType == swap.TypeIn && externalHtlc {
188+
if externalHtlc {
179189
fmt.Printf("On-chain fee for external loop in is not " +
180190
"included.\nSufficient fees will need to be paid " +
181191
"when constructing the transaction in the external " +
182192
"wallet.\n\n")
183193
}
184194

185-
fmt.Printf("Max swap fees for %d sat Loop %v: %d sat\n", amt, swapType,
195+
fmt.Printf("Max swap fees for %d sat Loop %v: %d sat\n", amt, swap.TypeIn,
186196
totalSuccessMax)
187197

188-
if warning != "" {
189-
fmt.Println(warning)
190-
}
191-
192198
fmt.Printf("CONTINUE SWAP? (y/n), expand fee detail (x): ")
193199

194200
var answer string
@@ -201,32 +207,55 @@ func displayLimits(swapType swap.Type, amt, minerFees btcutil.Amount, l *limits,
201207
fmt.Println()
202208
f := "%-36s %d sat\n"
203209

204-
switch swapType {
205-
case swap.TypeOut:
206-
fmt.Printf(f, "Estimated on-chain sweep fee:",
210+
if !externalHtlc {
211+
fmt.Printf(f, "Estimated on-chain HTLC fee:",
207212
minerFees)
208-
fmt.Printf(f, "Max on-chain sweep fee:", l.maxMinerFee)
209-
210-
case swap.TypeIn:
211-
if !externalHtlc {
212-
fmt.Printf(f, "Estimated on-chain HTLC fee:",
213-
minerFees)
214-
}
215213
}
216214

217-
if l.maxSwapRoutingFee != nil {
218-
fmt.Printf(f, "Max off-chain swap routing fee:",
219-
*l.maxSwapRoutingFee)
220-
}
215+
fmt.Printf(f, "Max swap fee:", l.maxSwapFee)
221216

222-
if l.maxPrepayAmt != nil {
223-
fmt.Printf(f, "Max no show penalty (prepay):",
224-
*l.maxPrepayAmt)
225-
}
226-
if l.maxPrepayRoutingFee != nil {
227-
fmt.Printf(f, "Max off-chain prepay routing fee:",
228-
*l.maxPrepayRoutingFee)
217+
fmt.Printf("CONTINUE SWAP? (y/n): ")
218+
fmt.Scanln(&answer)
219+
if answer == "y" {
220+
return nil
229221
}
222+
}
223+
224+
return errors.New("swap canceled")
225+
}
226+
227+
func displayOutLimits(amt, minerFees btcutil.Amount, l *outLimits,
228+
warning string) error {
229+
230+
totalSuccessMax := l.maxMinerFee + l.maxSwapFee + l.maxSwapRoutingFee +
231+
l.maxPrepayRoutingFee
232+
233+
fmt.Printf("Max swap fees for %d sat Loop %v: %d sat\n", amt, swap.TypeOut,
234+
totalSuccessMax)
235+
236+
if warning != "" {
237+
fmt.Println(warning)
238+
}
239+
240+
fmt.Printf("CONTINUE SWAP? (y/n), expand fee detail (x): ")
241+
242+
var answer string
243+
fmt.Scanln(&answer)
244+
245+
switch answer {
246+
case "y":
247+
return nil
248+
case "x":
249+
fmt.Println()
250+
f := "%-36s %d sat\n"
251+
252+
fmt.Printf(f, "Estimated on-chain sweep fee:", minerFees)
253+
fmt.Printf(f, "Max on-chain sweep fee:", l.maxMinerFee)
254+
fmt.Printf(f, "Max off-chain swap routing fee:",
255+
l.maxSwapRoutingFee)
256+
fmt.Printf(f, "Max no show penalty (prepay):", l.maxPrepayAmt)
257+
fmt.Printf(f, "Max off-chain prepay routing fee:",
258+
l.maxPrepayRoutingFee)
230259
fmt.Printf(f, "Max swap fee:", l.maxSwapFee)
231260

232261
fmt.Printf("CONTINUE SWAP? (y/n): ")

cmd/loop/quote.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func quoteIn(ctx *cli.Context) error {
5959
// HTLC. If the wallet doesn't have enough funds to create this TX, we
6060
// don't want to fail the quote. But the user should still be informed
6161
// why the fee shows as -1.
62-
if quoteResp.MinerFee == int64(loop.MinerFeeEstimationFailed) {
62+
if quoteResp.HtlcPublishFeeSat == int64(loop.MinerFeeEstimationFailed) {
6363
_, _ = fmt.Fprintf(os.Stderr, "Warning: Miner fee estimation "+
6464
"not possible, lnd has insufficient funds to "+
6565
"create a sample transaction for selected "+

cmd/loop/terms.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ import (
44
"context"
55
"fmt"
66

7-
"github.com/urfave/cli"
8-
97
"github.com/btcsuite/btcutil"
108
"github.com/lightninglabs/loop/looprpc"
9+
"github.com/urfave/cli"
1110
)
1211

1312
var termsCommand = cli.Command{
@@ -23,10 +22,9 @@ func terms(ctx *cli.Context) error {
2322
}
2423
defer cleanup()
2524

26-
printTerms := func(terms *looprpc.TermsResponse) {
25+
printAmountRange := func(min, max int64) {
2726
fmt.Printf("Amount: %d - %d\n",
28-
btcutil.Amount(terms.MinSwapAmount),
29-
btcutil.Amount(terms.MaxSwapAmount),
27+
btcutil.Amount(min), btcutil.Amount(max),
3028
)
3129
}
3230

@@ -37,7 +35,10 @@ func terms(ctx *cli.Context) error {
3735
if err != nil {
3836
fmt.Println(err)
3937
} else {
40-
printTerms(loopOutTerms)
38+
printAmountRange(
39+
loopOutTerms.MinSwapAmount,
40+
loopOutTerms.MaxSwapAmount,
41+
)
4142
}
4243

4344
fmt.Println()
@@ -50,7 +51,10 @@ func terms(ctx *cli.Context) error {
5051
if err != nil {
5152
fmt.Println(err)
5253
} else {
53-
printTerms(loopInTerms)
54+
printAmountRange(
55+
loopInTerms.MinSwapAmount,
56+
loopInTerms.MaxSwapAmount,
57+
)
5458
}
5559

5660
return nil

loopd/swapclient_server.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ func (s *swapClientServer) SwapInfo(_ context.Context,
328328

329329
// LoopOutTerms returns the terms that the server enforces for loop out swaps.
330330
func (s *swapClientServer) LoopOutTerms(ctx context.Context,
331-
req *looprpc.TermsRequest) (*looprpc.TermsResponse, error) {
331+
req *looprpc.TermsRequest) (*looprpc.OutTermsResponse, error) {
332332

333333
log.Infof("Loop out terms request received")
334334

@@ -338,7 +338,7 @@ func (s *swapClientServer) LoopOutTerms(ctx context.Context,
338338
return nil, err
339339
}
340340

341-
return &looprpc.TermsResponse{
341+
return &looprpc.OutTermsResponse{
342342
MinSwapAmount: int64(terms.MinSwapAmount),
343343
MaxSwapAmount: int64(terms.MaxSwapAmount),
344344
}, nil
@@ -347,7 +347,7 @@ func (s *swapClientServer) LoopOutTerms(ctx context.Context,
347347
// LoopOutQuote returns a quote for a loop out swap with the provided
348348
// parameters.
349349
func (s *swapClientServer) LoopOutQuote(ctx context.Context,
350-
req *looprpc.QuoteRequest) (*looprpc.QuoteResponse, error) {
350+
req *looprpc.QuoteRequest) (*looprpc.OutQuoteResponse, error) {
351351

352352
confTarget, err := validateConfTarget(
353353
req.ConfTarget, loop.DefaultSweepConfTarget,
@@ -366,18 +366,18 @@ func (s *swapClientServer) LoopOutQuote(ctx context.Context,
366366
return nil, err
367367
}
368368

369-
return &looprpc.QuoteResponse{
370-
MinerFee: int64(quote.MinerFee),
371-
PrepayAmt: int64(quote.PrepayAmount),
372-
SwapFee: int64(quote.SwapFee),
369+
return &looprpc.OutQuoteResponse{
370+
HtlcSweepFeeSat: int64(quote.MinerFee),
371+
PrepayAmtSat: int64(quote.PrepayAmount),
372+
SwapFeeSat: int64(quote.SwapFee),
373373
SwapPaymentDest: quote.SwapPaymentDest[:],
374374
CltvDelta: quote.CltvDelta,
375375
}, nil
376376
}
377377

378378
// GetTerms returns the terms that the server enforces for swaps.
379-
func (s *swapClientServer) GetLoopInTerms(ctx context.Context, req *looprpc.TermsRequest) (
380-
*looprpc.TermsResponse, error) {
379+
func (s *swapClientServer) GetLoopInTerms(ctx context.Context,
380+
req *looprpc.TermsRequest) (*looprpc.InTermsResponse, error) {
381381

382382
log.Infof("Loop in terms request received")
383383

@@ -387,15 +387,15 @@ func (s *swapClientServer) GetLoopInTerms(ctx context.Context, req *looprpc.Term
387387
return nil, err
388388
}
389389

390-
return &looprpc.TermsResponse{
390+
return &looprpc.InTermsResponse{
391391
MinSwapAmount: int64(terms.MinSwapAmount),
392392
MaxSwapAmount: int64(terms.MaxSwapAmount),
393393
}, nil
394394
}
395395

396396
// GetQuote returns a quote for a swap with the provided parameters.
397397
func (s *swapClientServer) GetLoopInQuote(ctx context.Context,
398-
req *looprpc.QuoteRequest) (*looprpc.QuoteResponse, error) {
398+
req *looprpc.QuoteRequest) (*looprpc.InQuoteResponse, error) {
399399

400400
log.Infof("Loop in quote request received")
401401

@@ -414,9 +414,9 @@ func (s *swapClientServer) GetLoopInQuote(ctx context.Context,
414414
if err != nil {
415415
return nil, err
416416
}
417-
return &looprpc.QuoteResponse{
418-
MinerFee: int64(quote.MinerFee),
419-
SwapFee: int64(quote.SwapFee),
417+
return &looprpc.InQuoteResponse{
418+
HtlcPublishFeeSat: int64(quote.MinerFee),
419+
SwapFeeSat: int64(quote.SwapFee),
420420
}, nil
421421
}
422422

0 commit comments

Comments
 (0)