@@ -14,6 +14,8 @@ import (
1414 "github.com/lightninglabs/loop/swap"
1515 "github.com/lightningnetwork/lnd/input"
1616 "github.com/lightningnetwork/lnd/keychain"
17+ "github.com/lightningnetwork/lnd/lntypes"
18+ "github.com/lightningnetwork/lnd/lnwallet/chainfee"
1719)
1820
1921// Sweeper creates htlc sweep txes.
@@ -181,10 +183,26 @@ func (s *Sweeper) GetSweepFee(ctx context.Context,
181183 destAddr btcutil.Address , sweepConfTarget int32 ) (
182184 btcutil.Amount , error ) {
183185
186+ // Use GetSweepFeeDetails to get the fee and other unused data.
187+ fee , _ , _ , err := s .GetSweepFeeDetails (
188+ ctx , addInputEstimate , destAddr , sweepConfTarget ,
189+ )
190+
191+ return fee , err
192+ }
193+
194+ // GetSweepFee calculates the required tx fee to spend to P2WKH. It takes a
195+ // function that is expected to add the weight of the input to the weight
196+ // estimator. It returns also the fee rate and transaction weight.
197+ func (s * Sweeper ) GetSweepFeeDetails (ctx context.Context ,
198+ addInputEstimate func (* input.TxWeightEstimator ) error ,
199+ destAddr btcutil.Address , sweepConfTarget int32 ) (
200+ btcutil.Amount , chainfee.SatPerKWeight , lntypes.WeightUnit , error ) {
201+
184202 // Get fee estimate from lnd.
185203 feeRate , err := s .Lnd .WalletKit .EstimateFeeRate (ctx , sweepConfTarget )
186204 if err != nil {
187- return 0 , fmt .Errorf ("estimate fee: %v" , err )
205+ return 0 , 0 , 0 , fmt .Errorf ("estimate fee: %v" , err )
188206 }
189207
190208 // Calculate weight for this tx.
@@ -206,16 +224,16 @@ func (s *Sweeper) GetSweepFee(ctx context.Context,
206224 weightEstimate .AddP2TROutput ()
207225
208226 default :
209- return 0 , fmt .Errorf ("estimate fee: unknown address type %T" ,
210- destAddr )
227+ return 0 , 0 , 0 , fmt .Errorf ("estimate fee: unknown address " +
228+ "type %T" , destAddr )
211229 }
212230
213231 err = addInputEstimate (& weightEstimate )
214232 if err != nil {
215- return 0 , err
233+ return 0 , 0 , 0 , err
216234 }
217235
218236 weight := weightEstimate .Weight ()
219237
220- return feeRate .FeeForWeight (weight ), nil
238+ return feeRate .FeeForWeight (weight ), feeRate , weight , nil
221239}
0 commit comments