Skip to content

Commit 0f0be28

Browse files
committed
liquidity: add loop in fees to fee categories
1 parent c067169 commit 0f0be28

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

liquidity/fees.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,27 @@ func (f *FeeCategoryLimit) loopOutLimits(amount btcutil.Amount,
223223
return nil
224224
}
225225

226+
func (f *FeeCategoryLimit) loopInLimits(amount btcutil.Amount,
227+
quote *loop.LoopInQuote) error {
228+
229+
maxServerFee := ppmToSat(amount, f.MaximumSwapFeePPM)
230+
if quote.SwapFee > maxServerFee {
231+
log.Debugf("quoted swap fee: %v > maximum swap fee: %v",
232+
quote.SwapFee, maxServerFee)
233+
234+
return newReasonError(ReasonSwapFee)
235+
}
236+
237+
if quote.MinerFee > f.MaximumMinerFee {
238+
log.Debugf("quoted miner fee: %v > maximum miner "+
239+
"fee: %v", quote.MinerFee, f.MaximumMinerFee)
240+
241+
return newReasonError(ReasonMinerFee)
242+
}
243+
244+
return nil
245+
}
246+
226247
// loopOutFees returns the prepay and routing and miner fees we are willing to
227248
// pay for a loop out swap.
228249
func (f *FeeCategoryLimit) loopOutFees(amount btcutil.Amount,
@@ -384,3 +405,42 @@ func splitOffChain(available, prepayAmt,
384405
func scaleMinerFee(estimate btcutil.Amount) btcutil.Amount {
385406
return estimate * btcutil.Amount(minerMultiplier)
386407
}
408+
409+
func (f *FeePortion) loopInLimits(amount btcutil.Amount,
410+
quote *loop.LoopInQuote) error {
411+
412+
// Calculate the total amount that this swap may spend in fees, as a
413+
// portion of the swap amount.
414+
totalFeeSpend := ppmToSat(amount, f.PartsPerMillion)
415+
416+
// Check individual fee components so that we can give more specific
417+
// feedback.
418+
if quote.MinerFee > totalFeeSpend {
419+
log.Debugf("miner fee: %v greater than fee limit: %v, at "+
420+
"%v ppm", quote.MinerFee, totalFeeSpend,
421+
f.PartsPerMillion)
422+
423+
return newReasonError(ReasonMinerFee)
424+
}
425+
426+
if quote.SwapFee > totalFeeSpend {
427+
log.Debugf("swap fee: %v greater than fee limit: %v, at "+
428+
"%v ppm", quote.SwapFee, totalFeeSpend,
429+
f.PartsPerMillion)
430+
431+
return newReasonError(ReasonSwapFee)
432+
}
433+
434+
fees := worstCaseInFees(
435+
quote.MinerFee, quote.SwapFee, defaultLoopInSweepFee,
436+
)
437+
438+
if fees > totalFeeSpend {
439+
log.Debugf("total fees for swap: %v > fee limit: %v, at "+
440+
"%v ppm", fees, totalFeeSpend, f.PartsPerMillion)
441+
442+
return newReasonError(ReasonFeePPMInsufficient)
443+
}
444+
445+
return nil
446+
}

liquidity/interface.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ type FeeLimit interface {
3232
// a swap amount and quote.
3333
loopOutFees(amount btcutil.Amount, quote *loop.LoopOutQuote) (
3434
btcutil.Amount, btcutil.Amount, btcutil.Amount)
35+
36+
// loopInLimits checks whether the quote provided is within our fee
37+
// limits for the swap amount.
38+
loopInLimits(amount btcutil.Amount,
39+
quote *loop.LoopInQuote) error
3540
}
3641

3742
// swapBuilder is an interface used to build our different swap types.

0 commit comments

Comments
 (0)