Skip to content

Commit 6608337

Browse files
authored
Merge pull request #71 from joostjager/address-estimate
multi: base sweep fee estimate on actually used address type
2 parents 9924583 + ecd36b9 commit 6608337

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

client.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,9 +359,20 @@ func (s *Client) LoopOutQuote(ctx context.Context,
359359
request.Amount, terms.SwapFeeBase, terms.SwapFeeRate,
360360
)
361361

362+
// Generate dummy p2wsh address for fee estimation. The p2wsh address
363+
// type is chosen because it adds the most weight of all output types
364+
// and we want the quote to return a worst case value.
365+
wsh := [32]byte{}
366+
p2wshAddress, err := btcutil.NewAddressWitnessScriptHash(
367+
wsh[:], s.lndServices.ChainParams,
368+
)
369+
if err != nil {
370+
return nil, err
371+
}
372+
362373
minerFee, err := s.sweeper.GetSweepFee(
363374
ctx, swap.QuoteHtlc.AddSuccessToEstimator,
364-
request.SweepConfTarget,
375+
p2wshAddress, request.SweepConfTarget,
365376
)
366377
if err != nil {
367378
return nil, err

loopin.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,8 @@ func (s *loopInSwap) publishTimeoutTx(ctx context.Context,
584584

585585
// Calculate sweep tx fee
586586
fee, err := s.sweeper.GetSweepFee(
587-
ctx, s.htlc.AddTimeoutToEstimator, TimeoutTxConfTarget,
587+
ctx, s.htlc.AddTimeoutToEstimator, s.timeoutAddr,
588+
TimeoutTxConfTarget,
588589
)
589590
if err != nil {
590591
return err

loopout.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var (
3131
// confirmation target.
3232
//
3333
// TODO(wilmer): tune?
34-
DefaultSweepConfTargetDelta int32 = DefaultSweepConfTarget * 2
34+
DefaultSweepConfTargetDelta = DefaultSweepConfTarget * 2
3535
)
3636

3737
// loopOutSwap contains all the in-memory state related to a pending loop out
@@ -602,7 +602,7 @@ func (s *loopOutSwap) sweep(ctx context.Context,
602602
confTarget = DefaultSweepConfTarget
603603
}
604604
fee, err := s.sweeper.GetSweepFee(
605-
ctx, s.htlc.AddSuccessToEstimator, confTarget,
605+
ctx, s.htlc.AddSuccessToEstimator, s.DestAddr, confTarget,
606606
)
607607
if err != nil {
608608
return err

sweep/sweeper.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (s *Sweeper) CreateSweepTx(
9191
// estimator.
9292
func (s *Sweeper) GetSweepFee(ctx context.Context,
9393
addInputEstimate func(*input.TxWeightEstimator),
94-
sweepConfTarget int32) (
94+
destAddr btcutil.Address, sweepConfTarget int32) (
9595
btcutil.Amount, error) {
9696

9797
// Get fee estimate from lnd.
@@ -102,7 +102,19 @@ func (s *Sweeper) GetSweepFee(ctx context.Context,
102102

103103
// Calculate weight for this tx.
104104
var weightEstimate input.TxWeightEstimator
105-
weightEstimate.AddP2WKHOutput()
105+
switch destAddr.(type) {
106+
case *btcutil.AddressWitnessScriptHash:
107+
weightEstimate.AddP2WSHOutput()
108+
case *btcutil.AddressWitnessPubKeyHash:
109+
weightEstimate.AddP2WKHOutput()
110+
case *btcutil.AddressScriptHash:
111+
weightEstimate.AddP2SHOutput()
112+
case *btcutil.AddressPubKeyHash:
113+
weightEstimate.AddP2PKHOutput()
114+
default:
115+
return 0, fmt.Errorf("unknown adress type %T", destAddr)
116+
}
117+
106118
addInputEstimate(&weightEstimate)
107119
weight := weightEstimate.Weight()
108120

0 commit comments

Comments
 (0)