Skip to content

Commit 605a463

Browse files
committed
feat: add network resource DTOs and update controller
1 parent 2501742 commit 605a463

5 files changed

Lines changed: 109 additions & 9 deletions

File tree

hiero-enterprise-spring-sample/src/main/java/org/hiero/spring/sample/controller/NetworkController.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
import java.util.List;
44
import java.util.Objects;
5-
import org.hiero.base.data.ExchangeRates;
6-
import org.hiero.base.data.NetworkFee;
7-
import org.hiero.base.data.NetworkStake;
8-
import org.hiero.base.data.NetworkSupplies;
95
import org.hiero.base.mirrornode.NetworkRepository;
6+
import org.hiero.spring.sample.dto.network.ExchangeRatesResponse;
7+
import org.hiero.spring.sample.dto.network.NetworkFeeResponse;
8+
import org.hiero.spring.sample.dto.network.NetworkStakeResponse;
9+
import org.hiero.spring.sample.dto.network.NetworkSuppliesResponse;
1010
import org.springframework.web.bind.annotation.GetMapping;
1111
import org.springframework.web.bind.annotation.RequestMapping;
1212
import org.springframework.web.bind.annotation.RestController;
@@ -30,9 +30,10 @@ public NetworkController(final NetworkRepository networkRepository) {
3030
* Retrieves the current and next exchange rates.
3131
*/
3232
@GetMapping("/exchange-rate")
33-
public ExchangeRates getExchangeRates() {
33+
public ExchangeRatesResponse getExchangeRates() {
3434
try {
3535
return networkRepository.exchangeRates()
36+
.map(ExchangeRatesResponse::fromDomain)
3637
.orElseThrow(() -> new RuntimeException("Exchange rates not available"));
3738
} catch (final Exception e) {
3839
throw new RuntimeException("Failed to query exchange rates", e);
@@ -43,9 +44,11 @@ public ExchangeRates getExchangeRates() {
4344
* Retrieves the network fees.
4445
*/
4546
@GetMapping("/fee")
46-
public List<NetworkFee> getFees() {
47+
public List<NetworkFeeResponse> getFees() {
4748
try {
48-
return networkRepository.fees();
49+
return networkRepository.fees().stream()
50+
.map(NetworkFeeResponse::fromDomain)
51+
.toList();
4952
} catch (final Exception e) {
5053
throw new RuntimeException("Failed to query network fees", e);
5154
}
@@ -55,9 +58,10 @@ public List<NetworkFee> getFees() {
5558
* Retrieves network staking information.
5659
*/
5760
@GetMapping("/stake")
58-
public NetworkStake getStake() {
61+
public NetworkStakeResponse getStake() {
5962
try {
6063
return networkRepository.stake()
64+
.map(NetworkStakeResponse::fromDomain)
6165
.orElseThrow(() -> new RuntimeException("Network stake info not available"));
6266
} catch (final Exception e) {
6367
throw new RuntimeException("Failed to query network stake", e);
@@ -68,9 +72,10 @@ public NetworkStake getStake() {
6872
* Retrieves network supply information.
6973
*/
7074
@GetMapping("/supplies")
71-
public NetworkSupplies getSupplies() {
75+
public NetworkSuppliesResponse getSupplies() {
7276
try {
7377
return networkRepository.supplies()
78+
.map(NetworkSuppliesResponse::fromDomain)
7479
.orElseThrow(() -> new RuntimeException("Network supply info not available"));
7580
} catch (final Exception e) {
7681
throw new RuntimeException("Failed to query network supplies", e);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.hiero.spring.sample.dto.network;
2+
3+
import java.time.Instant;
4+
import org.hiero.base.data.ExchangeRates;
5+
6+
/**
7+
* Response DTO for Network Exchange Rates.
8+
*/
9+
public record ExchangeRatesResponse(
10+
RateInfo currentRate,
11+
RateInfo nextRate
12+
) {
13+
public record RateInfo(
14+
int centEquivalent,
15+
int hbarEquivalent,
16+
Instant expirationTime
17+
) {}
18+
19+
public static ExchangeRatesResponse fromDomain(ExchangeRates rates) {
20+
return new ExchangeRatesResponse(
21+
new RateInfo(rates.currentRate().centEquivalent(), rates.currentRate().hbarEquivalent(), rates.currentRate().expirationTime()),
22+
new RateInfo(rates.nextRate().centEquivalent(), rates.nextRate().hbarEquivalent(), rates.nextRate().expirationTime())
23+
);
24+
}
25+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.hiero.spring.sample.dto.network;
2+
3+
import org.hiero.base.data.NetworkFee;
4+
5+
/**
6+
* Response DTO for Network Fees.
7+
*/
8+
public record NetworkFeeResponse(
9+
long gas,
10+
String transactionType
11+
) {
12+
public static NetworkFeeResponse fromDomain(NetworkFee fee) {
13+
return new NetworkFeeResponse(fee.gas(), fee.transactionType());
14+
}
15+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.hiero.spring.sample.dto.network;
2+
3+
import org.hiero.base.data.NetworkStake;
4+
5+
/**
6+
* Response DTO for Network Stake Information.
7+
*/
8+
public record NetworkStakeResponse(
9+
long maxStakeReward,
10+
long maxStakeRewardPerHbar,
11+
long maxTotalReward,
12+
double nodeRewardFeeFraction,
13+
long reservedStakingRewards,
14+
long rewardBalanceThreshold,
15+
long stakeTotal,
16+
long stakingPeriodDuration,
17+
long stakingPeriodsStored,
18+
double stakingRewardFeeFraction,
19+
long stakingRewardRate,
20+
long stakingStartThreshold,
21+
long unreservedStakingRewardBalance
22+
) {
23+
public static NetworkStakeResponse fromDomain(NetworkStake stake) {
24+
return new NetworkStakeResponse(
25+
stake.maxStakeReward(),
26+
stake.maxStakeRewardPerHbar(),
27+
stake.maxTotalReward(),
28+
stake.nodeRewardFeeFraction(),
29+
stake.reservedStakingRewards(),
30+
stake.rewardBalanceThreshold(),
31+
stake.stakeTotal(),
32+
stake.stakingPeriodDuration(),
33+
stake.stakingPeriodsStored(),
34+
stake.stakingRewardFeeFraction(),
35+
stake.stakingRewardRate(),
36+
stake.stakingStartThreshold(),
37+
stake.unreservedStakingRewardBalance()
38+
);
39+
}
40+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.hiero.spring.sample.dto.network;
2+
3+
import org.hiero.base.data.NetworkSupplies;
4+
5+
/**
6+
* Response DTO for Network Supplies.
7+
*/
8+
public record NetworkSuppliesResponse(
9+
String releasedSupply,
10+
String totalSupply
11+
) {
12+
public static NetworkSuppliesResponse fromDomain(NetworkSupplies supplies) {
13+
return new NetworkSuppliesResponse(supplies.releasedSupply(), supplies.totalSupply());
14+
}
15+
}

0 commit comments

Comments
 (0)