get available remote balance

This commit is contained in:
Carsten Otto
2021-11-12 18:13:19 +01:00
parent a746dfd664
commit 9cd344b7ae
13 changed files with 159 additions and 76 deletions

View File

@@ -120,10 +120,15 @@ public class LegacyController {
}
@GetMapping("/channel/{channelId}/available-local-balance")
public long getAvailableLocalBalance(ChannelId channelId) {
public long getAvailableLocalBalance(@PathVariable ChannelId channelId) {
return balanceService.getAvailableLocalBalance(channelId).satoshis();
}
@GetMapping("/channel/{channelId}/available-remote-balance")
public long getAvailableRemoteBalance(@PathVariable ChannelId channelId) {
return balanceService.getAvailableRemoteBalance(channelId).satoshis();
}
private Stream<ChannelId> getOpenChannelIdsSorted() {
return channelService.getOpenChannels().stream()
.map(Channel::getId)

View File

@@ -1,6 +1,7 @@
package de.cotto.lndmanagej.service;
import de.cotto.lndmanagej.grpc.GrpcChannels;
import de.cotto.lndmanagej.model.BalanceInformation;
import de.cotto.lndmanagej.model.ChannelId;
import de.cotto.lndmanagej.model.Coins;
import de.cotto.lndmanagej.model.LocalChannel;
@@ -14,21 +15,17 @@ public class BalanceService {
this.grpcChannels = grpcChannels;
}
public Coins getLocalBalance(ChannelId channelId) {
return grpcChannels.getChannel(channelId).map(LocalChannel::getLocalBalance).orElse(Coins.NONE);
}
public Coins getLocalReserve(ChannelId channelId) {
return grpcChannels.getChannel(channelId).map(LocalChannel::getLocalReserve).orElse(Coins.NONE);
}
public Coins getAvailableLocalBalance(ChannelId channelId) {
Coins available = grpcChannels.getChannel(channelId)
.map(c -> c.getLocalBalance().subtract(c.getLocalReserve()))
return grpcChannels.getChannel(channelId)
.map(LocalChannel::getBalanceInformation)
.map(BalanceInformation::availableLocalBalance)
.orElse(Coins.NONE);
}
public Coins getAvailableRemoteBalance(ChannelId channelId) {
return grpcChannels.getChannel(channelId)
.map(LocalChannel::getBalanceInformation)
.map(BalanceInformation::availableRemoteBalance)
.orElse(Coins.NONE);
if (available.isNegative()) {
return Coins.NONE;
}
return available;
}
}