Skip to content

Commit 882fd3c

Browse files
feat: create ContractRepository interface, implementation
Signed-off-by: Ndacyayisenga-droid <[email protected]>
1 parent ab28b3d commit 882fd3c

File tree

12 files changed

+724
-14
lines changed

12 files changed

+724
-14
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.openelements.hiero.base.data;
2+
3+
import com.hedera.hashgraph.sdk.AccountId;
4+
import com.hedera.hashgraph.sdk.ContractId;
5+
import com.hedera.hashgraph.sdk.PublicKey;
6+
import java.time.Instant;
7+
import java.util.Objects;
8+
import org.jspecify.annotations.NonNull;
9+
import org.jspecify.annotations.Nullable;
10+
11+
/**
12+
* Represents a smart contract on the Hiero network.
13+
*/
14+
public record Contract(
15+
@NonNull ContractId contractId,
16+
@Nullable PublicKey adminKey,
17+
@Nullable AccountId autoRenewAccount,
18+
int autoRenewPeriod,
19+
@NonNull Instant createdTimestamp,
20+
boolean deleted,
21+
@Nullable Instant expirationTimestamp,
22+
@Nullable String fileId,
23+
@Nullable String evmAddress,
24+
@Nullable String memo,
25+
@Nullable Integer maxAutomaticTokenAssociations,
26+
@Nullable Long nonce,
27+
@Nullable String obtainerId,
28+
boolean permanentRemoval,
29+
@Nullable String proxyAccountId,
30+
@NonNull Instant fromTimestamp,
31+
@NonNull Instant toTimestamp,
32+
@Nullable String bytecode,
33+
@Nullable String runtimeBytecode
34+
) {
35+
public Contract {
36+
Objects.requireNonNull(contractId, "contractId must not be null");
37+
Objects.requireNonNull(createdTimestamp, "createdTimestamp must not be null");
38+
Objects.requireNonNull(fromTimestamp, "fromTimestamp must not be null");
39+
Objects.requireNonNull(toTimestamp, "toTimestamp must not be null");
40+
}
41+
}

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

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package com.openelements.hiero.base.implementation;
22

33
import com.hedera.hashgraph.sdk.AccountId;
4+
import com.hedera.hashgraph.sdk.ContractId;
45
import com.hedera.hashgraph.sdk.TokenId;
56
import com.hedera.hashgraph.sdk.TopicId;
67
import com.openelements.hiero.base.HieroException;
8+
import com.openelements.hiero.base.data.Contract;
9+
import com.openelements.hiero.base.data.Nft;
10+
import com.openelements.hiero.base.data.NftMetadata;
711
import com.openelements.hiero.base.data.AccountInfo;
812
import com.openelements.hiero.base.data.ExchangeRates;
913
import com.openelements.hiero.base.data.NetworkFee;
1014
import com.openelements.hiero.base.data.NetworkStake;
1115
import com.openelements.hiero.base.data.NetworkSupplies;
12-
import com.openelements.hiero.base.data.Nft;
13-
import com.openelements.hiero.base.data.NftMetadata;
16+
import com.openelements.hiero.base.data.Page;
1417
import com.openelements.hiero.base.data.TokenInfo;
1518
import com.openelements.hiero.base.data.TransactionInfo;
1619
import com.openelements.hiero.base.data.Topic;
@@ -105,4 +108,37 @@ public final Optional<TopicMessage> queryTopicMessageBySequenceNumber(TopicId to
105108
throw new UnsupportedOperationException("Not yet implemented");
106109
}
107110

111+
@Override
112+
public @NonNull Page<Contract> queryContracts() throws HieroException {
113+
final JSON json = getRestClient().queryContracts();
114+
return getJsonConverter().toContractPage(json);
115+
}
116+
117+
@Override
118+
public @NonNull Optional<Contract> queryContractById(@NonNull final ContractId contractId) throws HieroException {
119+
Objects.requireNonNull(contractId, "contractId must not be null");
120+
final JSON json = getRestClient().queryContractById(contractId);
121+
return getJsonConverter().toContract(json);
122+
}
123+
124+
@Override
125+
public @NonNull Page<Contract> queryContractsByEvmAddress(@NonNull final String evmAddress) throws HieroException {
126+
Objects.requireNonNull(evmAddress, "evmAddress must not be null");
127+
final JSON json = getRestClient().queryContractsByEvmAddress(evmAddress);
128+
return getJsonConverter().toContractPage(json);
129+
}
130+
131+
@Override
132+
public @NonNull Page<Contract> queryContractsByFileId(@NonNull final String fileId) throws HieroException {
133+
Objects.requireNonNull(fileId, "fileId must not be null");
134+
final JSON json = getRestClient().queryContractsByFileId(fileId);
135+
return getJsonConverter().toContractPage(json);
136+
}
137+
138+
@Override
139+
public @NonNull Page<Contract> queryContractsByProxyAccountId(@NonNull final String proxyAccountId) throws HieroException {
140+
Objects.requireNonNull(proxyAccountId, "proxyAccountId must not be null");
141+
final JSON json = getRestClient().queryContractsByProxyAccountId(proxyAccountId);
142+
return getJsonConverter().toContractPage(json);
143+
}
108144
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.openelements.hiero.base.implementation;
2+
3+
import com.hedera.hashgraph.sdk.ContractId;
4+
import com.openelements.hiero.base.HieroException;
5+
import com.openelements.hiero.base.data.Contract;
6+
import com.openelements.hiero.base.data.Page;
7+
import com.openelements.hiero.base.mirrornode.ContractRepository;
8+
import com.openelements.hiero.base.mirrornode.MirrorNodeClient;
9+
import java.util.Objects;
10+
import java.util.Optional;
11+
import org.jspecify.annotations.NonNull;
12+
13+
/**
14+
* Implementation of ContractRepository that uses MirrorNodeClient to query contract data.
15+
*/
16+
public class ContractRepositoryImpl implements ContractRepository {
17+
18+
private final MirrorNodeClient mirrorNodeClient;
19+
20+
/**
21+
* Creates a new ContractRepositoryImpl with the given MirrorNodeClient.
22+
*
23+
* @param mirrorNodeClient the mirror node client to use for queries
24+
*/
25+
public ContractRepositoryImpl(@NonNull final MirrorNodeClient mirrorNodeClient) {
26+
this.mirrorNodeClient = Objects.requireNonNull(mirrorNodeClient, "mirrorNodeClient must not be null");
27+
}
28+
29+
@NonNull
30+
@Override
31+
public Page<Contract> findAll() throws HieroException {
32+
return mirrorNodeClient.queryContracts();
33+
}
34+
35+
@NonNull
36+
@Override
37+
public Optional<Contract> findById(@NonNull final ContractId contractId) throws HieroException {
38+
return mirrorNodeClient.queryContractById(contractId);
39+
}
40+
41+
@NonNull
42+
@Override
43+
public Page<Contract> findByEvmAddress(@NonNull final String evmAddress) throws HieroException {
44+
return mirrorNodeClient.queryContractsByEvmAddress(evmAddress);
45+
}
46+
47+
@NonNull
48+
@Override
49+
public Page<Contract> findByFileId(@NonNull final String fileId) throws HieroException {
50+
return mirrorNodeClient.queryContractsByFileId(fileId);
51+
}
52+
53+
@NonNull
54+
@Override
55+
public Page<Contract> findByProxyAccountId(@NonNull final String proxyAccountId) throws HieroException {
56+
return mirrorNodeClient.queryContractsByProxyAccountId(proxyAccountId);
57+
}
58+
}

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
import com.openelements.hiero.base.data.Balance;
1313
import com.openelements.hiero.base.data.Topic;
1414
import com.openelements.hiero.base.data.TopicMessage;
15+
import com.openelements.hiero.base.data.Contract;
16+
import com.openelements.hiero.base.data.Page;
1517

1618
import java.util.List;
1719
import java.util.Optional;
1820
import org.jspecify.annotations.NonNull;
1921

2022
public interface MirrorNodeJsonConverter<JSON> {
21-
2223
@NonNull
2324
Optional<Nft> toNft(@NonNull JSON json);
2425

@@ -59,4 +60,13 @@ public interface MirrorNodeJsonConverter<JSON> {
5960

6061
@NonNull
6162
List<TopicMessage> toTopicMessages(JSON json);
63+
64+
@NonNull
65+
Optional<Contract> toContract(@NonNull JSON json);
66+
67+
@NonNull
68+
Page<Contract> toContractPage(@NonNull JSON json);
69+
70+
@NonNull
71+
List<Contract> toContracts(@NonNull JSON json);
6272
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.openelements.hiero.base.implementation;
22

33
import com.hedera.hashgraph.sdk.AccountId;
4+
import com.hedera.hashgraph.sdk.ContractId;
45
import com.hedera.hashgraph.sdk.TokenId;
56
import com.hedera.hashgraph.sdk.TopicId;
67
import com.openelements.hiero.base.HieroException;
@@ -68,4 +69,33 @@ default JSON queryTopicMessageBySequenceNumber(TopicId topicId, long sequenceNum
6869

6970
@NonNull
7071
JSON doGetCall(@NonNull String path) throws HieroException;
72+
73+
@NonNull
74+
default JSON queryContracts() throws HieroException {
75+
return doGetCall("/api/v1/contracts");
76+
}
77+
78+
@NonNull
79+
default JSON queryContractById(@NonNull final ContractId contractId) throws HieroException {
80+
Objects.requireNonNull(contractId, "contractId must not be null");
81+
return doGetCall("/api/v1/contracts/" + contractId);
82+
}
83+
84+
@NonNull
85+
default JSON queryContractsByEvmAddress(@NonNull final String evmAddress) throws HieroException {
86+
Objects.requireNonNull(evmAddress, "evmAddress must not be null");
87+
return doGetCall("/api/v1/contracts?evm.address=" + evmAddress);
88+
}
89+
90+
@NonNull
91+
default JSON queryContractsByFileId(@NonNull final String fileId) throws HieroException {
92+
Objects.requireNonNull(fileId, "fileId must not be null");
93+
return doGetCall("/api/v1/contracts?file.id=" + fileId);
94+
}
95+
96+
@NonNull
97+
default JSON queryContractsByProxyAccountId(@NonNull final String proxyAccountId) throws HieroException {
98+
Objects.requireNonNull(proxyAccountId, "proxyAccountId must not be null");
99+
return doGetCall("/api/v1/contracts?proxy.account.id=" + proxyAccountId);
100+
}
71101
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.openelements.hiero.base.mirrornode;
2+
3+
import com.hedera.hashgraph.sdk.ContractId;
4+
import com.openelements.hiero.base.HieroException;
5+
import com.openelements.hiero.base.data.Contract;
6+
import com.openelements.hiero.base.data.Page;
7+
import java.util.Objects;
8+
import java.util.Optional;
9+
import org.jspecify.annotations.NonNull;
10+
11+
/**
12+
* Interface for interacting with smart contracts on a Hiero network. This interface provides methods for searching for contracts.
13+
*/
14+
public interface ContractRepository {
15+
16+
/**
17+
* Return all contracts.
18+
*
19+
* @return first page of contracts
20+
* @throws HieroException if the search fails
21+
*/
22+
@NonNull
23+
Page<Contract> findAll() throws HieroException;
24+
25+
/**
26+
* Return a contract by its contract ID.
27+
*
28+
* @param contractId id of the contract
29+
* @return {@link Optional} containing the found contract or null
30+
* @throws HieroException if the search fails
31+
*/
32+
@NonNull
33+
Optional<Contract> findById(@NonNull ContractId contractId) throws HieroException;
34+
35+
/**
36+
* Return a contract by its contract ID.
37+
*
38+
* @param contractId id of the contract
39+
* @return {@link Optional} containing the found contract or null
40+
* @throws HieroException if the search fails
41+
*/
42+
@NonNull
43+
default Optional<Contract> findById(@NonNull String contractId) throws HieroException {
44+
Objects.requireNonNull(contractId, "contractId must not be null");
45+
return findById(ContractId.fromString(contractId));
46+
}
47+
48+
/**
49+
* Return contracts by EVM address.
50+
*
51+
* @param evmAddress the EVM address of the contract
52+
* @return first page of contracts
53+
* @throws HieroException if the search fails
54+
*/
55+
@NonNull
56+
Page<Contract> findByEvmAddress(@NonNull String evmAddress) throws HieroException;
57+
58+
/**
59+
* Return contracts by file ID.
60+
*
61+
* @param fileId the file ID associated with the contract
62+
* @return first page of contracts
63+
* @throws HieroException if the search fails
64+
*/
65+
@NonNull
66+
Page<Contract> findByFileId(@NonNull String fileId) throws HieroException;
67+
68+
/**
69+
* Return contracts by proxy account ID.
70+
*
71+
* @param proxyAccountId the proxy account ID
72+
* @return first page of contracts
73+
* @throws HieroException if the search fails
74+
*/
75+
@NonNull
76+
Page<Contract> findByProxyAccountId(@NonNull String proxyAccountId) throws HieroException;
77+
}

0 commit comments

Comments
 (0)