@@ -38,10 +38,14 @@ const (
3838 // sweep fees, (750 * 4 /1000 = 3 sat/vByte).
3939 defaultSweepFeeRateLimit = chainfee .SatPerKWeight (750 )
4040
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
4549
4650 // defaultFeePPM is the default percentage of swap amount that we
4751 // allocate to fees, 2%.
@@ -341,6 +345,12 @@ func (f *FeePortion) loopOutLimits(swapAmt btcutil.Amount,
341345
342346 prepay , route , miner := f .loopOutFees (swapAmt , quote )
343347
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+
344354 // Calculate the worst case fees that we could pay for this swap,
345355 // ensuring that we are within our fee limit even if the swap fails.
346356 fees := worstCaseOutFees (
@@ -370,6 +380,8 @@ func (f *FeePortion) loopOutFees(amount btcutil.Amount,
370380 // amounts provided by the quote to get the total available for
371381 // off-chain fees.
372382 feeLimit := ppmToSat (amount , f .PartsPerMillion )
383+
384+ // Apply the small miner multiplier for the fee budget calculations.
373385 minerFee := scaleMinerFee (quote .MinerFee )
374386
375387 available := feeLimit - minerFee - quote .SwapFee
@@ -378,6 +390,9 @@ func (f *FeePortion) loopOutFees(amount btcutil.Amount,
378390 available , quote .PrepayAmount , amount ,
379391 )
380392
393+ // Apply the big miner multiplier to get the worst case miner fees.
394+ minerFee = scaleMaxMinerFee (minerFee )
395+
381396 return prepayMaxFee , routeMaxFee , minerFee
382397}
383398
@@ -394,11 +409,20 @@ func splitOffChain(available, prepayAmt,
394409 return prepayMaxFee , routeMaxFee
395410}
396411
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.
398415func scaleMinerFee (estimate btcutil.Amount ) btcutil.Amount {
399416 return estimate * btcutil .Amount (minerMultiplier )
400417}
401418
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+
402426func (f * FeePortion ) loopInLimits (amount btcutil.Amount ,
403427 quote * loop.LoopInQuote ) error {
404428
0 commit comments