mirror of
https://github.com/aljazceru/lnd-manageJ.git
synced 2026-01-21 15:04:22 +01:00
add incoming-fee-rate and outgoing-fee-rate endpoints
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package de.cotto.lndmanagej.controller;
|
||||
|
||||
import de.cotto.lndmanagej.service.ChannelService;
|
||||
import de.cotto.lndmanagej.service.FeeService;
|
||||
import de.cotto.lndmanagej.service.NodeService;
|
||||
import de.cotto.lndmanagej.service.OwnNodeService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -39,6 +40,9 @@ class LegacyControllerIT {
|
||||
@MockBean
|
||||
private OwnNodeService ownNodeService;
|
||||
|
||||
@MockBean
|
||||
private FeeService feeService;
|
||||
|
||||
@Test
|
||||
void getAlias() throws Exception {
|
||||
when(nodeService.getAlias(PUBKEY)).thenReturn(ALIAS);
|
||||
@@ -75,4 +79,18 @@ class LegacyControllerIT {
|
||||
mockMvc.perform(get("/legacy/peer-pubkeys"))
|
||||
.andExpect(content().string(PUBKEY_2 + "\n" + PUBKEY_3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getIncomingFeeRate() throws Exception {
|
||||
when(feeService.getIncomingFeeRate(CHANNEL_ID)).thenReturn(123L);
|
||||
mockMvc.perform(get("/legacy/channel/" + CHANNEL_ID + "/incoming-fee-rate"))
|
||||
.andExpect(content().string("123"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getOutgoingFeeRate() throws Exception {
|
||||
when(feeService.getOutgoingFeeRate(CHANNEL_ID)).thenReturn(123L);
|
||||
mockMvc.perform(get("/legacy/channel/" + CHANNEL_ID + "/outgoing-fee-rate"))
|
||||
.andExpect(content().string("123"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package de.cotto.lndmanagej.controller;
|
||||
|
||||
import de.cotto.lndmanagej.model.ChannelId;
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
@Component
|
||||
public class ChannelIdConverter implements Converter<String, ChannelId> {
|
||||
public ChannelIdConverter() {
|
||||
// default constructor
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChannelId convert(@Nonnull String source) {
|
||||
try {
|
||||
long shortChannelId = Long.parseLong(source);
|
||||
return ChannelId.fromShortChannelId(shortChannelId);
|
||||
} catch (NumberFormatException numberFormatException) {
|
||||
return ChannelId.fromCompactForm(source);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import de.cotto.lndmanagej.model.ChannelId;
|
||||
import de.cotto.lndmanagej.model.LocalChannel;
|
||||
import de.cotto.lndmanagej.model.Pubkey;
|
||||
import de.cotto.lndmanagej.service.ChannelService;
|
||||
import de.cotto.lndmanagej.service.FeeService;
|
||||
import de.cotto.lndmanagej.service.NodeService;
|
||||
import de.cotto.lndmanagej.service.OwnNodeService;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
@@ -21,11 +22,18 @@ public class LegacyController {
|
||||
private final NodeService nodeService;
|
||||
private final ChannelService channelService;
|
||||
private final OwnNodeService ownNodeService;
|
||||
private final FeeService feeService;
|
||||
|
||||
public LegacyController(NodeService nodeService, ChannelService channelService, OwnNodeService ownNodeService) {
|
||||
public LegacyController(
|
||||
NodeService nodeService,
|
||||
ChannelService channelService,
|
||||
OwnNodeService ownNodeService,
|
||||
FeeService feeService
|
||||
) {
|
||||
this.nodeService = nodeService;
|
||||
this.channelService = channelService;
|
||||
this.ownNodeService = ownNodeService;
|
||||
this.feeService = feeService;
|
||||
}
|
||||
|
||||
@GetMapping("/node/{pubkey}/alias")
|
||||
@@ -56,4 +64,14 @@ public class LegacyController {
|
||||
public boolean syncedToChain() {
|
||||
return ownNodeService.isSyncedToChain();
|
||||
}
|
||||
|
||||
@GetMapping("/channel/{channelId}/incoming-fee-rate")
|
||||
public long getIncomingFeeRate(@PathVariable ChannelId channelId) {
|
||||
return feeService.getIncomingFeeRate(channelId);
|
||||
}
|
||||
|
||||
@GetMapping("/channel/{channelId}/outgoing-fee-rate")
|
||||
public long getOutgoingFeeRate(@PathVariable ChannelId channelId) {
|
||||
return feeService.getOutgoingFeeRate(channelId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package de.cotto.lndmanagej.service;
|
||||
|
||||
import de.cotto.lndmanagej.grpc.GrpcFees;
|
||||
import de.cotto.lndmanagej.model.ChannelId;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class FeeService {
|
||||
private final GrpcFees grpcFees;
|
||||
|
||||
public FeeService(GrpcFees grpcFees) {
|
||||
this.grpcFees = grpcFees;
|
||||
}
|
||||
|
||||
public long getIncomingFeeRate(ChannelId channelId) {
|
||||
return grpcFees.getIncomingFeeRate(channelId).orElseThrow(IllegalStateException::new);
|
||||
}
|
||||
|
||||
public long getOutgoingFeeRate(ChannelId channelId) {
|
||||
return grpcFees.getOutgoingFeeRate(channelId).orElseThrow(IllegalStateException::new);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package de.cotto.lndmanagej.controller;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static de.cotto.lndmanagej.model.ChannelIdFixtures.CHANNEL_ID;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class ChannelIdConverterTest {
|
||||
@Test
|
||||
void convert() {
|
||||
assertThat(new ChannelIdConverter().convert(CHANNEL_ID.toString())).isEqualTo(CHANNEL_ID);
|
||||
}
|
||||
|
||||
@Test
|
||||
void convert_from_compact_form_with_x() {
|
||||
assertThat(new ChannelIdConverter().convert("712345x123x1")).isEqualTo(CHANNEL_ID);
|
||||
}
|
||||
|
||||
@Test
|
||||
void convert_from_compact_form() {
|
||||
assertThat(new ChannelIdConverter().convert("712345:123:1")).isEqualTo(CHANNEL_ID);
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package de.cotto.lndmanagej.controller;
|
||||
import de.cotto.lndmanagej.model.ChannelFixtures;
|
||||
import de.cotto.lndmanagej.model.LocalChannel;
|
||||
import de.cotto.lndmanagej.service.ChannelService;
|
||||
import de.cotto.lndmanagej.service.FeeService;
|
||||
import de.cotto.lndmanagej.service.NodeService;
|
||||
import de.cotto.lndmanagej.service.OwnNodeService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -40,6 +41,9 @@ class LegacyControllerTest {
|
||||
@Mock
|
||||
private OwnNodeService ownNodeService;
|
||||
|
||||
@Mock
|
||||
private FeeService feeService;
|
||||
|
||||
@Test
|
||||
void getAlias() {
|
||||
when(nodeService.getAlias(PUBKEY)).thenReturn(ALIAS);
|
||||
@@ -93,4 +97,16 @@ class LegacyControllerTest {
|
||||
when(channelService.getOpenChannels()).thenReturn(Set.of(LOCAL_CHANNEL, LOCAL_CHANNEL_2));
|
||||
assertThat(legacyController.getPeerPubkeys()).isEqualTo(PUBKEY_2.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getIncomingFeeRate() {
|
||||
when(feeService.getIncomingFeeRate(CHANNEL_ID)).thenReturn(123L);
|
||||
assertThat(legacyController.getIncomingFeeRate(CHANNEL_ID)).isEqualTo(123);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getOutgoingFeeRate() {
|
||||
when(feeService.getOutgoingFeeRate(CHANNEL_ID)).thenReturn(123L);
|
||||
assertThat(legacyController.getOutgoingFeeRate(CHANNEL_ID)).isEqualTo(123);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package de.cotto.lndmanagej.service;
|
||||
|
||||
import de.cotto.lndmanagej.grpc.GrpcFees;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import static de.cotto.lndmanagej.model.ChannelIdFixtures.CHANNEL_ID;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class FeeServiceTest {
|
||||
@InjectMocks
|
||||
private FeeService feeService;
|
||||
|
||||
@Mock
|
||||
private GrpcFees grpcFees;
|
||||
|
||||
@Test
|
||||
void getIncomingFeeRate() {
|
||||
when(grpcFees.getIncomingFeeRate(CHANNEL_ID)).thenReturn(Optional.of(123L));
|
||||
assertThat(feeService.getIncomingFeeRate(CHANNEL_ID)).isEqualTo(123);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getIncomingFeeRate_empty() {
|
||||
when(grpcFees.getIncomingFeeRate(CHANNEL_ID)).thenReturn(Optional.empty());
|
||||
assertThatIllegalStateException().isThrownBy(() -> feeService.getIncomingFeeRate(CHANNEL_ID));
|
||||
}
|
||||
|
||||
@Test
|
||||
void getOutgoingFeeRate() {
|
||||
when(grpcFees.getOutgoingFeeRate(CHANNEL_ID)).thenReturn(Optional.of(123L));
|
||||
assertThat(feeService.getOutgoingFeeRate(CHANNEL_ID)).isEqualTo(123);
|
||||
}
|
||||
|
||||
@Test
|
||||
void getOutgoingFeeRate_empty() {
|
||||
when(grpcFees.getOutgoingFeeRate(CHANNEL_ID)).thenReturn(Optional.empty());
|
||||
assertThatIllegalStateException().isThrownBy(() -> feeService.getOutgoingFeeRate(CHANNEL_ID));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user