get balance summed up for all channels with peer

This commit is contained in:
Carsten Otto
2021-11-13 14:35:29 +01:00
parent 887952d01e
commit 8ce2617f55
5 changed files with 120 additions and 21 deletions

View File

@@ -136,17 +136,29 @@ public class LegacyController {
}
@GetMapping("/channel/{channelId}/available-local-balance")
public long getAvailableLocalBalance(@PathVariable ChannelId channelId) {
mark("getAvailableLocalBalance");
public long getAvailableLocalBalanceForChannel(@PathVariable ChannelId channelId) {
mark("getAvailableLocalBalanceForChannel");
return balanceService.getAvailableLocalBalance(channelId).satoshis();
}
@GetMapping("/channel/{channelId}/available-remote-balance")
public long getAvailableRemoteBalance(@PathVariable ChannelId channelId) {
mark("getAvailableRemoteBalance");
public long getAvailableRemoteBalanceForChannel(@PathVariable ChannelId channelId) {
mark("getAvailableRemoteBalanceForChannel");
return balanceService.getAvailableRemoteBalance(channelId).satoshis();
}
@GetMapping("/node/{pubkey}/available-local-balance")
public long getAvailableLocalBalanceForPeer(@PathVariable Pubkey pubkey) {
mark("getAvailableLocalBalanceForPeer");
return balanceService.getAvailableLocalBalance(pubkey).satoshis();
}
@GetMapping("/node/{pubkey}/available-remote-balance")
public long getAvailableRemoteBalanceForPeer(@PathVariable Pubkey pubkey) {
mark("getAvailableRemoteBalanceForPeer");
return balanceService.getAvailableRemoteBalance(pubkey).satoshis();
}
private Stream<ChannelId> getOpenChannelIdsSorted() {
return channelService.getOpenChannels().stream()
.map(Channel::getId)

View File

@@ -5,27 +5,49 @@ import de.cotto.lndmanagej.model.BalanceInformation;
import de.cotto.lndmanagej.model.ChannelId;
import de.cotto.lndmanagej.model.Coins;
import de.cotto.lndmanagej.model.LocalChannel;
import de.cotto.lndmanagej.model.Pubkey;
import org.springframework.stereotype.Component;
import java.util.Optional;
@Component
public class BalanceService {
private final GrpcChannels grpcChannels;
private final ChannelService channelService;
public BalanceService(GrpcChannels grpcChannels) {
public BalanceService(GrpcChannels grpcChannels, ChannelService channelService) {
this.grpcChannels = grpcChannels;
this.channelService = channelService;
}
public Coins getAvailableLocalBalance(Pubkey peer) {
return channelService.getOpenChannelsWith(peer).stream()
.map(LocalChannel::getId)
.map(this::getAvailableLocalBalance)
.reduce(Coins.NONE, Coins::add);
}
public Coins getAvailableLocalBalance(ChannelId channelId) {
return grpcChannels.getChannel(channelId)
.map(LocalChannel::getBalanceInformation)
return getBalanceInformation(channelId)
.map(BalanceInformation::availableLocalBalance)
.orElse(Coins.NONE);
}
public Coins getAvailableRemoteBalance(Pubkey peer) {
return channelService.getOpenChannelsWith(peer).stream()
.map(LocalChannel::getId)
.map(this::getAvailableRemoteBalance)
.reduce(Coins.NONE, Coins::add);
}
public Coins getAvailableRemoteBalance(ChannelId channelId) {
return grpcChannels.getChannel(channelId)
.map(LocalChannel::getBalanceInformation)
return getBalanceInformation(channelId)
.map(BalanceInformation::availableRemoteBalance)
.orElse(Coins.NONE);
}
private Optional<BalanceInformation> getBalanceInformation(ChannelId channelId) {
return grpcChannels.getChannel(channelId)
.map(LocalChannel::getBalanceInformation);
}
}