From 6a6d02154ecabcee6a98b4e0f6d17a3f5c1a1877 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=CC=81=20A=2EP?= Date: Wed, 17 Apr 2024 18:59:13 +0200 Subject: [PATCH] Reduce nesting in invoice rebalancing logic in liquidator.go --- liquidator.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/liquidator.go b/liquidator.go index d33690f..b32de64 100644 --- a/liquidator.go +++ b/liquidator.go @@ -457,7 +457,7 @@ func manageChannelLiquidity(info ManageChannelLiquidityInfo) error { switch { //Both nodes are managed, then simply create 1 invoice and send it to the other node to pay it - case info.lightningClients[rule.NodePubkey] != nil && info.lightningClients[rule.RemoteNodePubkey] != nil: + case info.lightningClients[rule.NodePubkey] != nil && info.lightningClients[rule.RemoteNodePubkey] != nil && rule.MinimumLocalBalance != 0 && info.channelBalanceRatio < float64(rule.MinimumLocalBalance): { //Add attribute to the span of swap requested span.SetAttributes(attribute.String("swapRequestedType", "invoiceRebalance")) @@ -467,7 +467,6 @@ func manageChannelLiquidity(info ManageChannelLiquidityInfo) error { log.WithField("span", span).Infof("rebalancing via invoice on channel %v on node %v", channel.GetChanId(), info.nodeInfo.GetAlias()) - switch { //Create an invoice for the swap amount from the remote node and pay with the rule's node case rule.MinimumLocalBalance != 0 && info.channelBalanceRatio < float64(rule.MinimumLocalBalance): { @@ -493,6 +492,23 @@ func manageChannelLiquidity(info ManageChannelLiquidityInfo) error { } } + case info.lightningClients[rule.NodePubkey] != nil && info.lightningClients[rule.RemoteNodePubkey] != nil && rule.MinimumRemoteBalance != 0 && info.channelBalanceRatio > float64(rule.MinimumRemoteBalance): + { + //Add attribute to the span of swap requested + span.SetAttributes(attribute.String("swapRequestedType", "invoiceRebalance")) + span.SetAttributes(attribute.String("chanId", fmt.Sprintf("%v", channel.GetChanId()))) + span.SetAttributes(attribute.String("nodePubkey", info.nodeInfo.GetIdentityPubkey())) + span.SetAttributes(attribute.String("nodeAlias", info.nodeInfo.GetAlias())) + + log.WithField("span", span).Infof("rebalancing via invoice on channel %v on node %v", channel.GetChanId(), info.nodeInfo.GetAlias()) + swapAmount := helper.AbsInt64((channel.RemoteBalance - swapAmountTarget)) + + //Create an invoice for the swap amount from the rule's node and pay with the remote node + err := invoiceRebalance(info, swapAmount, rule.RemoteNodePubkey, rule.NodePubkey) + if err != nil { + return err + } + } case rule.MinimumLocalBalance != 0 && info.channelBalanceRatio < float64(rule.MinimumLocalBalance): { swapAmount := helper.AbsInt64((swapAmountTarget - channel.LocalBalance))