diff --git a/backend/src/main/java/de/cotto/lndmanagej/service/RebalanceService.java b/backend/src/main/java/de/cotto/lndmanagej/service/RebalanceService.java index dd76ad38..263c32e2 100644 --- a/backend/src/main/java/de/cotto/lndmanagej/service/RebalanceService.java +++ b/backend/src/main/java/de/cotto/lndmanagej/service/RebalanceService.java @@ -30,7 +30,9 @@ public class RebalanceService { getSourceCostsForChannel(channelId), getAmountFromChannel(channelId), getTargetCostsForChannel(channelId), - getAmountToChannel(channelId) + getAmountToChannel(channelId), + getSupportAsSourceAmountFromChannel(channelId), + getSupportAsTargetAmountToChannel(channelId) ); } @@ -40,7 +42,9 @@ public class RebalanceService { getSourceCostsForPeer(pubkey), getAmountFromPeer(pubkey), getTargetCostsForPeer(pubkey), - getAmountToPeer(pubkey) + getAmountToPeer(pubkey), + Coins.NONE, + Coins.NONE ); } @@ -84,6 +88,36 @@ public class RebalanceService { return getSumOfAmountPaid(getRebalancesToPeer(pubkey)); } + @Timed + public Coins getSupportAsSourceAmountFromChannel(ChannelId channelId) { + Coins amountSourceTotal = getSumOfAmountPaid(selfPaymentsService.getSelfPaymentsFromChannel(channelId)); + Coins amountRebalanceSource = getAmountFromChannel(channelId); + return amountSourceTotal.subtract(amountRebalanceSource); + } + + @Timed + public Coins getSupportAsTargetAmountToChannel(ChannelId channelId) { + Coins amountTargetTotal = getSumOfAmountPaid(selfPaymentsService.getSelfPaymentsToChannel(channelId)); + Coins amountRebalanceTarget = getAmountToChannel(channelId); + return amountTargetTotal.subtract(amountRebalanceTarget); + } + + @Timed + public Coins getSupportAsSourceAmountFromPeer(Pubkey pubkey) { + return channelService.getAllChannelsWith(pubkey).parallelStream() + .map(Channel::getId) + .map(this::getSupportAsSourceAmountFromChannel) + .reduce(Coins.NONE, Coins::add); + } + + @Timed + public Coins getSupportAsTargetAmountToPeer(Pubkey pubkey) { + return channelService.getAllChannelsWith(pubkey).parallelStream() + .map(Channel::getId) + .map(this::getSupportAsTargetAmountToChannel) + .reduce(Coins.NONE, Coins::add); + } + private Set getRebalancesFromChannel(ChannelId channelId) { return selfPaymentsService.getSelfPaymentsFromChannel(channelId).stream() .filter(selfPayment -> memoMentionsChannel(selfPayment, channelId)) diff --git a/backend/src/test/java/de/cotto/lndmanagej/service/RebalanceServiceTest.java b/backend/src/test/java/de/cotto/lndmanagej/service/RebalanceServiceTest.java index 365dd03f..fea4d01c 100644 --- a/backend/src/test/java/de/cotto/lndmanagej/service/RebalanceServiceTest.java +++ b/backend/src/test/java/de/cotto/lndmanagej/service/RebalanceServiceTest.java @@ -21,6 +21,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_3; +import static de.cotto.lndmanagej.model.CoopClosedChannelFixtures.CLOSED_CHANNEL; import static de.cotto.lndmanagej.model.CoopClosedChannelFixtures.CLOSED_CHANNEL_2; import static de.cotto.lndmanagej.model.LocalOpenChannelFixtures.LOCAL_OPEN_CHANNEL; import static de.cotto.lndmanagej.model.LocalOpenChannelFixtures.LOCAL_OPEN_CHANNEL_3; @@ -59,45 +60,105 @@ class RebalanceServiceTest { @Test void getRebalanceReportForChannel_source() { - RebalanceReport expected = - new RebalanceReport(FEE_FOR_TWO_REBALANCES, AMOUNT_FOR_TWO_REBALANCES, Coins.NONE, Coins.NONE); - mockSelfPaymentsFromChannel("from " + CHANNEL_ID.getShortChannelId()); + RebalanceReport expected = new RebalanceReport( + FEE_FOR_TWO_REBALANCES, + AMOUNT_FOR_TWO_REBALANCES, + Coins.NONE, + Coins.NONE, + Coins.NONE, + Coins.NONE + ); + mockSelfPaymentsFromChannel("source: " + CHANNEL_ID.getShortChannelId()); assertThat(rebalanceService.getReportForChannel(CHANNEL_ID)).isEqualTo(expected); } @Test void getRebalanceReportForChannel_target() { - RebalanceReport expected = - new RebalanceReport(Coins.NONE, Coins.NONE, FEE_FOR_TWO_REBALANCES, AMOUNT_FOR_TWO_REBALANCES); - mockSelfPaymentsToChannel("to " + CHANNEL_ID_2.getShortChannelId()); + RebalanceReport expected = new RebalanceReport( + Coins.NONE, + Coins.NONE, + FEE_FOR_TWO_REBALANCES, + AMOUNT_FOR_TWO_REBALANCES, + Coins.NONE, + Coins.NONE + ); + mockSelfPaymentsToChannel("sending to " + CHANNEL_ID_2.getShortChannelId()); + assertThat(rebalanceService.getReportForChannel(CHANNEL_ID_2)).isEqualTo(expected); + } + + @Test + void getRebalanceReportForChannel_supportAsSource() { + RebalanceReport expected = new RebalanceReport( + PAYMENT_FEES, + AMOUNT_PAID, + Coins.NONE, + Coins.NONE, + AMOUNT_FOR_TWO_REBALANCES, + Coins.NONE + ); + when(selfPaymentsService.getSelfPaymentsFromChannel(CHANNEL_ID)).thenReturn(List.of( + getSelfPayment("dest: " + CHANNEL_ID_2, 0), + getSelfPayment("into " + CHANNEL_ID_2, 1), + getSelfPayment(CHANNEL_ID.toString(), 1) + )); + assertThat(rebalanceService.getReportForChannel(CHANNEL_ID)).isEqualTo(expected); + } + + @Test + void getRebalanceReportForChannel_supportAsTarget() { + RebalanceReport expected = new RebalanceReport( + Coins.NONE, + Coins.NONE, + PAYMENT_FEES, + AMOUNT_PAID, + Coins.NONE, + AMOUNT_FOR_TWO_REBALANCES + ); + when(selfPaymentsService.getSelfPaymentsToChannel(CHANNEL_ID_2)).thenReturn(List.of( + getSelfPayment("filling up " + CHANNEL_ID_2, 0), + getSelfPayment("f: " + CHANNEL_ID, 1), + getSelfPayment(CHANNEL_ID + " is source", 1) + )); assertThat(rebalanceService.getReportForChannel(CHANNEL_ID_2)).isEqualTo(expected); } @Test void getRebalanceReportForPeer_source() { - RebalanceReport expected = - new RebalanceReport(FEE_FOR_TWO_REBALANCES, AMOUNT_FOR_TWO_REBALANCES, Coins.NONE, Coins.NONE); + RebalanceReport expected = new RebalanceReport( + FEE_FOR_TWO_REBALANCES, + AMOUNT_FOR_TWO_REBALANCES, + Coins.NONE, + Coins.NONE, + Coins.NONE, + Coins.NONE + ); mockTwoChannelsAndPaymentsFromPeer(); assertThat(rebalanceService.getReportForPeer(PUBKEY)).isEqualTo(expected); } @Test void getRebalanceReportForPeer_target() { - RebalanceReport expected = - new RebalanceReport(Coins.NONE, Coins.NONE, FEE_FOR_TWO_REBALANCES, AMOUNT_FOR_TWO_REBALANCES); + RebalanceReport expected = new RebalanceReport( + Coins.NONE, + Coins.NONE, + FEE_FOR_TWO_REBALANCES, + AMOUNT_FOR_TWO_REBALANCES, + Coins.NONE, + Coins.NONE + ); mockTwoChannelsAndPaymentsToPeer(); assertThat(rebalanceService.getReportForPeer(PUBKEY)).isEqualTo(expected); } @Test void getSourceCostsForChannel() { - mockSelfPaymentsFromChannel("from " + CHANNEL_ID.getShortChannelId()); + mockSelfPaymentsFromChannel(CHANNEL_ID.toString()); assertThat(rebalanceService.getSourceCostsForChannel(CHANNEL_ID)).isEqualTo(FEE_FOR_TWO_REBALANCES); } @Test void getTargetCostsForChannel() { - mockSelfPaymentsToChannel("to " + CHANNEL_ID_2.getShortChannelId()); + mockSelfPaymentsToChannel("sending into " + CHANNEL_ID_2.getShortChannelId()); assertThat(rebalanceService.getTargetCostsForChannel(CHANNEL_ID_2)).isEqualTo(FEE_FOR_TWO_REBALANCES); } @@ -127,13 +188,13 @@ class RebalanceServiceTest { @Test void getAmountFromChannel() { - mockSelfPaymentsFromChannel("from " + CHANNEL_ID.getShortChannelId()); + mockSelfPaymentsFromChannel("taking out of " + CHANNEL_ID.getShortChannelId()); assertThat(rebalanceService.getAmountFromChannel(CHANNEL_ID)).isEqualTo(AMOUNT_FOR_TWO_REBALANCES); } @Test void getAmountToChannel() { - mockSelfPaymentsToChannel("to " + CHANNEL_ID_2.getShortChannelId()); + mockSelfPaymentsToChannel("into " + CHANNEL_ID_2.getShortChannelId()); assertThat(rebalanceService.getAmountToChannel(CHANNEL_ID_2)).isEqualTo(AMOUNT_FOR_TWO_REBALANCES); } @@ -202,6 +263,48 @@ class RebalanceServiceTest { assertThat(rebalanceService.getTargetCostsForChannel(CHANNEL_ID_2)).isEqualTo(Coins.NONE); } + @Test + void getSupportAsSourceAmountFromChannel() { + when(selfPaymentsService.getSelfPaymentsFromChannel(CHANNEL_ID)).thenReturn(List.of( + getSelfPayment("out of " + CHANNEL_ID, 0), + getSelfPayment("emptying " + CHANNEL_ID, 1), + getSelfPayment("rebalancing " + CHANNEL_ID_2, 2) + )); + assertThat(rebalanceService.getSupportAsSourceAmountFromChannel(CHANNEL_ID)).isEqualTo(AMOUNT_PAID); + } + + @Test + void getSupportAsSourceAmountFromPeer() { + when(channelService.getAllChannelsWith(PUBKEY)).thenReturn(Set.of(CLOSED_CHANNEL)); + when(selfPaymentsService.getSelfPaymentsFromChannel(CHANNEL_ID)).thenReturn(List.of( + getSelfPayment("from " + CHANNEL_ID, 0), + getSelfPayment("from: " + CHANNEL_ID, 1), + getSelfPayment("to: " + CHANNEL_ID_2, 2) + )); + assertThat(rebalanceService.getSupportAsSourceAmountFromPeer(PUBKEY)).isEqualTo(AMOUNT_PAID); + } + + @Test + void getSupportAsTargetAmountToChannel() { + when(selfPaymentsService.getSelfPaymentsToChannel(CHANNEL_ID_2)).thenReturn(List.of( + getSelfPayment("from " + CHANNEL_ID, 0), + getSelfPayment("into " + CHANNEL_ID_2, 1), + getSelfPayment("to " + CHANNEL_ID_2, 2) + )); + assertThat(rebalanceService.getSupportAsTargetAmountToChannel(CHANNEL_ID_2)).isEqualTo(AMOUNT_PAID); + } + + @Test + void getSupportAsTargetAmountToPeer() { + when(channelService.getAllChannelsWith(PUBKEY)).thenReturn(Set.of(CLOSED_CHANNEL_2)); + when(selfPaymentsService.getSelfPaymentsToChannel(CHANNEL_ID_2)).thenReturn(List.of( + getSelfPayment("from " + CHANNEL_ID, 0), + getSelfPayment("to " + CHANNEL_ID_2, 1), + getSelfPayment("to " + CHANNEL_ID_2, 2) + )); + assertThat(rebalanceService.getSupportAsTargetAmountToPeer(PUBKEY)).isEqualTo(AMOUNT_PAID); + } + @Test void getTargetCostsForChannel_source_channel_not_known() { List noRoute = List.of(new PaymentRoute(List.of())); diff --git a/model/src/main/java/de/cotto/lndmanagej/model/RebalanceReport.java b/model/src/main/java/de/cotto/lndmanagej/model/RebalanceReport.java index ccd55aa3..15d1389f 100644 --- a/model/src/main/java/de/cotto/lndmanagej/model/RebalanceReport.java +++ b/model/src/main/java/de/cotto/lndmanagej/model/RebalanceReport.java @@ -4,7 +4,10 @@ public record RebalanceReport( Coins sourceCost, Coins sourceAmount, Coins targetCost, - Coins targetAmount + Coins targetAmount, + Coins supportAsSourceAmount, + Coins supportAsTargetAmount ) { - public static final RebalanceReport EMPTY = new RebalanceReport(Coins.NONE, Coins.NONE, Coins.NONE, Coins.NONE); + public static final RebalanceReport EMPTY = + new RebalanceReport(Coins.NONE, Coins.NONE, Coins.NONE, Coins.NONE, Coins.NONE, Coins.NONE); } diff --git a/model/src/test/java/de/cotto/lndmanagej/model/RebalanceReportTest.java b/model/src/test/java/de/cotto/lndmanagej/model/RebalanceReportTest.java index 8c3fcd69..9c282a2d 100644 --- a/model/src/test/java/de/cotto/lndmanagej/model/RebalanceReportTest.java +++ b/model/src/test/java/de/cotto/lndmanagej/model/RebalanceReportTest.java @@ -9,7 +9,8 @@ class RebalanceReportTest { @Test void empty() { - RebalanceReport empty = new RebalanceReport(Coins.NONE, Coins.NONE, Coins.NONE, Coins.NONE); + RebalanceReport empty = + new RebalanceReport(Coins.NONE, Coins.NONE, Coins.NONE, Coins.NONE, Coins.NONE, Coins.NONE); assertThat(RebalanceReport.EMPTY).isEqualTo(empty); } @@ -32,4 +33,14 @@ class RebalanceReportTest { void targetAmount() { assertThat(REBALANCE_REPORT.targetAmount()).isEqualTo(Coins.ofSatoshis(991)); } + + @Test + void supportAsSourceAmount() { + assertThat(REBALANCE_REPORT.supportAsSourceAmount()).isEqualTo(Coins.ofSatoshis(100)); + } + + @Test + void supportAsTargetAmount() { + assertThat(REBALANCE_REPORT.supportAsTargetAmount()).isEqualTo(Coins.ofSatoshis(200)); + } } \ No newline at end of file diff --git a/model/src/testFixtures/java/de/cotto/lndmanagej/model/ChannelDetailsFixtures.java b/model/src/testFixtures/java/de/cotto/lndmanagej/model/ChannelDetailsFixtures.java index a6b58d04..de5b6bc4 100644 --- a/model/src/testFixtures/java/de/cotto/lndmanagej/model/ChannelDetailsFixtures.java +++ b/model/src/testFixtures/java/de/cotto/lndmanagej/model/ChannelDetailsFixtures.java @@ -8,6 +8,7 @@ import static de.cotto.lndmanagej.model.NodeFixtures.ALIAS; import static de.cotto.lndmanagej.model.OnChainCostsFixtures.ON_CHAIN_COSTS; import static de.cotto.lndmanagej.model.PolicyFixtures.POLICIES; import static de.cotto.lndmanagej.model.RebalanceReportFixtures.REBALANCE_REPORT; +import static de.cotto.lndmanagej.model.RebalanceReportFixtures.REBALANCE_REPORT_2; public class ChannelDetailsFixtures { public static final ChannelDetails CHANNEL_DETAILS = new ChannelDetails( @@ -20,6 +21,16 @@ public class ChannelDetailsFixtures { REBALANCE_REPORT ); + public static final ChannelDetails CHANNEL_DETAILS_2 = new ChannelDetails( + LOCAL_OPEN_CHANNEL_PRIVATE, + ALIAS, + BALANCE_INFORMATION, + ON_CHAIN_COSTS, + POLICIES, + FEE_REPORT, + REBALANCE_REPORT_2 + ); + public static final ChannelDetails CHANNEL_DETAILS_CLOSED = new ChannelDetails( CLOSED_CHANNEL, ALIAS, diff --git a/model/src/testFixtures/java/de/cotto/lndmanagej/model/RebalanceReportFixtures.java b/model/src/testFixtures/java/de/cotto/lndmanagej/model/RebalanceReportFixtures.java index 22022677..727b11db 100644 --- a/model/src/testFixtures/java/de/cotto/lndmanagej/model/RebalanceReportFixtures.java +++ b/model/src/testFixtures/java/de/cotto/lndmanagej/model/RebalanceReportFixtures.java @@ -5,6 +5,17 @@ public class RebalanceReportFixtures { Coins.ofSatoshis(1000), Coins.ofSatoshis(665), Coins.ofSatoshis(2000), - Coins.ofSatoshis(991) + Coins.ofSatoshis(991), + Coins.ofSatoshis(100), + Coins.ofSatoshis(200) + ); + + public static final RebalanceReport REBALANCE_REPORT_2 = new RebalanceReport( + Coins.ofSatoshis(1001), + Coins.ofSatoshis(666), + Coins.ofSatoshis(2001), + Coins.ofSatoshis(992), + Coins.ofSatoshis(101), + Coins.ofSatoshis(201) ); } diff --git a/web/src/integrationTest/java/de/cotto/lndmanagej/controller/ChannelControllerIT.java b/web/src/integrationTest/java/de/cotto/lndmanagej/controller/ChannelControllerIT.java index 309ba28f..9c2a5e45 100644 --- a/web/src/integrationTest/java/de/cotto/lndmanagej/controller/ChannelControllerIT.java +++ b/web/src/integrationTest/java/de/cotto/lndmanagej/controller/ChannelControllerIT.java @@ -18,7 +18,7 @@ import org.springframework.test.web.servlet.MockMvc; import java.util.Optional; import static de.cotto.lndmanagej.model.BalanceInformationFixtures.BALANCE_INFORMATION_2; -import static de.cotto.lndmanagej.model.ChannelDetailsFixtures.CHANNEL_DETAILS; +import static de.cotto.lndmanagej.model.ChannelDetailsFixtures.CHANNEL_DETAILS_2; import static de.cotto.lndmanagej.model.ChannelDetailsFixtures.CHANNEL_DETAILS_CLOSED; import static de.cotto.lndmanagej.model.ChannelFixtures.CAPACITY; import static de.cotto.lndmanagej.model.ChannelIdFixtures.CHANNEL_ID; @@ -123,7 +123,7 @@ class ChannelControllerIT { @Test void getChannelDetails() throws Exception { when(channelService.getLocalChannel(CHANNEL_ID)).thenReturn(Optional.of(LOCAL_OPEN_CHANNEL_PRIVATE)); - when(channelDetailsService.getDetails(LOCAL_OPEN_CHANNEL_PRIVATE)).thenReturn(CHANNEL_DETAILS); + when(channelDetailsService.getDetails(LOCAL_OPEN_CHANNEL_PRIVATE)).thenReturn(CHANNEL_DETAILS_2); mockMvc.perform(get(DETAILS_PREFIX)) .andExpect(jsonPath("$.channelIdShort", is(String.valueOf(CHANNEL_ID.getShortChannelId())))) .andExpect(jsonPath("$.channelIdCompact", is(CHANNEL_ID.getCompactForm()))) @@ -143,10 +143,12 @@ class ChannelControllerIT { .andExpect(jsonPath("$.onChainCosts.openCosts", is("1000"))) .andExpect(jsonPath("$.onChainCosts.closeCosts", is("2000"))) .andExpect(jsonPath("$.onChainCosts.sweepCosts", is("3000"))) - .andExpect(jsonPath("$.rebalanceReport.sourceCosts", is("1000000"))) - .andExpect(jsonPath("$.rebalanceReport.sourceAmount", is("665000"))) - .andExpect(jsonPath("$.rebalanceReport.targetCosts", is("2000000"))) - .andExpect(jsonPath("$.rebalanceReport.targetAmount", is("991000"))) + .andExpect(jsonPath("$.rebalanceReport.sourceCosts", is("1001000"))) + .andExpect(jsonPath("$.rebalanceReport.sourceAmount", is("666000"))) + .andExpect(jsonPath("$.rebalanceReport.targetCosts", is("2001000"))) + .andExpect(jsonPath("$.rebalanceReport.targetAmount", is("992000"))) + .andExpect(jsonPath("$.rebalanceReport.supportAsSourceAmount", is("101000"))) + .andExpect(jsonPath("$.rebalanceReport.supportAsTargetAmount", is("201000"))) .andExpect(jsonPath("$.balance.localBalance", is("1000"))) .andExpect(jsonPath("$.balance.localReserve", is("100"))) .andExpect(jsonPath("$.balance.localAvailable", is("900"))) diff --git a/web/src/integrationTest/java/de/cotto/lndmanagej/controller/NodeControllerIT.java b/web/src/integrationTest/java/de/cotto/lndmanagej/controller/NodeControllerIT.java index 6ef0e661..640b29fc 100644 --- a/web/src/integrationTest/java/de/cotto/lndmanagej/controller/NodeControllerIT.java +++ b/web/src/integrationTest/java/de/cotto/lndmanagej/controller/NodeControllerIT.java @@ -87,6 +87,8 @@ class NodeControllerIT { .andExpect(jsonPath("$.rebalanceReport.targetCosts", is("2000000"))) .andExpect(jsonPath("$.rebalanceReport.sourceAmount", is("665000"))) .andExpect(jsonPath("$.rebalanceReport.targetAmount", is("991000"))) + .andExpect(jsonPath("$.rebalanceReport.supportAsSourceAmount", is("100000"))) + .andExpect(jsonPath("$.rebalanceReport.supportAsTargetAmount", is("200000"))) .andExpect(jsonPath("$.balance.localBalance", is("2000"))) .andExpect(jsonPath("$.balance.localReserve", is("200"))) .andExpect(jsonPath("$.balance.localAvailable", is("1800"))) diff --git a/web/src/integrationTest/java/de/cotto/lndmanagej/controller/RebalancesControllerIT.java b/web/src/integrationTest/java/de/cotto/lndmanagej/controller/RebalancesControllerIT.java index 45ff3aa7..c803a29c 100644 --- a/web/src/integrationTest/java/de/cotto/lndmanagej/controller/RebalancesControllerIT.java +++ b/web/src/integrationTest/java/de/cotto/lndmanagej/controller/RebalancesControllerIT.java @@ -85,4 +85,32 @@ class RebalancesControllerIT { mockMvc.perform(get(NODE_PREFIX + "/rebalance-target-amount/")) .andExpect(content().string("999")); } + + @Test + void getRebalanceSupportAsSourceAmountForChannel() throws Exception { + when(rebalanceService.getSupportAsSourceAmountFromChannel(CHANNEL_ID)).thenReturn(Coins.ofMilliSatoshis(1)); + mockMvc.perform(get(CHANNEL_PREFIX + "/rebalance-support-as-source-amount/")) + .andExpect(content().string("1")); + } + + @Test + void getRebalanceSupportAsSourceAmountForPeer() throws Exception { + when(rebalanceService.getSupportAsSourceAmountFromPeer(PUBKEY)).thenReturn(Coins.ofMilliSatoshis(2)); + mockMvc.perform(get(NODE_PREFIX + "/rebalance-support-as-source-amount/")) + .andExpect(content().string("2")); + } + + @Test + void getRebalanceSupportAsTargetAmountForChannel() throws Exception { + when(rebalanceService.getSupportAsTargetAmountToChannel(CHANNEL_ID)).thenReturn(Coins.ofMilliSatoshis(3)); + mockMvc.perform(get(CHANNEL_PREFIX + "/rebalance-support-as-target-amount/")) + .andExpect(content().string("3")); + } + + @Test + void getRebalanceSupportAsTargetAmountForPeer() throws Exception { + when(rebalanceService.getSupportAsTargetAmountToPeer(PUBKEY)).thenReturn(Coins.ofMilliSatoshis(4)); + mockMvc.perform(get(NODE_PREFIX + "/rebalance-support-as-target-amount/")) + .andExpect(content().string("4")); + } } \ No newline at end of file diff --git a/web/src/main/java/de/cotto/lndmanagej/controller/RebalancesController.java b/web/src/main/java/de/cotto/lndmanagej/controller/RebalancesController.java index e93fdd85..45e83159 100644 --- a/web/src/main/java/de/cotto/lndmanagej/controller/RebalancesController.java +++ b/web/src/main/java/de/cotto/lndmanagej/controller/RebalancesController.java @@ -65,4 +65,28 @@ public class RebalancesController { public long getRebalanceTargetAmountForChannel(@PathVariable ChannelId channelId) { return rebalanceService.getAmountToChannel(channelId).milliSatoshis(); } + + @Timed + @GetMapping("/channel/{channelId}/rebalance-support-as-source-amount") + public long getRebalanceSupportAsSourceAmountFromChannel(@PathVariable ChannelId channelId) { + return rebalanceService.getSupportAsSourceAmountFromChannel(channelId).milliSatoshis(); + } + + @Timed + @GetMapping("/node/{pubkey}/rebalance-support-as-source-amount") + public long getRebalanceSupportAsSourceAmountFromPeer(@PathVariable Pubkey pubkey) { + return rebalanceService.getSupportAsSourceAmountFromPeer(pubkey).milliSatoshis(); + } + + @Timed + @GetMapping("/channel/{channelId}/rebalance-support-as-target-amount") + public long getRebalanceSupportAsTargetAmountToChannel(@PathVariable ChannelId channelId) { + return rebalanceService.getSupportAsTargetAmountToChannel(channelId).milliSatoshis(); + } + + @Timed + @GetMapping("/node/{pubkey}/rebalance-support-as-target-amount") + public long getRebalanceSupportAsTargetAmountToPeer(@PathVariable Pubkey pubkey) { + return rebalanceService.getSupportAsTargetAmountToPeer(pubkey).milliSatoshis(); + } } diff --git a/web/src/main/java/de/cotto/lndmanagej/controller/dto/RebalanceReportDto.java b/web/src/main/java/de/cotto/lndmanagej/controller/dto/RebalanceReportDto.java index 26b00028..65562c94 100644 --- a/web/src/main/java/de/cotto/lndmanagej/controller/dto/RebalanceReportDto.java +++ b/web/src/main/java/de/cotto/lndmanagej/controller/dto/RebalanceReportDto.java @@ -3,13 +3,29 @@ package de.cotto.lndmanagej.controller.dto; import de.cotto.lndmanagej.model.Coins; import de.cotto.lndmanagej.model.RebalanceReport; -public record RebalanceReportDto(String sourceCosts, String sourceAmount, String targetCosts, String targetAmount) { - public RebalanceReportDto(Coins sourceCost, Coins sourceAmount, Coins targetCost, Coins targetAmount) { +public record RebalanceReportDto( + String sourceCosts, + String sourceAmount, + String targetCosts, + String targetAmount, + String supportAsSourceAmount, + String supportAsTargetAmount +) { + public RebalanceReportDto( + Coins sourceCost, + Coins sourceAmount, + Coins targetCost, + Coins targetAmount, + Coins supportAsSourceAmount, + Coins supportAsTargetAmount + ) { this( toMilliSatoshisString(sourceCost), toMilliSatoshisString(sourceAmount), toMilliSatoshisString(targetCost), - toMilliSatoshisString(targetAmount) + toMilliSatoshisString(targetAmount), + toMilliSatoshisString(supportAsSourceAmount), + toMilliSatoshisString(supportAsTargetAmount) ); } @@ -18,7 +34,9 @@ public record RebalanceReportDto(String sourceCosts, String sourceAmount, String rebalanceReport.sourceCost(), rebalanceReport.sourceAmount(), rebalanceReport.targetCost(), - rebalanceReport.targetAmount() + rebalanceReport.targetAmount(), + rebalanceReport.supportAsSourceAmount(), + rebalanceReport.supportAsTargetAmount() ); } diff --git a/web/src/test/java/de/cotto/lndmanagej/controller/RebalancesControllerTest.java b/web/src/test/java/de/cotto/lndmanagej/controller/RebalancesControllerTest.java index b4372794..f82ff34a 100644 --- a/web/src/test/java/de/cotto/lndmanagej/controller/RebalancesControllerTest.java +++ b/web/src/test/java/de/cotto/lndmanagej/controller/RebalancesControllerTest.java @@ -70,4 +70,28 @@ class RebalancesControllerTest { when(rebalanceService.getAmountToPeer(PUBKEY)).thenReturn(COINS); assertThat(rebalancesController.getRebalanceTargetAmountForPeer(PUBKEY)).isEqualTo(123); } + + @Test + void getRebalanceSupportAsSourceAmountForChannel() { + when(rebalanceService.getSupportAsSourceAmountFromChannel(CHANNEL_ID)).thenReturn(COINS); + assertThat(rebalancesController.getRebalanceSupportAsSourceAmountFromChannel(CHANNEL_ID)).isEqualTo(123); + } + + @Test + void getRebalanceSupportAsSourceAmountFromPeer() { + when(rebalanceService.getSupportAsSourceAmountFromPeer(PUBKEY)).thenReturn(COINS); + assertThat(rebalancesController.getRebalanceSupportAsSourceAmountFromPeer(PUBKEY)).isEqualTo(123); + } + + @Test + void getRebalanceSupportAsTargetAmountToChannel() { + when(rebalanceService.getSupportAsTargetAmountToChannel(CHANNEL_ID)).thenReturn(COINS); + assertThat(rebalancesController.getRebalanceSupportAsTargetAmountToChannel(CHANNEL_ID)).isEqualTo(123); + } + + @Test + void getRebalanceSupportAsTargetAmountToPeer() { + when(rebalanceService.getSupportAsTargetAmountToPeer(PUBKEY)).thenReturn(COINS); + assertThat(rebalancesController.getRebalanceSupportAsTargetAmountToPeer(PUBKEY)).isEqualTo(123); + } } \ No newline at end of file diff --git a/web/src/test/java/de/cotto/lndmanagej/controller/dto/RebalanceReportDtoTest.java b/web/src/test/java/de/cotto/lndmanagej/controller/dto/RebalanceReportDtoTest.java index 415c9f27..5b6d3224 100644 --- a/web/src/test/java/de/cotto/lndmanagej/controller/dto/RebalanceReportDtoTest.java +++ b/web/src/test/java/de/cotto/lndmanagej/controller/dto/RebalanceReportDtoTest.java @@ -12,7 +12,9 @@ class RebalanceReportDtoTest { Coins.ofMilliSatoshis(1_234), Coins.ofSatoshis(9_000), Coins.ofMilliSatoshis(567), - Coins.ofSatoshis(1_000) + Coins.ofSatoshis(1_000), + Coins.ofSatoshis(100), + Coins.ofSatoshis(200) ); @Test @@ -35,10 +37,20 @@ class RebalanceReportDtoTest { assertThat(REBALANCE_REPORT_DTO.targetAmount()).isEqualTo("1000000"); } + @Test + void supportAsSourceAmount() { + assertThat(REBALANCE_REPORT_DTO.supportAsSourceAmount()).isEqualTo("100000"); + } + + @Test + void supportAsTargetAmount() { + assertThat(REBALANCE_REPORT_DTO.supportAsTargetAmount()).isEqualTo("200000"); + } + @Test void createFromModel() { assertThat(RebalanceReportDto.createFromModel(REBALANCE_REPORT)).isEqualTo( - new RebalanceReportDto("1000000", "665000", "2000000", "991000") + new RebalanceReportDto("1000000", "665000", "2000000", "991000", "100000", "200000") ); } } \ No newline at end of file