From 4df6a4e7ca6fe65e00a44005a9181febbea19b81 Mon Sep 17 00:00:00 2001 From: carla Date: Mon, 6 Sep 2021 14:05:39 +0200 Subject: [PATCH] liquidity: move loopout swap suggestion out of interface file --- liquidity/interface.go | 52 --------------------------------- liquidity/loopout.go | 66 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 52 deletions(-) create mode 100644 liquidity/loopout.go diff --git a/liquidity/interface.go b/liquidity/interface.go index c05091c..4f06768 100644 --- a/liquidity/interface.go +++ b/liquidity/interface.go @@ -50,55 +50,3 @@ type swapSuggestion interface { // can be looked up. peers(knownChans map[uint64]route.Vertex) []route.Vertex } - -// Compile-time assertion that loopOutSwapSuggestion satisfies the -// swapSuggestion interface. -var _ swapSuggestion = (*loopOutSwapSuggestion)(nil) - -type loopOutSwapSuggestion struct { - loop.OutRequest -} - -func (l *loopOutSwapSuggestion) amount() btcutil.Amount { - return l.Amount -} - -func (l *loopOutSwapSuggestion) fees() btcutil.Amount { - return worstCaseOutFees( - l.MaxPrepayRoutingFee, l.MaxSwapRoutingFee, l.MaxSwapFee, - l.MaxMinerFee, l.MaxPrepayAmount, - ) -} - -func (l *loopOutSwapSuggestion) channels() []lnwire.ShortChannelID { - channels := make([]lnwire.ShortChannelID, len(l.OutgoingChanSet)) - - for i, id := range l.OutgoingChanSet { - channels[i] = lnwire.NewShortChanIDFromInt(id) - } - - return channels -} - -// peers returns the set of peers that the loop out swap is restricted to. -func (l *loopOutSwapSuggestion) peers( - knownChans map[uint64]route.Vertex) []route.Vertex { - - peers := make(map[route.Vertex]struct{}, len(knownChans)) - - for _, channel := range l.OutgoingChanSet { - peer, ok := knownChans[channel] - if !ok { - log.Warnf("peer for channel: %v unknown", channel) - } - - peers[peer] = struct{}{} - } - - peerList := make([]route.Vertex, 0, len(peers)) - for peer := range peers { - peerList = append(peerList, peer) - } - - return peerList -} diff --git a/liquidity/loopout.go b/liquidity/loopout.go new file mode 100644 index 0000000..a571f54 --- /dev/null +++ b/liquidity/loopout.go @@ -0,0 +1,66 @@ +package liquidity + +import ( + "github.com/btcsuite/btcutil" + "github.com/lightninglabs/loop" + "github.com/lightningnetwork/lnd/lnwire" + "github.com/lightningnetwork/lnd/routing/route" +) + +// Compile-time assertion that loopOutSwapSuggestion satisfies the +// swapSuggestion interface. +var _ swapSuggestion = (*loopOutSwapSuggestion)(nil) + +// loopOutSwapSuggestion is an implementation of the swapSuggestion interface +// implemented to allow us to abstract away from the details of a specific +// swap. +type loopOutSwapSuggestion struct { + loop.OutRequest +} + +// amount returns the amount being swapped. +func (l *loopOutSwapSuggestion) amount() btcutil.Amount { + return l.Amount +} + +// fees returns the maximum fees we could possibly pay for this swap. +func (l *loopOutSwapSuggestion) fees() btcutil.Amount { + return worstCaseOutFees( + l.MaxPrepayRoutingFee, l.MaxSwapRoutingFee, l.MaxSwapFee, + l.MaxMinerFee, l.MaxPrepayAmount, + ) +} + +// channels returns the set of channels the loop out swap is restricted to. +func (l *loopOutSwapSuggestion) channels() []lnwire.ShortChannelID { + channels := make([]lnwire.ShortChannelID, len(l.OutgoingChanSet)) + + for i, id := range l.OutgoingChanSet { + channels[i] = lnwire.NewShortChanIDFromInt(id) + } + + return channels +} + +// peers returns the set of peers that the loop out swap is restricted to. +func (l *loopOutSwapSuggestion) peers( + knownChans map[uint64]route.Vertex) []route.Vertex { + + peers := make(map[route.Vertex]struct{}, len(knownChans)) + + for _, channel := range l.OutgoingChanSet { + peer, ok := knownChans[channel] + if !ok { + log.Warnf("peer for channel: %v unknown", channel) + } + + peers[peer] = struct{}{} + } + + peerList := make([]route.Vertex, 0, len(peers)) + for peer := range peers { + peerList = append(peerList, peer) + } + + return peerList +}