Skip to content

Commit 9b1fcb0

Browse files
authored
Refactor invoice rebalancing logic in liquidator.go (#77)
* Reduce nesting in invoice rebalancing logic in liquidator.go * Simplify invoice rebalancing logic in liquidator.go
1 parent 8dce412 commit 9b1fcb0

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

liquidator.go

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ func manageChannelLiquidity(info ManageChannelLiquidityInfo) error {
457457

458458
switch {
459459
//Both nodes are managed, then simply create 1 invoice and send it to the other node to pay it
460-
case info.lightningClients[rule.NodePubkey] != nil && info.lightningClients[rule.RemoteNodePubkey] != nil:
460+
case info.lightningClients[rule.NodePubkey] != nil && info.lightningClients[rule.RemoteNodePubkey] != nil && rule.MinimumLocalBalance != 0 && info.channelBalanceRatio < float64(rule.MinimumLocalBalance):
461461
{
462462
//Add attribute to the span of swap requested
463463
span.SetAttributes(attribute.String("swapRequestedType", "invoiceRebalance"))
@@ -467,32 +467,33 @@ func manageChannelLiquidity(info ManageChannelLiquidityInfo) error {
467467

468468
log.WithField("span", span).Infof("rebalancing via invoice on channel %v on node %v", channel.GetChanId(), info.nodeInfo.GetAlias())
469469

470-
switch {
471470
//Create an invoice for the swap amount from the remote node and pay with the rule's node
472-
case rule.MinimumLocalBalance != 0 && info.channelBalanceRatio < float64(rule.MinimumLocalBalance):
473-
{
474-
swapAmount := helper.AbsInt64((swapAmountTarget - channel.LocalBalance))
475471

476-
err := invoiceRebalance(info, swapAmount, rule.NodePubkey, rule.RemoteNodePubkey)
477-
if err != nil {
478-
return err
479-
}
480-
481-
}
482-
//Create an invoice for the swap amount from the rule's node and pay with the remote node
483-
case rule.MinimumRemoteBalance != 0 && info.channelBalanceRatio > float64(rule.MinimumRemoteBalance):
484-
{
485-
swapAmount := helper.AbsInt64((channel.RemoteBalance - swapAmountTarget))
486-
487-
err := invoiceRebalance(info, swapAmount, rule.RemoteNodePubkey, rule.NodePubkey)
488-
if err != nil {
489-
return err
490-
}
491-
}
472+
swapAmount := helper.AbsInt64((swapAmountTarget - channel.LocalBalance))
492473

474+
err := invoiceRebalance(info, swapAmount, rule.NodePubkey, rule.RemoteNodePubkey)
475+
if err != nil {
476+
return err
493477
}
494478

495479
}
480+
case info.lightningClients[rule.NodePubkey] != nil && info.lightningClients[rule.RemoteNodePubkey] != nil && rule.MinimumRemoteBalance != 0 && info.channelBalanceRatio > float64(rule.MinimumRemoteBalance):
481+
{
482+
//Add attribute to the span of swap requested
483+
span.SetAttributes(attribute.String("swapRequestedType", "invoiceRebalance"))
484+
span.SetAttributes(attribute.String("chanId", fmt.Sprintf("%v", channel.GetChanId())))
485+
span.SetAttributes(attribute.String("nodePubkey", info.nodeInfo.GetIdentityPubkey()))
486+
span.SetAttributes(attribute.String("nodeAlias", info.nodeInfo.GetAlias()))
487+
488+
log.WithField("span", span).Infof("rebalancing via invoice on channel %v on node %v", channel.GetChanId(), info.nodeInfo.GetAlias())
489+
swapAmount := helper.AbsInt64((channel.RemoteBalance - swapAmountTarget))
490+
491+
//Create an invoice for the swap amount from the rule's node and pay with the remote node
492+
err := invoiceRebalance(info, swapAmount, rule.RemoteNodePubkey, rule.NodePubkey)
493+
if err != nil {
494+
return err
495+
}
496+
}
496497
case rule.MinimumLocalBalance != 0 && info.channelBalanceRatio < float64(rule.MinimumLocalBalance):
497498
{
498499
swapAmount := helper.AbsInt64((swapAmountTarget - channel.LocalBalance))

0 commit comments

Comments
 (0)