Skip to content

Commit 3e7782e

Browse files
committed
liquidity: use builder in single place
In preparation of supporting multiple swap types, we move our swap builder into a single place, so that we can check our `maySwap` restriction per-swap (since we'll now have different checks for different swap types. To save ourselves from making multiple calls to the loop server for the restrictions placed on each swap type, we still pass a single set of restrictions in.
1 parent 91a9096 commit 3e7782e

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed

liquidity/liquidity.go

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -386,10 +386,6 @@ type Manager struct {
386386
// current liquidity balance.
387387
cfg *Config
388388

389-
// builder is the swap builder responsible for creating swaps of our
390-
// chosen type for us.
391-
builder swapBuilder
392-
393389
// params is the set of parameters we are currently using. These may be
394390
// updated at runtime.
395391
params Parameters
@@ -428,9 +424,8 @@ func (m *Manager) Run(ctx context.Context) error {
428424
// NewManager creates a liquidity manager which has no rules set.
429425
func NewManager(cfg *Config) *Manager {
430426
return &Manager{
431-
cfg: cfg,
432-
params: defaultParameters,
433-
builder: newLoopOutBuilder(cfg),
427+
cfg: cfg,
428+
params: defaultParameters,
434429
}
435430
}
436431

@@ -617,23 +612,8 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoloop bool) (
617612
return m.singleReasonSuggestion(ReasonBudgetNotStarted), nil
618613
}
619614

620-
// Before we get any swap suggestions, we check what the current fee
621-
// estimate is to sweep within our target number of confirmations. If
622-
// This fee exceeds the fee limit we have set, we will not suggest any
623-
// swaps at present.
624-
if err := m.builder.maySwap(ctx, m.params); err != nil {
625-
var reasonErr *reasonError
626-
if errors.As(err, &reasonErr) {
627-
return m.singleReasonSuggestion(reasonErr.reason), nil
628-
629-
}
630-
631-
return nil, err
632-
}
633-
634-
// Get the current server side restrictions, combined with the client
635-
// set restrictions, if any.
636-
restrictions, err := m.getSwapRestrictions(ctx, m.builder.swapType())
615+
// Get restrictions placed on swaps by the server.
616+
outRestrictions, err := m.getSwapRestrictions(ctx, swap.TypeOut)
637617
if err != nil {
638618
return nil, err
639619
}
@@ -721,7 +701,8 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoloop bool) (
721701
}
722702

723703
suggestion, err := m.suggestSwap(
724-
ctx, traffic, balances, rule, restrictions, autoloop,
704+
ctx, traffic, balances, rule, outRestrictions,
705+
autoloop,
725706
)
726707
var reasonErr *reasonError
727708
if errors.As(err, &reasonErr) {
@@ -746,7 +727,8 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoloop bool) (
746727
}
747728

748729
suggestion, err := m.suggestSwap(
749-
ctx, traffic, balance, rule, restrictions, autoloop,
730+
ctx, traffic, balance, rule, outRestrictions,
731+
autoloop,
750732
)
751733

752734
var reasonErr *reasonError
@@ -841,12 +823,34 @@ func (m *Manager) SuggestSwaps(ctx context.Context, autoloop bool) (
841823
// suggestSwap checks whether we can currently perform a swap, and creates a
842824
// swap request for the rule provided.
843825
func (m *Manager) suggestSwap(ctx context.Context, traffic *swapTraffic,
844-
balance *balances, rule *SwapRule, restrictions *Restrictions,
826+
balance *balances, rule *SwapRule, outRestrictions *Restrictions,
845827
autoloop bool) (swapSuggestion, error) {
846828

829+
var (
830+
builder swapBuilder
831+
restrictions *Restrictions
832+
)
833+
834+
switch rule.Type {
835+
case swap.TypeOut:
836+
builder = newLoopOutBuilder(m.cfg)
837+
restrictions = outRestrictions
838+
839+
default:
840+
return nil, fmt.Errorf("unsupported swap type: %v", rule.Type)
841+
}
842+
843+
// Before we get any swap suggestions, we check what the current fee
844+
// estimate is to sweep within our target number of confirmations. If
845+
// This fee exceeds the fee limit we have set, we will not suggest any
846+
// swaps at present.
847+
if err := builder.maySwap(ctx, m.params); err != nil {
848+
return nil, err
849+
}
850+
847851
// First, check whether this peer/channel combination is already in use
848852
// for our swap.
849-
err := m.builder.inUse(traffic, balance.pubkey, balance.channels)
853+
err := builder.inUse(traffic, balance.pubkey, balance.channels)
850854
if err != nil {
851855
return nil, err
852856
}
@@ -858,7 +862,7 @@ func (m *Manager) suggestSwap(ctx context.Context, traffic *swapTraffic,
858862
return nil, newReasonError(ReasonLiquidityOk)
859863
}
860864

861-
return m.builder.buildSwap(
865+
return builder.buildSwap(
862866
ctx, balance.pubkey, balance.channels, amount, autoloop,
863867
m.params,
864868
)

0 commit comments

Comments
 (0)