From 9c35946cab1143f8824f756df25c79a1925dd6e4 Mon Sep 17 00:00:00 2001 From: carla Date: Tue, 16 Feb 2021 13:31:48 +0200 Subject: [PATCH] liquidity: simplify suggest swap to return swap amount --- liquidity/liquidity.go | 24 +++++++++++----------- liquidity/suggestions.go | 34 -------------------------------- liquidity/threshold_rule.go | 18 +++++++---------- liquidity/threshold_rule_test.go | 12 +++++------ 4 files changed, 24 insertions(+), 64 deletions(-) delete mode 100644 liquidity/suggestions.go diff --git a/liquidity/liquidity.go b/liquidity/liquidity.go index 6586a00..30153a0 100644 --- a/liquidity/liquidity.go +++ b/liquidity/liquidity.go @@ -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, diff --git a/liquidity/suggestions.go b/liquidity/suggestions.go deleted file mode 100644 index 3f2bfc0..0000000 --- a/liquidity/suggestions.go +++ /dev/null @@ -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, - } -} diff --git a/liquidity/threshold_rule.go b/liquidity/threshold_rule.go index 559449d..47ef735 100644 --- a/liquidity/threshold_rule.go +++ b/liquidity/threshold_rule.go @@ -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 } } diff --git a/liquidity/threshold_rule_test.go b/liquidity/threshold_rule_test.go index ea6aa1e..b90c6c6 100644 --- a/liquidity/threshold_rule_test.go +++ b/liquidity/threshold_rule_test.go @@ -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)