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 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. // required, so we skip over them.
suggestion := rule.suggestSwap(balance, restrictions) amount := rule.swapAmount(balance, restrictions)
if suggestion == nil { if amount == 0 {
disqualified[balance.channelID] = ReasonLiquidityOk disqualified[balance.channelID] = ReasonLiquidityOk
continue continue
} }
@ -724,7 +724,7 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoloop bool) (
// Get a quote for a swap of this amount. // Get a quote for a swap of this amount.
quote, err := m.cfg.LoopOutQuote( quote, err := m.cfg.LoopOutQuote(
ctx, &loop.LoopOutQuoteRequest{ ctx, &loop.LoopOutQuoteRequest{
Amount: suggestion.Amount, Amount: amount,
SweepConfTarget: m.params.SweepConfTarget, SweepConfTarget: m.params.SweepConfTarget,
SwapPublicationDeadline: m.cfg.Clock.Now(), 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, "+ 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) quote.MinerFee, quote.PrepayAmount)
// Check that the estimated fees for the suggested swap are // Check that the estimated fees for the suggested swap are
// below the fee limits configured by the manager. // below the fee limits configured by the manager.
feeReason := m.checkFeeLimits(quote, suggestion.Amount) feeReason := m.checkFeeLimits(quote, amount)
if feeReason != ReasonNone { if feeReason != ReasonNone {
disqualified[balance.channelID] = feeReason disqualified[balance.channelID] = feeReason
continue continue
} }
outRequest, err := m.makeLoopOutRequest( outRequest, err := m.makeLoopOutRequest(
ctx, suggestion, quote, autoloop, ctx, amount, balance, quote, autoloop,
) )
if err != nil { if err != nil {
return nil, err 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 // 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). // non-auto requests, because the client api will set it anyway).
func (m *Manager) makeLoopOutRequest(ctx context.Context, 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) { autoloop bool) (loop.OutRequest, error) {
prepayMaxFee := ppmToSat( prepayMaxFee := ppmToSat(
quote.PrepayAmount, m.params.MaximumPrepayRoutingFeePPM, quote.PrepayAmount, m.params.MaximumPrepayRoutingFeePPM,
) )
routeMaxFee := ppmToSat( routeMaxFee := ppmToSat(amount, m.params.MaximumRoutingFeePPM)
suggestion.Amount, m.params.MaximumRoutingFeePPM,
)
request := loop.OutRequest{ request := loop.OutRequest{
Amount: suggestion.Amount, Amount: amount,
OutgoingChanSet: loopdb.ChannelSet{ OutgoingChanSet: loopdb.ChannelSet{
suggestion.Channel.ToUint64(), balance.channelID.ToUint64(),
}, },
MaxPrepayRoutingFee: prepayMaxFee, MaxPrepayRoutingFee: prepayMaxFee,
MaxSwapRoutingFee: routeMaxFee, 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 return nil
} }
// suggestSwap suggests a swap based on the liquidity thresholds configured, // swapAmount suggests a swap based on the liquidity thresholds configured,
// returning nil if no swap is recommended. // returning zero if no swap is recommended.
func (r *ThresholdRule) suggestSwap(channel *balances, func (r *ThresholdRule) swapAmount(channel *balances,
outRestrictions *Restrictions) *LoopOutRecommendation { outRestrictions *Restrictions) btcutil.Amount {
// Examine our total balance and required ratios to decide whether we // Examine our total balance and required ratios to decide whether we
// need to swap. // need to swap.
@ -76,17 +76,13 @@ func (r *ThresholdRule) suggestSwap(channel *balances,
// Limit our swap amount by the minimum/maximum thresholds set. // Limit our swap amount by the minimum/maximum thresholds set.
switch { switch {
case amount < outRestrictions.Minimum: case amount < outRestrictions.Minimum:
return nil return 0
case amount > outRestrictions.Maximum: case amount > outRestrictions.Maximum:
return newLoopOutRecommendation( return outRestrictions.Maximum
outRestrictions.Maximum, channel.channelID,
)
default: default:
return newLoopOutRecommendation( return amount
amount, channel.channelID,
)
} }
} }

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

Loading…
Cancel
Save