Skip to content

Commit 019d3c6

Browse files
committed
sweep: factor out function AddOutputEstimate
It adds output to transaction weight estimator depending on address type.
1 parent e814157 commit 019d3c6

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

sweep/sweeper.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
229-
}
230-
231-
err = addInputEstimate(&weightEstimate)
232-
if err != nil {
233-
return 0, 0, 0, err
251+
return fmt.Errorf("unknown address type %T", destAddr)
234252
}
235253

236-
weight := weightEstimate.Weight()
237-
238-
return feeRate.FeeForWeight(weight), feeRate, weight, nil
254+
return nil
239255
}

0 commit comments

Comments
 (0)