@@ -38,10 +38,14 @@ const (
38
38
// sweep fees, (750 * 4 /1000 = 3 sat/vByte).
39
39
defaultSweepFeeRateLimit = chainfee .SatPerKWeight (750 )
40
40
41
- // minerMultiplier is a multiplier we use to scale our miner fee to
42
- // ensure that we will still be able to complete our swap in the case
43
- // of a severe fee spike.
44
- minerMultiplier = 100
41
+ // minerMultiplier is a multiplier we use to predict the average chain
42
+ // costs towards miner fees.
43
+ minerMultiplier = 2
44
+
45
+ // maxMinerMultiplier is the maximum multiplier we use to scale our
46
+ // miner fee to ensure that we will still be able to complete our swap
47
+ // in the case of a severe fee spike.
48
+ maxMinerMultiplier = 50
45
49
46
50
// defaultFeePPM is the default percentage of swap amount that we
47
51
// allocate to fees, 2%.
@@ -341,6 +345,12 @@ func (f *FeePortion) loopOutLimits(swapAmt btcutil.Amount,
341
345
342
346
prepay , route , miner := f .loopOutFees (swapAmt , quote )
343
347
348
+ // Before checking our fees against our budget we remove the large
349
+ // multiplier from the miner fees. We do this because we want to
350
+ // consider the average case for our budget calculations and not the
351
+ // severe edge-case miner fees.
352
+ miner = miner / maxMinerMultiplier
353
+
344
354
// Calculate the worst case fees that we could pay for this swap,
345
355
// ensuring that we are within our fee limit even if the swap fails.
346
356
fees := worstCaseOutFees (
@@ -370,6 +380,8 @@ func (f *FeePortion) loopOutFees(amount btcutil.Amount,
370
380
// amounts provided by the quote to get the total available for
371
381
// off-chain fees.
372
382
feeLimit := ppmToSat (amount , f .PartsPerMillion )
383
+
384
+ // Apply the small miner multiplier for the fee budget calculations.
373
385
minerFee := scaleMinerFee (quote .MinerFee )
374
386
375
387
available := feeLimit - minerFee - quote .SwapFee
@@ -378,6 +390,9 @@ func (f *FeePortion) loopOutFees(amount btcutil.Amount,
378
390
available , quote .PrepayAmount , amount ,
379
391
)
380
392
393
+ // Apply the big miner multiplier to get the worst case miner fees.
394
+ minerFee = scaleMaxMinerFee (minerFee )
395
+
381
396
return prepayMaxFee , routeMaxFee , minerFee
382
397
}
383
398
@@ -394,11 +409,20 @@ func splitOffChain(available, prepayAmt,
394
409
return prepayMaxFee , routeMaxFee
395
410
}
396
411
397
- // scaleMinerFee scales our miner fee by our constant multiplier.
412
+ // scaleMinerFee scales our miner fee by a smaller multiplier. This scale does
413
+ // not represent the worst-case maximum miner fees, but the average expected
414
+ // fees.
398
415
func scaleMinerFee (estimate btcutil.Amount ) btcutil.Amount {
399
416
return estimate * btcutil .Amount (minerMultiplier )
400
417
}
401
418
419
+ // scaleMaxMinerFee scales our miner fee by a big multiplier. The returned value
420
+ // represents the maximum amount that we consider spending for miner fees in
421
+ // worst-case scenarios (fee-spikes).
422
+ func scaleMaxMinerFee (estimate btcutil.Amount ) btcutil.Amount {
423
+ return estimate * btcutil .Amount (maxMinerMultiplier )
424
+ }
425
+
402
426
func (f * FeePortion ) loopInLimits (amount btcutil.Amount ,
403
427
quote * loop.LoopInQuote ) error {
404
428
0 commit comments