Skip to content

Commit

Permalink
fix: Refactor logic of balance to include both channels between manag…
Browse files Browse the repository at this point in the history
…ed nodes and balance with channels from external to managed nodes
  • Loading branch information
Jossec101 committed Jul 26, 2023
1 parent 5b31508 commit 982c8f1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
38 changes: 26 additions & 12 deletions src/Pages/Channels.razor
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
Wallet wallet = null;
if (context.OpenedWithId != null)
{
wallet =context.ChannelOperationRequests.FirstOrDefault()?.Wallet;
wallet = context.ChannelOperationRequests.FirstOrDefault()?.Wallet;
}
@(wallet == null ? "Unknown" : wallet.Name)
}
Expand Down Expand Up @@ -152,7 +152,7 @@
<DisplayTemplate>
@{
var balance = Task.Run(() => GetPercentageBalance(context)).Result;

}
@if (balance >= 0)
{
Expand Down Expand Up @@ -473,30 +473,44 @@

private async Task<int> GetPercentageBalance(Channel channel)
{
//If the last update was more than 60 seconds ago, we update the balance dictionary
//If the last update was more than 60 seconds ago, we update the balance dictionary
if (DateTimeOffset.Now.Subtract(_lastBalanceUpdate).TotalSeconds > 60)
{
_channelsBalance = await LightningService.GetChannelsBalance();
_lastBalanceUpdate = DateTimeOffset.Now;
}

try
{
var result = -1.0;
if (_channelsBalance.TryGetValue(channel.ChanId, out var values))
{
var sourceNodeId = values.Item1;
//If the source node is the local node, the remote balance is the third value, otherwise is the second
// Extract the managed node ID from the tuple
var managedNodeId = values.Item1;

// Calculate the capacity as the sum of the second and third values in the tuple
var capacity = values.Item2 + values.Item3;
if (sourceNodeId == channel.SourceNodeId)

// Initialize the remote balance
var remoteBalance = 0L;

// Check if the source node ID of the channel is the same as the managed node ID
// or if the source node is not managed
// If either condition is true, set the remote balance to the third value in the tuple
if (channel.SourceNodeId == managedNodeId || !channel.SourceNode.IsManaged)
{
result = (values.Item3 / (double) capacity) * 100;
remoteBalance = values.Item3;
}
else
{
result = (values.Item2 / (double) capacity) * 100;
// If neither condition is true, set the remote balance to the second value in the tuple
remoteBalance = values.Item2;
}


// Calculate the result as the percentage of the remote balance to the capacity
result = (remoteBalance / (double) capacity) * 100;


result = Math.Round(result, 2);
}

Expand Down Expand Up @@ -743,9 +757,9 @@

private bool OnWalletFilter(object? itemValue, object? searchValue)
{
//If the wallet is null it might be a externally created channel, so we return true
//If the wallet is null it might be a externally created channel, so we return true
if (itemValue == null) return true;

return searchValue == null || (int) searchValue == 0 || (int) itemValue == (int) searchValue;
}

Expand Down
4 changes: 3 additions & 1 deletion src/Services/LightningService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1432,8 +1432,10 @@ public async Task CloseChannel(ChannelOperationRequest channelOperationRequest,
var htlcsLocal = channel.PendingHtlcs.Where(x => x.Incoming == true).Sum(x => x.Amount);
var htlcsRemote = channel.PendingHtlcs.Where(x => x.Incoming == false).Sum(x => x.Amount);

//The nodeguard sided node is the one that is managed by nodeguard
var nodeguardManagedNodeId = node.Id;
result.TryAdd(channel.ChanId,
(node.Id, channel.LocalBalance + htlcsLocal, channel.RemoteBalance + htlcsRemote));
(nodeguardManagedNodeId, channel.LocalBalance + htlcsLocal, channel.RemoteBalance + htlcsRemote));
}
}

Expand Down

0 comments on commit 982c8f1

Please sign in to comment.