Skip to content

Commit

Permalink
Implement MirrorNodeClient.queryTransaction
Browse files Browse the repository at this point in the history
Signed-off-by: arihoSeth <[email protected]>
  • Loading branch information
Ariho-Seth committed Dec 12, 2024
1 parent 8681d78 commit d6a1c44
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 3 deletions.
6 changes: 6 additions & 0 deletions hiero-enterprise-base/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.vaadin.external.google</groupId>
<artifactId>android-json</artifactId>
<version>0.0.20131108.vaadin1</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,169 @@
package com.openelements.hiero.base.data;

import java.util.Objects;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public record TransactionInfo(@NonNull String transactionId) {
public record TransactionInfo(
@Nullable Byte bytes,
long chargedTxFee,
String consensusTimestamp,
String entityId,
String maxFee,
String memoBase64,
String name,
List<NftTransfers> nftTransfers,
String node,
int nonce,
String parentConsensusTimestamp,
String result,
boolean scheduled,
@Nullable List<Object> stakingRewardTransfers,
@Nullable List<Object> tokenTransfers,
String transactionHash,
@NonNull String transactionId,
List<Transfers> transfers,
String validDurationSeconds,
String validStartTimestamp
) {

public TransactionInfo {
Objects.requireNonNull(transactionId, "transactionId must not be null");
}
public TransactionInfo(@NonNull String transactionId) {
this(null,
0L,
null,
null,
null,
null,
null,
null,
null,
0,
null,
null,
false,
null,
null,
null,
transactionId,
null,
null,
null
);
}
public static TransactionInfo fromJson(JSONObject json) throws JSONException {
Byte bytes = json.has("bytes") ? (byte) json.getInt("bytes") : null;
long chargedTxFee = json.getLong("charged_tx_fee");
String consensusTimestamp = json.getString("consensus_timestamp");
String entityId = json.getString("entity_id");
String maxFee = json.getString("max_fee");
String memoBase64 = json.getString("memo_base64");
String name = json.getString("name");

List<NftTransfers> nftTransfers = parseNftTransfers(json.getJSONArray("nft_transfers"));
List<Transfers> transfers = parseTransfers(json.getJSONArray("transfers"));

String node = json.getString("node");
int nonce = json.getInt("nonce");
String parentConsensusTimestamp = json.getString("parent_consensus_timestamp");
String result = json.getString("result");
boolean scheduled = json.getBoolean("scheduled");

// stakingRewardTransfers might be null if "staking_reward_transfers" is absent in the JSON.
List<Object> stakingRewardTransfers = json.has("staking_reward_transfers")
? fromJsonToArray(json.getJSONArray("staking_reward_transfers")) : null;
// tokenTransfers might be null if "token_transfers" is absent in the JSON.
List<Object> tokenTransfers = json.has("token_transfers")
? fromJsonToArray(json.getJSONArray("token_transfers")) : null;

String transactionHash = json.getString("transaction_hash");
// transactionId can never be null
String transactionId = json.getString("transaction_id");
String validDurationSeconds = json.getString("valid_duration_seconds");
String validStartTimestamp = json.getString("valid_start_timestamp");

return new TransactionInfo(
bytes, chargedTxFee, consensusTimestamp, entityId, maxFee, memoBase64, name,
nftTransfers, node, nonce, parentConsensusTimestamp, result, scheduled,
stakingRewardTransfers, tokenTransfers, transactionHash, transactionId,
transfers, validDurationSeconds, validStartTimestamp
);
}

private static List<Object> fromJsonToArray(JSONArray jsonArray) throws JSONException {
List<Object> lists = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
lists.add(jsonArray.getJSONArray(i));
}
return lists;
}

private static List<NftTransfers> parseNftTransfers(JSONArray jsonArray) throws JSONException {
List<NftTransfers> list = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
list.add(NftTransfers.fromJson(jsonArray.getJSONObject(i)));
}
return list;
}

private static List<Transfers> parseTransfers(JSONArray jsonArray) throws JSONException {
List<Transfers> list = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
list.add(Transfers.fromJson(jsonArray.getJSONObject(i)));
}
return list;
}

static class NftTransfers {
private final boolean isApproval;
private final String receiverAccountId;
private final String senderAccountId;
private final int serialNumber;
private final String tokenId;

public NftTransfers(boolean isApproval, String receiverAccountId, String senderAccountId, int serialNumber, String tokenId) {
this.isApproval = isApproval;
this.receiverAccountId = receiverAccountId;
this.senderAccountId = senderAccountId;
this.serialNumber = serialNumber;
this.tokenId = tokenId;
}

public static NftTransfers fromJson(JSONObject json) throws JSONException {
boolean isApproval = json.getBoolean("is_approval");
String receiverAccountId = json.getString("receiver_account_id");
String senderAccountId = json.optString("sender_account_id", null);
int serialNumber = json.getInt("serial_number");
String tokenId = json.getString("token_id");
return new NftTransfers(isApproval, receiverAccountId, senderAccountId, serialNumber, tokenId);
}
}

static class Transfers {
private final String account;
private final long amount;
private final boolean isApproved;

public Transfers(String account, long amount, boolean isApproved) {
this.account = account;
this.amount = amount;
this.isApproved = isApproved;
}

public static Transfers fromJson(JSONObject jsonObject) throws JSONException {
String account = jsonObject.getString("account");
long amount = jsonObject.getLong("amount");
boolean isApproved = jsonObject.getBoolean("is_approved");
return new Transfers(account, amount, isApproved);
}
}
}

1 change: 1 addition & 0 deletions hiero-enterprise-base/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
requires org.slf4j;
requires com.google.protobuf; //TODO: We should not have the need to use it
requires static org.jspecify;
requires android.json;
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,18 @@ public Page<TransactionInfo> queryTransactionsByAccount(@NonNull final AccountId
public Optional<TransactionInfo> queryTransaction(@NonNull final String transactionId) throws HieroException {
Objects.requireNonNull(transactionId, "transactionId must not be null");
final JsonNode jsonNode = doGetCall("/api/v1/transactions/" + transactionId);
TransactionInfo transactionInfo;

if (jsonNode == null || !jsonNode.fieldNames().hasNext()) {
return Optional.empty();
}
return Optional.of(new TransactionInfo(transactionId));
try {
transactionInfo = objectMapper.treeToValue(jsonNode, TransactionInfo.class);
return Optional.ofNullable(transactionInfo);
} catch (JsonProcessingException jsonProcessingException) {
System.err.println("Error parsing transaction data: " + jsonProcessingException.getMessage());
return Optional.empty();
}
}

@Override
Expand Down

0 comments on commit d6a1c44

Please sign in to comment.