@@ -705,53 +705,21 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoloop bool) (
705705 continue
706706 }
707707
708- // Check whether we can perform a swap, adding the channel to
709- // our set of disqualified swaps if it is not eligible.
710- reason := traffic .maySwap (channel .PubKeyBytes , balance .channelID )
711- if reason != ReasonNone {
712- disqualified [balance .channelID ] = reason
713- continue
714- }
715-
716- // We can have zero amount in the case where no action is
717- // required, so we skip over them.
718- amount := rule .swapAmount (balance , restrictions )
719- if amount == 0 {
720- disqualified [balance .channelID ] = ReasonLiquidityOk
721- continue
722- }
723-
724- // Get a quote for a swap of this amount.
725- quote , err := m .cfg .LoopOutQuote (
726- ctx , & loop.LoopOutQuoteRequest {
727- Amount : amount ,
728- SweepConfTarget : m .params .SweepConfTarget ,
729- SwapPublicationDeadline : m .cfg .Clock .Now (),
730- },
708+ suggestion , err := m .suggestSwap (
709+ ctx , traffic , balance , rule , restrictions , autoloop ,
731710 )
732- if err != nil {
733- return nil , err
734- }
735711
736- log .Debugf ("quote for suggestion: %v, swap fee: %v, " +
737- "miner fee: %v, prepay: %v" , amount , quote .SwapFee ,
738- quote .MinerFee , quote .PrepayAmount )
739-
740- // Check that the estimated fees for the suggested swap are
741- // below the fee limits configured by the manager.
742- feeReason := m .checkFeeLimits (quote , amount )
743- if feeReason != ReasonNone {
744- disqualified [balance .channelID ] = feeReason
712+ var reasonErr * reasonError
713+ if errors .As (err , & reasonErr ) {
714+ disqualified [balance .channelID ] = reasonErr .reason
745715 continue
746716 }
747717
748- outRequest , err := m .makeLoopOutRequest (
749- ctx , amount , balance , quote , autoloop ,
750- )
751718 if err != nil {
752719 return nil , err
753720 }
754- suggestions = append (suggestions , outRequest )
721+
722+ suggestions = append (suggestions , * suggestion )
755723 }
756724
757725 // Finally, run through all possible swaps, excluding swaps that are
@@ -824,6 +792,67 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoloop bool) (
824792 return resp , nil
825793}
826794
795+ // suggestSwap checks whether we can currently perform a swap, and creates a
796+ // swap request for the rule provided.
797+ func (m * Manager ) suggestSwap (ctx context.Context , traffic * swapTraffic ,
798+ balance * balances , rule * ThresholdRule , restrictions * Restrictions ,
799+ autoloop bool ) (* loop.OutRequest , error ) {
800+
801+ // Check whether we can perform a swap.
802+ err := traffic .maySwap (balance .pubkey , balance .channelID )
803+ if err != nil {
804+ return nil , err
805+ }
806+
807+ // We can have nil suggestions in the case where no action is
808+ // required, so we skip over them.
809+ amount := rule .swapAmount (balance , restrictions )
810+ if amount == 0 {
811+ return nil , newReasonError (ReasonLiquidityOk )
812+ }
813+
814+ return m .loopOutSwap (ctx , amount , balance , autoloop )
815+ }
816+
817+ // loopOutSwap creates a loop out swap with the amount provided for the balance
818+ // described by the balance set provided. A reason that indicates whether we
819+ // can swap is returned. If this value is not ReasonNone, there is no possible
820+ // swap and the loop out request returned will be nil.
821+ func (m * Manager ) loopOutSwap (ctx context.Context , amount btcutil.Amount ,
822+ balance * balances , autoloop bool ) (* loop.OutRequest , error ) {
823+
824+ quote , err := m .cfg .LoopOutQuote (
825+ ctx , & loop.LoopOutQuoteRequest {
826+ Amount : amount ,
827+ SweepConfTarget : m .params .SweepConfTarget ,
828+ SwapPublicationDeadline : m .cfg .Clock .Now (),
829+ },
830+ )
831+ if err != nil {
832+ return nil , err
833+ }
834+
835+ log .Debugf ("quote for suggestion: %v, swap fee: %v, " +
836+ "miner fee: %v, prepay: %v" , amount , quote .SwapFee ,
837+ quote .MinerFee , quote .PrepayAmount )
838+
839+ // Check that the estimated fees for the suggested swap are
840+ // below the fee limits configured by the manager.
841+ feeReason := m .checkFeeLimits (quote , amount )
842+ if feeReason != ReasonNone {
843+ return nil , newReasonError (feeReason )
844+ }
845+
846+ outRequest , err := m .makeLoopOutRequest (
847+ ctx , amount , balance , quote , autoloop ,
848+ )
849+ if err != nil {
850+ return nil , err
851+ }
852+
853+ return & outRequest , nil
854+ }
855+
827856// getSwapRestrictions queries the server for its latest swap size restrictions,
828857// validates client restrictions (if present) against these values and merges
829858// the client's custom requirements with the server's limits to produce a single
@@ -1093,31 +1122,31 @@ func newSwapTraffic() *swapTraffic {
10931122// maySwap returns a boolean that indicates whether we may perform a swap for a
10941123// peer and its set of channels.
10951124func (s * swapTraffic ) maySwap (peer route.Vertex ,
1096- chanID lnwire.ShortChannelID ) Reason {
1125+ chanID lnwire.ShortChannelID ) error {
10971126
10981127 lastFail , recentFail := s .failedLoopOut [chanID ]
10991128 if recentFail {
11001129 log .Debugf ("Channel: %v not eligible for suggestions, was " +
11011130 "part of a failed swap at: %v" , chanID , lastFail )
11021131
1103- return ReasonFailureBackoff
1132+ return newReasonError ( ReasonFailureBackoff )
11041133 }
11051134
11061135 if s .ongoingLoopOut [chanID ] {
11071136 log .Debugf ("Channel: %v not eligible for suggestions, " +
11081137 "ongoing loop out utilizing channel" , chanID )
11091138
1110- return ReasonLoopOut
1139+ return newReasonError ( ReasonLoopOut )
11111140 }
11121141
11131142 if s .ongoingLoopIn [peer ] {
11141143 log .Debugf ("Peer: %x not eligible for suggestions ongoing " +
11151144 "loop in utilizing peer" , peer )
11161145
1117- return ReasonLoopIn
1146+ return newReasonError ( ReasonLoopIn )
11181147 }
11191148
1120- return ReasonNone
1149+ return nil
11211150}
11221151
11231152// checkFeeLimits takes a set of fees for a swap and checks whether they exceed
0 commit comments