mirror of
https://github.com/aljazceru/lnd-manageJ.git
synced 2026-01-24 16:34:19 +01:00
get available local balance
This commit is contained in:
@@ -4,6 +4,7 @@ import de.cotto.lndmanagej.model.Channel;
|
||||
import de.cotto.lndmanagej.model.ChannelId;
|
||||
import de.cotto.lndmanagej.model.LocalChannel;
|
||||
import de.cotto.lndmanagej.model.Pubkey;
|
||||
import de.cotto.lndmanagej.service.BalanceService;
|
||||
import de.cotto.lndmanagej.service.ChannelService;
|
||||
import de.cotto.lndmanagej.service.FeeService;
|
||||
import de.cotto.lndmanagej.service.NodeService;
|
||||
@@ -25,17 +26,20 @@ public class LegacyController {
|
||||
private final ChannelService channelService;
|
||||
private final OwnNodeService ownNodeService;
|
||||
private final FeeService feeService;
|
||||
private final BalanceService balanceService;
|
||||
|
||||
public LegacyController(
|
||||
NodeService nodeService,
|
||||
ChannelService channelService,
|
||||
OwnNodeService ownNodeService,
|
||||
FeeService feeService
|
||||
FeeService feeService,
|
||||
BalanceService balanceService
|
||||
) {
|
||||
this.nodeService = nodeService;
|
||||
this.channelService = channelService;
|
||||
this.ownNodeService = ownNodeService;
|
||||
this.feeService = feeService;
|
||||
this.balanceService = balanceService;
|
||||
}
|
||||
|
||||
@GetMapping("/node/{pubkey}/alias")
|
||||
@@ -115,6 +119,11 @@ public class LegacyController {
|
||||
return feeService.getOutgoingBaseFee(channelId).milliSatoshis();
|
||||
}
|
||||
|
||||
@GetMapping("/channel/{channelId}/available-local-balance")
|
||||
public long getAvailableLocalBalance(ChannelId channelId) {
|
||||
return balanceService.getAvailableLocalBalance(channelId).satoshis();
|
||||
}
|
||||
|
||||
private Stream<ChannelId> getOpenChannelIdsSorted() {
|
||||
return channelService.getOpenChannels().stream()
|
||||
.map(Channel::getId)
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package de.cotto.lndmanagej.service;
|
||||
|
||||
import de.cotto.lndmanagej.grpc.GrpcChannels;
|
||||
import de.cotto.lndmanagej.model.ChannelId;
|
||||
import de.cotto.lndmanagej.model.Coins;
|
||||
import de.cotto.lndmanagej.model.LocalChannel;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class BalanceService {
|
||||
private final GrpcChannels grpcChannels;
|
||||
|
||||
public BalanceService(GrpcChannels grpcChannels) {
|
||||
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()))
|
||||
.orElse(Coins.NONE);
|
||||
if (available.isNegative()) {
|
||||
return Coins.NONE;
|
||||
}
|
||||
return available;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user