Skip to content

Commit 8958772

Browse files
committed
Implement MirrorNodeClient.queryTransaction
1 parent 8681d78 commit 8958772

File tree

4 files changed

+176
-3
lines changed

4 files changed

+176
-3
lines changed

hiero-enterprise-base/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@
5757
<artifactId>slf4j-simple</artifactId>
5858
<scope>test</scope>
5959
</dependency>
60+
<dependency>
61+
<groupId>com.vaadin.external.google</groupId>
62+
<artifactId>android-json</artifactId>
63+
<version>0.0.20131108.vaadin1</version>
64+
<scope>compile</scope>
65+
</dependency>
6066
</dependencies>
6167

6268
<build>
Lines changed: 160 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,169 @@
11
package com.openelements.hiero.base.data;
22

3-
import java.util.Objects;
3+
import org.json.JSONArray;
4+
import org.json.JSONException;
5+
import org.json.JSONObject;
46
import org.jspecify.annotations.NonNull;
7+
import org.jspecify.annotations.Nullable;
8+
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
import java.util.Objects;
512

6-
public record TransactionInfo(@NonNull String transactionId) {
13+
public record TransactionInfo(
14+
@Nullable Byte bytes,
15+
long chargedTxFee,
16+
String consensusTimestamp,
17+
String entityId,
18+
String maxFee,
19+
String memoBase64,
20+
String name,
21+
List<NftTransfers> nftTransfers,
22+
String node,
23+
int nonce,
24+
String parentConsensusTimestamp,
25+
String result,
26+
boolean scheduled,
27+
@Nullable List<Object> stakingRewardTransfers,
28+
@Nullable List<Object> tokenTransfers,
29+
String transactionHash,
30+
@NonNull String transactionId,
31+
List<Transfers> transfers,
32+
String validDurationSeconds,
33+
String validStartTimestamp
34+
) {
735

836
public TransactionInfo {
937
Objects.requireNonNull(transactionId, "transactionId must not be null");
1038
}
39+
public TransactionInfo(@NonNull String transactionId) {
40+
this(null,
41+
0L,
42+
null,
43+
null,
44+
null,
45+
null,
46+
null,
47+
null,
48+
null,
49+
0,
50+
null,
51+
null,
52+
false,
53+
null,
54+
null,
55+
null,
56+
transactionId,
57+
null,
58+
null,
59+
null
60+
);
61+
}
62+
public static TransactionInfo fromJson(JSONObject json) throws JSONException {
63+
Byte bytes = json.has("bytes") ? (byte) json.getInt("bytes") : null;
64+
long chargedTxFee = json.getLong("charged_tx_fee");
65+
String consensusTimestamp = json.getString("consensus_timestamp");
66+
String entityId = json.getString("entity_id");
67+
String maxFee = json.getString("max_fee");
68+
String memoBase64 = json.getString("memo_base64");
69+
String name = json.getString("name");
70+
71+
List<NftTransfers> nftTransfers = parseNftTransfers(json.getJSONArray("nft_transfers"));
72+
List<Transfers> transfers = parseTransfers(json.getJSONArray("transfers"));
73+
74+
String node = json.getString("node");
75+
int nonce = json.getInt("nonce");
76+
String parentConsensusTimestamp = json.getString("parent_consensus_timestamp");
77+
String result = json.getString("result");
78+
boolean scheduled = json.getBoolean("scheduled");
79+
80+
// stakingRewardTransfers might be null if "staking_reward_transfers" is absent in the JSON.
81+
List<Object> stakingRewardTransfers = json.has("staking_reward_transfers")
82+
? fromJsonToArray(json.getJSONArray("staking_reward_transfers")) : null;
83+
// tokenTransfers might be null if "token_transfers" is absent in the JSON.
84+
List<Object> tokenTransfers = json.has("token_transfers")
85+
? fromJsonToArray(json.getJSONArray("token_transfers")) : null;
86+
87+
String transactionHash = json.getString("transaction_hash");
88+
// transactionId can never be null
89+
String transactionId = json.getString("transaction_id");
90+
String validDurationSeconds = json.getString("valid_duration_seconds");
91+
String validStartTimestamp = json.getString("valid_start_timestamp");
92+
93+
return new TransactionInfo(
94+
bytes, chargedTxFee, consensusTimestamp, entityId, maxFee, memoBase64, name,
95+
nftTransfers, node, nonce, parentConsensusTimestamp, result, scheduled,
96+
stakingRewardTransfers, tokenTransfers, transactionHash, transactionId,
97+
transfers, validDurationSeconds, validStartTimestamp
98+
);
99+
}
100+
101+
private static List<Object> fromJsonToArray(JSONArray jsonArray) throws JSONException {
102+
List<Object> lists = new ArrayList<>();
103+
for (int i = 0; i < jsonArray.length(); i++) {
104+
lists.add(jsonArray.getJSONArray(i));
105+
}
106+
return lists;
107+
}
108+
109+
private static List<NftTransfers> parseNftTransfers(JSONArray jsonArray) throws JSONException {
110+
List<NftTransfers> list = new ArrayList<>();
111+
for (int i = 0; i < jsonArray.length(); i++) {
112+
list.add(NftTransfers.fromJson(jsonArray.getJSONObject(i)));
113+
}
114+
return list;
115+
}
116+
117+
private static List<Transfers> parseTransfers(JSONArray jsonArray) throws JSONException {
118+
List<Transfers> list = new ArrayList<>();
119+
for (int i = 0; i < jsonArray.length(); i++) {
120+
list.add(Transfers.fromJson(jsonArray.getJSONObject(i)));
121+
}
122+
return list;
123+
}
124+
125+
static class NftTransfers {
126+
private final boolean isApproval;
127+
private final String receiverAccountId;
128+
private final String senderAccountId;
129+
private final int serialNumber;
130+
private final String tokenId;
131+
132+
public NftTransfers(boolean isApproval, String receiverAccountId, String senderAccountId, int serialNumber, String tokenId) {
133+
this.isApproval = isApproval;
134+
this.receiverAccountId = receiverAccountId;
135+
this.senderAccountId = senderAccountId;
136+
this.serialNumber = serialNumber;
137+
this.tokenId = tokenId;
138+
}
139+
140+
public static NftTransfers fromJson(JSONObject json) throws JSONException {
141+
boolean isApproval = json.getBoolean("is_approval");
142+
String receiverAccountId = json.getString("receiver_account_id");
143+
String senderAccountId = json.optString("sender_account_id", null);
144+
int serialNumber = json.getInt("serial_number");
145+
String tokenId = json.getString("token_id");
146+
return new NftTransfers(isApproval, receiverAccountId, senderAccountId, serialNumber, tokenId);
147+
}
148+
}
149+
150+
static class Transfers {
151+
private final String account;
152+
private final long amount;
153+
private final boolean isApproved;
154+
155+
public Transfers(String account, long amount, boolean isApproved) {
156+
this.account = account;
157+
this.amount = amount;
158+
this.isApproved = isApproved;
159+
}
160+
161+
public static Transfers fromJson(JSONObject jsonObject) throws JSONException {
162+
String account = jsonObject.getString("account");
163+
long amount = jsonObject.getLong("amount");
164+
boolean isApproved = jsonObject.getBoolean("is_approved");
165+
return new Transfers(account, amount, isApproved);
166+
}
167+
}
11168
}
169+

hiero-enterprise-base/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
requires org.slf4j;
1212
requires com.google.protobuf; //TODO: We should not have the need to use it
1313
requires static org.jspecify;
14+
requires android.json;
1415
}

hiero-enterprise-spring/src/main/java/com/openelements/hiero/spring/implementation/MirrorNodeClientImpl.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,18 @@ public Page<TransactionInfo> queryTransactionsByAccount(@NonNull final AccountId
107107
public Optional<TransactionInfo> queryTransaction(@NonNull final String transactionId) throws HieroException {
108108
Objects.requireNonNull(transactionId, "transactionId must not be null");
109109
final JsonNode jsonNode = doGetCall("/api/v1/transactions/" + transactionId);
110+
TransactionInfo transactionInfo;
111+
110112
if (jsonNode == null || !jsonNode.fieldNames().hasNext()) {
111113
return Optional.empty();
112114
}
113-
return Optional.of(new TransactionInfo(transactionId));
115+
try {
116+
transactionInfo = objectMapper.treeToValue(jsonNode, TransactionInfo.class);
117+
return Optional.ofNullable(transactionInfo);
118+
} catch (JsonProcessingException jsonProcessingException) {
119+
System.err.println("Error parsing transaction data: " + jsonProcessingException.getMessage());
120+
return Optional.empty();
121+
}
114122
}
115123

116124
@Override

0 commit comments

Comments
 (0)