mirror of
https://github.com/aljazceru/lnd-manageJ.git
synced 2026-01-24 08:24:20 +01:00
get balance summed up for all channels with peer
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user