Skip to content

Commit 605153f

Browse files
Merge pull request #66 from manishdait/38-issue
feat: Implement NetworkRepository to interact with Mirror Node REST API
2 parents 43ff747 + d5c56b9 commit 605153f

File tree

11 files changed

+419
-0
lines changed

11 files changed

+419
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.openelements.hedera.base;
2+
3+
import com.openelements.hedera.base.mirrornode.ExchangeRates;
4+
import com.openelements.hedera.base.mirrornode.NetworkFee;
5+
import com.openelements.hedera.base.mirrornode.NetworkStake;
6+
import com.openelements.hedera.base.mirrornode.NetworkSupplies;
7+
8+
import java.util.List;
9+
import java.util.Optional;
10+
11+
/**
12+
* Interface for interacting with a Hedera network. This interface provides methods to get information related to Network.
13+
*/
14+
public interface NetworkRepository {
15+
/**
16+
* Return the ExchangeRates for network.
17+
*
18+
* @return {@link Optional} containing the ExchangeRates or null
19+
* @throws HederaException if the search fails
20+
*/
21+
Optional<ExchangeRates> exchangeRates() throws HederaException;
22+
23+
/**
24+
* Return the List of NetworkFee for network.
25+
*
26+
* @return {@link List} containing NetworkFee or empty list
27+
* @throws HederaException if the search fails
28+
*/
29+
List<NetworkFee> fees() throws HederaException;
30+
31+
/**
32+
* Return the NetworkStake for network.
33+
*
34+
* @return {@link Optional} containing NetworkStake or null
35+
* @throws HederaException if the search fails
36+
*/
37+
Optional<NetworkStake> stake() throws HederaException;
38+
39+
/**
40+
* Return the NetworkSupplies for network.
41+
*
42+
* @return {@link Optional} containing NetworkSupplies or null
43+
* @throws HederaException if the search fails
44+
*/
45+
Optional<NetworkSupplies> supplies() throws HederaException;
46+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.openelements.hedera.base.implementation;
2+
3+
import com.openelements.hedera.base.HederaException;
4+
import com.openelements.hedera.base.NetworkRepository;
5+
import com.openelements.hedera.base.mirrornode.ExchangeRates;
6+
import com.openelements.hedera.base.mirrornode.MirrorNodeClient;
7+
import com.openelements.hedera.base.mirrornode.NetworkFee;
8+
import com.openelements.hedera.base.mirrornode.NetworkStake;
9+
import com.openelements.hedera.base.mirrornode.NetworkSupplies;
10+
import org.jspecify.annotations.NonNull;
11+
12+
import java.util.List;
13+
import java.util.Objects;
14+
import java.util.Optional;
15+
16+
public class NetworkRepositoryImpl implements NetworkRepository {
17+
private final MirrorNodeClient mirrorNodeClient;
18+
19+
public NetworkRepositoryImpl(@NonNull final MirrorNodeClient mirrorNodeClient) {
20+
this.mirrorNodeClient = Objects.requireNonNull(mirrorNodeClient, "mirrorNodeClient must not be null");
21+
}
22+
23+
@Override
24+
public Optional<ExchangeRates> exchangeRates() throws HederaException {
25+
return mirrorNodeClient.queryExchangeRates();
26+
}
27+
28+
@Override
29+
public List<NetworkFee> fees() throws HederaException {
30+
return mirrorNodeClient.queryNetworkFees();
31+
}
32+
33+
@Override
34+
public Optional<NetworkStake> stake() throws HederaException {
35+
return mirrorNodeClient.queryNetworkStake();
36+
}
37+
38+
@Override
39+
public Optional<NetworkSupplies> supplies() throws HederaException {
40+
return mirrorNodeClient.queryNetworkSupplies();
41+
}
42+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.openelements.hedera.base.mirrornode;
2+
3+
import org.jspecify.annotations.NonNull;
4+
5+
import java.time.Instant;
6+
import java.util.Objects;
7+
8+
public record ExchangeRate(int centEquivalent, int hbarEquivalent, @NonNull Instant expirationTime) {
9+
public ExchangeRate {
10+
Objects.requireNonNull(expirationTime, "expirationTime must not be null");
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.openelements.hedera.base.mirrornode;
2+
3+
import org.jspecify.annotations.NonNull;
4+
5+
import java.util.Objects;
6+
7+
public record ExchangeRates(@NonNull ExchangeRate currentRate, @NonNull ExchangeRate nextRate) {
8+
public ExchangeRates {
9+
Objects.requireNonNull(currentRate, "currentRate must not be null");
10+
Objects.requireNonNull(nextRate, "nextRate must not be null");
11+
}
12+
}

hedera-base/src/main/java/com/openelements/hedera/base/mirrornode/MirrorNodeClient.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.openelements.hedera.base.mirrornode;
22

3+
import java.util.List;
34
import java.util.Objects;
45
import java.util.Optional;
56

@@ -189,4 +190,40 @@ default Optional<AccountInfo> queryAccount(@NonNull String accountId) throws Hed
189190
Objects.requireNonNull(accountId, "accountId must not be null");
190191
return queryAccount(AccountId.fromString(accountId));
191192
}
193+
194+
/**
195+
* Queries the ExchangeRates for the network.
196+
*
197+
* @return the Optional of ExchangeRates for the Network
198+
* @throws HederaException if an error occurs
199+
*/
200+
@NonNull
201+
Optional<ExchangeRates> queryExchangeRates() throws HederaException;
202+
203+
/**
204+
* Queries the NetworkFee for the network.
205+
*
206+
* @return the List of NetworkFee for the Network
207+
* @throws HederaException if an error occurs
208+
*/
209+
@NonNull
210+
List<NetworkFee> queryNetworkFees() throws HederaException;
211+
212+
/**
213+
* Queries the NetworkStake for the network.
214+
*
215+
* @return the Optional of NetworkStake for the Network
216+
* @throws HederaException if an error occurs
217+
*/
218+
@NonNull
219+
Optional<NetworkStake> queryNetworkStake() throws HederaException;
220+
221+
/**
222+
* Queries the NetworkSupplies for the network.
223+
*
224+
* @return the Optional of NetworkSupplies for the Network
225+
* @throws HederaException if an error occurs
226+
*/
227+
@NonNull
228+
Optional<NetworkSupplies> queryNetworkSupplies() throws HederaException;
192229
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.openelements.hedera.base.mirrornode;
2+
3+
import org.jspecify.annotations.NonNull;
4+
5+
import java.util.Objects;
6+
7+
public record NetworkFee(long gas, @NonNull String transactionType) {
8+
public NetworkFee {
9+
Objects.requireNonNull(transactionType, "transactionType must not be null");
10+
}
11+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.openelements.hedera.base.mirrornode;
2+
3+
public record NetworkStake(
4+
long maxStakeReward,
5+
long maxStakeRewardPerHbar,
6+
long maxTotalReward,
7+
double nodeRewardFeeFraction,
8+
long reservedStakingRewards,
9+
long rewardBalanceThreshold,
10+
long stakeTotal,
11+
long stakingPeriodDuration,
12+
long stakingPeriodsStored,
13+
double stakingRewardFeeFraction,
14+
long stakingRewardRate,
15+
long stakingStartThreshold,
16+
long unreservedStakingRewardBalance
17+
) {
18+
public NetworkStake {}
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.openelements.hedera.base.mirrornode;
2+
3+
import org.jspecify.annotations.NonNull;
4+
5+
import java.util.Objects;
6+
7+
public record NetworkSupplies(@NonNull String releasedSupply, @NonNull String totalSupply) {
8+
public NetworkSupplies {
9+
Objects.requireNonNull(releasedSupply, "releasedSupply must not be null");
10+
Objects.requireNonNull(totalSupply, "totalSupply must not be null");
11+
}
12+
}

hedera-spring/src/main/java/com/openelements/hedera/spring/implementation/HederaAutoConfiguration.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.openelements.hedera.base.NftClient;
1111
import com.openelements.hedera.base.NftRepository;
1212
import com.openelements.hedera.base.AccountRepository;
13+
import com.openelements.hedera.base.NetworkRepository;
1314
import com.openelements.hedera.base.SmartContractClient;
1415
import com.openelements.hedera.base.implementation.AccountClientImpl;
1516
import com.openelements.hedera.base.implementation.FileClientImpl;
@@ -19,6 +20,7 @@
1920
import com.openelements.hedera.base.implementation.ProtocolLayerClientImpl;
2021
import com.openelements.hedera.base.implementation.SmartContractClientImpl;
2122
import com.openelements.hedera.base.implementation.AccountRepositoryImpl;
23+
import com.openelements.hedera.base.implementation.NetworkRepositoryImpl;
2224
import com.openelements.hedera.base.mirrornode.MirrorNodeClient;
2325
import com.openelements.hedera.base.protocol.ProtocolLayerClient;
2426
import java.net.URI;
@@ -209,6 +211,13 @@ AccountRepository accountRepository(final MirrorNodeClient mirrorNodeClient) {
209211
return new AccountRepositoryImpl(mirrorNodeClient);
210212
}
211213

214+
@Bean
215+
@ConditionalOnProperty(prefix = "spring.hedera", name = "mirrorNodeSupported",
216+
havingValue = "true", matchIfMissing = true)
217+
NetworkRepository networkRepository(final MirrorNodeClient mirrorNodeClient) {
218+
return new NetworkRepositoryImpl(mirrorNodeClient);
219+
}
220+
212221
@Bean
213222
ContractVerificationClient contractVerificationClient(HederaNetwork hederaNetwork) {
214223
return new ContractVerificationClientImplementation(hederaNetwork);

0 commit comments

Comments
 (0)