@@ -191,8 +191,8 @@ func (s *Sweeper) GetSweepFee(ctx context.Context,
191191 return fee , err
192192}
193193
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
194+ // GetSweepFeeDetails calculates the required tx fee to spend to P2WKH. It takes
195+ // a function that is expected to add the weight of the input to the weight
196196// estimator. It returns also the fee rate and transaction weight.
197197func (s * Sweeper ) GetSweepFeeDetails (ctx context.Context ,
198198 addInputEstimate func (* input.TxWeightEstimator ) error ,
@@ -207,6 +207,30 @@ func (s *Sweeper) GetSweepFeeDetails(ctx context.Context,
207207
208208 // Calculate weight for this tx.
209209 var weightEstimate input.TxWeightEstimator
210+
211+ // Add output.
212+ if err := AddOutputEstimate (& weightEstimate , destAddr ); err != nil {
213+ return 0 , 0 , 0 , fmt .Errorf ("failed to add output weight " +
214+ "estimate: %w" , err )
215+ }
216+
217+ // Add input.
218+ err = addInputEstimate (& weightEstimate )
219+ if err != nil {
220+ return 0 , 0 , 0 , fmt .Errorf ("failed to add input weight " +
221+ "estimate: %w" , err )
222+ }
223+
224+ // Find weight.
225+ weight := weightEstimate .Weight ()
226+
227+ return feeRate .FeeForWeight (weight ), feeRate , weight , nil
228+ }
229+
230+ // AddOutputEstimate adds output to weight estimator.
231+ func AddOutputEstimate (weightEstimate * input.TxWeightEstimator ,
232+ destAddr btcutil.Address ) error {
233+
210234 switch destAddr .(type ) {
211235 case * btcutil.AddressWitnessScriptHash :
212236 weightEstimate .AddP2WSHOutput ()
@@ -224,16 +248,8 @@ func (s *Sweeper) GetSweepFeeDetails(ctx context.Context,
224248 weightEstimate .AddP2TROutput ()
225249
226250 default :
227- return 0 , 0 , 0 , fmt .Errorf ("estimate fee: unknown address " +
228- "type %T" , destAddr )
251+ return fmt .Errorf ("unknown address type %T" , destAddr )
229252 }
230253
231- err = addInputEstimate (& weightEstimate )
232- if err != nil {
233- return 0 , 0 , 0 , err
234- }
235-
236- weight := weightEstimate .Weight ()
237-
238- return feeRate .FeeForWeight (weight ), feeRate , weight , nil
254+ return nil
239255}
0 commit comments