Skip to content

Commit 9039e5f

Browse files
authored
Merge pull request #88 from halseth/proto-quote-amount
server proto: add amt to ServerLoopOutQuoteRequest
2 parents 556845e + 0023d1a commit 9039e5f

File tree

12 files changed

+585
-328
lines changed

12 files changed

+585
-328
lines changed

client.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -353,11 +353,14 @@ func (s *Client) LoopOutQuote(ctx context.Context,
353353
return nil, ErrSwapAmountTooHigh
354354
}
355355

356-
logger.Infof("Offchain swap destination: %x", terms.SwapPaymentDest)
356+
quote, err := s.Server.GetLoopOutQuote(ctx, request.Amount)
357+
if err != nil {
358+
return nil, err
359+
}
357360

358-
swapFee := swap.CalcFee(
359-
request.Amount, terms.SwapFeeBase, terms.SwapFeeRate,
360-
)
361+
logger.Infof("Offchain swap destination: %x", quote.SwapPaymentDest)
362+
363+
swapFee := quote.SwapFee
361364

362365
// Generate dummy p2wsh address for fee estimation. The p2wsh address
363366
// type is chosen because it adds the most weight of all output types
@@ -379,9 +382,11 @@ func (s *Client) LoopOutQuote(ctx context.Context,
379382
}
380383

381384
return &LoopOutQuote{
382-
SwapFee: swapFee,
383-
MinerFee: minerFee,
384-
PrepayAmount: btcutil.Amount(terms.PrepayAmt),
385+
SwapFee: swapFee,
386+
MinerFee: minerFee,
387+
PrepayAmount: btcutil.Amount(quote.PrepayAmount),
388+
SwapPaymentDest: quote.SwapPaymentDest,
389+
CltvDelta: quote.CltvDelta,
385390
}, nil
386391
}
387392

@@ -465,10 +470,12 @@ func (s *Client) LoopInQuote(ctx context.Context,
465470
return nil, ErrSwapAmountTooHigh
466471
}
467472

468-
// Calculate swap fee.
469-
swapFee := terms.SwapFeeBase +
470-
request.Amount*btcutil.Amount(terms.SwapFeeRate)/
471-
btcutil.Amount(swap.FeeRateTotalParts)
473+
quote, err := s.Server.GetLoopInQuote(ctx, request.Amount)
474+
if err != nil {
475+
return nil, err
476+
}
477+
478+
swapFee := quote.SwapFee
472479

473480
// We don't calculate the on-chain fee if the HTLC is going to be
474481
// published externally.
@@ -478,7 +485,7 @@ func (s *Client) LoopInQuote(ctx context.Context,
478485
MinerFee: 0,
479486
}, nil
480487
}
481-
488+
482489
// Get estimate for miner fee.
483490
minerFee, err := s.lndServices.Client.EstimateFeeToP2WSH(
484491
ctx, request.Amount, request.HtlcConfTarget,
@@ -488,8 +495,9 @@ func (s *Client) LoopInQuote(ctx context.Context,
488495
}
489496

490497
return &LoopInQuote{
491-
SwapFee: swapFee,
492-
MinerFee: minerFee,
498+
SwapFee: swapFee,
499+
MinerFee: minerFee,
500+
CltvDelta: quote.CltvDelta,
493501
}, nil
494502
}
495503

cmd/loop/terms.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
"github.com/btcsuite/btcutil"
1010
"github.com/lightninglabs/loop/looprpc"
11-
"github.com/lightninglabs/loop/swap"
1211
)
1312

1413
var termsCommand = cli.Command{
@@ -29,13 +28,6 @@ func terms(ctx *cli.Context) error {
2928
btcutil.Amount(terms.MinSwapAmount),
3029
btcutil.Amount(terms.MaxSwapAmount),
3130
)
32-
fmt.Printf("Fee: %d + %.4f %% (%d prepaid)\n",
33-
btcutil.Amount(terms.SwapFeeBase),
34-
swap.FeeRateAsPercentage(terms.SwapFeeRate),
35-
btcutil.Amount(terms.PrepayAmt),
36-
)
37-
38-
fmt.Printf("Cltv delta: %v blocks\n", terms.CltvDelta)
3931
}
4032

4133
fmt.Println("Loop Out")

cmd/loopd/swapclient_server.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,6 @@ func (s *swapClientServer) LoopOutTerms(ctx context.Context,
244244
return &looprpc.TermsResponse{
245245
MinSwapAmount: int64(terms.MinSwapAmount),
246246
MaxSwapAmount: int64(terms.MaxSwapAmount),
247-
PrepayAmt: int64(terms.PrepayAmt),
248-
SwapFeeBase: int64(terms.SwapFeeBase),
249-
SwapFeeRate: int64(terms.SwapFeeRate),
250-
CltvDelta: int32(terms.CltvDelta),
251247
}, nil
252248
}
253249

@@ -269,10 +265,13 @@ func (s *swapClientServer) LoopOutQuote(ctx context.Context,
269265
if err != nil {
270266
return nil, err
271267
}
268+
272269
return &looprpc.QuoteResponse{
273-
MinerFee: int64(quote.MinerFee),
274-
PrepayAmt: int64(quote.PrepayAmount),
275-
SwapFee: int64(quote.SwapFee),
270+
MinerFee: int64(quote.MinerFee),
271+
PrepayAmt: int64(quote.PrepayAmount),
272+
SwapFee: int64(quote.SwapFee),
273+
SwapPaymentDest: quote.SwapPaymentDest[:],
274+
CltvDelta: quote.CltvDelta,
276275
}, nil
277276
}
278277

@@ -291,9 +290,6 @@ func (s *swapClientServer) GetLoopInTerms(ctx context.Context, req *looprpc.Term
291290
return &looprpc.TermsResponse{
292291
MinSwapAmount: int64(terms.MinSwapAmount),
293292
MaxSwapAmount: int64(terms.MaxSwapAmount),
294-
SwapFeeBase: int64(terms.SwapFeeBase),
295-
SwapFeeRate: int64(terms.SwapFeeRate),
296-
CltvDelta: int32(terms.CltvDelta),
297293
}, nil
298294
}
299295

interface.go

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@ type LoopOutQuoteRequest struct {
105105
// final cltv delta values for the off-chain payments.
106106
}
107107

108+
// LoopOutTerms are the server terms on which it executes swaps.
109+
type LoopOutTerms struct {
110+
// MinSwapAmount is the minimum amount that the server requires for a
111+
// swap.
112+
MinSwapAmount btcutil.Amount
113+
114+
// MaxSwapAmount is the maximum amount that the server accepts for a
115+
// swap.
116+
MaxSwapAmount btcutil.Amount
117+
}
118+
108119
// LoopOutQuote contains estimates for the fees making up the total swap cost
109120
// for the client.
110121
type LoopOutQuote struct {
@@ -118,27 +129,6 @@ type LoopOutQuote struct {
118129
// MinerFee is an estimate of the on-chain fee that needs to be paid to
119130
// sweep the htlc.
120131
MinerFee btcutil.Amount
121-
}
122-
123-
// LoopOutTerms are the server terms on which it executes swaps.
124-
type LoopOutTerms struct {
125-
// SwapFeeBase is the fixed per-swap base fee.
126-
SwapFeeBase btcutil.Amount
127-
128-
// SwapFeeRate is the variable fee in parts per million.
129-
SwapFeeRate int64
130-
131-
// PrepayAmt is the fixed part of the swap fee that needs to be
132-
// prepaid.
133-
PrepayAmt btcutil.Amount
134-
135-
// MinSwapAmount is the minimum amount that the server requires for a
136-
// swap.
137-
MinSwapAmount btcutil.Amount
138-
139-
// MaxSwapAmount is the maximum amount that the server accepts for a
140-
// swap.
141-
MaxSwapAmount btcutil.Amount
142132

143133
// Time lock delta relative to current block height that swap server
144134
// will accept on the swap initiation call.
@@ -185,23 +175,13 @@ type LoopInRequest struct {
185175

186176
// LoopInTerms are the server terms on which it executes charge swaps.
187177
type LoopInTerms struct {
188-
// SwapFeeBase is the fixed per-swap base fee.
189-
SwapFeeBase btcutil.Amount
190-
191-
// SwapFeeRate is the variable fee in parts per million.
192-
SwapFeeRate int64
193-
194178
// MinSwapAmount is the minimum amount that the server requires for a
195179
// swap.
196180
MinSwapAmount btcutil.Amount
197181

198182
// MaxSwapAmount is the maximum amount that the server accepts for a
199183
// swap.
200184
MaxSwapAmount btcutil.Amount
201-
202-
// Time lock delta relative to current block height that swap server
203-
// will accept on the swap initiation call.
204-
CltvDelta int32
205185
}
206186

207187
// In contains status information for a loop in swap.
@@ -239,6 +219,10 @@ type LoopInQuote struct {
239219
// MinerFee is an estimate of the on-chain fee that needs to be paid to
240220
// sweep the htlc.
241221
MinerFee btcutil.Amount
222+
223+
// Time lock delta relative to current block height that swap server
224+
// will accept on the swap initiation call.
225+
CltvDelta int32
242226
}
243227

244228
// SwapInfoKit contains common swap info fields.

loopin.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,12 @@ func newLoopInSwap(globalCtx context.Context, cfg *swapConfig,
6060
// Request current server loop in terms and use these to calculate the
6161
// swap fee that we should subtract from the swap amount in the payment
6262
// request that we send to the server.
63-
quote, err := cfg.server.GetLoopInTerms(globalCtx)
63+
quote, err := cfg.server.GetLoopInQuote(globalCtx, request.Amount)
6464
if err != nil {
6565
return nil, fmt.Errorf("loop in terms: %v", err)
6666
}
6767

68-
swapFee := swap.CalcFee(
69-
request.Amount, quote.SwapFeeBase, quote.SwapFeeRate,
70-
)
68+
swapFee := quote.SwapFee
7169

7270
if swapFee > request.MaxSwapFee {
7371
logger.Warnf("Swap fee %v exceeding maximum of %v",

0 commit comments

Comments
 (0)