mirror of
https://github.com/aljazceru/lnd-manageJ.git
synced 2026-01-30 19:24:29 +01:00
get off chain costs for channel
This commit is contained in:
@@ -26,6 +26,14 @@ public class OffChainCostService {
|
||||
);
|
||||
}
|
||||
|
||||
@Timed
|
||||
public OffChainCosts getOffChainCostsForChannel(ChannelId channelId) {
|
||||
return new OffChainCosts(
|
||||
getRebalanceSourceCostsForChannel(channelId),
|
||||
getRebalanceTargetCostsForChannel(channelId)
|
||||
);
|
||||
}
|
||||
|
||||
@Timed
|
||||
public Coins getRebalanceSourceCostsForChannel(ChannelId channelId) {
|
||||
return getSumOfFees(rebalanceService.getRebalancesFromChannel(channelId));
|
||||
|
||||
@@ -12,6 +12,7 @@ import java.util.Set;
|
||||
|
||||
import static de.cotto.lndmanagej.model.ChannelIdFixtures.CHANNEL_ID;
|
||||
import static de.cotto.lndmanagej.model.ChannelIdFixtures.CHANNEL_ID_2;
|
||||
import static de.cotto.lndmanagej.model.ChannelIdFixtures.CHANNEL_ID_4;
|
||||
import static de.cotto.lndmanagej.model.PubkeyFixtures.PUBKEY;
|
||||
import static de.cotto.lndmanagej.model.SelfPaymentFixtures.SELF_PAYMENT;
|
||||
import static de.cotto.lndmanagej.model.SelfPaymentFixtures.SELF_PAYMENT_2;
|
||||
@@ -48,6 +49,26 @@ class OffChainCostServiceTest {
|
||||
assertThat(offChainCostService.getOffChainCostsForPeer(PUBKEY)).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getOffChainCostsForChannel_source() {
|
||||
when(rebalanceService.getRebalancesFromChannel(CHANNEL_ID_4)).thenReturn(Set.of(SELF_PAYMENT, SELF_PAYMENT_2));
|
||||
OffChainCosts expected = new OffChainCosts(
|
||||
COST_FOR_TWO_SELF_PAYMENTS,
|
||||
Coins.NONE
|
||||
);
|
||||
assertThat(offChainCostService.getOffChainCostsForChannel(CHANNEL_ID_4)).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getOffChainCostsForChannel_target() {
|
||||
when(rebalanceService.getRebalancesToChannel(CHANNEL_ID_4)).thenReturn(Set.of(SELF_PAYMENT, SELF_PAYMENT_2));
|
||||
OffChainCosts expected = new OffChainCosts(
|
||||
Coins.NONE,
|
||||
COST_FOR_TWO_SELF_PAYMENTS
|
||||
);
|
||||
assertThat(offChainCostService.getOffChainCostsForChannel(CHANNEL_ID_4)).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getRebalanceSourceCostsForChannel_no_self_payments() {
|
||||
assertThat(offChainCostService.getRebalanceSourceCostsForChannel(CHANNEL_ID)).isEqualTo(Coins.NONE);
|
||||
|
||||
@@ -3,6 +3,7 @@ package de.cotto.lndmanagej.controller;
|
||||
import de.cotto.lndmanagej.model.ChannelIdResolver;
|
||||
import de.cotto.lndmanagej.model.Coins;
|
||||
import de.cotto.lndmanagej.model.FeeReport;
|
||||
import de.cotto.lndmanagej.model.OffChainCosts;
|
||||
import de.cotto.lndmanagej.model.OnChainCosts;
|
||||
import de.cotto.lndmanagej.service.BalanceService;
|
||||
import de.cotto.lndmanagej.service.ChannelService;
|
||||
@@ -35,6 +36,7 @@ import static de.cotto.lndmanagej.model.LocalOpenChannelFixtures.TOTAL_RECEIVED_
|
||||
import static de.cotto.lndmanagej.model.LocalOpenChannelFixtures.TOTAL_SENT;
|
||||
import static de.cotto.lndmanagej.model.LocalOpenChannelFixtures.TOTAL_SENT_2;
|
||||
import static de.cotto.lndmanagej.model.NodeFixtures.ALIAS_2;
|
||||
import static de.cotto.lndmanagej.model.OffChainCostsFixtures.OFF_CHAIN_COSTS;
|
||||
import static de.cotto.lndmanagej.model.OnChainCostsFixtures.ON_CHAIN_COSTS;
|
||||
import static de.cotto.lndmanagej.model.PolicyFixtures.POLICIES;
|
||||
import static de.cotto.lndmanagej.model.PubkeyFixtures.PUBKEY_2;
|
||||
@@ -129,8 +131,7 @@ class ChannelControllerIT {
|
||||
when(nodeService.getAlias(PUBKEY_2)).thenReturn(ALIAS_2);
|
||||
when(channelService.getLocalChannel(CHANNEL_ID)).thenReturn(Optional.of(LOCAL_OPEN_CHANNEL_PRIVATE));
|
||||
when(onChainCostService.getOnChainCostsForChannelId(CHANNEL_ID)).thenReturn(ON_CHAIN_COSTS);
|
||||
when(offChainCostService.getRebalanceSourceCostsForChannel(CHANNEL_ID)).thenReturn(Coins.ofMilliSatoshis(1));
|
||||
when(offChainCostService.getRebalanceTargetCostsForChannel(CHANNEL_ID)).thenReturn(Coins.ofMilliSatoshis(2));
|
||||
when(offChainCostService.getOffChainCostsForChannel(CHANNEL_ID)).thenReturn(OFF_CHAIN_COSTS);
|
||||
when(balanceService.getBalanceInformation(CHANNEL_ID)).thenReturn(Optional.of(BALANCE_INFORMATION_2));
|
||||
when(feeService.getFeeReportForChannel(CHANNEL_ID)).thenReturn(FEE_REPORT);
|
||||
mockMvc.perform(get(DETAILS_PREFIX))
|
||||
@@ -152,8 +153,8 @@ class ChannelControllerIT {
|
||||
.andExpect(jsonPath("$.onChainCosts.openCosts", is("1000")))
|
||||
.andExpect(jsonPath("$.onChainCosts.closeCosts", is("2000")))
|
||||
.andExpect(jsonPath("$.onChainCosts.sweepCosts", is("3000")))
|
||||
.andExpect(jsonPath("$.offChainCosts.rebalanceSource", is("1")))
|
||||
.andExpect(jsonPath("$.offChainCosts.rebalanceTarget", is("2")))
|
||||
.andExpect(jsonPath("$.offChainCosts.rebalanceSource", is("1000000")))
|
||||
.andExpect(jsonPath("$.offChainCosts.rebalanceTarget", is("2000000")))
|
||||
.andExpect(jsonPath("$.balance.localBalance", is("2000")))
|
||||
.andExpect(jsonPath("$.balance.localReserve", is("200")))
|
||||
.andExpect(jsonPath("$.balance.localAvailable", is("1800")))
|
||||
@@ -175,8 +176,7 @@ class ChannelControllerIT {
|
||||
when(channelService.getLocalChannel(CHANNEL_ID)).thenReturn(Optional.of(CLOSED_CHANNEL));
|
||||
when(feeService.getFeeReportForChannel(CHANNEL_ID)).thenReturn(new FeeReport(Coins.NONE, Coins.NONE));
|
||||
when(onChainCostService.getOnChainCostsForChannelId(CHANNEL_ID)).thenReturn(OnChainCosts.NONE);
|
||||
when(offChainCostService.getRebalanceSourceCostsForChannel(CHANNEL_ID)).thenReturn(Coins.ofMilliSatoshis(1));
|
||||
when(offChainCostService.getRebalanceTargetCostsForChannel(CHANNEL_ID)).thenReturn(Coins.ofMilliSatoshis(2));
|
||||
when(offChainCostService.getOffChainCostsForChannel(CHANNEL_ID)).thenReturn(OffChainCosts.NONE);
|
||||
mockMvc.perform(get(DETAILS_PREFIX))
|
||||
.andExpect(jsonPath("$.closeDetails.initiator", is("REMOTE")))
|
||||
.andExpect(jsonPath("$.closeDetails.height", is(987_654)))
|
||||
|
||||
@@ -7,12 +7,10 @@ import de.cotto.lndmanagej.controller.dto.ChannelDto;
|
||||
import de.cotto.lndmanagej.controller.dto.ClosedChannelDetailsDto;
|
||||
import de.cotto.lndmanagej.controller.dto.FeeReportDto;
|
||||
import de.cotto.lndmanagej.controller.dto.ObjectMapperConfiguration;
|
||||
import de.cotto.lndmanagej.controller.dto.OffChainCostsDto;
|
||||
import de.cotto.lndmanagej.controller.dto.PoliciesDto;
|
||||
import de.cotto.lndmanagej.model.BalanceInformation;
|
||||
import de.cotto.lndmanagej.model.ChannelId;
|
||||
import de.cotto.lndmanagej.model.ClosedChannel;
|
||||
import de.cotto.lndmanagej.model.Coins;
|
||||
import de.cotto.lndmanagej.model.FeeReport;
|
||||
import de.cotto.lndmanagej.model.LocalChannel;
|
||||
import de.cotto.lndmanagej.model.OpenCloseStatus;
|
||||
@@ -89,7 +87,7 @@ public class ChannelController {
|
||||
remoteAlias,
|
||||
getBalanceInformation(channelId),
|
||||
onChainCostService.getOnChainCostsForChannelId(channelId),
|
||||
getOffChainCosts(channelId),
|
||||
offChainCostService.getOffChainCostsForChannel(channelId),
|
||||
getPoliciesForChannel(localChannel),
|
||||
getCloseDetailsForChannel(localChannel),
|
||||
getFeeReportFromService(localChannel.getId())
|
||||
@@ -144,12 +142,6 @@ public class ChannelController {
|
||||
.orElse(BalanceInformation.EMPTY);
|
||||
}
|
||||
|
||||
private OffChainCostsDto getOffChainCosts(ChannelId channelId) {
|
||||
Coins rebalanceSource = offChainCostService.getRebalanceSourceCostsForChannel(channelId);
|
||||
Coins rebalanceTarget = offChainCostService.getRebalanceTargetCostsForChannel(channelId);
|
||||
return new OffChainCostsDto(rebalanceSource, rebalanceTarget);
|
||||
}
|
||||
|
||||
private ClosedChannelDetailsDto getCloseDetailsForChannel(LocalChannel localChannel) {
|
||||
return ClosedChannelDetailsDto.createFromModel(localChannel);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import de.cotto.lndmanagej.model.BalanceInformation;
|
||||
import de.cotto.lndmanagej.model.ChannelPoint;
|
||||
import de.cotto.lndmanagej.model.FeeReport;
|
||||
import de.cotto.lndmanagej.model.LocalChannel;
|
||||
import de.cotto.lndmanagej.model.OffChainCosts;
|
||||
import de.cotto.lndmanagej.model.OnChainCosts;
|
||||
import de.cotto.lndmanagej.model.OpenInitiator;
|
||||
import de.cotto.lndmanagej.model.Pubkey;
|
||||
@@ -33,7 +34,7 @@ public record ChannelDetailsDto(
|
||||
String remoteAlias,
|
||||
BalanceInformation balanceInformation,
|
||||
OnChainCosts onChainCosts,
|
||||
OffChainCostsDto offChainCosts,
|
||||
OffChainCosts offChainCosts,
|
||||
PoliciesDto policies,
|
||||
FeeReport feeReport
|
||||
) {
|
||||
@@ -52,7 +53,7 @@ public record ChannelDetailsDto(
|
||||
channelDto.status(),
|
||||
BalanceInformationDto.createFromModel(balanceInformation),
|
||||
OnChainCostsDto.createFromModel(onChainCosts),
|
||||
offChainCosts,
|
||||
OffChainCostsDto.createFromModel(offChainCosts),
|
||||
policies,
|
||||
channelDto.closeDetails(),
|
||||
FeeReportDto.createFromModel(feeReport)
|
||||
@@ -64,7 +65,7 @@ public record ChannelDetailsDto(
|
||||
String remoteAlias,
|
||||
BalanceInformation balanceInformation,
|
||||
OnChainCosts onChainCosts,
|
||||
OffChainCostsDto offChainCosts,
|
||||
OffChainCosts offChainCosts,
|
||||
PoliciesDto policies,
|
||||
ClosedChannelDetailsDto closeDetails,
|
||||
FeeReport feeReport
|
||||
|
||||
@@ -5,13 +5,11 @@ import de.cotto.lndmanagej.controller.dto.ChannelDetailsDto;
|
||||
import de.cotto.lndmanagej.controller.dto.ChannelDto;
|
||||
import de.cotto.lndmanagej.controller.dto.ClosedChannelDetailsDto;
|
||||
import de.cotto.lndmanagej.controller.dto.FeeReportDto;
|
||||
import de.cotto.lndmanagej.controller.dto.OffChainCostsDto;
|
||||
import de.cotto.lndmanagej.controller.dto.PoliciesDto;
|
||||
import de.cotto.lndmanagej.model.BalanceInformation;
|
||||
import de.cotto.lndmanagej.model.Coins;
|
||||
import de.cotto.lndmanagej.model.FeeReport;
|
||||
import de.cotto.lndmanagej.model.LocalChannel;
|
||||
import de.cotto.lndmanagej.model.OnChainCosts;
|
||||
import de.cotto.lndmanagej.service.BalanceService;
|
||||
import de.cotto.lndmanagej.service.ChannelService;
|
||||
import de.cotto.lndmanagej.service.FeeService;
|
||||
@@ -34,6 +32,8 @@ import static de.cotto.lndmanagej.model.CoopClosedChannelFixtures.CLOSED_CHANNEL
|
||||
import static de.cotto.lndmanagej.model.LocalOpenChannelFixtures.LOCAL_OPEN_CHANNEL;
|
||||
import static de.cotto.lndmanagej.model.LocalOpenChannelFixtures.LOCAL_OPEN_CHANNEL_PRIVATE;
|
||||
import static de.cotto.lndmanagej.model.NodeFixtures.ALIAS_2;
|
||||
import static de.cotto.lndmanagej.model.OffChainCostsFixtures.OFF_CHAIN_COSTS;
|
||||
import static de.cotto.lndmanagej.model.OnChainCostsFixtures.ON_CHAIN_COSTS;
|
||||
import static de.cotto.lndmanagej.model.PolicyFixtures.POLICIES;
|
||||
import static de.cotto.lndmanagej.model.PubkeyFixtures.PUBKEY_2;
|
||||
import static de.cotto.lndmanagej.model.WaitingCloseChannelFixtures.WAITING_CLOSE_CHANNEL;
|
||||
@@ -44,13 +44,6 @@ import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class ChannelControllerTest {
|
||||
private static final Coins OPEN_COSTS = Coins.ofSatoshis(1);
|
||||
private static final Coins CLOSE_COSTS = Coins.ofSatoshis(2);
|
||||
private static final Coins SWEEP_COSTS = Coins.ofSatoshis(3);
|
||||
private static final Coins SOURCE_COSTS = Coins.ofSatoshis(3);
|
||||
private static final Coins TARGET_COSTS = Coins.ofSatoshis(4);
|
||||
private static final OnChainCosts ON_CHAIN_COSTS = new OnChainCosts(OPEN_COSTS, CLOSE_COSTS, SWEEP_COSTS);
|
||||
private static final OffChainCostsDto OFF_CHAIN_COSTS = new OffChainCostsDto(SOURCE_COSTS, TARGET_COSTS);
|
||||
private static final PoliciesDto FEE_CONFIGURATION_DTO = PoliciesDto.createFromModel(POLICIES);
|
||||
private static final ClosedChannelDetailsDto CLOSED_CHANNEL_DETAILS_DTO =
|
||||
ClosedChannelDetailsDto.createFromModel(CLOSED_CHANNEL);
|
||||
@@ -83,8 +76,7 @@ class ChannelControllerTest {
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
lenient().when(onChainCostService.getOnChainCostsForChannelId(CHANNEL_ID)).thenReturn(ON_CHAIN_COSTS);
|
||||
lenient().when(offChainCostService.getRebalanceSourceCostsForChannel(CHANNEL_ID)).thenReturn(SOURCE_COSTS);
|
||||
lenient().when(offChainCostService.getRebalanceTargetCostsForChannel(CHANNEL_ID)).thenReturn(TARGET_COSTS);
|
||||
lenient().when(offChainCostService.getOffChainCostsForChannel(CHANNEL_ID)).thenReturn(OFF_CHAIN_COSTS);
|
||||
lenient().when(policyService.getPolicies(CHANNEL_ID)).thenReturn(POLICIES);
|
||||
lenient().when(feeService.getFeeReportForChannel(CHANNEL_ID)).thenReturn(FEE_REPORT);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package de.cotto.lndmanagej.controller.dto;
|
||||
import de.cotto.lndmanagej.model.ChannelStatus;
|
||||
import de.cotto.lndmanagej.model.Coins;
|
||||
import de.cotto.lndmanagej.model.FeeReport;
|
||||
import de.cotto.lndmanagej.model.OnChainCosts;
|
||||
import de.cotto.lndmanagej.model.OpenInitiator;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -14,16 +13,14 @@ import static de.cotto.lndmanagej.model.ChannelPointFixtures.CHANNEL_POINT;
|
||||
import static de.cotto.lndmanagej.model.CoopClosedChannelFixtures.CLOSED_CHANNEL;
|
||||
import static de.cotto.lndmanagej.model.LocalOpenChannelFixtures.LOCAL_OPEN_CHANNEL;
|
||||
import static de.cotto.lndmanagej.model.NodeFixtures.ALIAS;
|
||||
import static de.cotto.lndmanagej.model.OffChainCostsFixtures.OFF_CHAIN_COSTS;
|
||||
import static de.cotto.lndmanagej.model.OnChainCostsFixtures.ON_CHAIN_COSTS;
|
||||
import static de.cotto.lndmanagej.model.OpenCloseStatus.OPEN;
|
||||
import static de.cotto.lndmanagej.model.PubkeyFixtures.PUBKEY_2;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class ChannelDetailsDtoTest {
|
||||
|
||||
private static final OnChainCosts ON_CHAIN_COSTS =
|
||||
new OnChainCosts(Coins.ofSatoshis(1), Coins.ofSatoshis(2), Coins.ofSatoshis(3));
|
||||
private static final OffChainCostsDto OFF_CHAIN_COSTS =
|
||||
new OffChainCostsDto(Coins.ofSatoshis(3), Coins.ofSatoshis(4));
|
||||
private static final ClosedChannelDetailsDto CLOSE_DETAILS =
|
||||
ClosedChannelDetailsDto.createFromModel(CLOSED_CHANNEL);
|
||||
private static final FeeReport FEE_REPORT =
|
||||
@@ -123,6 +120,6 @@ class ChannelDetailsDtoTest {
|
||||
|
||||
@Test
|
||||
void offChainCosts() {
|
||||
assertThat(CHANNEL_DETAILS_DTO.offChainCosts()).isEqualTo(OFF_CHAIN_COSTS);
|
||||
assertThat(CHANNEL_DETAILS_DTO.offChainCosts()).isEqualTo(OffChainCostsDto.createFromModel(OFF_CHAIN_COSTS));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user