liquidity: simplify suggest swap to return swap amount

pull/333/head
carla 3 years ago
parent 7d5e1a91c1
commit 9c35946cab
No known key found for this signature in database
GPG Key ID: 4CA7FE54A6213C91

@ -713,10 +713,10 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoloop bool) (
continue
}
// We can have nil suggestions in the case where no action is
// We can have zero amount in the case where no action is
// required, so we skip over them.
suggestion := rule.suggestSwap(balance, restrictions)
if suggestion == nil {
amount := rule.swapAmount(balance, restrictions)
if amount == 0 {
disqualified[balance.channelID] = ReasonLiquidityOk
continue
}
@ -724,7 +724,7 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoloop bool) (
// Get a quote for a swap of this amount.
quote, err := m.cfg.LoopOutQuote(
ctx, &loop.LoopOutQuoteRequest{
Amount: suggestion.Amount,
Amount: amount,
SweepConfTarget: m.params.SweepConfTarget,
SwapPublicationDeadline: m.cfg.Clock.Now(),
},
@ -734,19 +734,19 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoloop bool) (
}
log.Debugf("quote for suggestion: %v, swap fee: %v, "+
"miner fee: %v, prepay: %v", suggestion, quote.SwapFee,
"miner fee: %v, prepay: %v", amount, quote.SwapFee,
quote.MinerFee, quote.PrepayAmount)
// Check that the estimated fees for the suggested swap are
// below the fee limits configured by the manager.
feeReason := m.checkFeeLimits(quote, suggestion.Amount)
feeReason := m.checkFeeLimits(quote, amount)
if feeReason != ReasonNone {
disqualified[balance.channelID] = feeReason
continue
}
outRequest, err := m.makeLoopOutRequest(
ctx, suggestion, quote, autoloop,
ctx, amount, balance, quote, autoloop,
)
if err != nil {
return nil, err
@ -871,21 +871,19 @@ func (m *Manager) getSwapRestrictions(ctx context.Context, swapType swap.Type) (
// dispatched, and decides whether we set a sweep address (we don't bother for
// non-auto requests, because the client api will set it anyway).
func (m *Manager) makeLoopOutRequest(ctx context.Context,
suggestion *LoopOutRecommendation, quote *loop.LoopOutQuote,
amount btcutil.Amount, balance *balances, quote *loop.LoopOutQuote,
autoloop bool) (loop.OutRequest, error) {
prepayMaxFee := ppmToSat(
quote.PrepayAmount, m.params.MaximumPrepayRoutingFeePPM,
)
routeMaxFee := ppmToSat(
suggestion.Amount, m.params.MaximumRoutingFeePPM,
)
routeMaxFee := ppmToSat(amount, m.params.MaximumRoutingFeePPM)
request := loop.OutRequest{
Amount: suggestion.Amount,
Amount: amount,
OutgoingChanSet: loopdb.ChannelSet{
suggestion.Channel.ToUint64(),
balance.channelID.ToUint64(),
},
MaxPrepayRoutingFee: prepayMaxFee,
MaxSwapRoutingFee: routeMaxFee,

@ -1,34 +0,0 @@
package liquidity
import (
"fmt"
"github.com/btcsuite/btcutil"
"github.com/lightningnetwork/lnd/lnwire"
)
// LoopOutRecommendation contains the information required to recommend a loop
// out.
type LoopOutRecommendation struct {
// Amount is the total amount to swap.
Amount btcutil.Amount
// Channel is the target outgoing channel.
Channel lnwire.ShortChannelID
}
// String returns a string representation of a loop out recommendation.
func (l *LoopOutRecommendation) String() string {
return fmt.Sprintf("loop out: %v over %v", l.Amount,
l.Channel.ToUint64())
}
// newLoopOutRecommendation creates a new loop out swap.
func newLoopOutRecommendation(amount btcutil.Amount,
channelID lnwire.ShortChannelID) *LoopOutRecommendation {
return &LoopOutRecommendation{
Amount: amount,
Channel: channelID,
}
}

@ -62,10 +62,10 @@ func (r *ThresholdRule) validate() error {
return nil
}
// suggestSwap suggests a swap based on the liquidity thresholds configured,
// returning nil if no swap is recommended.
func (r *ThresholdRule) suggestSwap(channel *balances,
outRestrictions *Restrictions) *LoopOutRecommendation {
// swapAmount suggests a swap based on the liquidity thresholds configured,
// returning zero if no swap is recommended.
func (r *ThresholdRule) swapAmount(channel *balances,
outRestrictions *Restrictions) btcutil.Amount {
// Examine our total balance and required ratios to decide whether we
// need to swap.
@ -76,17 +76,13 @@ func (r *ThresholdRule) suggestSwap(channel *balances,
// Limit our swap amount by the minimum/maximum thresholds set.
switch {
case amount < outRestrictions.Minimum:
return nil
return 0
case amount > outRestrictions.Maximum:
return newLoopOutRecommendation(
outRestrictions.Maximum, channel.channelID,
)
return outRestrictions.Maximum
default:
return newLoopOutRecommendation(
amount, channel.channelID,
)
return amount
}
}

@ -184,7 +184,7 @@ func TestSuggestSwap(t *testing.T) {
rule *ThresholdRule
channel *balances
outRestrictions *Restrictions
swap *LoopOutRecommendation
swap btcutil.Amount
}{
{
name: "liquidity ok",
@ -205,7 +205,7 @@ func TestSuggestSwap(t *testing.T) {
incoming: 0,
outgoing: 100,
},
swap: &LoopOutRecommendation{Amount: 50},
swap: 50,
},
{
name: "amount below minimum",
@ -216,7 +216,7 @@ func TestSuggestSwap(t *testing.T) {
incoming: 0,
outgoing: 100,
},
swap: nil,
swap: 0,
},
{
name: "amount above maximum",
@ -227,7 +227,7 @@ func TestSuggestSwap(t *testing.T) {
incoming: 0,
outgoing: 100,
},
swap: &LoopOutRecommendation{Amount: 20},
swap: 20,
},
{
name: "loop in",
@ -238,7 +238,7 @@ func TestSuggestSwap(t *testing.T) {
incoming: 100,
outgoing: 0,
},
swap: nil,
swap: 0,
},
}
@ -246,7 +246,7 @@ func TestSuggestSwap(t *testing.T) {
test := test
t.Run(test.name, func(t *testing.T) {
swap := test.rule.suggestSwap(
swap := test.rule.swapAmount(
test.channel, test.outRestrictions,
)
require.Equal(t, test.swap, swap)

Loading…
Cancel
Save