mirror of
https://github.com/aljazceru/lnd-manageJ.git
synced 2026-01-24 08:24:20 +01:00
add aggregated balance information to node details
This commit is contained in:
@@ -1,16 +1,19 @@
|
||||
package de.cotto.lndmanagej.controller;
|
||||
|
||||
import com.codahale.metrics.MetricRegistry;
|
||||
import de.cotto.lndmanagej.controller.dto.BalanceInformationDto;
|
||||
import de.cotto.lndmanagej.controller.dto.ChannelsForNodeDto;
|
||||
import de.cotto.lndmanagej.controller.dto.NodeDetailsDto;
|
||||
import de.cotto.lndmanagej.controller.dto.ObjectMapperConfiguration;
|
||||
import de.cotto.lndmanagej.controller.dto.OnChainCostsDto;
|
||||
import de.cotto.lndmanagej.metrics.Metrics;
|
||||
import de.cotto.lndmanagej.model.BalanceInformation;
|
||||
import de.cotto.lndmanagej.model.Channel;
|
||||
import de.cotto.lndmanagej.model.ChannelId;
|
||||
import de.cotto.lndmanagej.model.Coins;
|
||||
import de.cotto.lndmanagej.model.Node;
|
||||
import de.cotto.lndmanagej.model.Pubkey;
|
||||
import de.cotto.lndmanagej.service.BalanceService;
|
||||
import de.cotto.lndmanagej.service.ChannelService;
|
||||
import de.cotto.lndmanagej.service.NodeService;
|
||||
import de.cotto.lndmanagej.service.OnChainCostService;
|
||||
@@ -32,17 +35,20 @@ public class NodeController {
|
||||
private final Metrics metrics;
|
||||
private final ChannelService channelService;
|
||||
private final OnChainCostService onChainCostService;
|
||||
private final BalanceService balanceService;
|
||||
|
||||
public NodeController(
|
||||
NodeService nodeService,
|
||||
ChannelService channelService,
|
||||
Metrics metrics,
|
||||
OnChainCostService onChainCostService
|
||||
OnChainCostService onChainCostService,
|
||||
BalanceService balanceService
|
||||
) {
|
||||
this.nodeService = nodeService;
|
||||
this.metrics = metrics;
|
||||
this.channelService = channelService;
|
||||
this.onChainCostService = onChainCostService;
|
||||
this.balanceService = balanceService;
|
||||
}
|
||||
|
||||
@GetMapping("/alias")
|
||||
@@ -57,6 +63,7 @@ public class NodeController {
|
||||
Node node = nodeService.getNode(pubkey);
|
||||
Coins openCosts = onChainCostService.getOpenCostsWith(pubkey);
|
||||
Coins closeCosts = onChainCostService.getCloseCostsWith(pubkey);
|
||||
BalanceInformation balanceInformation = balanceService.getBalanceInformation(pubkey);
|
||||
return new NodeDetailsDto(
|
||||
pubkey,
|
||||
node.alias(),
|
||||
@@ -65,6 +72,7 @@ public class NodeController {
|
||||
toSortedList(channelService.getWaitingCloseChannelsFor(pubkey)),
|
||||
toSortedList(channelService.getForceClosingChannelsFor(pubkey)),
|
||||
new OnChainCostsDto(openCosts, closeCosts),
|
||||
BalanceInformationDto.createFrom(balanceInformation),
|
||||
node.online()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package de.cotto.lndmanagej.controller.dto;
|
||||
|
||||
import de.cotto.lndmanagej.model.BalanceInformation;
|
||||
import de.cotto.lndmanagej.model.Coins;
|
||||
|
||||
public record BalanceInformationDto(
|
||||
String localBalance,
|
||||
String localReserve,
|
||||
String localAvailable,
|
||||
String remoteBalance,
|
||||
String remoteReserve,
|
||||
String remoteAvailable
|
||||
) {
|
||||
public static BalanceInformationDto createFrom(BalanceInformation balanceInformation) {
|
||||
return new BalanceInformationDto(
|
||||
toString(balanceInformation.localBalance()),
|
||||
toString(balanceInformation.localReserve()),
|
||||
toString(balanceInformation.localAvailable()),
|
||||
toString(balanceInformation.remoteBalance()),
|
||||
toString(balanceInformation.remoteReserve()),
|
||||
toString(balanceInformation.remoteAvailable())
|
||||
);
|
||||
}
|
||||
|
||||
private static String toString(Coins coins) {
|
||||
return String.valueOf(coins.satoshis());
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ public record NodeDetailsDto(
|
||||
List<ChannelId> waitingCloseChannels,
|
||||
List<ChannelId> pendingForceClosingChannels,
|
||||
OnChainCostsDto onChainCosts,
|
||||
BalanceInformationDto balance,
|
||||
boolean online
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package de.cotto.lndmanagej.service;
|
||||
|
||||
import de.cotto.lndmanagej.grpc.GrpcChannels;
|
||||
import de.cotto.lndmanagej.model.BalanceInformation;
|
||||
import de.cotto.lndmanagej.model.Channel;
|
||||
import de.cotto.lndmanagej.model.ChannelId;
|
||||
import de.cotto.lndmanagej.model.Coins;
|
||||
import de.cotto.lndmanagej.model.LocalOpenChannel;
|
||||
@@ -29,7 +30,7 @@ public class BalanceService {
|
||||
|
||||
public Coins getAvailableLocalBalance(ChannelId channelId) {
|
||||
return getBalanceInformation(channelId)
|
||||
.map(BalanceInformation::availableLocalBalance)
|
||||
.map(BalanceInformation::localAvailable)
|
||||
.orElse(Coins.NONE);
|
||||
}
|
||||
|
||||
@@ -42,10 +43,18 @@ public class BalanceService {
|
||||
|
||||
public Coins getAvailableRemoteBalance(ChannelId channelId) {
|
||||
return getBalanceInformation(channelId)
|
||||
.map(BalanceInformation::availableRemoteBalance)
|
||||
.map(BalanceInformation::remoteAvailable)
|
||||
.orElse(Coins.NONE);
|
||||
}
|
||||
|
||||
public BalanceInformation getBalanceInformation(Pubkey pubkey) {
|
||||
return channelService.getOpenChannelsWith(pubkey).stream()
|
||||
.map(Channel::getId)
|
||||
.map(this::getBalanceInformation)
|
||||
.flatMap(Optional::stream)
|
||||
.reduce(BalanceInformation.EMPTY, BalanceInformation::add);
|
||||
}
|
||||
|
||||
private Optional<BalanceInformation> getBalanceInformation(ChannelId channelId) {
|
||||
return grpcChannels.getChannel(channelId)
|
||||
.map(LocalOpenChannel::getBalanceInformation);
|
||||
|
||||
Reference in New Issue
Block a user