diff --git a/client.go b/client.go index 8814f87..8e48bae 100644 --- a/client.go +++ b/client.go @@ -359,9 +359,20 @@ func (s *Client) LoopOutQuote(ctx context.Context, request.Amount, terms.SwapFeeBase, terms.SwapFeeRate, ) + // Generate dummy p2wsh address for fee estimation. The p2wsh address + // type is chosen because it adds the most weight of all output types + // and we want the quote to return a worst case value. + wsh := [32]byte{} + p2wshAddress, err := btcutil.NewAddressWitnessScriptHash( + wsh[:], s.lndServices.ChainParams, + ) + if err != nil { + return nil, err + } + minerFee, err := s.sweeper.GetSweepFee( ctx, swap.QuoteHtlc.AddSuccessToEstimator, - request.SweepConfTarget, + p2wshAddress, request.SweepConfTarget, ) if err != nil { return nil, err diff --git a/loopin.go b/loopin.go index cc9a9f9..e7874a1 100644 --- a/loopin.go +++ b/loopin.go @@ -584,7 +584,8 @@ func (s *loopInSwap) publishTimeoutTx(ctx context.Context, // Calculate sweep tx fee fee, err := s.sweeper.GetSweepFee( - ctx, s.htlc.AddTimeoutToEstimator, TimeoutTxConfTarget, + ctx, s.htlc.AddTimeoutToEstimator, s.timeoutAddr, + TimeoutTxConfTarget, ) if err != nil { return err diff --git a/loopout.go b/loopout.go index 2b95a42..9479151 100644 --- a/loopout.go +++ b/loopout.go @@ -602,7 +602,7 @@ func (s *loopOutSwap) sweep(ctx context.Context, confTarget = DefaultSweepConfTarget } fee, err := s.sweeper.GetSweepFee( - ctx, s.htlc.AddSuccessToEstimator, confTarget, + ctx, s.htlc.AddSuccessToEstimator, s.DestAddr, confTarget, ) if err != nil { return err diff --git a/sweep/sweeper.go b/sweep/sweeper.go index 42f5338..93d6007 100644 --- a/sweep/sweeper.go +++ b/sweep/sweeper.go @@ -91,7 +91,7 @@ func (s *Sweeper) CreateSweepTx( // estimator. func (s *Sweeper) GetSweepFee(ctx context.Context, addInputEstimate func(*input.TxWeightEstimator), - sweepConfTarget int32) ( + destAddr btcutil.Address, sweepConfTarget int32) ( btcutil.Amount, error) { // Get fee estimate from lnd. @@ -102,7 +102,19 @@ func (s *Sweeper) GetSweepFee(ctx context.Context, // Calculate weight for this tx. var weightEstimate input.TxWeightEstimator - weightEstimate.AddP2WKHOutput() + switch destAddr.(type) { + case *btcutil.AddressWitnessScriptHash: + weightEstimate.AddP2WSHOutput() + case *btcutil.AddressWitnessPubKeyHash: + weightEstimate.AddP2WKHOutput() + case *btcutil.AddressScriptHash: + weightEstimate.AddP2SHOutput() + case *btcutil.AddressPubKeyHash: + weightEstimate.AddP2PKHOutput() + default: + return 0, fmt.Errorf("unknown adress type %T", destAddr) + } + addInputEstimate(&weightEstimate) weight := weightEstimate.Weight()