Skip to content

Commit 688d003

Browse files
committed
added topicRepository
Signed-off-by: Manish Dait <[email protected]>
1 parent 4f76442 commit 688d003

File tree

15 files changed

+683
-10
lines changed

15 files changed

+683
-10
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.openelements.hiero.base.data;
2+
3+
import com.hedera.hashgraph.sdk.TransactionId;
4+
import org.jspecify.annotations.NonNull;
5+
6+
import java.util.Objects;
7+
8+
public record ChunkInfo(
9+
@NonNull TransactionId initialTransactionId,
10+
int nonce,
11+
int number,
12+
int total,
13+
boolean scheduled
14+
) {
15+
public ChunkInfo {
16+
Objects.requireNonNull(initialTransactionId, "initialTransactionId must not be null");
17+
}
18+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.openelements.hiero.base.data;
2+
3+
import com.hedera.hashgraph.sdk.AccountId;
4+
import com.hedera.hashgraph.sdk.PublicKey;
5+
import com.hedera.hashgraph.sdk.TopicId;
6+
import org.jspecify.annotations.NonNull;
7+
import org.jspecify.annotations.Nullable;
8+
9+
import java.time.Instant;
10+
import java.util.List;
11+
import java.util.Objects;
12+
13+
public record Topic(
14+
@NonNull TopicId topicId,
15+
@Nullable PublicKey adminKey,
16+
@Nullable AccountId autoRenewAccount,
17+
int autoRenewPeriod,
18+
@NonNull Instant createdTimestamp,
19+
@NonNull List<FixedFee> fixedFees,
20+
@Nullable List<PublicKey> feeExemptKeyList,
21+
@Nullable PublicKey feeScheduleKey,
22+
@Nullable PublicKey submitKey,
23+
boolean deleted,
24+
String memo,
25+
@NonNull Instant fromTimestamp,
26+
@NonNull Instant toTimestamp
27+
) {
28+
public Topic {
29+
Objects.requireNonNull(topicId, "topicId must not be null");
30+
Objects.requireNonNull(createdTimestamp, "createdTimestamp must not be null");
31+
Objects.requireNonNull(fixedFees, "fixedFees must not be null");
32+
Objects.requireNonNull(fromTimestamp, "fromTimestamp must not be null");
33+
Objects.requireNonNull(toTimestamp, "toTimestamp must not be null");
34+
}
35+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.openelements.hiero.base.data;
2+
3+
import com.hedera.hashgraph.sdk.AccountId;
4+
import com.hedera.hashgraph.sdk.TopicId;
5+
import org.jspecify.annotations.NonNull;
6+
import org.jspecify.annotations.Nullable;
7+
8+
import java.time.Instant;
9+
import java.util.Objects;
10+
11+
public record TopicMessage(
12+
@Nullable ChunkInfo chunkInfo,
13+
@NonNull Instant consensusTimestamp,
14+
@NonNull String message,
15+
@NonNull AccountId payerAccountId,
16+
byte[] runningHash,
17+
int runningHashVersion,
18+
long sequenceNumber,
19+
@NonNull TopicId topicId
20+
) {
21+
public TopicMessage {
22+
Objects.requireNonNull(consensusTimestamp, "consensusTimestamp must not be null");
23+
Objects.requireNonNull(message, "message must not be null");
24+
Objects.requireNonNull(payerAccountId, "payerAccountId must not be null");
25+
Objects.requireNonNull(topicId, "topicId must not be null");
26+
}
27+
}

hiero-enterprise-base/src/main/java/com/openelements/hiero/base/implementation/AbstractMirrorNodeClient.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.hedera.hashgraph.sdk.AccountId;
44
import com.hedera.hashgraph.sdk.TokenId;
5+
import com.hedera.hashgraph.sdk.TopicId;
56
import com.openelements.hiero.base.HieroException;
67
import com.openelements.hiero.base.data.AccountInfo;
78
import com.openelements.hiero.base.data.ExchangeRates;
@@ -12,6 +13,8 @@
1213
import com.openelements.hiero.base.data.NftMetadata;
1314
import com.openelements.hiero.base.data.TokenInfo;
1415
import com.openelements.hiero.base.data.TransactionInfo;
16+
import com.openelements.hiero.base.data.Topic;
17+
import com.openelements.hiero.base.data.TopicMessage;
1518
import com.openelements.hiero.base.mirrornode.MirrorNodeClient;
1619
import java.util.List;
1720
import java.util.Objects;
@@ -83,6 +86,20 @@ public final Optional<TransactionInfo> queryTransaction(@NonNull String transact
8386
return getJsonConverter().toTransactionInfo(json);
8487
}
8588

89+
@Override
90+
@NonNull
91+
public final Optional<Topic> queryTopicById(TopicId topicId) throws HieroException {
92+
final JSON json = getRestClient().queryTopicById(topicId);
93+
return getJsonConverter().toTopic(json);
94+
}
95+
96+
@Override
97+
@NonNull
98+
public final Optional<TopicMessage> queryTopicMessageBySequenceNumber(TopicId topicId, long sequenceNumber) throws HieroException {
99+
final JSON json = getRestClient().queryTopicMessageBySequenceNumber(topicId, sequenceNumber);
100+
return getJsonConverter().toTopicMessage(json);
101+
}
102+
86103
@Override
87104
public @NonNull Optional<NftMetadata> getNftMetadata(TokenId tokenId) throws HieroException {
88105
throw new UnsupportedOperationException("Not yet implemented");

hiero-enterprise-base/src/main/java/com/openelements/hiero/base/implementation/MirrorNodeJsonConverter.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import com.openelements.hiero.base.data.Token;
1111
import com.openelements.hiero.base.data.TokenInfo;
1212
import com.openelements.hiero.base.data.Balance;
13+
import com.openelements.hiero.base.data.Topic;
14+
import com.openelements.hiero.base.data.TopicMessage;
1315

1416
import java.util.List;
1517
import java.util.Optional;
@@ -48,4 +50,13 @@ public interface MirrorNodeJsonConverter<JSON> {
4850
List<Balance> toBalances(JSON node);
4951

5052
List<Token> toTokens(JSON node);
53+
54+
@NonNull
55+
Optional<Topic> toTopic(JSON json);
56+
57+
@NonNull
58+
Optional<TopicMessage> toTopicMessage(JSON json);
59+
60+
@NonNull
61+
List<TopicMessage> toTopicMessages(JSON json);
5162
}

hiero-enterprise-base/src/main/java/com/openelements/hiero/base/implementation/MirrorNodeRestClient.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.hedera.hashgraph.sdk.AccountId;
44
import com.hedera.hashgraph.sdk.TokenId;
5+
import com.hedera.hashgraph.sdk.TopicId;
56
import com.openelements.hiero.base.HieroException;
67
import java.util.Objects;
78
import org.jspecify.annotations.NonNull;
@@ -55,6 +56,17 @@ default JSON queryTokenById(TokenId tokenId) throws HieroException {
5556
return doGetCall("/api/v1/tokens/" + tokenId);
5657
}
5758

59+
@NonNull
60+
default JSON queryTopicById(TopicId topicId) throws HieroException {
61+
System.out.println("/api/v1/topics/" + topicId);
62+
return doGetCall("/api/v1/topics/" + topicId);
63+
}
64+
65+
@NonNull
66+
default JSON queryTopicMessageBySequenceNumber(TopicId topicId, long sequenceNumber) throws HieroException {
67+
return doGetCall("/api/v1/topics/" + topicId + "/messages/" + sequenceNumber);
68+
}
69+
5870
@NonNull
5971
JSON doGetCall(@NonNull String path) throws HieroException;
6072
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.openelements.hiero.base.implementation;
2+
3+
import com.hedera.hashgraph.sdk.TopicId;
4+
import com.openelements.hiero.base.HieroException;
5+
import com.openelements.hiero.base.data.Page;
6+
import com.openelements.hiero.base.data.Topic;
7+
import com.openelements.hiero.base.data.TopicMessage;
8+
import com.openelements.hiero.base.mirrornode.MirrorNodeClient;
9+
import com.openelements.hiero.base.mirrornode.TopicRepository;
10+
import org.jspecify.annotations.NonNull;
11+
12+
import java.util.Objects;
13+
import java.util.Optional;
14+
15+
public class TopicRepositoryImpl implements TopicRepository {
16+
private final MirrorNodeClient mirrorNodeClient;
17+
18+
public TopicRepositoryImpl(@NonNull final MirrorNodeClient mirrorNodeClient) {
19+
this.mirrorNodeClient = Objects.requireNonNull(mirrorNodeClient, "mirrorNodeClient must not be null");
20+
}
21+
22+
@Override
23+
public @NonNull Optional<Topic> findTopicById(TopicId topicId) throws HieroException {
24+
Objects.requireNonNull(topicId, "topicId must not be null");
25+
return mirrorNodeClient.queryTopicById(topicId);
26+
}
27+
28+
@Override
29+
public @NonNull Page<TopicMessage> getMessages(TopicId topicId) throws HieroException {
30+
Objects.requireNonNull(topicId, "topicId must not be null");
31+
return mirrorNodeClient.queryTopicMessages(topicId);
32+
}
33+
34+
@Override
35+
public @NonNull Optional<TopicMessage> getMessageBySequenceNumber(TopicId topicId, long sequenceNumber) throws HieroException {
36+
Objects.requireNonNull(topicId, "topicId must not be null");
37+
if (sequenceNumber < 1) {
38+
throw new IllegalArgumentException("sequenceNumber must be greater than 0");
39+
}
40+
return mirrorNodeClient.queryTopicMessageBySequenceNumber(topicId, sequenceNumber);
41+
}
42+
}

hiero-enterprise-base/src/main/java/com/openelements/hiero/base/mirrornode/MirrorNodeClient.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.hedera.hashgraph.sdk.AccountId;
44
import com.hedera.hashgraph.sdk.TokenId;
5+
import com.hedera.hashgraph.sdk.TopicId;
56
import com.openelements.hiero.base.HieroException;
67
import com.openelements.hiero.base.data.AccountInfo;
78
import com.openelements.hiero.base.data.Balance;
@@ -15,6 +16,8 @@
1516
import com.openelements.hiero.base.data.Token;
1617
import com.openelements.hiero.base.data.TokenInfo;
1718
import com.openelements.hiero.base.data.TransactionInfo;
19+
import com.openelements.hiero.base.data.Topic;
20+
import com.openelements.hiero.base.data.TopicMessage;
1821
import com.openelements.hiero.base.data.Result;
1922
import com.openelements.hiero.base.data.BalanceModification;
2023
import java.util.List;
@@ -377,6 +380,77 @@ default Page<Balance> queryTokenBalancesForAccount(@NonNull String tokenId, @Non
377380
return queryTokenBalancesForAccount(TokenId.fromString(tokenId), AccountId.fromString(accountId));
378381
}
379382

383+
/**
384+
* Return Topic for given topicId.
385+
*
386+
* @param topicId id of the topic
387+
* @return Optional of Topic
388+
* @throws HieroException if the search fails
389+
*/
390+
@NonNull
391+
Optional<Topic> queryTopicById(TopicId topicId) throws HieroException;
392+
393+
/**
394+
* Return Topic for given topicId.
395+
*
396+
* @param topicId id of the topic
397+
* @return Optional of Topic
398+
* @throws HieroException if the search fails
399+
*/
400+
@NonNull
401+
default Optional<Topic> queryTopicById(String topicId) throws HieroException {
402+
Objects.requireNonNull(topicId, "topicId must not be null");
403+
return queryTopicById(TopicId.fromString(topicId));
404+
}
405+
406+
/**
407+
* Return TopicMessages for given topicId.
408+
*
409+
* @param topicId id of the topic
410+
* @return Page of TopicMessage
411+
* @throws HieroException if the search fails
412+
*/
413+
@NonNull
414+
Page<TopicMessage> queryTopicMessages(TopicId topicId) throws HieroException;
415+
416+
/**
417+
* Return TopicMessages for given topicId.
418+
*
419+
* @param topicId id of the topic
420+
* @return Page of TopicMessage
421+
* @throws HieroException if the search fails
422+
*/
423+
@NonNull
424+
default Page<TopicMessage> queryTopicMessages(String topicId) throws HieroException {
425+
Objects.requireNonNull(topicId, "topicId must not be null");
426+
return queryTopicMessages(TopicId.fromString(topicId));
427+
}
428+
429+
/**
430+
* Return TopicMessage for given topicId.
431+
*
432+
* @param topicId id of the topic
433+
* @param sequenceNumber sequenceNumber of the message
434+
* @return Optional of TopicMessage
435+
* @throws HieroException if the search fails
436+
*/
437+
@NonNull
438+
Optional<TopicMessage> queryTopicMessageBySequenceNumber(TopicId topicId, long sequenceNumber) throws HieroException;
439+
440+
/**
441+
* Return TopicMessage for given topicId.
442+
*
443+
* @param topicId id of the topic
444+
* @param sequenceNumber sequenceNumber of the message
445+
* @return Optional of TopicMessage
446+
* @throws HieroException if the search fails
447+
*/
448+
@NonNull
449+
default Optional<TopicMessage> queryTopicMessageBySequenceNumber(String topicId, long sequenceNumber) throws HieroException {
450+
Objects.requireNonNull(topicId, "topicId must not be null");
451+
return queryTopicMessageBySequenceNumber(TopicId.fromString(topicId), sequenceNumber);
452+
}
453+
380454
@NonNull
381455
Optional<NftMetadata> getNftMetadata(@NonNull TokenId tokenId) throws HieroException;
382456

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.openelements.hiero.base.mirrornode;
2+
3+
import com.hedera.hashgraph.sdk.TopicId;
4+
import com.openelements.hiero.base.HieroException;
5+
import com.openelements.hiero.base.data.Page;
6+
import com.openelements.hiero.base.data.Topic;
7+
import com.openelements.hiero.base.data.TopicMessage;
8+
import org.jspecify.annotations.NonNull;
9+
10+
import java.util.Objects;
11+
import java.util.Optional;
12+
13+
/**
14+
* Interface for interacting with a Hiero network. This interface provides methods
15+
* for searching Topic and TopicMessages.
16+
*/
17+
public interface TopicRepository {
18+
/**
19+
* Return Topic for given topicId.
20+
*
21+
* @param topicId id of the topic
22+
* @return Optional of Topic
23+
* @throws HieroException if the search fails
24+
*/
25+
@NonNull
26+
Optional<Topic> findTopicById(TopicId topicId) throws HieroException;
27+
28+
/**
29+
* Return Topic for given topicId.
30+
*
31+
* @param topicId id of the topic
32+
* @return Optional of Topic
33+
* @throws HieroException if the search fails
34+
*/
35+
@NonNull
36+
default Optional<Topic> findTopicById(String topicId) throws HieroException {
37+
Objects.requireNonNull(topicId, "topicId must not be null");
38+
return findTopicById(TopicId.fromString(topicId));
39+
}
40+
41+
/**
42+
* Return TopicMessages for given topicId.
43+
*
44+
* @param topicId id of the topic
45+
* @return Page of TopicMessage
46+
* @throws HieroException if the search fails
47+
*/
48+
@NonNull
49+
Page<TopicMessage> getMessages(TopicId topicId) throws HieroException;
50+
51+
/**
52+
* Return TopicMessages for given topicId.
53+
*
54+
* @param topicId id of the topic
55+
* @return Page of TopicMessage
56+
* @throws HieroException if the search fails
57+
*/
58+
@NonNull
59+
default Page<TopicMessage> getMessages(String topicId) throws HieroException {
60+
Objects.requireNonNull(topicId, "topicId must not be null");
61+
return getMessages(TopicId.fromString(topicId));
62+
};
63+
64+
/**
65+
* Return TopicMessage for given topicId.
66+
*
67+
* @param topicId id of the topic
68+
* @param sequenceNumber sequenceNumber of the message
69+
* @return Optional of TopicMessage
70+
* @throws HieroException if the search fails
71+
*/
72+
@NonNull
73+
Optional<TopicMessage> getMessageBySequenceNumber(TopicId topicId, long sequenceNumber) throws HieroException;
74+
75+
/**
76+
* Return TopicMessage for given topicId.
77+
*
78+
* @param topicId id of the topic
79+
* @param sequenceNumber sequenceNumber of the message
80+
* @return Optional of TopicMessage
81+
* @throws HieroException if the search fails
82+
*/
83+
@NonNull
84+
default Optional<TopicMessage> getMessageBySequenceNumber(String topicId, long sequenceNumber) throws HieroException {
85+
Objects.requireNonNull(topicId, "topicId must not be null");
86+
return getMessageBySequenceNumber(TopicId.fromString(topicId), sequenceNumber);
87+
};
88+
}

0 commit comments

Comments
 (0)