|
1 | 1 | package com.openelements.hiero.base.data; |
2 | 2 |
|
3 | | -import java.util.Objects; |
| 3 | +import org.json.JSONArray; |
| 4 | +import org.json.JSONException; |
| 5 | +import org.json.JSONObject; |
4 | 6 | 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; |
5 | 12 |
|
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 | +) { |
7 | 35 |
|
8 | 36 | public TransactionInfo { |
9 | 37 | Objects.requireNonNull(transactionId, "transactionId must not be null"); |
10 | 38 | } |
| 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 | + } |
11 | 168 | } |
| 169 | + |
0 commit comments