Skip to content

Commit bf59159

Browse files
committed
client: LoopOutQuote to support estimate using v3 htlc
1 parent 391ef57 commit bf59159

File tree

2 files changed

+48
-21
lines changed

2 files changed

+48
-21
lines changed

client.go

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,23 @@ func (s *Client) LoopOutQuote(ctx context.Context,
499499

500500
log.Infof("Offchain swap destination: %x", quote.SwapPaymentDest)
501501

502-
swapFee := quote.SwapFee
502+
minerFee, err := s.getLoopOutSweepFee(ctx, request.SweepConfTarget)
503+
if err != nil {
504+
return nil, err
505+
}
506+
507+
return &LoopOutQuote{
508+
SwapFee: quote.SwapFee,
509+
MinerFee: minerFee,
510+
PrepayAmount: quote.PrepayAmount,
511+
SwapPaymentDest: quote.SwapPaymentDest,
512+
}, nil
513+
}
514+
515+
// getLoopOutSweepFee is a helper method to estimate the loop out htlc sweep
516+
// fee to a p2wsh address.
517+
func (s *Client) getLoopOutSweepFee(ctx context.Context, confTarget int32) (
518+
btcutil.Amount, error) {
503519

504520
// Generate dummy p2wsh address for fee estimation. The p2wsh address
505521
// type is chosen because it adds the most weight of all output types
@@ -509,23 +525,21 @@ func (s *Client) LoopOutQuote(ctx context.Context,
509525
wsh[:], s.lndServices.ChainParams,
510526
)
511527
if err != nil {
512-
return nil, err
528+
return 0, err
513529
}
514530

515-
minerFee, err := s.sweeper.GetSweepFee(
516-
ctx, swap.QuoteHtlc.AddSuccessToEstimator,
517-
p2wshAddress, request.SweepConfTarget,
531+
scriptVersion := GetHtlcScriptVersion(
532+
loopdb.CurrentProtocolVersion(),
518533
)
519-
if err != nil {
520-
return nil, err
534+
535+
htlc := swap.QuoteHtlcP2TR
536+
if scriptVersion != swap.HtlcV3 {
537+
htlc = swap.QuoteHtlcP2WSH
521538
}
522539

523-
return &LoopOutQuote{
524-
SwapFee: swapFee,
525-
MinerFee: minerFee,
526-
PrepayAmount: quote.PrepayAmount,
527-
SwapPaymentDest: quote.SwapPaymentDest,
528-
}, nil
540+
return s.sweeper.GetSweepFee(
541+
ctx, htlc.AddSuccessToEstimator, p2wshAddress, confTarget,
542+
)
529543
}
530544

531545
// LoopOutTerms returns the terms on which the server executes swaps.

swap/htlc.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,30 @@ type Htlc struct {
108108
}
109109

110110
var (
111-
quoteKey [33]byte
111+
// dummyPubKey is a valid public key use for the quote htlc
112+
// construction.
113+
dummyPubKey = [33]byte{
114+
0x03, 0x26, 0x89, 0xc7, 0xc2, 0xda, 0xb1, 0x33, 0x09, 0xfb,
115+
0x14, 0x3e, 0x0e, 0x8f, 0xe3, 0x96, 0x34, 0x25, 0x21, 0x88,
116+
0x7e, 0x97, 0x66, 0x90, 0xb6, 0xb4, 0x7f, 0x5b, 0x2a, 0x4b,
117+
0x7d, 0x44, 0x8e,
118+
}
112119

120+
// quoteHash is an empty hash used for the quote htlc construction.
113121
quoteHash lntypes.Hash
114122

115-
// QuoteHtlc is a template script just used for fee estimation. It uses
116-
// the maximum value for cltv expiry to get the maximum (worst case)
117-
// script size.
118-
QuoteHtlc, _ = NewHtlc(
119-
HtlcV2,
120-
^int32(0), quoteKey, quoteKey, quoteHash, HtlcP2WSH,
121-
&chaincfg.MainNetParams,
123+
// QuoteHtlcP2WSH is a template script just used for sweep fee
124+
// estimation.
125+
QuoteHtlcP2WSH, _ = NewHtlc(
126+
HtlcV2, ^int32(0), dummyPubKey, dummyPubKey, quoteHash,
127+
HtlcP2WSH, &chaincfg.MainNetParams,
128+
)
129+
130+
// QuoteHtlcP2TR is a template script just used for sweep fee
131+
// estimation.
132+
QuoteHtlcP2TR, _ = NewHtlc(
133+
HtlcV3, ^int32(0), dummyPubKey, dummyPubKey, quoteHash,
134+
HtlcP2TR, &chaincfg.MainNetParams,
122135
)
123136

124137
// ErrInvalidScriptVersion is returned when an unknown htlc version

0 commit comments

Comments
 (0)