Skip to content

Commit 9c35946

Browse files
committed
liquidity: simplify suggest swap to return swap amount
1 parent 7d5e1a9 commit 9c35946

File tree

4 files changed

+24
-64
lines changed

4 files changed

+24
-64
lines changed

liquidity/liquidity.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -713,18 +713,18 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoloop bool) (
713713
continue
714714
}
715715

716-
// We can have nil suggestions in the case where no action is
716+
// We can have zero amount in the case where no action is
717717
// required, so we skip over them.
718-
suggestion := rule.suggestSwap(balance, restrictions)
719-
if suggestion == nil {
718+
amount := rule.swapAmount(balance, restrictions)
719+
if amount == 0 {
720720
disqualified[balance.channelID] = ReasonLiquidityOk
721721
continue
722722
}
723723

724724
// Get a quote for a swap of this amount.
725725
quote, err := m.cfg.LoopOutQuote(
726726
ctx, &loop.LoopOutQuoteRequest{
727-
Amount: suggestion.Amount,
727+
Amount: amount,
728728
SweepConfTarget: m.params.SweepConfTarget,
729729
SwapPublicationDeadline: m.cfg.Clock.Now(),
730730
},
@@ -734,19 +734,19 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoloop bool) (
734734
}
735735

736736
log.Debugf("quote for suggestion: %v, swap fee: %v, "+
737-
"miner fee: %v, prepay: %v", suggestion, quote.SwapFee,
737+
"miner fee: %v, prepay: %v", amount, quote.SwapFee,
738738
quote.MinerFee, quote.PrepayAmount)
739739

740740
// Check that the estimated fees for the suggested swap are
741741
// below the fee limits configured by the manager.
742-
feeReason := m.checkFeeLimits(quote, suggestion.Amount)
742+
feeReason := m.checkFeeLimits(quote, amount)
743743
if feeReason != ReasonNone {
744744
disqualified[balance.channelID] = feeReason
745745
continue
746746
}
747747

748748
outRequest, err := m.makeLoopOutRequest(
749-
ctx, suggestion, quote, autoloop,
749+
ctx, amount, balance, quote, autoloop,
750750
)
751751
if err != nil {
752752
return nil, err
@@ -871,21 +871,19 @@ func (m *Manager) getSwapRestrictions(ctx context.Context, swapType swap.Type) (
871871
// dispatched, and decides whether we set a sweep address (we don't bother for
872872
// non-auto requests, because the client api will set it anyway).
873873
func (m *Manager) makeLoopOutRequest(ctx context.Context,
874-
suggestion *LoopOutRecommendation, quote *loop.LoopOutQuote,
874+
amount btcutil.Amount, balance *balances, quote *loop.LoopOutQuote,
875875
autoloop bool) (loop.OutRequest, error) {
876876

877877
prepayMaxFee := ppmToSat(
878878
quote.PrepayAmount, m.params.MaximumPrepayRoutingFeePPM,
879879
)
880880

881-
routeMaxFee := ppmToSat(
882-
suggestion.Amount, m.params.MaximumRoutingFeePPM,
883-
)
881+
routeMaxFee := ppmToSat(amount, m.params.MaximumRoutingFeePPM)
884882

885883
request := loop.OutRequest{
886-
Amount: suggestion.Amount,
884+
Amount: amount,
887885
OutgoingChanSet: loopdb.ChannelSet{
888-
suggestion.Channel.ToUint64(),
886+
balance.channelID.ToUint64(),
889887
},
890888
MaxPrepayRoutingFee: prepayMaxFee,
891889
MaxSwapRoutingFee: routeMaxFee,

liquidity/suggestions.go

Lines changed: 0 additions & 34 deletions
This file was deleted.

liquidity/threshold_rule.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ func (r *ThresholdRule) validate() error {
6262
return nil
6363
}
6464

65-
// suggestSwap suggests a swap based on the liquidity thresholds configured,
66-
// returning nil if no swap is recommended.
67-
func (r *ThresholdRule) suggestSwap(channel *balances,
68-
outRestrictions *Restrictions) *LoopOutRecommendation {
65+
// swapAmount suggests a swap based on the liquidity thresholds configured,
66+
// returning zero if no swap is recommended.
67+
func (r *ThresholdRule) swapAmount(channel *balances,
68+
outRestrictions *Restrictions) btcutil.Amount {
6969

7070
// Examine our total balance and required ratios to decide whether we
7171
// need to swap.
@@ -76,17 +76,13 @@ func (r *ThresholdRule) suggestSwap(channel *balances,
7676
// Limit our swap amount by the minimum/maximum thresholds set.
7777
switch {
7878
case amount < outRestrictions.Minimum:
79-
return nil
79+
return 0
8080

8181
case amount > outRestrictions.Maximum:
82-
return newLoopOutRecommendation(
83-
outRestrictions.Maximum, channel.channelID,
84-
)
82+
return outRestrictions.Maximum
8583

8684
default:
87-
return newLoopOutRecommendation(
88-
amount, channel.channelID,
89-
)
85+
return amount
9086
}
9187
}
9288

liquidity/threshold_rule_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func TestSuggestSwap(t *testing.T) {
184184
rule *ThresholdRule
185185
channel *balances
186186
outRestrictions *Restrictions
187-
swap *LoopOutRecommendation
187+
swap btcutil.Amount
188188
}{
189189
{
190190
name: "liquidity ok",
@@ -205,7 +205,7 @@ func TestSuggestSwap(t *testing.T) {
205205
incoming: 0,
206206
outgoing: 100,
207207
},
208-
swap: &LoopOutRecommendation{Amount: 50},
208+
swap: 50,
209209
},
210210
{
211211
name: "amount below minimum",
@@ -216,7 +216,7 @@ func TestSuggestSwap(t *testing.T) {
216216
incoming: 0,
217217
outgoing: 100,
218218
},
219-
swap: nil,
219+
swap: 0,
220220
},
221221
{
222222
name: "amount above maximum",
@@ -227,7 +227,7 @@ func TestSuggestSwap(t *testing.T) {
227227
incoming: 0,
228228
outgoing: 100,
229229
},
230-
swap: &LoopOutRecommendation{Amount: 20},
230+
swap: 20,
231231
},
232232
{
233233
name: "loop in",
@@ -238,15 +238,15 @@ func TestSuggestSwap(t *testing.T) {
238238
incoming: 100,
239239
outgoing: 0,
240240
},
241-
swap: nil,
241+
swap: 0,
242242
},
243243
}
244244

245245
for _, test := range tests {
246246
test := test
247247

248248
t.Run(test.name, func(t *testing.T) {
249-
swap := test.rule.suggestSwap(
249+
swap := test.rule.swapAmount(
250250
test.channel, test.outRestrictions,
251251
)
252252
require.Equal(t, test.swap, swap)

0 commit comments

Comments
 (0)