Skip to content

Commit d1f121c

Browse files
committed
liquidity: move swap suggestions behind interface
1 parent c6e816a commit d1f121c

File tree

2 files changed

+82
-14
lines changed

2 files changed

+82
-14
lines changed

liquidity/interface.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package liquidity
2+
3+
import (
4+
"github.com/btcsuite/btcutil"
5+
"github.com/lightninglabs/loop"
6+
"github.com/lightningnetwork/lnd/lnwire"
7+
)
8+
9+
// swapSuggestion is an interface implemented by suggested swaps for our
10+
// different swap types. This interface is used to allow us to handle different
11+
// swap types with the same autoloop logic.
12+
type swapSuggestion interface {
13+
// fees returns the highest possible fee amount we could pay for a swap
14+
// in satoshis.
15+
fees() btcutil.Amount
16+
17+
// amount returns the swap amount in satoshis.
18+
amount() btcutil.Amount
19+
20+
// channels returns the set of channels involved in the swap.
21+
channels() []lnwire.ShortChannelID
22+
}
23+
24+
type loopOutSwapSuggestion struct {
25+
loop.OutRequest
26+
}
27+
28+
func (l *loopOutSwapSuggestion) amount() btcutil.Amount {
29+
return l.Amount
30+
}
31+
32+
func (l *loopOutSwapSuggestion) fees() btcutil.Amount {
33+
return worstCaseOutFees(
34+
l.MaxPrepayRoutingFee, l.MaxSwapRoutingFee, l.MaxSwapFee,
35+
l.MaxMinerFee, l.MaxPrepayAmount,
36+
)
37+
}
38+
39+
func (l *loopOutSwapSuggestion) channels() []lnwire.ShortChannelID {
40+
channels := make([]lnwire.ShortChannelID, len(l.OutgoingChanSet))
41+
42+
for i, id := range l.OutgoingChanSet {
43+
channels[i] = lnwire.NewShortChanIDFromInt(id)
44+
}
45+
46+
return channels
47+
}

liquidity/liquidity.go

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,17 @@ func newSuggestions() *Suggestions {
574574
}
575575
}
576576

577+
func (s *Suggestions) addSwap(swap swapSuggestion) error {
578+
out, ok := swap.(*loopOutSwapSuggestion)
579+
if !ok {
580+
return fmt.Errorf("unexpected swap type: %T", swap)
581+
}
582+
583+
s.OutSwaps = append(s.OutSwaps, out.OutRequest)
584+
585+
return nil
586+
}
587+
577588
// singleReasonSuggestion is a helper function which returns a set of
578589
// suggestions where all of our rules are disqualified due to a reason that
579590
// applies to all of them (such as being out of budget).
@@ -693,7 +704,7 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoloop bool) (
693704
traffic := m.currentSwapTraffic(loopOut, loopIn)
694705

695706
var (
696-
suggestions []loop.OutRequest
707+
suggestions []swapSuggestion
697708
disqualified = make(map[lnwire.ShortChannelID]Reason)
698709
)
699710

@@ -719,7 +730,7 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoloop bool) (
719730
return nil, err
720731
}
721732

722-
suggestions = append(suggestions, *suggestion)
733+
suggestions = append(suggestions, suggestion)
723734
}
724735

725736
// Finally, run through all possible swaps, excluding swaps that are
@@ -736,7 +747,7 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoloop bool) (
736747

737748
// Sort suggestions by amount in descending order.
738749
sort.SliceStable(suggestions, func(i, j int) bool {
739-
return suggestions[i].Amount > suggestions[j].Amount
750+
return suggestions[i].amount() > suggestions[j].amount()
740751
})
741752

742753
// Run through our suggested swaps in descending order of amount and
@@ -745,11 +756,14 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoloop bool) (
745756

746757
// setReason is a helper that adds a swap's channels to our disqualified
747758
// list with the reason provided.
748-
setReason := func(reason Reason, swap loop.OutRequest) {
749-
for _, id := range swap.OutgoingChanSet {
750-
chanID := lnwire.NewShortChanIDFromInt(id)
759+
setReason := func(reason Reason, swap swapSuggestion) {
760+
for _, channel := range swap.channels() {
761+
_, ok := m.params.ChannelRules[channel]
762+
if !ok {
763+
continue
764+
}
751765

752-
resp.DisqualifiedChans[chanID] = reason
766+
resp.DisqualifiedChans[channel] = reason
753767
}
754768
}
755769

@@ -773,17 +787,17 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoloop bool) (
773787
continue
774788
}
775789

776-
fees := worstCaseOutFees(
777-
swap.MaxPrepayRoutingFee, swap.MaxSwapRoutingFee,
778-
swap.MaxSwapFee, swap.MaxMinerFee, swap.MaxPrepayAmount,
779-
)
790+
fees := swap.fees()
780791

781792
// If the maximum fee we expect our swap to use is less than the
782793
// amount we have available, we add it to our set of swaps that
783794
// fall within the budget and decrement our available amount.
784795
if fees <= available {
785796
available -= fees
786-
resp.OutSwaps = append(resp.OutSwaps, swap)
797+
798+
if err := resp.addSwap(swap); err != nil {
799+
return nil, err
800+
}
787801
} else {
788802
setReason(ReasonBudgetInsufficient, swap)
789803
}
@@ -796,7 +810,7 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoloop bool) (
796810
// swap request for the rule provided.
797811
func (m *Manager) suggestSwap(ctx context.Context, traffic *swapTraffic,
798812
balance *balances, rule *ThresholdRule, restrictions *Restrictions,
799-
autoloop bool) (*loop.OutRequest, error) {
813+
autoloop bool) (swapSuggestion, error) {
800814

801815
// Check whether we can perform a swap.
802816
err := traffic.maySwap(balance.pubkey, balance.channelID)
@@ -811,7 +825,14 @@ func (m *Manager) suggestSwap(ctx context.Context, traffic *swapTraffic,
811825
return nil, newReasonError(ReasonLiquidityOk)
812826
}
813827

814-
return m.loopOutSwap(ctx, amount, balance, autoloop)
828+
swap, err := m.loopOutSwap(ctx, amount, balance, autoloop)
829+
if err != nil {
830+
return nil, err
831+
}
832+
833+
return &loopOutSwapSuggestion{
834+
OutRequest: *swap,
835+
}, nil
815836
}
816837

817838
// loopOutSwap creates a loop out swap with the amount provided for the balance

0 commit comments

Comments
 (0)